MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / resolve_ssh_gateway

Function resolve_ssh_gateway

crates/openshell-core/src/forward.rs:711–748  ·  view source on GitHub ↗

Resolve the SSH gateway host and port for a sandbox connection. If the server-provided gateway host is a loopback address, use the host and port from the cluster endpoint instead so the client connects to the right machine. The server returns its internal bind address (e.g. 0.0.0.0:8080) which may not be reachable from outside — the cluster URL has the actual Docker-mapped or tunnel port.

(
    gateway_host: &str,
    gateway_port: u16,
    cluster_url: &str,
)

Source from the content-addressed store, hash-verified

709/// which may not be reachable from outside — the cluster URL has the actual
710/// Docker-mapped or tunnel port.
711pub fn resolve_ssh_gateway(
712 gateway_host: &str,
713 gateway_port: u16,
714 cluster_url: &str,
715) -> (String, u16) {
716 let is_loopback = gateway_host == "127.0.0.1"
717 || gateway_host == "0.0.0.0"
718 || gateway_host == "localhost"
719 || gateway_host == "::1";
720
721 if !is_loopback {
722 return (gateway_host.to_string(), gateway_port);
723 }
724
725 // Extract host and port from the cluster URL. The cluster URL represents
726 // the externally reachable endpoint (e.g. Docker port-mapped address).
727 if let Ok(url) = url::Url::parse(cluster_url)
728 && let Some(host) = url.host_str()
729 {
730 let cluster_port = url.port_or_known_default().unwrap_or(gateway_port);
731 let cluster_is_loopback =
732 host == "127.0.0.1" || host == "0.0.0.0" || host == "localhost" || host == "::1";
733 if !cluster_is_loopback {
734 // Remote cluster: use the remote host but keep the cluster URL port.
735 return (host.to_string(), cluster_port);
736 }
737 // Both endpoints loopback. The unspecified addresses (0.0.0.0 / ::)
738 // are bind-only — they aren't valid connect targets and aren't in TLS
739 // cert SANs, so fall back to the cluster URL's host (which the CLI
740 // is already using to reach the gateway).
741 if gateway_host == "0.0.0.0" || gateway_host == "::" {
742 return (host.to_string(), cluster_port);
743 }
744 return (gateway_host.to_string(), cluster_port);
745 }
746
747 (gateway_host.to_string(), gateway_port)
748}
749
750/// Format a gateway URL, bracketing IPv6 literals when needed.
751pub fn format_gateway_url(scheme: &str, host: &str, port: u16) -> String {

Calls

no outgoing calls