#![deny(warnings)] use std::net::SocketAddr; use bytes::Bytes; use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full}; use hyper::service::service_fn; use hyper::upgrade::Upgraded; use hyper::{Method, Request, Response}; use tokio::net::{TcpListener, TcpStream}; #[path = "../benches/support/mod.rs"] mod support; use support::TokioIo; type ClientBuilder = hyper::client::conn::http1::Builder; type ServerBuilder = hyper::server::conn::http1::Builder; // To try this example: // 1. cargo run --example http_proxy // 2. config http_proxy in command line // $ export http_proxy=http://127.0.0.1:8100 // $ export https_proxy=http://127.0.0.1:8100 // 3. send requests // $ curl -i https://www.some_domain.com/ #[tokio::main] async fn main() -> Result<(), Box> { let addr = SocketAddr::from(([127, 0, 0, 1], 8100)); let listener = TcpListener::bind(addr).await?; println!("Listening on http://{}", addr); loop { let (stream, _) = listener.accept().await?; let io = TokioIo::new(stream); tokio::task::spawn(async move { if let Err(err) = ServerBuilder::new() .preserve_header_case(true) .title_case_headers(true) .serve_connection(io, service_fn(proxy)) .with_upgrades() .await { println!("Failed to serve connection: {:?}", err); } }); } } async fn proxy( req: Request, ) -> Result>, hyper::Error> { println!("req: {:?}", req); if Method::CONNECT == req.method() { // Received an HTTP request like: // ``` // CONNECT www.domain.com:443 HTTP/1.1 // Host: www.domain.com:443 // Proxy-Connection: Keep-Alive // ``` // // When HTTP method is CONNECT we should return an empty body // then we can eventually upgrade the connection and talk a new protocol. // // Note: only after client received an empty body with STATUS_OK can the // connection be upgraded, so we can't return a response inside // `on_upgrade` future. if let Some(addr) = host_addr(req.uri()) { tokio::task::spawn(async move { match hyper::upgrade::on(req).await { Ok(upgraded) => { if let Err(e) = tunnel(upgraded, addr).await { eprintln!("server io error: {}", e); }; } Err(e) => eprintln!("upgrade error: {}", e), } }); Ok(Response::new(empty())) } else { eprintln!("CONNECT host is not socket addr: {:?}", req.uri()); let mut resp = Response::new(full("CONNECT must be to a socket address")); *resp.status_mut() = http::StatusCode::BAD_REQUEST; Ok(resp) } } else { let host = req.uri().host().expect("uri has no host"); let port = req.uri().port_u16().unwrap_or(80); let stream = TcpStream::connect((host, port)).await.unwrap(); let io = TokioIo::new(stream); let (mut sender, conn) = ClientBuilder::new() .preserve_header_case(true) .title_case_headers(true) .handshake(io) .await?; tokio::task::spawn(async move { if let Err(err) = conn.await { println!("Connection failed: {:?}", err); } }); let resp = sender.send_request(req).await?; Ok(resp.map(|b| b.boxed())) } } fn host_addr(uri: &http::Uri) -> Option { uri.authority().map(|auth| auth.to_string()) } fn empty() -> BoxBody { Empty::::new() .map_err(|never| match never {}) .boxed() } fn full>(chunk: T) -> BoxBody { Full::new(chunk.into()) .map_err(|never| match never {}) .boxed() } // Create a TCP connection to host:port, build a tunnel between the connection and // the upgraded connection async fn tunnel(upgraded: Upgraded, addr: String) -> std::io::Result<()> { // Connect to remote server let mut server = TcpStream::connect(addr).await?; let mut upgraded = TokioIo::new(upgraded); // Proxying data let (from_client, from_server) = tokio::io::copy_bidirectional(&mut upgraded, &mut server).await?; // Print message when done println!( "client wrote {} bytes and received {} bytes", from_client, from_server ); Ok(()) }