|
| 1 | +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +use std::net::SocketAddr; |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +use bytes::Bytes; |
| 7 | +use http_body_util::BodyExt; |
| 8 | +use tokio::io::AsyncReadExt; |
| 9 | +use tokio::io::AsyncWriteExt; |
| 10 | + |
| 11 | +use super::create_http_client; |
| 12 | +use super::CreateHttpClientOptions; |
| 13 | + |
| 14 | +static EXAMPLE_CRT: &[u8] = include_bytes!("../tls/testdata/example1_cert.der"); |
| 15 | +static EXAMPLE_KEY: &[u8] = |
| 16 | + include_bytes!("../tls/testdata/example1_prikey.der"); |
| 17 | + |
| 18 | +#[tokio::test] |
| 19 | +async fn test_https_proxy_http11() { |
| 20 | + let src_addr = create_https_server(false).await; |
| 21 | + let prx_addr = create_http_proxy(src_addr).await; |
| 22 | + run_test_client(prx_addr, src_addr, false, http::Version::HTTP_11).await; |
| 23 | +} |
| 24 | + |
| 25 | +#[tokio::test] |
| 26 | +async fn test_https_proxy_h2() { |
| 27 | + let src_addr = create_https_server(true).await; |
| 28 | + let prx_addr = create_http_proxy(src_addr).await; |
| 29 | + run_test_client(prx_addr, src_addr, false, http::Version::HTTP_2).await; |
| 30 | +} |
| 31 | + |
| 32 | +#[tokio::test] |
| 33 | +async fn test_https_proxy_https_h2() { |
| 34 | + let src_addr = create_https_server(true).await; |
| 35 | + let prx_addr = create_https_proxy(src_addr).await; |
| 36 | + run_test_client(prx_addr, src_addr, true, http::Version::HTTP_2).await; |
| 37 | +} |
| 38 | + |
| 39 | +async fn run_test_client( |
| 40 | + prx_addr: SocketAddr, |
| 41 | + src_addr: SocketAddr, |
| 42 | + https: bool, |
| 43 | + ver: http::Version, |
| 44 | +) { |
| 45 | + let client = create_http_client( |
| 46 | + "fetch/test", |
| 47 | + CreateHttpClientOptions { |
| 48 | + root_cert_store: None, |
| 49 | + ca_certs: vec![], |
| 50 | + proxy: Some(deno_tls::Proxy { |
| 51 | + url: format!("http{}://{}", if https { "s" } else { "" }, prx_addr), |
| 52 | + basic_auth: None, |
| 53 | + }), |
| 54 | + unsafely_ignore_certificate_errors: Some(vec![]), |
| 55 | + client_cert_chain_and_key: None, |
| 56 | + pool_max_idle_per_host: None, |
| 57 | + pool_idle_timeout: None, |
| 58 | + http1: true, |
| 59 | + http2: true, |
| 60 | + }, |
| 61 | + ) |
| 62 | + .unwrap(); |
| 63 | + |
| 64 | + let req = http::Request::builder() |
| 65 | + .uri(format!("https://{}/foo", src_addr)) |
| 66 | + .body( |
| 67 | + http_body_util::Empty::new() |
| 68 | + .map_err(|err| match err {}) |
| 69 | + .boxed(), |
| 70 | + ) |
| 71 | + .unwrap(); |
| 72 | + let resp = client.send(req).await.unwrap(); |
| 73 | + assert_eq!(resp.status(), http::StatusCode::OK); |
| 74 | + assert_eq!(resp.version(), ver); |
| 75 | + let hello = resp.collect().await.unwrap().to_bytes(); |
| 76 | + assert_eq!(hello, "hello from server"); |
| 77 | +} |
| 78 | + |
| 79 | +async fn create_https_server(allow_h2: bool) -> SocketAddr { |
| 80 | + let mut tls_config = deno_tls::rustls::server::ServerConfig::builder() |
| 81 | + .with_no_client_auth() |
| 82 | + .with_single_cert( |
| 83 | + vec![EXAMPLE_CRT.into()], |
| 84 | + webpki::types::PrivateKeyDer::try_from(EXAMPLE_KEY).unwrap(), |
| 85 | + ) |
| 86 | + .unwrap(); |
| 87 | + if allow_h2 { |
| 88 | + tls_config.alpn_protocols.push("h2".into()); |
| 89 | + } |
| 90 | + tls_config.alpn_protocols.push("http/1.1".into()); |
| 91 | + let tls_acceptor = tokio_rustls::TlsAcceptor::from(Arc::from(tls_config)); |
| 92 | + let src_tcp = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 93 | + let src_addr = src_tcp.local_addr().unwrap(); |
| 94 | + |
| 95 | + tokio::spawn(async move { |
| 96 | + while let Ok((sock, _)) = src_tcp.accept().await { |
| 97 | + let conn = tls_acceptor.accept(sock).await.unwrap(); |
| 98 | + if conn.get_ref().1.alpn_protocol() == Some(b"h2") { |
| 99 | + let fut = hyper::server::conn::http2::Builder::new( |
| 100 | + hyper_util::rt::TokioExecutor::new(), |
| 101 | + ) |
| 102 | + .serve_connection( |
| 103 | + hyper_util::rt::TokioIo::new(conn), |
| 104 | + hyper::service::service_fn(|_req| async { |
| 105 | + Ok::<_, std::convert::Infallible>(http::Response::new( |
| 106 | + http_body_util::Full::<Bytes>::new("hello from server".into()), |
| 107 | + )) |
| 108 | + }), |
| 109 | + ); |
| 110 | + tokio::spawn(fut); |
| 111 | + } else { |
| 112 | + let fut = hyper::server::conn::http1::Builder::new().serve_connection( |
| 113 | + hyper_util::rt::TokioIo::new(conn), |
| 114 | + hyper::service::service_fn(|_req| async { |
| 115 | + Ok::<_, std::convert::Infallible>(http::Response::new( |
| 116 | + http_body_util::Full::<Bytes>::new("hello from server".into()), |
| 117 | + )) |
| 118 | + }), |
| 119 | + ); |
| 120 | + tokio::spawn(fut); |
| 121 | + } |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + src_addr |
| 126 | +} |
| 127 | + |
| 128 | +async fn create_http_proxy(src_addr: SocketAddr) -> SocketAddr { |
| 129 | + let prx_tcp = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 130 | + let prx_addr = prx_tcp.local_addr().unwrap(); |
| 131 | + |
| 132 | + tokio::spawn(async move { |
| 133 | + while let Ok((mut sock, _)) = prx_tcp.accept().await { |
| 134 | + let fut = async move { |
| 135 | + let mut buf = [0u8; 4096]; |
| 136 | + let _n = sock.read(&mut buf).await.unwrap(); |
| 137 | + assert_eq!(&buf[..7], b"CONNECT"); |
| 138 | + let mut dst_tcp = |
| 139 | + tokio::net::TcpStream::connect(src_addr).await.unwrap(); |
| 140 | + sock.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await.unwrap(); |
| 141 | + tokio::io::copy_bidirectional(&mut sock, &mut dst_tcp) |
| 142 | + .await |
| 143 | + .unwrap(); |
| 144 | + }; |
| 145 | + tokio::spawn(fut); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + prx_addr |
| 150 | +} |
| 151 | + |
| 152 | +async fn create_https_proxy(src_addr: SocketAddr) -> SocketAddr { |
| 153 | + let mut tls_config = deno_tls::rustls::server::ServerConfig::builder() |
| 154 | + .with_no_client_auth() |
| 155 | + .with_single_cert( |
| 156 | + vec![EXAMPLE_CRT.into()], |
| 157 | + webpki::types::PrivateKeyDer::try_from(EXAMPLE_KEY).unwrap(), |
| 158 | + ) |
| 159 | + .unwrap(); |
| 160 | + // Set ALPN, to check our proxy connector. But we shouldn't receive anything. |
| 161 | + tls_config.alpn_protocols.push("h2".into()); |
| 162 | + tls_config.alpn_protocols.push("http/1.1".into()); |
| 163 | + let tls_acceptor = tokio_rustls::TlsAcceptor::from(Arc::from(tls_config)); |
| 164 | + let prx_tcp = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 165 | + let prx_addr = prx_tcp.local_addr().unwrap(); |
| 166 | + |
| 167 | + tokio::spawn(async move { |
| 168 | + while let Ok((sock, _)) = prx_tcp.accept().await { |
| 169 | + let mut sock = tls_acceptor.accept(sock).await.unwrap(); |
| 170 | + assert_eq!(sock.get_ref().1.alpn_protocol(), None); |
| 171 | + |
| 172 | + let fut = async move { |
| 173 | + let mut buf = [0u8; 4096]; |
| 174 | + let _n = sock.read(&mut buf).await.unwrap(); |
| 175 | + assert_eq!(&buf[..7], b"CONNECT"); |
| 176 | + let mut dst_tcp = |
| 177 | + tokio::net::TcpStream::connect(src_addr).await.unwrap(); |
| 178 | + sock.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await.unwrap(); |
| 179 | + tokio::io::copy_bidirectional(&mut sock, &mut dst_tcp) |
| 180 | + .await |
| 181 | + .unwrap(); |
| 182 | + }; |
| 183 | + tokio::spawn(fut); |
| 184 | + } |
| 185 | + }); |
| 186 | + |
| 187 | + prx_addr |
| 188 | +} |
0 commit comments