feat: getopts -> clap
This commit is contained in:
82
Cargo.lock
generated
82
Cargo.lock
generated
@@ -1,5 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# 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]]
|
||||
name = "arrayref"
|
||||
version = "0.3.6"
|
||||
@@ -12,6 +21,17 @@ version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
@@ -24,6 +44,12 @@ version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
|
||||
[[package]]
|
||||
name = "blake2b_simd"
|
||||
version = "0.5.10"
|
||||
@@ -47,6 +73,21 @@ version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.1.5"
|
||||
@@ -81,15 +122,6 @@ version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "getrandom"
|
||||
version = "0.1.14"
|
||||
@@ -101,6 +133,15 @@ dependencies = [
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
@@ -205,11 +246,17 @@ dependencies = [
|
||||
name = "simple-rust-udp-proxy"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"getopts",
|
||||
"clap",
|
||||
"rand 0.3.23",
|
||||
"rust_util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
|
||||
[[package]]
|
||||
name = "term"
|
||||
version = "0.5.2"
|
||||
@@ -231,12 +278,27 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||
dependencies = [
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.9.0+wasi-snapshot-preview1"
|
||||
|
||||
@@ -3,10 +3,11 @@ name = "simple-rust-udp-proxy"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Very simple rust UDP proxy"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
getopts = "0.2"
|
||||
rand = "0.3"
|
||||
rust_util = "0.6"
|
||||
clap = "2.33"
|
||||
|
||||
56
src/main.rs
56
src/main.rs
@@ -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(),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user