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

Function open_ws

crates/openshell-cli/src/edge_tunnel.rs:151–183  ·  view source on GitHub ↗

Open a WebSocket connection to the edge proxy.

(config: &TunnelConfig)

Source from the content-addressed store, hash-verified

149
150/// Open a WebSocket connection to the edge proxy.
151async fn open_ws(config: &TunnelConfig) -> Result<WebSocketStream<MaybeTlsStream<TcpStream>>> {
152 let mut request = (&config.ws_url).into_client_request().into_diagnostic()?;
153
154 // Inject the bearer token via multiple headers for compatibility with
155 // Cloudflare Access (which checks `Cf-Access-Token`, the
156 // `CF_Authorization` cookie, and the `Cf-Access-Jwt-Assertion` header).
157 let token_val = HeaderValue::from_str(&config.edge_token)
158 .map_err(|e| miette::miette!("invalid edge token header value: {e}"))?;
159 request
160 .headers_mut()
161 .insert("Cf-Access-Token", token_val.clone());
162 request
163 .headers_mut()
164 .insert("Cf-Access-Jwt-Assertion", token_val);
165 request.headers_mut().insert(
166 "Cookie",
167 HeaderValue::from_str(&format!("CF_Authorization={}", config.edge_token))
168 .map_err(|e| miette::miette!("invalid edge token cookie value: {e}"))?,
169 );
170
171 debug!(url = %config.ws_url, "opening WebSocket to edge");
172
173 let (ws_stream, response) = tokio_tungstenite::connect_async(request)
174 .await
175 .map_err(|e| miette::miette!("WebSocket connect failed: {e}"))?;
176
177 debug!(
178 status = %response.status(),
179 "WebSocket connected to edge"
180 );
181
182 Ok(ws_stream)
183}
184
185/// Copy bytes from a local TCP reader into WebSocket binary frames.
186async fn copy_tcp_to_ws(

Callers 1

handle_connectionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected