feat: getopts -> clap

This commit is contained in:
2020-09-06 20:02:49 +08:00
parent fcb4f51a82
commit a9ba9ea94e
3 changed files with 94 additions and 47 deletions

View File

@@ -1,9 +1,7 @@
extern crate getopts;
extern crate rand;
#[macro_use]
extern crate rust_util;
use getopts::Options;
use std::collections::HashMap;
use std::env;
use std::net::UdpSocket;
@@ -12,45 +10,31 @@ use std::sync::atomic::{ AtomicBool, Ordering };
use std::sync::mpsc::channel;
use std::thread;
use std::time::Duration;
use clap::{ Arg, App };
const TIMEOUT: u64 = 3 * 60 * 100; //3 minutes
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", program_name);
information!("{}", opts.usage(&brief));
}
fn main() {
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let matches = App::new("Simple Rust UDP Proxy")
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(Arg::with_name("local_port").short("l").long("local-port").takes_value(true).required(true)
.help("The local port to which udpproxy should bind to"))
.arg(Arg::with_name("remote_port").short("r").long("remote-port").takes_value(true).required(true)
.help("The remote port to which UDP packets should be forwarded"))
.arg(Arg::with_name("host").short("h").long("host").takes_value(true).required(true)
.help("The remote address to which packets will be forwarded"))
.arg(Arg::with_name("bind").short("b").long("bind").takes_value(true)
.help("The address on which to listen for incoming requests"))
.arg(Arg::with_name("debug").short("d").long("debug").takes_value(true)
.help("Enable debug mode"))
.get_matches();
let mut opts = Options::new();
opts.reqopt("l", "local-port",
"The local port to which udpproxy should bind to",
"LOCAL_PORT");
opts.reqopt("r", "remote-port",
"The remote port to which UDP 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",
"BIND_ADDR");
opts.optflag("d", "debug", "Enable debug mode");
let matches = opts.parse(&args[1..]).unwrap_or_else(|_| {
print_usage(&program, opts);
std::process::exit(-1);
});
let local_port: i32 = matches.opt_str("l").unwrap().parse().unwrap();
let remote_port: i32 = matches.opt_str("r").unwrap().parse().unwrap();
let remote_host = matches.opt_str("h").unwrap();
let bind_addr = match matches.opt_str("b") {
Some(addr) => addr,
let local_port: i32 = matches.value_of("local_port").unwrap().parse().unwrap();
let remote_port: i32 = matches.value_of("remote_port").unwrap().parse().unwrap();
let remote_host = matches.value_of("h").unwrap();
let bind_addr = match matches.value_of("bind") {
Some(addr) => addr.to_owned(),
None => "127.0.0.1".to_owned(),
};