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

+158 -3
View File
@@ -24,6 +24,7 @@ use tokio::net::TcpListener;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tokio_rustls::TlsAcceptor;
use tokio_tungstenite::MaybeTlsStream;
use tokio_tungstenite::accept_async;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
@@ -34,6 +35,7 @@ use super::*;
use crate::AsyncIo;
use crate::WebSocketConnection;
use crate::WebSocketConnector;
use crate::WebSocketTlsMode;
#[tokio::test]
async fn public_connector_uses_factory_and_exposes_stream_and_sink() {
@@ -48,6 +50,7 @@ async fn public_connector_uses_factory_and_exposes_stream_and_sink() {
.connect(request, WebSocketConfig::default())
.await
.expect("websocket handshake should succeed");
assert!(!websocket_tcp_nodelay(&websocket));
let expected = Message::Text("hello".into());
websocket
.send(expected.clone())
@@ -63,6 +66,120 @@ async fn public_connector_uses_factory_and_exposes_stream_and_sink() {
target_task.await.expect("target task should finish");
}
#[tokio::test]
async fn public_connector_enables_tcp_nodelay_when_requested() {
let (target_addr, target_task) = start_echo_websocket_server(/*acceptor*/ None).await;
let request = format!("ws://localhost:{}/v1/responses", target_addr.port())
.into_client_request()
.expect("websocket request should build");
let factory = HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault);
let connector = WebSocketConnector::new(&factory)
.expect("connector should build")
.with_tcp_nodelay();
let (mut websocket, _) = connector
.connect(request, WebSocketConfig::default())
.await
.expect("websocket handshake should succeed");
assert!(websocket_tcp_nodelay(&websocket));
let expected = Message::Text("latency-sensitive".into());
websocket
.send(expected.clone())
.await
.expect("websocket should send");
assert_eq!(
websocket
.next()
.await
.expect("websocket should receive a message")
.expect("websocket message should be valid"),
expected
);
target_task.await.expect("target task should finish");
}
#[tokio::test]
async fn tungstenite_default_tls_mode_ignores_invalid_custom_ca_in_a_subprocess() {
let (target_addr, target_task) = start_echo_websocket_server(/*acceptor*/ None).await;
let target_url = format!("ws://127.0.0.1:{}/v1/responses", target_addr.port());
let executable = std::env::current_exe().expect("test executable should be available");
let output = tokio::task::spawn_blocking(move || {
let mut command = Command::new(executable);
command.args([
"--exact",
"dialer::tests::tungstenite_default_tls_mode_subprocess_probe",
"--nocapture",
]);
for key in [
"HTTP_PROXY",
"http_proxy",
"HTTPS_PROXY",
"https_proxy",
"ALL_PROXY",
"all_proxy",
"NO_PROXY",
"no_proxy",
"SSL_CERT_FILE",
] {
command.env_remove(key);
}
command
.env(
"CODEX_CA_CERTIFICATE",
"/codex-websocket-client-nonexistent-custom-ca.pem",
)
.env("CODEX_WEBSOCKET_DEFAULT_TLS_PROBE_URL", target_url)
.output()
.expect("WebSocket default-TLS subprocess should run")
})
.await
.expect("WebSocket default-TLS subprocess should join");
assert!(
output.status.success(),
"WebSocket default-TLS subprocess failed\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
target_task.await.expect("target task should finish");
}
#[tokio::test]
async fn tungstenite_default_tls_mode_subprocess_probe() {
let Ok(url) = std::env::var("CODEX_WEBSOCKET_DEFAULT_TLS_PROBE_URL") else {
return;
};
let factory = HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault);
assert!(
WebSocketConnector::new(&factory).is_err(),
"explicit Codex TLS should reject the invalid custom CA"
);
let connector =
WebSocketConnector::new_with_tls_mode(&factory, WebSocketTlsMode::TungsteniteDefault)
.expect("Tungstenite-default TLS should ignore custom CA configuration");
let request = url
.into_client_request()
.expect("websocket request should build");
let (mut websocket, _) = connector
.connect(request, WebSocketConfig::default())
.await
.expect("WebSocket should connect without constructing Codex TLS");
let expected = Message::Text("Tungstenite default TLS".into());
websocket
.send(expected.clone())
.await
.expect("WebSocket should send");
assert_eq!(
websocket
.next()
.await
.expect("WebSocket should receive a message")
.expect("WebSocket message should be valid"),
expected
);
}
#[tokio::test]
async fn direct_route_connects_secure_websocket() {
let (tls_config, acceptor, _) = test_tls_configs();
@@ -74,8 +191,9 @@ async fn direct_route_connects_secure_websocket() {
let (inner, _) = connect(
request,
WebSocketConfig::default(),
tls_config,
Some(tls_config),
OutboundProxyRoute::Direct,
TcpNodelay::Enabled,
)
.await
.expect("direct websocket handshake should succeed");
@@ -157,11 +275,12 @@ async fn no_proxy_subprocess_probe() {
let (inner, _) = connect(
request,
WebSocketConfig::default(),
tls_config,
Some(tls_config),
OutboundProxyRoute::Proxy {
url: proxy_url,
no_proxy: Some(no_proxy),
},
TcpNodelay::Enabled,
)
.await
.expect("websocket handshake should succeed");
@@ -238,6 +357,41 @@ async fn happy_eyeballs_does_not_wait_for_stalled_preferred_family() {
assert_eq!(connected, reachable);
}
#[tokio::test]
async fn routed_tcp_connections_only_enable_nodelay_when_requested() {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener
.local_addr()
.expect("listener should have an address")
.to_string();
let default_stream = connect_tcp(address.clone(), TcpNodelay::Default)
.await
.expect("default TCP connection should succeed");
assert!(!default_stream.nodelay().expect("TCP_NODELAY should read"));
let low_latency_stream = connect_tcp(address, TcpNodelay::Enabled)
.await
.expect("low-latency TCP connection should succeed");
assert!(
low_latency_stream
.nodelay()
.expect("TCP_NODELAY should read")
);
}
fn websocket_tcp_nodelay(websocket: &WebSocketConnection) -> bool {
let ConnectionInner::TransportDefault(stream) = &websocket.inner else {
panic!("default connector should use Tungstenite's transport");
};
let MaybeTlsStream::Plain(stream) = stream.get_ref() else {
panic!("test websocket should use a plain TCP stream");
};
stream.nodelay().expect("TCP_NODELAY should read")
}
async fn start_echo_websocket_server(
acceptor: Option<TlsAcceptor>,
) -> (SocketAddr, JoinHandle<()>) {
@@ -453,11 +607,12 @@ async fn assert_proxy_tunnels_secure_websocket(proxy_tls: bool) {
let (inner, _) = connect(
request,
WebSocketConfig::default(),
tls_config,
Some(tls_config),
OutboundProxyRoute::Proxy {
url: format!("{proxy_scheme}://localhost:{}", proxy_addr.port()),
no_proxy: None,
},
TcpNodelay::Enabled,
)
.await
.expect("proxied websocket handshake should succeed");