Route exec-server WebSockets through configured proxies (#35056)

## Why

Remote environment connections need to honor Codex's effective outbound proxy policy, including when a rendezvous connection reconnects.

## What changed

- Pass the configured `HttpClientFactory` into remote environment transports and use `WebSocketConnector` for exec-server and rendezvous WebSockets.
- Resolve proxy routes asynchronously so these connections can use the configured system proxy.
- Add connector options that preserve Tungstenite's default TLS behavior and enable `TCP_NODELAY` for latency-sensitive rendezvous traffic.

## Testing

- Verify prepared remote environments connect through a configured system proxy.
- Verify initial and reconnected encrypted relay peers use the system proxy.
- Cover default TLS selection and opt-in `TCP_NODELAY` behavior in the WebSocket client.

GitOrigin-RevId: 8a8da2116e37cb3a891269d0c0b037986fecdd3c
This commit is contained in:
Celia Chen authored and copyberry committed 2026-07-24 00:16:42 +00:00
1 parent fb4e6ba2f4
commit 94ebae725e
24 files changed
+727 -226

No files matched your search

+49 -17
View File
@@ -6,13 +6,14 @@ use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Command;
use tokio::time::timeout;
use tokio_tungstenite::connect_async;
use tokio_tungstenite::connect_async_with_config;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tracing::debug;
use tracing::warn;
use codex_http_client::HttpClientFactory;
use codex_utils_rustls_provider::ensure_rustls_crypto_provider;
use codex_websocket_client::WebSocketConnector;
use codex_websocket_client::WebSocketTlsMode;
use crate::ExecServerClient;
use crate::ExecServerError;
@@ -50,6 +51,7 @@ pub(crate) enum ExecServerReconnectStrategy {
client_name: String,
connect_timeout: Duration,
initialize_timeout: Duration,
http_client_factory: HttpClientFactory,
},
}
@@ -71,6 +73,7 @@ impl ExecServerReconnectStrategy {
client_name,
connect_timeout,
initialize_timeout,
http_client_factory,
} => {
let bundle = provider.connect_bundle(identity.public_key()).await?;
ExecServerClient::open_noise_rendezvous_connection(NoiseRendezvousConnectArgs {
@@ -80,6 +83,7 @@ impl ExecServerReconnectStrategy {
connect_timeout: *connect_timeout,
initialize_timeout: *initialize_timeout,
resume_session_id: Some(session_id.to_string()),
http_client_factory: http_client_factory.clone(),
})
.await
}
@@ -93,6 +97,7 @@ impl ExecServerClient {
/// and authorization without replacing the harness identity.
pub(crate) async fn connect_for_transport(
transport_params: ExecServerTransportParams,
http_client_factory: HttpClientFactory,
) -> Result<Self, ExecServerError> {
let (transport_params, deferred_readiness) = match transport_params {
ExecServerTransportParams::Deferred(deferred) => {
@@ -130,9 +135,14 @@ impl ExecServerClient {
client_name: ENVIRONMENT_CLIENT_NAME.to_string(),
connect_timeout: DEFAULT_REMOTE_EXEC_SERVER_CONNECT_TIMEOUT,
initialize_timeout: DEFAULT_REMOTE_EXEC_SERVER_INITIALIZE_TIMEOUT,
http_client_factory: http_client_factory.clone(),
};
let (connection, options) =
Self::open_initial_noise_rendezvous_connection(&provider, &identity).await?;
let (connection, options) = Self::open_initial_noise_rendezvous_connection(
&provider,
&identity,
http_client_factory,
)
.await?;
return Self::connect_with_recovery(connection, options, Some(reconnect_strategy))
.await;
}
@@ -155,6 +165,7 @@ impl ExecServerClient {
connect_timeout,
initialize_timeout,
resume_session_id: None,
http_client_factory,
})
.await
}
@@ -162,6 +173,7 @@ impl ExecServerClient {
async fn open_initial_noise_rendezvous_connection(
provider: &Arc<dyn NoiseRendezvousConnectProvider>,
identity: &NoiseChannelIdentity,
http_client_factory: HttpClientFactory,
) -> Result<(JsonRpcConnection, ExecServerClientConnectOptions), ExecServerError> {
let open_connection = |bundle: NoiseRendezvousConnectBundle| {
Self::open_noise_rendezvous_connection(NoiseRendezvousConnectArgs {
@@ -171,6 +183,7 @@ impl ExecServerClient {
connect_timeout: DEFAULT_REMOTE_EXEC_SERVER_CONNECT_TIMEOUT,
initialize_timeout: DEFAULT_REMOTE_EXEC_SERVER_INITIALIZE_TIMEOUT,
resume_session_id: None,
http_client_factory: http_client_factory.clone(),
})
};
let bundle = provider.connect_bundle(identity.public_key()).await?;
@@ -212,16 +225,34 @@ impl ExecServerClient {
ensure_rustls_crypto_provider();
let websocket_url = args.websocket_url.clone();
let connect_timeout = args.connect_timeout;
let (stream, _) = timeout(connect_timeout, connect_async(websocket_url.as_str()))
.await
.map_err(|_| ExecServerError::WebSocketConnectTimeout {
url: websocket_url.clone(),
timeout: connect_timeout,
})?
let request = websocket_url
.as_str()
.into_client_request()
.map_err(|source| ExecServerError::WebSocketConnect {
url: websocket_url.clone(),
source,
})?;
let connector = WebSocketConnector::new_with_tls_mode(
&args.http_client_factory,
WebSocketTlsMode::TungsteniteDefault,
)
.map_err(|error| ExecServerError::WebSocketConfiguration(error.to_string()))?;
let (stream, _) = timeout(
connect_timeout,
connector.connect(
request,
tokio_tungstenite::tungstenite::protocol::WebSocketConfig::default(),
),
)
.await
.map_err(|_| ExecServerError::WebSocketConnectTimeout {
url: websocket_url.clone(),
timeout: connect_timeout,
})?
.map_err(|source| ExecServerError::WebSocketConnect {
url: websocket_url.clone(),
source,
})?;
let connection_label = format!("exec-server websocket {websocket_url}");
let connection = if is_rendezvous_harness_url(&websocket_url) {
@@ -267,6 +298,7 @@ impl ExecServerClient {
connect_timeout,
initialize_timeout,
resume_session_id,
http_client_factory,
} = args;
let NoiseRendezvousConnectBundle {
websocket_url,
@@ -292,13 +324,13 @@ impl ExecServerClient {
.extend(current_trace_context_headers());
let (stream, _) = timeout(
connect_timeout,
connect_async_with_config(
request,
Some(noise_relay_websocket_config()),
// Rendezvous sends small, latency-sensitive frames, so avoid Nagle's coalescing delay.
/*disable_nagle*/
true,
),
WebSocketConnector::new_with_tls_mode(
&http_client_factory,
WebSocketTlsMode::TungsteniteDefault,
)
.map_err(|error| ExecServerError::WebSocketConfiguration(error.to_string()))?
.with_tcp_nodelay()
.connect(request, noise_relay_websocket_config()),
)
.await
.map_err(|_| ExecServerError::WebSocketConnectTimeout {