diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index cf8d794bf9..195a9db2ea 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -5144,6 +5144,7 @@ dependencies = [ "anyhow", "bytes", "clap", + "h2", "h3", "h3-quinn", "http 1.4.0", @@ -5155,6 +5156,7 @@ dependencies = [ "rustls-native-certs", "serde_json", "tokio", + "tokio-rustls", "url", ] diff --git a/codex-rs/tcp-tunnel/Cargo.toml b/codex-rs/tcp-tunnel/Cargo.toml index 617ed7e067..bbaaaa4c34 100644 --- a/codex-rs/tcp-tunnel/Cargo.toml +++ b/codex-rs/tcp-tunnel/Cargo.toml @@ -11,6 +11,7 @@ doctest = false anyhow = { workspace = true } bytes = { workspace = true } clap = { workspace = true, features = ["derive"] } +h2 = "0.4.19" h3 = { git = "https://github.com/hyperium/h3", rev = "e07e69412876f7e26f026bd75a48b2704d8c8283" } h3-quinn = { git = "https://github.com/hyperium/h3", rev = "e07e69412876f7e26f026bd75a48b2704d8c8283" } http = { workspace = true } @@ -20,6 +21,7 @@ rustls = { workspace = true, features = ["ring", "std"] } rustls-native-certs = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true, features = ["io-util", "macros", "net", "rt-multi-thread", "sync", "time"] } +tokio-rustls = { workspace = true } url = { workspace = true } [dev-dependencies] diff --git a/codex-rs/tcp-tunnel/src/http2.rs b/codex-rs/tcp-tunnel/src/http2.rs new file mode 100644 index 0000000000..f3201cd476 --- /dev/null +++ b/codex-rs/tcp-tunnel/src/http2.rs @@ -0,0 +1,166 @@ +//! TLS HTTP/2 CONNECT transport for the same trusted proxy origin as HTTP/3. +//! Each connection carries one CONNECT and preserves TCP half-close and bounded buffering. + +use std::future::Future; +use std::future::poll_fn; +use std::net::SocketAddr; +use std::pin::Pin; +use std::sync::Arc; +use std::task::Poll; + +use anyhow::Context; +use anyhow::Result; +use anyhow::anyhow; +use anyhow::ensure; +use bytes::Bytes; +use http::Request; +use rustls::pki_types::ServerName; +use tokio::io::AsyncRead; +use tokio::io::AsyncReadExt; +use tokio::io::AsyncWrite; +use tokio::io::AsyncWriteExt; +use tokio::net::TcpStream; +use tokio_rustls::TlsConnector; + +use crate::MAX_FIELD_SECTION_SIZE; +use crate::ProxyTarget; +use crate::diagnostics::Code; + +const COPY_BUFFER_SIZE: usize = 64 * 1024; + +pub(super) type Sender = h2::client::SendRequest; +pub(super) type Connection = + h2::client::Connection, Bytes>; + +pub(super) async fn connect( + target: &ProxyTarget, + tls: Arc, + addresses: &[SocketAddr], +) -> Result<(Sender, Connection)> { + let name = ServerName::try_from(target.host.clone()) + .map_err(|_| anyhow!("invalid proxy TLS server name"))?; + let connector = TlsConnector::from(tls); + for &address in addresses { + let attempt = async { + let socket = TcpStream::connect(address).await?; + socket.set_nodelay(/*nodelay*/ true)?; + let stream = connector.connect(name.clone(), socket).await?; + ensure!( + stream.get_ref().1.alpn_protocol() == Some(b"h2"), + "proxy did not negotiate HTTP/2" + ); + let (sender, mut connection) = h2::client::Builder::new() + .max_header_list_size(MAX_FIELD_SECTION_SIZE as u32) + .max_send_buffer_size(COPY_BUFFER_SIZE) + .initial_max_send_streams(0) + .enable_push(/*enabled*/ false) + .handshake(stream) + .await?; + // A TLS peer advertising h2 is not usable until it sends SETTINGS + // admitting a stream. Poll the connection without sending credentials. + poll_fn(|cx| { + if let Poll::Ready(result) = Pin::new(&mut connection).poll(cx) { + return Poll::Ready(Err(result + .err() + .map(anyhow::Error::from) + .unwrap_or_else(|| anyhow!("HTTP/2 proxy closed during setup")))); + } + if connection.max_concurrent_send_streams() > 0 { + Poll::Ready(Ok(())) + } else { + Poll::Pending + } + }) + .await?; + Ok::<_, anyhow::Error>((sender, connection)) + }; + match tokio::time::timeout(std::time::Duration::from_secs(5), attempt).await { + Ok(Ok(connection)) => return Ok(connection), + Ok(Err(error)) + if error.chain().any(|cause| { + matches!( + cause.downcast_ref::(), + Some(rustls::Error::InvalidCertificate(_)) + ) + }) => + { + return Err(anyhow!("HTTP/2 proxy certificate verification failed")); + } + _ => {} + } + } + Err(anyhow!("HTTP/2 proxy connection failed")) +} + +pub(super) async fn admit( + sender: Sender, + request: Request<()>, +) -> Result<(h2::SendStream, h2::RecvStream)> { + let mut sender = sender.ready().await.map_err(|_| stream_error())?; + let (response, mut send) = sender + .send_request(request, /*end_of_stream*/ false) + .map_err(|_| stream_error())?; + let response = response.await.map_err(|_| stream_error())?; + let status = response.status(); + if !status.is_success() { + send.send_reset(h2::Reason::CANCEL); + return Err(Code::Rejected(status).into()); + } + Ok((send, response.into_body())) +} + +pub(super) async fn bridge( + mut read: impl AsyncRead + Unpin, + mut write: impl AsyncWrite + Unpin, + mut send: h2::SendStream, + mut recv: h2::RecvStream, + exit_on_remote_eof: bool, +) -> Result<()> { + let upload = async { + let mut buffer = vec![0; COPY_BUFFER_SIZE]; + loop { + let count = read.read(&mut buffer).await.context("reading local TCP")?; + if count == 0 { + send.send_data(Bytes::new(), /*end_of_stream*/ true) + .map_err(|_| stream_error())?; + return Ok::<(), anyhow::Error>(()); + } + let mut data = Bytes::copy_from_slice(&buffer[..count]); + send.reserve_capacity(data.len()); + while !data.is_empty() { + let capacity = poll_fn(|cx| send.poll_capacity(cx)) + .await + .context("HTTP/2 send stream closed")? + .map_err(|_| stream_error())?; + let count = capacity.min(data.len()); + send.send_data(data.split_to(count), /*end_of_stream*/ false) + .map_err(|_| stream_error())?; + } + } + }; + let download = async { + while let Some(data) = recv.data().await { + let data = data.map_err(|_| stream_error())?; + write.write_all(&data).await.context("writing local TCP")?; + recv.flow_control() + .release_capacity(data.len()) + .map_err(|_| stream_error())?; + } + write.shutdown().await.context("half-closing local TCP")?; + Ok::<(), anyhow::Error>(()) + }; + tokio::pin!(upload, download); + tokio::select! { + result = &mut upload => { result?; download.await } + result = &mut download => { result?; if exit_on_remote_eof { Ok(()) } else { upload.await } } + } +} + +fn stream_error() -> anyhow::Error { + // Peer-supplied GOAWAY debug text must not reach diagnostics. + anyhow!("HTTP/2 CONNECT stream failed") +} + +#[cfg(test)] +#[path = "http2_tests.rs"] +pub(super) mod tests; diff --git a/codex-rs/tcp-tunnel/src/http2_tests.rs b/codex-rs/tcp-tunnel/src/http2_tests.rs new file mode 100644 index 0000000000..2d160529d4 --- /dev/null +++ b/codex-rs/tcp-tunnel/src/http2_tests.rs @@ -0,0 +1,191 @@ +//! Test TLS admission, stream forwarding, and CONNECT rejection. +#![cfg(test)] + +use std::sync::Arc; +use std::time::Duration; + +use anyhow::Result; +use bytes::Bytes; +use http::HeaderMap; +use http::HeaderValue; +use http::Method; +use http::Response; +use pretty_assertions::assert_eq; +use rcgen::CertifiedKey; +use rcgen::generate_simple_self_signed; +use rustls::pki_types::PrivatePkcs8KeyDer; +use tokio::io::AsyncReadExt; +use tokio::io::AsyncWriteExt; +use tokio::net::TcpListener; +use tokio::time::timeout; +use tokio_rustls::TlsAcceptor; + +use super::connect; +use crate::ProxyTarget; +use crate::diagnostics::Code; +use crate::transport::Connection; + +pub(crate) fn tls_configs() -> Result<(rustls::ServerConfig, rustls::ClientConfig)> { + let CertifiedKey { cert, signing_key } = + generate_simple_self_signed(vec!["127.0.0.1".to_string()])?; + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let mut server = rustls::ServerConfig::builder_with_provider(provider.clone()) + .with_safe_default_protocol_versions()? + .with_no_client_auth() + .with_single_cert( + vec![cert.der().clone()], + PrivatePkcs8KeyDer::from(signing_key.serialize_der()).into(), + )?; + server.alpn_protocols = vec![b"h3".to_vec(), b"h2".to_vec()]; + let mut roots = rustls::RootCertStore::empty(); + roots.add(cert.der().clone())?; + let mut client = rustls::ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions()? + .with_root_certificates(roots) + .with_no_client_auth(); + client.alpn_protocols = vec![b"h2".to_vec()]; + Ok((server, client)) +} + +pub(crate) fn target(address: std::net::SocketAddr) -> ProxyTarget { + ProxyTarget { + host: address.ip().to_string(), + port: address.port(), + authority: "127.0.0.1:22".to_string(), + } +} + +#[tokio::test] +async fn trusted_h2_requires_settings_and_certificate() -> Result<()> { + timeout(Duration::from_secs(5), async { + let (server, client) = tls_configs()?; + let listener = TcpListener::bind("127.0.0.1:0").await?; + let address = listener.local_addr()?; + let task = tokio::spawn(async move { + let tls = TlsAcceptor::from(Arc::new(server)) + .accept(listener.accept().await?.0) + .await?; + let mut connection: h2::server::Connection<_, Bytes> = + h2::server::handshake(tls).await?; + let _ = connection.accept().await; + Ok::<_, anyhow::Error>(()) + }); + let (sender, connection) = connect(&target(address), Arc::new(client), &[address]).await?; + drop((sender, connection)); + task.await??; + + let (bad_server, _) = tls_configs()?; + let (_, client) = tls_configs()?; + let listener = TcpListener::bind("127.0.0.1:0").await?; + let address = listener.local_addr()?; + let task = tokio::spawn(async move { + TlsAcceptor::from(Arc::new(bad_server)) + .accept(listener.accept().await.unwrap().0) + .await + }); + assert!( + connect(&target(address), Arc::new(client), &[address]) + .await + .is_err() + ); + let _ = task.await?; + Ok::<_, anyhow::Error>(()) + }) + .await? +} + +#[tokio::test] +async fn forwards_bounded_data_in_both_half_close_orders_and_rejects_before_upload() -> Result<()> { + timeout(Duration::from_secs(15), async { + for (remote_first, status) in [(false, 200), (true, 200), (false, 403)] { + let (server, client) = tls_configs()?; + let listener = TcpListener::bind("127.0.0.1:0").await?; + let address = listener.local_addr()?; + let upload = vec![42; 192 * 1024]; + let download = vec![81; 192 * 1024]; + let expected_upload = upload.clone(); + let response_bytes = download.clone(); + let (done, finished) = tokio::sync::oneshot::channel(); + let peer = tokio::spawn(async move { + let tls = TlsAcceptor::from(Arc::new(server)).accept(listener.accept().await?.0).await?; + let mut connection = h2::server::handshake(tls).await?; + let (request, mut respond) = connection.accept().await.unwrap()?; + assert_eq!(request.method(), Method::CONNECT); + assert_eq!(request.uri().authority().unwrap().as_str(), "127.0.0.1:22"); + assert_eq!(request.headers()[http::header::AUTHORIZATION], "Bearer fixture"); + assert_eq!(request.headers()["x-portals-auth-mode"], "oauth"); + assert_eq!(request.headers()["x-portals-environment-id"], "env_fixture"); + assert_eq!(request.headers()["x-portals-port"], "22"); + let mut send = respond.send_response(Response::builder().status(status).body(())?, status != 200)?; + let handler = async move { + if status != 200 { return Ok::<_, anyhow::Error>(()); } + if remote_first { send.send_data(Bytes::from(response_bytes.clone()), true)?; } + let mut body = request.into_body(); + let mut received = Vec::new(); + while let Some(data) = body.data().await { let data = data?; received.extend_from_slice(&data); body.flow_control().release_capacity(data.len())?; } + assert_eq!(received, expected_upload); + if !remote_first { send.send_data(Bytes::from(response_bytes), true)?; } + Ok(()) + }; + let complete = async { handler.await?; finished.await?; Ok::<_, anyhow::Error>(()) }; + tokio::pin!(complete); + tokio::select! { result = &mut complete => result, closed = std::future::poll_fn(|cx| connection.poll_closed(cx)) => { closed?; complete.await } } + }); + let (sender, connection) = connect(&target(address), Arc::new(client), &[address]).await?; + let mut headers = HeaderMap::new(); + headers.insert("x-portals-auth-mode", HeaderValue::from_static("oauth")); + headers.insert("x-portals-environment-id", HeaderValue::from_static("env_fixture")); + headers.insert("x-portals-port", HeaderValue::from_static("22")); + let request = target(address).request(headers, HeaderValue::from_static("Bearer fixture"))?; + let (mut local, socket) = tokio::io::duplex(256 * 1024); + let (input, output) = tokio::io::split(socket); + let relay = tokio::spawn(async move { + Connection::Http2(sender, Box::new(connection)).admit(request).await? + .relay(input, output, /*exit_on_remote_eof*/ false).await + }); + if status == 200 { + let mut received = Vec::new(); + if remote_first { local.read_to_end(&mut received).await?; local.write_all(&upload).await?; local.shutdown().await?; } + else { local.write_all(&upload).await?; local.shutdown().await?; local.read_to_end(&mut received).await?; } + assert_eq!(received.len(), download.len()); + assert!(received == download); + relay.await??; + } else { + local.write_all(b"must not be forwarded").await?; + let error = relay.await?.unwrap_err(); + assert!(matches!(error.downcast_ref::(), Some(Code::Rejected(status)) if status.as_u16() == 403)); + } + let _ = done.send(()); + peer.await??; + } + Ok::<_, anyhow::Error>(()) + }).await? +} + +#[tokio::test] +async fn retries_next_address_after_non_certificate_tls_failure() -> Result<()> { + timeout(Duration::from_secs(5), async { + let (server, client) = tls_configs()?; + let bad = TcpListener::bind("127.0.0.1:0").await?; + let good = TcpListener::bind("127.0.0.1:0").await?; + let addresses = [bad.local_addr()?, good.local_addr()?]; + let bad_task = tokio::spawn(async move { + drop(bad.accept().await.unwrap()); + }); + let good_task = tokio::spawn(async move { + let tls = TlsAcceptor::from(Arc::new(server)) + .accept(good.accept().await?.0) + .await?; + let mut connection: h2::server::Connection<_, Bytes> = + h2::server::handshake(tls).await?; + let _ = connection.accept().await; + Ok::<_, anyhow::Error>(()) + }); + let connection = connect(&target(addresses[0]), Arc::new(client), &addresses).await?; + drop(connection); + bad_task.await?; + good_task.await??; + Ok::<_, anyhow::Error>(()) + }) + .await? +} diff --git a/codex-rs/tcp-tunnel/src/lib.rs b/codex-rs/tcp-tunnel/src/lib.rs index cac930c953..4eef966dcd 100644 --- a/codex-rs/tcp-tunnel/src/lib.rs +++ b/codex-rs/tcp-tunnel/src/lib.rs @@ -39,7 +39,9 @@ use url::Url; mod control; mod diagnostics; +mod http2; mod ssh; +mod transport; pub use ssh::run_ssh_proxy; @@ -330,7 +332,7 @@ fn control_input( Ok((initial_metadata, tokens)) } -fn client_config(mut transport: quinn::TransportConfig) -> Result { +fn tls_config() -> Result { let native = rustls_native_certs::load_native_certs(); let mut roots = rustls::RootCertStore::empty(); roots.add_parsable_certificates(native.certs); @@ -339,12 +341,16 @@ fn client_config(mut transport: quinn::TransportConfig) -> Result Result { + let mut tls = tls_config()?; tls.alpn_protocols = vec![b"h3".to_vec()]; let mut config = quinn::ClientConfig::new(Arc::new(QuicClientConfig::try_from(tls)?)); transport.max_idle_timeout(Some(Duration::from_secs(30).try_into()?)); @@ -447,7 +453,7 @@ async fn serve( let request = target.request(headers.connect.clone(), headers.auth.borrow().clone())?; tokio::spawn(async move { let (read, write) = socket.into_split(); - if let Err(error) = bridge(read, write, &mut sender, request, BridgeMode::Listener(draining)).await { + if let Err(error) = bridge(read, write, &mut sender, request, draining).await { diagnostics.report( Phase::Connect, &error, @@ -530,29 +536,35 @@ async fn serve( } } -enum BridgeMode { - Listener(mpsc::Sender<()>), - ProxyCommand, -} - async fn bridge( - mut local_read: impl AsyncRead + Unpin, - mut local_write: impl AsyncWrite + Unpin, + local_read: impl AsyncRead + Unpin, + local_write: impl AsyncWrite + Unpin, sender: &mut h3::client::SendRequest, request: Request<()>, - mode: BridgeMode, + draining: mpsc::Sender<()>, ) -> Result<()> { let mut stream = match sender.send_request(request).await { Ok(stream) => stream, Err(error) => { - if let BridgeMode::Listener(draining) = &mode - && matches!(error, h3::error::StreamError::RemoteClosing { .. }) - { + if matches!(error, h3::error::StreamError::RemoteClosing { .. }) { let _ = draining.try_send(()); } return Err(error).context("sending CONNECT"); } }; + check_h3_response(&mut stream).await?; + bridge_h3_stream( + local_read, + local_write, + stream, + /*exit_on_remote_eof*/ false, + ) + .await +} + +type H3Stream = h3::client::RequestStream, Bytes>; + +async fn check_h3_response(stream: &mut H3Stream) -> Result<()> { let status = stream .recv_response() .await @@ -561,6 +573,15 @@ async fn bridge( if !status.is_success() { return Err(Code::Rejected(status).into()); } + Ok(()) +} + +async fn bridge_h3_stream( + mut local_read: impl AsyncRead + Unpin, + mut local_write: impl AsyncWrite + Unpin, + stream: H3Stream, + exit_on_remote_eof: bool, +) -> Result<()> { let (mut send, mut recv) = stream.split(); let upload = async { let mut buffer = vec![0; 64 * 1024]; @@ -591,10 +612,7 @@ async fn bridge( result = &mut upload => { result?; download.await } result = &mut download => { result?; - match mode { - BridgeMode::Listener(_) => upload.await, - BridgeMode::ProxyCommand => Ok(()), - } + if exit_on_remote_eof { Ok(()) } else { upload.await } } } } diff --git a/codex-rs/tcp-tunnel/src/lib_tests.rs b/codex-rs/tcp-tunnel/src/lib_tests.rs index a72909f46b..38e4d33875 100644 --- a/codex-rs/tcp-tunnel/src/lib_tests.rs +++ b/codex-rs/tcp-tunnel/src/lib_tests.rs @@ -327,16 +327,17 @@ async fn stdio_propagates_half_close_and_exits_on_gateway_eof_or_rejection() -> input.shutdown().await?; } let mut output = Vec::new(); - let result = timeout( - Duration::from_secs(3), - super::bridge( + let result = timeout(Duration::from_secs(3), async { + let mut stream = sender.send_request(request).await?; + super::check_h3_response(&mut stream).await?; + super::bridge_h3_stream( reader, &mut output, - &mut sender, - request, - super::BridgeMode::ProxyCommand, - ), - ) + stream, + /*exit_on_remote_eof*/ true, + ) + .await + }) .await?; match index { 0 => { diff --git a/codex-rs/tcp-tunnel/src/ssh.rs b/codex-rs/tcp-tunnel/src/ssh.rs index ad9c14a168..ef5135792c 100644 --- a/codex-rs/tcp-tunnel/src/ssh.rs +++ b/codex-rs/tcp-tunnel/src/ssh.rs @@ -13,13 +13,10 @@ use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt; use super::Args; -use super::BridgeMode; use super::Code; -use super::ProxyConnection; use super::ProxyTarget; -use super::bridge; -use super::client_config; use super::control::bearer_header; +use super::transport::Config as TransportConfig; // Only gateways shipped with the CLI may receive the signed-in account's credential. pub(super) const PUBLIC_GATEWAY: &str = "https://codex-portals.api.openai.com"; @@ -84,17 +81,12 @@ pub async fn run_ssh_proxy( stdout: impl AsyncWrite + Unpin, ) -> Result<()> { let (target, headers) = args.ssh_destination()?; - // Linux can advertise UDP segmentation on paths where batched packets are lost. - // Single datagrams keep bulk SSH transfers moving on those paths. - let mut transport = quinn::TransportConfig::default(); - transport.enable_segmentation_offload(!cfg!(target_os = "linux")); - let ProxyConnection { - endpoint: _endpoint, - quic: _quic, - mut connection, - mut sender, - } = ProxyConnection::connect(&target, &client_config(transport)?).await?; - let request = target.request(headers, oauth_bearer(&access_token.await?)?)?; + let tunnel = TransportConfig::new(super::tls_config()?)? + .open(&target, async { + target.request(headers, oauth_bearer(&access_token.await?)?) + }) + .await + .map_err(safe_tunnel_error)?; // OpenSSH needs EOF when the peer finishes, even if it still holds stdin open. // A detached blocking reader lets the Tokio runtime exit in that case. @@ -120,13 +112,7 @@ pub async fn run_ssh_proxy( }; let _ = outcome.send(result); })?; - let relay = bridge( - reader, - stdout, - &mut sender, - request, - BridgeMode::ProxyCommand, - ); + let relay = tunnel.relay(reader, stdout, /*exit_on_remote_eof*/ true); let forward = async { tokio::pin!(relay); tokio::select! { @@ -134,17 +120,17 @@ pub async fn run_ssh_proxy( result = input_done => { result??; relay.await } } }; - tokio::pin!(forward); - let result = tokio::select! { - result = &mut forward => result, - // A closing connection may still have a complete status or payload buffered. - _ = connection.wait_idle() => forward.await, - }; + let result = forward.await; // A peer's close reason can contain arbitrary text. Only surface the HTTP status. - result.map_err(|err| match err.downcast_ref::() { + result.map_err(safe_tunnel_error) +} + +fn safe_tunnel_error(err: anyhow::Error) -> anyhow::Error { + match err.downcast_ref::() { Some(Code::Rejected(status)) => Code::Rejected(*status).into(), + Some(Code::Timeout) => Code::Timeout.into(), _ => anyhow::anyhow!("SSH tunnel stream failed"), - }) + } } #[cfg(test)] diff --git a/codex-rs/tcp-tunnel/src/ssh_tests.rs b/codex-rs/tcp-tunnel/src/ssh_tests.rs index 690c2771e6..a4d546d5c0 100644 --- a/codex-rs/tcp-tunnel/src/ssh_tests.rs +++ b/codex-rs/tcp-tunnel/src/ssh_tests.rs @@ -5,6 +5,16 @@ use super::Args; use super::PUBLIC_GATEWAY; use super::oauth_bearer; use super::run_ssh_proxy; + +#[test] +fn setup_and_relay_errors_do_not_expose_peer_debug_text() { + let error = super::safe_tunnel_error(anyhow::anyhow!("peer supplied secret")); + assert!(!error.to_string().contains("secret")); + let denied = super::safe_tunnel_error( + crate::diagnostics::Code::Rejected(http::StatusCode::FORBIDDEN).into(), + ); + assert!(denied.to_string().contains("403")); +} use clap::Parser; use http::HeaderMap; use http::Method; diff --git a/codex-rs/tcp-tunnel/src/transport.rs b/codex-rs/tcp-tunnel/src/transport.rs new file mode 100644 index 0000000000..5da49a3850 --- /dev/null +++ b/codex-rs/tcp-tunnel/src/transport.rs @@ -0,0 +1,209 @@ +//! Select the first authenticated CONNECT, preferring QUIC with a short head start. +use std::future::Future; +use std::sync::Arc; +use std::time::Duration; + +use anyhow::Context; +use anyhow::Result; +use anyhow::anyhow; +use bytes::Bytes; +use http::Request; +use quinn::crypto::rustls::QuicClientConfig; +use tokio::io::AsyncRead; +use tokio::io::AsyncWrite; + +use crate::PROXY_CONNECT_TIMEOUT; +use crate::ProxyConnection; +use crate::ProxyTarget; +use crate::diagnostics::Code; +use crate::http2; + +const QUIC_HEAD_START: Duration = Duration::from_millis(250); + +#[derive(Clone)] +pub(super) struct Config { + quic: quinn::ClientConfig, + tls: Arc, +} + +impl Config { + pub(super) fn new(mut tls: rustls::ClientConfig) -> Result { + tls.enable_early_data = false; + tls.alpn_protocols = vec![b"h3".to_vec()]; + let mut quic = quinn::ClientConfig::new(Arc::new(QuicClientConfig::try_from(tls.clone())?)); + let mut transport = quinn::TransportConfig::default(); + transport.enable_segmentation_offload(!cfg!(target_os = "linux")); + transport.max_idle_timeout(Some(Duration::from_secs(30).try_into()?)); + transport.keep_alive_interval(Some(Duration::from_secs(10))); + quic.transport_config(Arc::new(transport)); + tls.alpn_protocols = vec![b"h2".to_vec()]; + Ok(Self { + quic, + tls: Arc::new(tls), + }) + } + + /// The request future is polled only after a gateway has passed TLS verification. + pub(super) async fn open( + &self, + target: &ProxyTarget, + request: impl Future>>, + ) -> Result { + tokio::time::timeout(PROXY_CONNECT_TIMEOUT, async { + let quic = async { + ProxyConnection::connect(target, &self.quic) + .await + .map(|p| Connection::Http3(Box::new(p))) + }; + let tcp = async { + tokio::time::sleep(QUIC_HEAD_START).await; + let addresses: Vec<_> = + tokio::net::lookup_host((target.host.as_str(), target.port)) + .await? + .collect(); + http2::connect(target, self.tls.clone(), &addresses) + .await + .map(|(sender, connection)| Connection::Http2(sender, Box::new(connection))) + }; + tokio::pin!(quic, tcp); + tokio::select! { + biased; + first = &mut quic => finish_setup(first, tcp, request).await, + first = &mut tcp => finish_setup(first, quic, request).await, + } + }) + .await + .context(Code::Timeout)? + } +} + +async fn finish_setup( + first: Result, + other: impl Future>, + request: impl Future>>, +) -> Result { + let first = match first { + Ok(connection) => connection, + Err(_) => return other.await?.admit(request.await?).await, + }; + let request = request.await?; + let preferred = first.admit(request.clone()); + let alternate = async { other.await?.admit(request).await }; + tokio::pin!(preferred, alternate); + tokio::select! { + biased; + result = &mut preferred => complete_or_try_other(result, alternate).await, + result = &mut alternate => complete_or_try_other(result, preferred).await, + } +} + +async fn complete_or_try_other( + result: Result, + other: impl Future>, +) -> Result { + match result { + Ok(tunnel) => Ok(tunnel), + Err(error) + if error + .downcast_ref::() + .is_some_and(|c| matches!(c, Code::Rejected(_))) => + { + Err(error) + } + Err(_) => other.await, + } +} + +pub(super) enum Connection { + Http3(Box), + Http2(http2::Sender, Box), +} + +pub(super) enum Tunnel { + Http3(Box<(ProxyConnection, crate::H3Stream)>), + Http2 { + connection: Box, + send: h2::SendStream, + recv: h2::RecvStream, + }, +} + +impl Connection { + pub(super) async fn admit(self, request: Request<()>) -> Result { + match self { + Self::Http3(mut proxy) => { + let admit = async { + let mut stream = proxy + .sender + .send_request(request) + .await + .context("sending CONNECT")?; + crate::check_h3_response(&mut stream).await?; + Ok::<_, anyhow::Error>(stream) + }; + let stream = tokio::select! { + result = admit => result?, + _ = proxy.connection.wait_idle() => return Err(anyhow!("HTTP/3 proxy transport closed")), + }; + Ok(Tunnel::Http3(Box::new((*proxy, stream)))) + } + Self::Http2(sender, mut connection) => { + let (send, recv) = tokio::select! { + result = http2::admit(sender, request) => result?, + _ = &mut *connection => return Err(anyhow!("HTTP/2 proxy transport closed")), + }; + Ok(Tunnel::Http2 { + connection, + send, + recv, + }) + } + } + } +} + +impl Tunnel { + pub(super) async fn relay( + self, + input: impl AsyncRead + Unpin, + output: impl AsyncWrite + Unpin, + exit_on_remote_eof: bool, + ) -> Result<()> { + match self { + Self::Http3(tunnel) => { + let (mut proxy, stream) = *tunnel; + let forward = crate::bridge_h3_stream(input, output, stream, exit_on_remote_eof); + tokio::pin!(forward); + tokio::select! { + result = &mut forward => result, + _ = proxy.connection.wait_idle() => forward.await, + } + } + Self::Http2 { + mut connection, + send, + recv, + } => { + let result = { + let forward = http2::bridge(input, output, send, recv, exit_on_remote_eof); + tokio::pin!(forward); + tokio::select! { + result = &mut forward => result, + result = &mut *connection => { + result.map_err(|_| anyhow!("HTTP/2 proxy transport closed"))?; + return forward.await; + }, + } + }; + result?; + connection + .await + .map_err(|_| anyhow!("HTTP/2 proxy transport closed")) + } + } + } +} + +#[cfg(test)] +#[path = "transport_tests.rs"] +mod tests; diff --git a/codex-rs/tcp-tunnel/src/transport_tests.rs b/codex-rs/tcp-tunnel/src/transport_tests.rs new file mode 100644 index 0000000000..0c09b51aac --- /dev/null +++ b/codex-rs/tcp-tunnel/src/transport_tests.rs @@ -0,0 +1,203 @@ +//! Exercise admission, QUIC preference, and fallback against real H2/H3 peers. +#![cfg(test)] + +use std::net::UdpSocket; +use std::sync::Arc; +use std::time::Duration; + +use anyhow::Result; +use bytes::Bytes; +use http::HeaderMap; +use http::HeaderValue; +use http::Request; +use http::Response; +use http::StatusCode; +use tokio::net::TcpListener; +use tokio::time::timeout; + +use super::Config; +use super::Tunnel; +use crate::http2::tests::target; +use crate::http2::tests::tls_configs; + +async fn sockets() -> Result<(TcpListener, UdpSocket)> { + for _ in 0..32 { + let udp = UdpSocket::bind("127.0.0.1:0")?; + if let Ok(tcp) = TcpListener::bind(udp.local_addr()?).await { + udp.set_nonblocking(true)?; + return Ok((tcp, udp)); + } + } + anyhow::bail!("could not reserve TCP and UDP test port") +} +fn endpoint(udp: UdpSocket, tls: rustls::ServerConfig) -> Result { + let crypto = quinn::crypto::rustls::QuicServerConfig::try_from(tls)?; + Ok(quinn::Endpoint::new( + quinn::EndpointConfig::default(), + Some(quinn::ServerConfig::with_crypto(Arc::new(crypto))), + udp, + Arc::new(quinn::TokioRuntime), + )?) +} +fn request(address: std::net::SocketAddr) -> Result> { + target(address).request(HeaderMap::new(), HeaderValue::from_static("Bearer fixture")) +} +async fn h2_peer(tcp: TcpListener, tls: rustls::ServerConfig) -> Result<()> { + let tls = tokio_rustls::TlsAcceptor::from(Arc::new(tls)) + .accept(tcp.accept().await?.0) + .await?; + let mut conn: h2::server::Connection<_, Bytes> = h2::server::handshake(tls).await?; + let (req, mut send) = conn.accept().await.unwrap()?; + assert_eq!(req.headers()["authorization"], "Bearer fixture"); + let _stream = send.send_response(Response::new(()), false)?; + let _ = conn.accept().await; + Ok(()) +} + +#[tokio::test] +async fn prefers_admitted_quic_without_opening_tcp() -> Result<()> { + timeout(Duration::from_secs(5), async { + let (tls, client) = tls_configs()?; + let (tcp, udp) = sockets().await?; + let address = tcp.local_addr()?; + let endpoint = endpoint(udp, tls)?; + let peer = tokio::spawn(async move { + let quic = endpoint.accept().await.unwrap().await?; + let mut http: h3::server::Connection<_, Bytes> = + h3::server::Connection::new(h3_quinn::Connection::new(quic.clone())).await?; + let (req, mut stream) = http.accept().await?.unwrap().resolve_request().await?; + assert_eq!(req.headers()["authorization"], "Bearer fixture"); + stream.send_response(Response::new(())).await?; + quic.closed().await; + Ok::<_, anyhow::Error>(()) + }); + let tunnel = Config::new(client)? + .open(&target(address), async { request(address) }) + .await?; + assert!(matches!(tunnel, Tunnel::Http3(_))); + assert!( + timeout(Duration::from_millis(20), tcp.accept()) + .await + .is_err() + ); + drop(tunnel); + peer.abort(); + Ok::<_, anyhow::Error>(()) + }) + .await? +} + +#[tokio::test] +async fn blocked_udp_uses_admitted_h2() -> Result<()> { + let (tls, client) = tls_configs()?; + let (tcp, _udp) = sockets().await?; + let address = tcp.local_addr()?; + let peer = tokio::spawn(h2_peer(tcp, tls)); + let tunnel = Config::new(client)? + .open(&target(address), async { request(address) }) + .await?; + assert!(matches!(tunnel, Tunnel::Http2 { .. })); + drop(tunnel); + peer.await??; + Ok(()) +} + +#[tokio::test] +async fn quic_handshake_without_working_http3_does_not_defeat_h2() -> Result<()> { + timeout(Duration::from_secs(3), async { + let (tls, client) = tls_configs()?; + let (tcp, udp) = sockets().await?; + let address = tcp.local_addr()?; + let endpoint = endpoint(udp, tls.clone())?; + let quic = tokio::spawn(async move { + let connection = endpoint.accept().await.unwrap().await?; + connection.closed().await; + Ok::<_, anyhow::Error>(()) + }); + let h2 = tokio::spawn(h2_peer(tcp, tls)); + let tunnel = Config::new(client)? + .open(&target(address), async { request(address) }) + .await?; + assert!(matches!(tunnel, Tunnel::Http2 { .. })); + drop(tunnel); + h2.await??; + quic.abort(); + Ok::<_, anyhow::Error>(()) + }) + .await? +} + +#[tokio::test] +async fn admission_rejection_is_terminal() -> Result<()> { + let (tls, client) = tls_configs()?; + let (tcp, udp) = sockets().await?; + let address = tcp.local_addr()?; + let endpoint = endpoint(udp, tls)?; + let peer = tokio::spawn(async move { + let quic = endpoint.accept().await.unwrap().await?; + let mut http: h3::server::Connection<_, Bytes> = + h3::server::Connection::new(h3_quinn::Connection::new(quic.clone())).await?; + let (_, mut stream) = http.accept().await?.unwrap().resolve_request().await?; + stream + .send_response(Response::builder().status(StatusCode::FORBIDDEN).body(())?) + .await?; + quic.closed().await; + Ok::<_, anyhow::Error>(()) + }); + let error = Config::new(client)? + .open(&target(address), async { request(address) }) + .await + .err() + .unwrap(); + assert!(matches!( + error.downcast_ref::(), + Some(crate::diagnostics::Code::Rejected(StatusCode::FORBIDDEN)) + )); + assert!( + timeout(Duration::from_millis(20), tcp.accept()) + .await + .is_err() + ); + peer.abort(); + Ok(()) +} + +#[tokio::test] +async fn setup_deadline_includes_connect_response() -> Result<()> { + let (tls, client) = tls_configs()?; + let (tcp, udp) = sockets().await?; + let address = tcp.local_addr()?; + let endpoint = endpoint(udp, tls.clone())?; + let quic = tokio::spawn(async move { + let quic = endpoint.accept().await.unwrap().await?; + let mut http: h3::server::Connection<_, Bytes> = + h3::server::Connection::new(h3_quinn::Connection::new(quic.clone())).await?; + let _stream = http.accept().await?.unwrap().resolve_request().await?; + quic.closed().await; + Ok::<_, anyhow::Error>(()) + }); + let h2 = tokio::spawn(async move { + let tls = tokio_rustls::TlsAcceptor::from(Arc::new(tls)) + .accept(tcp.accept().await?.0) + .await?; + let mut conn: h2::server::Connection<_, Bytes> = h2::server::handshake(tls).await?; + let _stream = conn.accept().await.unwrap()?; + let _ = conn.accept().await; + Ok::<_, anyhow::Error>(()) + }); + let result = timeout( + crate::PROXY_CONNECT_TIMEOUT + Duration::from_secs(2), + Config::new(client)?.open(&target(address), async { request(address) }), + ) + .await?; + assert!(matches!( + result + .err() + .unwrap() + .downcast_ref::(), + Some(crate::diagnostics::Code::Timeout) + )); + quic.abort(); + h2.abort(); + Ok(()) +}