Add -V, --version argument, env variables only apply for long options

This commit is contained in:
Travis Burtrum 2019-12-22 23:59:42 -05:00
parent 2c902bdddf
commit d026f700a5
4 changed files with 49 additions and 15 deletions

View File

@ -51,12 +51,13 @@ usage: wireguard-proxy [options...]
Common Options: Common Options:
-h, --help print this usage text -h, --help print this usage text
-V, --version Show version number and TLS support then quit
-st, --socket-timeout <seconds> Socket timeout (time to wait for data) -st, --socket-timeout <seconds> Socket timeout (time to wait for data)
before terminating, default: 0 before terminating, default: 0
Environment variable support: Environment variable support:
For every command line option, short and long, if you replace all For every long command line option (starting with --), if you replace the
leading - with WGP_, and replace all remaining - with _, and uppercase leading -- with WGP_, and replace all remaining - with _, and uppercase
the whole thing, if you don't specify that command line option we will the whole thing, if you don't specify that command line option we will
read that environment variable for the argument. boolean arguments are read that environment variable for the argument. boolean arguments are
true if anything but unset, empty, 0, or false. true if anything but unset, empty, 0, or false.

View File

@ -86,6 +86,18 @@ impl Server {
fn main() { fn main() {
let raw_args = env::args().collect(); let raw_args = env::args().collect();
let args = Args::new(&raw_args); let args = Args::new(&raw_args);
if args.flag("-V") || args.flag("--version") {
print!("udp-test {} ", env!("CARGO_PKG_VERSION"));
#[cfg(not(any(feature = "tls", feature = "openssl_vendored")))]
println!("TLS support: None");
#[cfg(feature = "openssl_vendored")]
println!("TLS support: Static/Vendored OpenSSL");
#[cfg(feature = "tls")]
println!("TLS support: System OpenSSL");
return;
}
let default_udp_host_target = "127.0.0.1:51820"; let default_udp_host_target = "127.0.0.1:51820";
let default_socket_timeout = 10; let default_socket_timeout = 10;
@ -106,6 +118,7 @@ fn main() {
if args.flag("-h") || args.flag("--help") { if args.flag("-h") || args.flag("--help") {
println!(r#"usage: udp-test [options...] println!(r#"usage: udp-test [options...]
-h, --help print this usage text -h, --help print this usage text
-V, --version Show version number and TLS support then quit
-s, --self-test run a self test through proxy -s, --self-test run a self test through proxy
-is, --internal-self-test run a self test through proxy without -is, --internal-self-test run a self test through proxy without
spawning other processes spawning other processes
@ -130,8 +143,8 @@ fn main() {
one pem file one pem file
Environment variable support: Environment variable support:
For every command line option, short and long, if you replace all For every long command line option (starting with --), if you replace the
leading - with WGP_, and replace all remaining - with _, and uppercase leading -- with WGP_, and replace all remaining - with _, and uppercase
the whole thing, if you don't specify that command line option we will the whole thing, if you don't specify that command line option we will
read that environment variable for the argument. boolean arguments are read that environment variable for the argument. boolean arguments are
true if anything but unset, empty, 0, or false. true if anything but unset, empty, 0, or false.

View File

@ -5,6 +5,17 @@ fn main() {
let raw_args = env::args().collect(); let raw_args = env::args().collect();
let args = Args::new(&raw_args); let args = Args::new(&raw_args);
if args.flag("-V") || args.flag("--version") {
print!("wireguard-proxy {} ", env!("CARGO_PKG_VERSION"));
#[cfg(not(any(feature = "tls", feature = "openssl_vendored")))]
println!("TLS support: None");
#[cfg(feature = "openssl_vendored")]
println!("TLS support: Static/Vendored OpenSSL");
#[cfg(feature = "tls")]
println!("TLS support: System OpenSSL");
return;
}
let default_udp_host_target = "127.0.0.1:51820"; let default_udp_host_target = "127.0.0.1:51820";
let default_socket_timeout = 0; let default_socket_timeout = 0;
@ -56,12 +67,13 @@ fn main() {
Common Options: Common Options:
-h, --help print this usage text -h, --help print this usage text
-V, --version Show version number and TLS support then quit
-st, --socket-timeout <seconds> Socket timeout (time to wait for data) -st, --socket-timeout <seconds> Socket timeout (time to wait for data)
before terminating, default: {} before terminating, default: {}
Environment variable support: Environment variable support:
For every command line option, short and long, if you replace all For every long command line option (starting with --), if you replace the
leading - with WGP_, and replace all remaining - with _, and uppercase leading -- with WGP_, and replace all remaining - with _, and uppercase
the whole thing, if you don't specify that command line option we will the whole thing, if you don't specify that command line option we will
read that environment variable for the argument. boolean arguments are read that environment variable for the argument. boolean arguments are
true if anything but unset, empty, 0, or false. true if anything but unset, empty, 0, or false.

View File

@ -24,11 +24,18 @@ mod tls {
use tls::{TlsStream, TlsListener}; use tls::{TlsStream, TlsListener};
fn arg_to_env<'a>(arg: &'a str) -> String { fn arg_to_env(arg: &str) -> Option<String> {
if !arg.starts_with("--") {
return None;
}
let env = "WGP_".to_owned(); let env = "WGP_".to_owned();
let mut env = env + &arg.trim_matches('-').replace("-", "_"); let mut env = env + &arg.trim_matches('-').replace("-", "_");
env.make_ascii_uppercase(); env.make_ascii_uppercase();
env Some(env)
}
fn env_for_arg(arg: &str) -> Option<String> {
arg_to_env(arg).and_then(|key| std::env::var(key).ok())
} }
#[cfg(test)] #[cfg(test)]
@ -37,9 +44,10 @@ mod tests {
#[test] #[test]
fn test_arg_to_env() { fn test_arg_to_env() {
assert_eq!(arg_to_env("--tcp-host"), "WGP_TCP_HOST"); assert_eq!(arg_to_env("--tcp-host"), Some("WGP_TCP_HOST".to_owned()));
assert_eq!(arg_to_env("--tls"), "WGP_TLS"); assert_eq!(arg_to_env("--tls"), Some("WGP_TLS".to_owned()));
assert_eq!(arg_to_env("-h"), "WGP_H"); assert_eq!(arg_to_env("-h"), None);
assert_eq!(arg_to_env("-th"), None);
} }
} }
@ -56,9 +64,9 @@ impl<'a> Args<'a> {
return true; return true;
} }
// because env we want slightly special handling of empty/0/false // because env we want slightly special handling of empty/0/false
match std::env::var(arg_to_env(flag)) { match env_for_arg(flag) {
Ok(env) => &env != "" && &env != "0" && &env != "false", Some(env) => &env != "" && &env != "0" && &env != "false",
Err(_) => false, None => false,
} }
} }
pub fn get_option(&self, flags: &[&'a str]) -> Option<String> { pub fn get_option(&self, flags: &[&'a str]) -> Option<String> {
@ -75,7 +83,7 @@ impl<'a> Args<'a> {
} }
// no matching arguments are found, so check env variables as a fallback // no matching arguments are found, so check env variables as a fallback
for flag in flags.iter() { for flag in flags.iter() {
let env = std::env::var(arg_to_env(flag)).ok(); let env = env_for_arg(flag);
if env.is_some() { if env.is_some() {
return env; return env;
} }