Start the local tunnel proxy. Returns an [`EdgeTunnelProxy`] with the local address to connect to. The proxy runs as a background tokio task.
(
gateway_endpoint: &str,
edge_token: &str,
)
| 60 | /// Returns an [`EdgeTunnelProxy`] with the local address to connect to. |
| 61 | /// The proxy runs as a background tokio task. |
| 62 | pub async fn start_tunnel_proxy( |
| 63 | gateway_endpoint: &str, |
| 64 | edge_token: &str, |
| 65 | ) -> Result<EdgeTunnelProxy> { |
| 66 | let listener = TcpListener::bind("127.0.0.1:0").await.into_diagnostic()?; |
| 67 | let local_addr = listener.local_addr().into_diagnostic()?; |
| 68 | |
| 69 | // Convert the gateway endpoint to a WebSocket URL. |
| 70 | // https://foo.com -> wss://foo.com |
| 71 | // http://foo.com -> ws://foo.com (edge proxy over plain HTTP, uncommon but handled) |
| 72 | let ws_url = format!( |
| 73 | "{}/_ws_tunnel", |
| 74 | gateway_endpoint |
| 75 | .replacen("https://", "wss://", 1) |
| 76 | .replacen("http://", "ws://", 1) |
| 77 | .trim_end_matches('/') |
| 78 | ); |
| 79 | |
| 80 | let config = Arc::new(TunnelConfig { |
| 81 | ws_url, |
| 82 | edge_token: edge_token.to_string(), |
| 83 | }); |
| 84 | |
| 85 | debug!( |
| 86 | local_addr = %local_addr, |
| 87 | gateway = %gateway_endpoint, |
| 88 | "starting edge tunnel proxy" |
| 89 | ); |
| 90 | |
| 91 | // Spawn the accept loop. |
| 92 | tokio::spawn(accept_loop(listener, config)); |
| 93 | |
| 94 | Ok(EdgeTunnelProxy { local_addr }) |
| 95 | } |
| 96 | |
| 97 | /// Accept loop: for each incoming TCP connection, spawn a handler that |
| 98 | /// opens a WebSocket to the edge and pipes bytes bidirectionally. |
no test coverage detected