docs(example): support requests to domain names in example http_proxy (#2513)
This commit is contained in:
@@ -3,8 +3,6 @@
|
|||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use futures_util::future::try_join;
|
|
||||||
|
|
||||||
use hyper::service::{make_service_fn, service_fn};
|
use hyper::service::{make_service_fn, service_fn};
|
||||||
use hyper::upgrade::Upgraded;
|
use hyper::upgrade::Upgraded;
|
||||||
use hyper::{Body, Client, Method, Request, Response, Server};
|
use hyper::{Body, Client, Method, Request, Response, Server};
|
||||||
@@ -18,8 +16,8 @@ type HttpClient = Client<hyper::client::HttpConnector>;
|
|||||||
// 2. config http_proxy in command line
|
// 2. config http_proxy in command line
|
||||||
// $ export http_proxy=http://127.0.0.1:8100
|
// $ export http_proxy=http://127.0.0.1:8100
|
||||||
// $ export https_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)
|
// 3. send requests
|
||||||
// $ curl -i https://8.8.8.8
|
// $ curl -i https://www.some_domain.com/
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 8100));
|
let addr = SocketAddr::from(([127, 0, 0, 1], 8100));
|
||||||
@@ -88,38 +86,25 @@ async fn proxy(client: HttpClient, req: Request<Body>) -> Result<Response<Body>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn host_addr(uri: &http::Uri) -> Option<SocketAddr> {
|
fn host_addr(uri: &http::Uri) -> Option<String> {
|
||||||
uri.authority().and_then(|auth| auth.as_str().parse().ok())
|
uri.authority().and_then(|auth| Some(auth.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a TCP connection to host:port, build a tunnel between the connection and
|
// Create a TCP connection to host:port, build a tunnel between the connection and
|
||||||
// the upgraded connection
|
// the upgraded connection
|
||||||
async fn tunnel(upgraded: Upgraded, addr: SocketAddr) -> std::io::Result<()> {
|
async fn tunnel(mut upgraded: Upgraded, addr: String) -> std::io::Result<()> {
|
||||||
// Connect to remote server
|
// Connect to remote server
|
||||||
let mut server = TcpStream::connect(addr).await?;
|
let mut server = TcpStream::connect(addr).await?;
|
||||||
|
|
||||||
// Proxying data
|
// Proxying data
|
||||||
let amounts = {
|
let (from_client, from_server) =
|
||||||
let (mut server_rd, mut server_wr) = server.split();
|
tokio::io::copy_bidirectional(&mut upgraded, &mut server).await?;
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
// Print message when done
|
// Print message when done
|
||||||
match amounts {
|
println!(
|
||||||
Ok((from_client, from_server)) => {
|
"client wrote {} bytes and received {} bytes",
|
||||||
println!(
|
from_client, from_server
|
||||||
"client wrote {} bytes and received {} bytes",
|
);
|
||||||
from_client, from_server
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("tunnel error: {}", e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user