Browse Source

added logging crate

pull/5/head
Karan Gauswami 3 years ago
parent
commit
8dc1b3d65f
  1. 76
      Cargo.lock
  2. 10
      Cargo.toml
  3. 22
      src/main.rs

76
Cargo.lock

@ -2,6 +2,15 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "anyhow"
version = "1.0.41"
@ -30,6 +39,17 @@ dependencies = [
"syn",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -93,6 +113,19 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "env_logger"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -271,6 +304,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.9"
@ -580,6 +619,23 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -643,10 +699,12 @@ name = "sthp"
version = "0.2.0"
dependencies = [
"anyhow",
"env_logger",
"futures-util",
"http",
"hyper",
"hyper-socks2",
"log",
"structopt",
"tokio",
"tokio-socks",
@ -701,6 +759,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -874,6 +941,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

10
Cargo.toml

@ -7,11 +7,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures-util= "0.3"
futures-util = "0.3"
http = "0.2"
hyper = { version = "0.14", features = ["http1", "server", "tcp","client"]}
structopt = { version="0.3", default-features = false}
hyper = { version = "0.14", features = ["http1", "server", "tcp", "client"] }
structopt = { version = "0.3", default-features = false }
tokio-socks = "0.5"
tokio = { version = "1.13", features = ["macros", "rt-multi-thread"] }
anyhow = "1.0"
hyper-socks2 = { version = "0.6" }
hyper-socks2 = { version = "0.6" }
log = "0.4"
env_logger = "0.9"

22
src/main.rs

@ -5,11 +5,12 @@ use hyper::service::{make_service_fn, service_fn};
use hyper::upgrade::Upgraded;
use hyper::{Body, Request, Response, Server};
use hyper_socks2::SocksConnector;
use log::debug;
use std::convert::Infallible;
use std::net::SocketAddr;
use structopt::StructOpt;
use tokio_socks::tcp::Socks5Stream;
use tokio_socks::IntoTargetAddr;
use tokio_socks::{IntoTargetAddr, ToProxyAddrs};
#[derive(StructOpt, Debug)]
#[structopt(name = "sthp", about = "Convert Socks5 proxy into Http proxy")]
@ -25,10 +26,12 @@ struct Cli {
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let args = Cli::from_args();
let socks_address = args.socks_address;
let port = args.port;
let addr = SocketAddr::from(([127, 0, 0, 1], port));
let addr = SocketAddr::from(([0, 0, 0, 0], port));
let make_service = make_service_fn(move |_| async move {
Ok::<_, Infallible>(service_fn(move |req| proxy(req, socks_address)))
});
@ -36,9 +39,9 @@ async fn main() -> Result<()> {
.http1_preserve_header_case(true)
.http1_title_case_headers(true)
.serve(make_service);
println!("Server is listening on http://{}", addr);
debug!("Server is listening on http://{}", addr);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
debug!("server error: {}", e);
};
Ok(())
}
@ -56,10 +59,10 @@ async fn proxy(req: Request<Body>, socks_address: SocketAddr) -> Result<Response
match hyper::upgrade::on(req).await {
Ok(upgraded) => {
if let Err(e) = tunnel(upgraded, plain, socks_address).await {
eprintln!("server io error: {}", e);
debug!("server io error: {}", e);
};
}
Err(e) => eprintln!("upgrade error: {}", e),
Err(e) => debug!("upgrade error: {}", e),
}
});
Ok(Response::new(Body::empty()))
@ -80,9 +83,10 @@ async fn proxy(req: Request<Body>, socks_address: SocketAddr) -> Result<Response
}
}
async fn tunnel<'t, I>(mut upgraded: Upgraded, plain: I, socks_address: SocketAddr) -> Result<()>
async fn tunnel<'t, P, T>(mut upgraded: Upgraded, plain: T, socks_address: P) -> Result<()>
where
I: IntoTargetAddr<'t>,
P: ToProxyAddrs,
T: IntoTargetAddr<'t>,
{
let mut stream = Socks5Stream::connect(socks_address, plain).await?;
@ -91,7 +95,7 @@ where
tokio::io::copy_bidirectional(&mut upgraded, &mut stream).await?;
// Print message when done
println!(
debug!(
"client wrote {} bytes and received {} bytes",
from_client, from_server
);

Loading…
Cancel
Save