From 851c8b37548de5aef3ff67044407ce77b2c77870 Mon Sep 17 00:00:00 2001 From: telotic Date: Sat, 24 Apr 2021 04:41:32 -0700 Subject: [PATCH 1/2] feat(example): support requests to domain names in example http_proxy --- examples/http_proxy.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/http_proxy.rs b/examples/http_proxy.rs index 5911a2dbe8..52d30e32f4 100644 --- a/examples/http_proxy.rs +++ b/examples/http_proxy.rs @@ -18,8 +18,8 @@ type HttpClient = Client; // 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 (don't use a domain name) -// $ curl -i https://8.8.8.8 +// 3. send requests +// $ curl -i https://www.some_domain.com/ #[tokio::main] async fn main() { let addr = SocketAddr::from(([127, 0, 0, 1], 8100)); @@ -88,13 +88,13 @@ async fn proxy(client: HttpClient, req: Request) -> Result, } } -fn host_addr(uri: &http::Uri) -> Option { - uri.authority().and_then(|auth| auth.as_str().parse().ok()) +fn host_addr(uri: &http::Uri) -> Option { + uri.authority().and_then(|auth| Some(auth.to_string())) } // Create a TCP connection to host:port, build a tunnel between the connection and // the upgraded connection -async fn tunnel(upgraded: Upgraded, addr: SocketAddr) -> std::io::Result<()> { +async fn tunnel(upgraded: Upgraded, addr: String) -> std::io::Result<()> { // Connect to remote server let mut server = TcpStream::connect(addr).await?; From 1a982a9add5e92b9175d1a5ef07d6da57f006bfc Mon Sep 17 00:00:00 2001 From: telotic Date: Sat, 24 Apr 2021 04:44:49 -0700 Subject: [PATCH 2/2] refactor(example): simplify tunnel helper using tokio::io::copy_bidirectional --- examples/http_proxy.rs | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/examples/http_proxy.rs b/examples/http_proxy.rs index 52d30e32f4..bc56db1a20 100644 --- a/examples/http_proxy.rs +++ b/examples/http_proxy.rs @@ -3,8 +3,6 @@ use std::convert::Infallible; use std::net::SocketAddr; -use futures_util::future::try_join; - use hyper::service::{make_service_fn, service_fn}; use hyper::upgrade::Upgraded; use hyper::{Body, Client, Method, Request, Response, Server}; @@ -94,32 +92,19 @@ fn host_addr(uri: &http::Uri) -> Option { // 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<()> { +async fn tunnel(mut upgraded: Upgraded, addr: String) -> std::io::Result<()> { // Connect to remote server let mut server = TcpStream::connect(addr).await?; // Proxying data - let amounts = { - let (mut server_rd, mut server_wr) = server.split(); - let (mut client_rd, mut client_wr) = tokio::io::split(upgraded); - - let client_to_server = tokio::io::copy(&mut client_rd, &mut server_wr); - let server_to_client = tokio::io::copy(&mut server_rd, &mut client_wr); - - try_join(client_to_server, server_to_client).await - }; + let (from_client, from_server) = + tokio::io::copy_bidirectional(&mut upgraded, &mut server).await?; // Print message when done - match amounts { - Ok((from_client, from_server)) => { - println!( - "client wrote {} bytes and received {} bytes", - from_client, from_server - ); - } - Err(e) => { - println!("tunnel error: {}", e); - } - }; + println!( + "client wrote {} bytes and received {} bytes", + from_client, from_server + ); + Ok(()) }