From 233602131e698f0f6e7ff6f16c18d232d9d2b44a Mon Sep 17 00:00:00 2001 From: Anmol Bhatia Date: Thu, 5 Oct 2023 10:49:21 +0200 Subject: [PATCH 1/2] Removing use of clap::builder::PossibleValuesParser --- README.md | 1 + socksx/examples/client.rs | 5 ++--- socksx/examples/functions.rs | 4 ++-- socksx/examples/redirector.rs | 4 ++-- socksx/src/main.rs | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 675629a..f7edd02 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Check out the `docker-compose-proxy.yml` or `docker-compose-extensive.yml` file ## TODO +- [ ] make socksx work for macOS - [ ] support chaining in socks 5 - [ ] add badge for coverage (coveralls) - [ ] add badge for crates link diff --git a/socksx/examples/client.rs b/socksx/examples/client.rs index eb8e79b..f928019 100644 --- a/socksx/examples/client.rs +++ b/socksx/examples/client.rs @@ -3,7 +3,6 @@ /// This also serves as a test to ensure that the crate works as expected. use anyhow::Result; use clap::Parser; -use clap::builder::PossibleValuesParser; use socksx::{Socks5Client, Socks6Client}; use tokio::io::AsyncWriteExt; @@ -12,7 +11,7 @@ use tokio::io::AsyncWriteExt; #[derive(Debug, Parser)] #[clap(name = "Client")] struct Arguments { - #[clap(name="VERSION", short='s', long="socks", value_parser=PossibleValuesParser::new(["5", "6"]), default_value="6", help="The SOCKS version to use")] + #[clap(name="VERSION", short='s', long="socks", default_value="6", help="The SOCKS version to use")] version : u8, #[clap(name="PROXY_HOST", long="host", default_value="127.0.0.1", help="The IP/hostname of the proxy")] proxy_host : String, @@ -38,7 +37,7 @@ async fn main() -> Result<()> { let proxy_addr = format!("{}:{}", args.proxy_host, args.proxy_port); let dest_addr = format!("{}:{}", args.dest_host, args.dest_port); - // Determine the SOCKS version specified in the arguments. + // Determine the appropriate SOCKS handler based on the specified version and restricting them to 5 and 6 match args.version { 5 => connect_v5(proxy_addr, dest_addr).await, 6 => connect_v6(proxy_addr, dest_addr).await, diff --git a/socksx/examples/functions.rs b/socksx/examples/functions.rs index 99dc341..7ad9350 100644 --- a/socksx/examples/functions.rs +++ b/socksx/examples/functions.rs @@ -10,7 +10,6 @@ use bytes::BytesMut; use chacha20::{ChaCha20, Key, Nonce}; use chacha20::cipher::{KeyIvInit as _, StreamCipher}; use clap::Parser; -use clap::builder::PossibleValuesParser; use dotenv::dotenv; use pin_project_lite::pin_project; use tokio::io::{self, AsyncBufRead, BufReader, BufWriter}; @@ -34,7 +33,7 @@ struct Args { port: u16, /// SOCKS version - #[clap(short, long, env = "SOCKS", default_value = "6", value_parser = PossibleValuesParser::new(["5", "6"]))] + #[clap(short, long, env = "SOCKS", default_value = "6")] socks: u8, #[clap(subcommand)] @@ -63,6 +62,7 @@ async fn main() -> Result<()> { // Create a TCP listener bound to the specified host and port. let listener = TcpListener::bind(format!("{}:{}", args.host, args.port)).await?; // Determine the appropriate SOCKS handler based on the specified version. + // Determine the appropriate SOCKS handler based on the specified version and restricting them to 5 and 6 let handler: Handler = match args.socks { 5 => Arc::new(Socks5Handler::default()), 6 => Arc::new(Socks6Handler::default()), diff --git a/socksx/examples/redirector.rs b/socksx/examples/redirector.rs index 50f1af5..69fe244 100644 --- a/socksx/examples/redirector.rs +++ b/socksx/examples/redirector.rs @@ -3,7 +3,6 @@ /// through a proxy. use anyhow::Result; use clap::Parser; -use clap::builder::PossibleValuesParser; use tokio::net::{TcpListener, TcpStream}; use socksx::{self, Socks5Client, Socks6Client}; @@ -13,7 +12,7 @@ use socksx::{self, Socks5Client, Socks6Client}; #[derive(Debug, Parser)] #[clap(name = "Redirector")] struct Arguments { - #[clap(name="VERSION", short='s', long="socks", value_parser=PossibleValuesParser::new(["5", "6"]), default_value="6", help="The SOCKS version to use")] + #[clap(name="VERSION", short='s', long="socks", default_value="6", help="The SOCKS version to use")] version : u8, #[clap(name="PROXY_HOST", long="host", default_value="127.0.0.1", help="The IP/hostname of the proxy")] proxy_host : String, @@ -33,6 +32,7 @@ async fn main() -> Result<()> { let proxy_addr = format!("{}:{}", args.proxy_host, args.proxy_port); let listener = TcpListener::bind("127.0.0.1:42000").await?; + // Determine the appropriate SOCKS handler based on the specified version and restricting them to 5 and 6 match args.version { 5 => { let client = Socks5Client::new(proxy_addr, None).await?; diff --git a/socksx/src/main.rs b/socksx/src/main.rs index 15263e4..a212484 100644 --- a/socksx/src/main.rs +++ b/socksx/src/main.rs @@ -12,7 +12,6 @@ use std::{convert::TryInto, sync::Arc}; use anyhow::Result; use clap::Parser; -use clap::builder::PossibleValuesParser; use dotenv::dotenv; use itertools::Itertools; use log::LevelFilter; @@ -50,7 +49,7 @@ struct Args { port: u16, /// SOCKS version - #[clap(short, long, env = "SOCKS", default_value = "6", value_parser = PossibleValuesParser::new(["5", "6"]))] + #[clap(short, long, env = "SOCKS", default_value = "6")] socks: u8, } @@ -93,6 +92,7 @@ async fn main() -> Result<()> { // Bind TCP listener to the specified host and port let listener = TcpListener::bind(format!("{}:{}", args.host, args.port)).await?; + // Determine the appropriate SOCKS handler based on the specified version and restricting them to 5 and 6 let handler: Handler = match args.socks { 5 => Arc::new(Socks5Handler::new(chain)), 6 => Arc::new(Socks6Handler::new(chain)), From 401dfd40663c4a09f190caa7ad0d9f3fa25b0802 Mon Sep 17 00:00:00 2001 From: Lut99 Date: Thu, 5 Oct 2023 11:16:06 +0200 Subject: [PATCH 2/2] Changed SOCKSX-version error handling Now using a more graceful error message than panics --- socksx/examples/client.rs | 2 +- socksx/examples/functions.rs | 2 +- socksx/examples/redirector.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/socksx/examples/client.rs b/socksx/examples/client.rs index f928019..478e5e4 100644 --- a/socksx/examples/client.rs +++ b/socksx/examples/client.rs @@ -41,7 +41,7 @@ async fn main() -> Result<()> { match args.version { 5 => connect_v5(proxy_addr, dest_addr).await, 6 => connect_v6(proxy_addr, dest_addr).await, - version => panic!("Unsupported version: {}", version), + version => { eprintln!("ERROR: Unsupported SOCKS-version '{version}' (supported: `5`, `6`)"); std::process::exit(1); }, } } diff --git a/socksx/examples/functions.rs b/socksx/examples/functions.rs index 7ad9350..15b02d3 100644 --- a/socksx/examples/functions.rs +++ b/socksx/examples/functions.rs @@ -66,7 +66,7 @@ async fn main() -> Result<()> { let handler: Handler = match args.socks { 5 => Arc::new(Socks5Handler::default()), 6 => Arc::new(Socks6Handler::default()), - _ => unreachable!(), + version => { eprintln!("ERROR: Unsupported SOCKS-version '{version}' (supported: `5`, `6`)"); std::process::exit(1); }, }; // Main loop for accepting incoming connections and processing them. diff --git a/socksx/examples/redirector.rs b/socksx/examples/redirector.rs index 69fe244..cffd518 100644 --- a/socksx/examples/redirector.rs +++ b/socksx/examples/redirector.rs @@ -50,7 +50,7 @@ async fn main() -> Result<()> { tokio::spawn(redirect_v6(stream, client.clone())); } } - version => panic!("Unsupported version: {}", version), + version => { eprintln!("ERROR: Unsupported SOCKS-version '{version}' (supported: `5`, `6`)"); std::process::exit(1); }, }; }