From 677b6cd33a85caa02bc8d51b72e2cc031a0ebf26 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Thu, 30 Jun 2022 16:27:13 -0500 Subject: [PATCH] Make options easier to use The local port is now optional, in which case a random free port will be used (and reported in the output). The `--host` argument is now `--remote-host`. --- src/main.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index f22be0b..8a3100c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,8 @@ static DEBUG: AtomicBool = AtomicBool::new(false); fn print_usage(program: &str, opts: Options) { let program_path = std::path::PathBuf::from(program); - let program_name = program_path.file_stem().unwrap().to_str().unwrap(); - let brief = format!("Usage: {} [-b BIND_ADDR] -l LOCAL_PORT -h REMOTE_ADDR -r REMOTE_PORT", + let program_name = program_path.file_stem().unwrap().to_string_lossy(); + let brief = format!("Usage: {} [-b BIND_ADDR] -h REMOTE_HOST -r REMOTE_PORT [-l LOCAL_PORT]", program_name); print!("{}", opts.usage(&brief)); } @@ -21,34 +21,36 @@ async fn main() -> Result<(), BoxedError> { let program = args[0].clone(); let mut opts = Options::new(); - opts.reqopt("l", - "local-port", - "The local port to which tcpproxy should bind to", - "LOCAL_PORT"); + opts.reqopt("h", + "remote-host", + "The remote host (ip or host name) to which packets will be forwarded", + "REMOTE_HOST"); opts.reqopt("r", "remote-port", "The remote port to which TCP packets should be forwarded", "REMOTE_PORT"); - opts.reqopt("h", - "host", - "The remote address to which packets will be forwarded", - "REMOTE_ADDR"); opts.optopt("b", "bind", - "The address on which to listen for incoming requests", + "The address on which to listen for incoming requests, defaulting to localhost", "BIND_ADDR"); + opts.optopt("l", + "local-port", + "The local port to which tcpproxy should bind to, randomly chosen otherwise", + "LOCAL_PORT"); opts.optflag("d", "debug", "Enable debug mode"); let matches = match opts.parse(&args[1..]) { Ok(opts) => opts, - Err(_) => { + Err(e) => { + eprintln!("{}", e); print_usage(&program, opts); std::process::exit(-1); } }; DEBUG.store(matches.opt_present("d"), Ordering::Relaxed); - let local_port: i32 = matches.opt_str("l").unwrap().parse()?; + // let local_port: i32 = matches.opt_str("l").unwrap_or("0".to_string()).parse()?; + let local_port: i32 = matches.opt_str("l").map(|s| s.parse()).unwrap_or(Ok(0))?; let remote_port: i32 = matches.opt_str("r").unwrap().parse()?; let remote_host = matches.opt_str("h").unwrap(); let bind_addr = match matches.opt_str("b") {