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`.
This commit is contained in:
Mahmoud Al-Qudsi 2022-06-30 16:27:13 -05:00
parent 9ad24e5a96
commit 677b6cd33a
1 changed files with 15 additions and 13 deletions

View File

@ -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") {