From 899e493f8205e547fa3be9808c15ce1078454273 Mon Sep 17 00:00:00 2001 From: Karan Gauswami Date: Sun, 18 Jun 2023 11:57:44 +0530 Subject: [PATCH] used Argument Relations feature in clap to avoid doing double validation in auth username and password --- src/main.rs | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index 29f5682..94e96b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ -use clap::error::ErrorKind; -use clap::CommandFactory; -use clap::Parser; +use clap::{Args, Parser}; use color_eyre::eyre::Result; use http::Uri; use hyper::client::HttpConnector; @@ -14,6 +12,18 @@ use std::net::{Ipv4Addr, SocketAddr}; use tokio_socks::tcp::Socks5Stream; use tokio_socks::{IntoTargetAddr, ToProxyAddrs}; +#[derive(Debug, Args)] +#[group()] +struct Auths { + /// Socks5 username + #[arg(short = 'u', long, required = false)] + username: String, + + /// Socks5 password + #[arg(short = 'P', long, required = false)] + password: String, +} + #[derive(Parser, Debug)] #[command(author, version, about,long_about=None)] struct Cli { @@ -24,18 +34,13 @@ struct Cli { #[arg(long, default_value = "0.0.0.0")] listen_ip: Ipv4Addr, + #[command(flatten)] + auth: Option, + /// Socks5 proxy address #[arg(short, long, default_value = "127.0.0.1:1080")] socks_address: SocketAddr, - /// Socks5 username - #[arg(short = 'u', long)] - username: Option, - - /// Socks5 password - #[arg(short = 'P', long)] - password: Option, - /// Comma-separated list of allowed domains #[arg(long)] allowed_domains: Option, @@ -50,27 +55,12 @@ async fn main() -> Result<()> { let socks_address = args.socks_address; let port = args.port; - let username = args.username; - let password = args.password; - let mut cmd = Cli::command(); - - let auth = match (username, password) { - (Some(username), Some(password)) => Some(Auth::new(username, password)), - (Some(_), None) => cmd - .error( - ErrorKind::MissingRequiredArgument, - "--password is required if --username is used", - ) - .exit(), - (None, Some(_)) => cmd - .error( - ErrorKind::MissingRequiredArgument, - "--username is required if --password is used", - ) - .exit(), - (None, None) => None, + let auth = match args.auth { + Some(auth) => Some(Auth::new(auth.username, auth.password)), + None => None, }; let auth = &*Box::leak(Box::new(auth)); + let addr = SocketAddr::from((args.listen_ip, port)); let mut connector = HttpConnector::new(); connector.enforce_http(false);