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

82
Cargo.lock generated
View File

@@ -1,5 +1,14 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "arrayref" name = "arrayref"
version = "0.3.6" version = "0.3.6"
@@ -12,6 +21,17 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.0.1" version = "1.0.1"
@@ -24,6 +44,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]] [[package]]
name = "blake2b_simd" name = "blake2b_simd"
version = "0.5.10" version = "0.5.10"
@@ -47,6 +73,21 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]] [[package]]
name = "constant_time_eq" name = "constant_time_eq"
version = "0.1.5" version = "0.1.5"
@@ -81,15 +122,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "getopts"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.14" version = "0.1.14"
@@ -101,6 +133,15 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "hermit-abi"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@@ -205,11 +246,17 @@ dependencies = [
name = "simple-rust-udp-proxy" name = "simple-rust-udp-proxy"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"getopts", "clap",
"rand 0.3.23", "rand 0.3.23",
"rust_util", "rust_util",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "term" name = "term"
version = "0.5.2" version = "0.5.2"
@@ -231,12 +278,27 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.8" version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" version = "0.9.0+wasi-snapshot-preview1"

View File

@@ -3,10 +3,11 @@ name = "simple-rust-udp-proxy"
version = "0.1.0" version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Very simple rust UDP proxy"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
getopts = "0.2"
rand = "0.3" rand = "0.3"
rust_util = "0.6" rust_util = "0.6"
clap = "2.33"

View File

@@ -1,9 +1,7 @@
extern crate getopts;
extern crate rand; extern crate rand;
#[macro_use] #[macro_use]
extern crate rust_util; extern crate rust_util;
use getopts::Options;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::net::UdpSocket; use std::net::UdpSocket;
@@ -12,45 +10,31 @@ use std::sync::atomic::{ AtomicBool, Ordering };
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use clap::{ Arg, App };
const TIMEOUT: u64 = 3 * 60 * 100; //3 minutes 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() { fn main() {
let args: Vec<String> = env::args().collect(); let matches = App::new("Simple Rust UDP Proxy")
let program = args[0].clone(); .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(); let local_port: i32 = matches.value_of("local_port").unwrap().parse().unwrap();
opts.reqopt("l", "local-port", let remote_port: i32 = matches.value_of("remote_port").unwrap().parse().unwrap();
"The local port to which udpproxy should bind to", let remote_host = matches.value_of("h").unwrap();
"LOCAL_PORT"); let bind_addr = match matches.value_of("bind") {
opts.reqopt("r", "remote-port", Some(addr) => addr.to_owned(),
"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,
None => "127.0.0.1".to_owned(), None => "127.0.0.1".to_owned(),
}; };