|
|
@ -26,6 +26,7 @@ async fn main() { |
|
|
|
let socks_address = args.socks_address; |
|
|
|
let port = args.port; |
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], port)); |
|
|
|
let socks_address = socks_address.to_socket_addrs().unwrap().next().unwrap(); |
|
|
|
let make_service = make_service_fn(move |_| { |
|
|
|
let socks_address = socks_address.clone(); |
|
|
|
async move { |
|
|
@ -41,21 +42,12 @@ async fn main() { |
|
|
|
eprintln!("{:?}", e); |
|
|
|
}; |
|
|
|
} |
|
|
|
async fn proxy(req: Request<Body>, socks_address: String) -> Result<Response<Body>> { |
|
|
|
async fn proxy(req: Request<Body>, socks_address: SocketAddr) -> Result<Response<Body>> { |
|
|
|
let _response = Response::new(Body::empty()); |
|
|
|
|
|
|
|
if req.method() == hyper::Method::CONNECT { |
|
|
|
tokio::task::spawn(async move { |
|
|
|
let plain = req.uri().authority().unwrap().as_str().to_string(); |
|
|
|
let addr = req |
|
|
|
.uri() |
|
|
|
.authority() |
|
|
|
.unwrap() |
|
|
|
.as_str() |
|
|
|
.to_socket_addrs() |
|
|
|
.unwrap() |
|
|
|
.next() |
|
|
|
.unwrap(); |
|
|
|
match hyper::upgrade::on(req).await { |
|
|
|
Ok(upgraded) => { |
|
|
|
if let Err(e) = tunnel(upgraded, plain, socks_address).await { |
|
|
@ -73,19 +65,19 @@ async fn proxy(req: Request<Body>, socks_address: String) -> Result<Response<Bod |
|
|
|
|
|
|
|
async fn tunnel( |
|
|
|
upgraded: hyper::upgrade::Upgraded, |
|
|
|
plain: String, |
|
|
|
socks_address: String, |
|
|
|
target_addr: String, |
|
|
|
socks_address: SocketAddr, |
|
|
|
) -> std::io::Result<()> { |
|
|
|
let socket_address = socks_address.to_socket_addrs().unwrap().next().unwrap(); |
|
|
|
|
|
|
|
let target_addr = plain.into_target_addr(); |
|
|
|
let target_addr = target_addr.into_target_addr(); |
|
|
|
let target_addr = target_addr.unwrap(); |
|
|
|
let a = tokio_socks::tcp::Socks5Stream::connect(socket_address, target_addr) |
|
|
|
let socks_stream = tokio_socks::tcp::Socks5Stream::connect(socket_address, target_addr) |
|
|
|
.await |
|
|
|
.expect("Cannot Connect to Socks5 Server"); |
|
|
|
|
|
|
|
let amounts = { |
|
|
|
let (mut server_rd, mut server_wr) = tokio::io::split(a); |
|
|
|
let (mut server_rd, mut server_wr) = tokio::io::split(socks_stream); |
|
|
|
let (mut client_rd, mut client_wr) = tokio::io::split(upgraded); |
|
|
|
|
|
|
|
let client_to_server = tokio::io::copy(&mut client_rd, &mut server_wr); |
|
|
|