diff --git a/Cargo.lock b/Cargo.lock index b1cf2ae..1736077 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,19 +3,10 @@ version = 3 [[package]] -name = "addr2line" -version = "0.15.2" +name = "anyhow" +version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7a2e47a1fbe209ee101dd6d61285226744c6c8d3c21c8dc878ba6cb9f467f3a" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +checksum = "15af2628f6890fe2609a3b91bef4c83450512802e59489f9c1cb1fa5df064a61" [[package]] name = "autocfg" @@ -23,21 +14,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" -[[package]] -name = "backtrace" -version = "0.3.60" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7815ea54e4d821e791162e078acbebfd6d8c8939cd559c9335dceb1c8ca7282" -dependencies = [ - "addr2line", - "cc", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", -] - [[package]] name = "bitflags" version = "1.2.1" @@ -50,12 +26,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" -[[package]] -name = "cc" -version = "1.0.68" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" - [[package]] name = "cfg-if" version = "1.0.0" @@ -73,35 +43,12 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "color-eyre" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f1885697ee8a177096d42f158922251a41973117f6d8a234cee94b9509157b7" -dependencies = [ - "backtrace", - "eyre", - "indenter", - "once_cell", - "owo-colors", -] - [[package]] name = "either" version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" -[[package]] -name = "eyre" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "221239d1d5ea86bf5d6f91c9d6bc3646ffe471b08ff9b0f91c44f115ac969d2b" -dependencies = [ - "indenter", - "once_cell", -] - [[package]] name = "fnv" version = "1.0.7" @@ -159,12 +106,6 @@ dependencies = [ "slab", ] -[[package]] -name = "gimli" -version = "0.24.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" - [[package]] name = "heck" version = "0.3.3" @@ -240,12 +181,6 @@ dependencies = [ "want", ] -[[package]] -name = "indenter" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" - [[package]] name = "itoa" version = "0.4.7" @@ -279,16 +214,6 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" -[[package]] -name = "miniz_oxide" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" -dependencies = [ - "adler", - "autocfg", -] - [[package]] name = "mio" version = "0.7.13" @@ -330,27 +255,6 @@ dependencies = [ "libc", ] -[[package]] -name = "object" -version = "0.25.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a38f2be3697a57b4060074ff41b44c16870d916ad7877c17696e063257482bc7" -dependencies = [ - "memchr", -] - -[[package]] -name = "once_cell" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" - -[[package]] -name = "owo-colors" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2386b4ebe91c2f7f51082d4cefa145d030e33a1842a96b12e4885cc3c01f7a55" - [[package]] name = "pin-project-lite" version = "0.2.7" @@ -417,12 +321,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rustc-demangle" -version = "0.1.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dead70b0b5e03e9c814bcb6b01e03e68f7c57a80aa48c72ec92152ab3e818d49" - [[package]] name = "slab" version = "0.4.3" @@ -443,7 +341,7 @@ dependencies = [ name = "sthp" version = "0.1.0" dependencies = [ - "color-eyre", + "anyhow", "futures-util", "http", "hyper", diff --git a/Cargo.toml b/Cargo.toml index eb68d53..7c4d720 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,4 @@ hyper = { version = "0.14", features = ["http1", "server", "tcp"]} structopt = { version="0.3", default-features = false} tokio-socks = "0.5" tokio = { version = "1.7", features = ["macros", "rt-multi-thread"] } -color-eyre = { version = "0.5", default-features = false } +anyhow = "1.0.41" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fc183ff..81857fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ -use color_eyre::eyre::Result; +use anyhow::Result; use hyper::service::{make_service_fn, service_fn}; use hyper::upgrade::Upgraded; use hyper::{Body, Request, Response, Server}; use std::convert::Infallible; -use std::net::{SocketAddr, ToSocketAddrs}; +use std::net::SocketAddr; use structopt::StructOpt; +use tokio_socks::tcp::Socks5Stream; use tokio_socks::IntoTargetAddr; #[derive(StructOpt, Debug)] @@ -16,20 +17,17 @@ struct Cli { /// Socks5 proxy address #[structopt(short, long, default_value = "127.0.0.1:1080")] - socks_address: String, + socks_address: SocketAddr, } #[tokio::main] -async fn main() { +async fn main() -> Result<()> { let args = Cli::from_args(); let socks_address = args.socks_address; - let socks_address = socks_address.to_socket_addrs().unwrap().next().unwrap(); 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 { Ok::<_, Infallible>(service_fn(move |req| proxy(req, socks_address.clone()))) } + let make_service = make_service_fn(move |_| async move { + Ok::<_, Infallible>(service_fn(move |req| proxy(req, socks_address.clone()))) }); let server = Server::bind(&addr) .http1_preserve_header_case(true) @@ -39,10 +37,9 @@ async fn main() { if let Err(e) = server.await { eprintln!("{:?}", e); }; + Ok(()) } async fn proxy(req: Request, socks_address: SocketAddr) -> Result> { - 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(); @@ -61,21 +58,15 @@ async fn proxy(req: Request, socks_address: SocketAddr) -> Result( - mut upgraded: Upgraded, - plain: I, - socks_address: SocketAddr, -) -> std::io::Result<()> +async fn tunnel<'t, I>(mut upgraded: Upgraded, plain: I, socks_address: SocketAddr) -> Result<()> where I: IntoTargetAddr<'t>, { - let mut server = tokio_socks::tcp::Socks5Stream::connect(socks_address, plain) - .await - .expect("Cannot Connect to Socks5 Server"); + let mut stream = Socks5Stream::connect(socks_address, plain).await?; // Proxying data let (from_client, from_server) = - tokio::io::copy_bidirectional(&mut upgraded, &mut server).await?; + tokio::io::copy_bidirectional(&mut upgraded, &mut stream).await?; // Print message when done println!(