Bump clap version

This commit is contained in:
Luca Barbato 2023-12-15 07:56:47 +01:00 committed by Aram Peres
parent 29fb98f02b
commit 1613d2bb5c
3 changed files with 211 additions and 89 deletions

181
Cargo.lock generated
View file

@ -26,6 +26,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.75" version = "1.0.75"
@ -98,6 +104,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]] [[package]]
name = "boringtun" name = "boringtun"
version = "0.4.0" version = "0.4.0"
@ -157,16 +169,31 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.34.0" version = "4.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2"
dependencies = [ dependencies = [
"bitflags", "clap_builder",
"strsim",
"textwrap",
"unicode-width",
] ]
[[package]]
name = "clap_builder"
version = "4.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb"
dependencies = [
"anstyle",
"clap_lex",
"strsim",
"terminal_size",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]] [[package]]
name = "combine" name = "combine"
version = "4.6.6" version = "4.6.6"
@ -190,6 +217,16 @@ dependencies = [
"termcolor", "termcolor",
] ]
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "futures" name = "futures"
version = "0.3.29" version = "0.3.29"
@ -399,6 +436,12 @@ version = "0.2.151"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "linux-raw-sys"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.11" version = "0.4.11"
@ -450,7 +493,7 @@ checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
dependencies = [ dependencies = [
"libc", "libc",
"wasi", "wasi",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -530,7 +573,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"windows-targets", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@ -631,7 +674,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
] ]
[[package]] [[package]]
@ -684,6 +727,19 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.38.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316"
dependencies = [
"bitflags 2.4.1",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "same-file" name = "same-file"
version = "1.0.6" version = "1.0.6"
@ -720,7 +776,7 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee34c1e1bfc7e9206cc0fb8030a90129b4e319ab53856249bb27642cab914fb3" checksum = "ee34c1e1bfc7e9206cc0fb8030a90129b4e319ab53856249bb27642cab914fb3"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
"byteorder", "byteorder",
"log", "log",
"managed", "managed",
@ -733,7 +789,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -744,9 +800,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
@ -769,12 +825,13 @@ dependencies = [
] ]
[[package]] [[package]]
name = "textwrap" name = "terminal_size"
version = "0.11.0" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7"
dependencies = [ dependencies = [
"unicode-width", "rustix",
"windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -811,7 +868,7 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -863,12 +920,6 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-width"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.7.1" version = "0.7.1"
@ -998,7 +1049,16 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
] ]
[[package]] [[package]]
@ -1007,13 +1067,28 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.48.5",
"windows_i686_gnu", "windows_i686_gnu 0.48.5",
"windows_i686_msvc", "windows_i686_msvc 0.48.5",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
] ]
[[package]] [[package]]
@ -1022,38 +1097,80 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

View file

@ -28,7 +28,7 @@ base64 = "0.13"
tracing = { version = "0.1", default-features = false, features = ["log"] } tracing = { version = "0.1", default-features = false, features = ["log"] }
# bin-only dependencies # bin-only dependencies
clap = { version = "2.34", default-features = false, features = ["suggestions"], optional = true } clap = { version = "4.4.11", default-features = false, features = ["suggestions", "std", "env", "help", "wrap_help"], optional = true }
pretty_env_logger = { version = "0.4", optional = true } pretty_env_logger = { version = "0.4", optional = true }
async-recursion = "1.0" async-recursion = "1.0"

View file

@ -31,18 +31,17 @@ pub struct Config {
impl Config { impl Config {
#[cfg(feature = "bin")] #[cfg(feature = "bin")]
pub fn from_args() -> anyhow::Result<Self> { pub fn from_args() -> anyhow::Result<Self> {
use clap::{App, Arg}; use clap::{Arg, Command};
let mut warnings = vec![]; let mut warnings = vec![];
let matches = App::new("onetun") let matches = Command::new("onetun")
.author("Aram Peres <aram.peres@gmail.com>") .author("Aram Peres <aram.peres@gmail.com>")
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.args(&[ .args(&[
Arg::with_name("PORT_FORWARD") Arg::new("PORT_FORWARD")
.required(false) .required(false)
.multiple(true) .num_args(1..)
.takes_value(true)
.help("Port forward configurations. The format of each argument is [src_host:]<src_port>:<dst_host>:<dst_port>[:TCP,UDP,...], \ .help("Port forward configurations. The format of each argument is [src_host:]<src_port>:<dst_host>:<dst_port>[:TCP,UDP,...], \
where [src_host] is the local IP to listen on, <src_port> is the local port to listen on, <dst_host> is the remote peer IP to forward to, and <dst_port> is the remote port to forward to. \ where [src_host] is the local IP to listen on, <src_port> is the local port to listen on, <dst_host> is the remote peer IP to forward to, and <dst_port> is the remote port to forward to. \
Environment variables of the form 'ONETUN_PORT_FORWARD_[#]' are also accepted, where [#] starts at 1.\n\ Environment variables of the form 'ONETUN_PORT_FORWARD_[#]' are also accepted, where [#] starts at 1.\n\
@ -56,80 +55,79 @@ impl Config {
\tlocalhost:8080:192.168.4.1:8081:TCP\n\ \tlocalhost:8080:192.168.4.1:8081:TCP\n\
\tlocalhost:8080:peer.intranet:8081:TCP\ \tlocalhost:8080:peer.intranet:8081:TCP\
"), "),
Arg::with_name("private-key") Arg::new("private-key")
.required_unless("private-key-file") .conflicts_with("private-key-file")
.takes_value(true) .num_args(1)
.long("private-key") .long("private-key")
.env("ONETUN_PRIVATE_KEY") .env("ONETUN_PRIVATE_KEY")
.help("The private key of this peer. The corresponding public key should be registered in the WireGuard endpoint. \ .help("The private key of this peer. The corresponding public key should be registered in the WireGuard endpoint. \
You can also use '--private-key-file' to specify a file containing the key instead."), You can also use '--private-key-file' to specify a file containing the key instead."),
Arg::with_name("private-key-file") Arg::new("private-key-file")
.takes_value(true) .num_args(1)
.long("private-key-file") .long("private-key-file")
.env("ONETUN_PRIVATE_KEY_FILE") .env("ONETUN_PRIVATE_KEY_FILE")
.help("The path to a file containing the private key of this peer. The corresponding public key should be registered in the WireGuard endpoint."), .help("The path to a file containing the private key of this peer. The corresponding public key should be registered in the WireGuard endpoint."),
Arg::with_name("endpoint-public-key") Arg::new("endpoint-public-key")
.required(true) .required(true)
.takes_value(true) .num_args(1)
.long("endpoint-public-key") .long("endpoint-public-key")
.env("ONETUN_ENDPOINT_PUBLIC_KEY") .env("ONETUN_ENDPOINT_PUBLIC_KEY")
.help("The public key of the WireGuard endpoint (remote)."), .help("The public key of the WireGuard endpoint (remote)."),
Arg::with_name("preshared-key") Arg::new("preshared-key")
.required(false) .required(false)
.takes_value(true) .num_args(1)
.long("preshared-key") .long("preshared-key")
.env("ONETUN_PRESHARED_KEY") .env("ONETUN_PRESHARED_KEY")
.help("The pre-shared key (PSK) as configured with the peer."), .help("The pre-shared key (PSK) as configured with the peer."),
Arg::with_name("endpoint-addr") Arg::new("endpoint-addr")
.required(true) .required(true)
.takes_value(true) .num_args(1)
.long("endpoint-addr") .long("endpoint-addr")
.env("ONETUN_ENDPOINT_ADDR") .env("ONETUN_ENDPOINT_ADDR")
.help("The address (IP + port) of the WireGuard endpoint (remote). Example: 1.2.3.4:51820"), .help("The address (IP + port) of the WireGuard endpoint (remote). Example: 1.2.3.4:51820"),
Arg::with_name("endpoint-bind-addr") Arg::new("endpoint-bind-addr")
.required(false) .required(false)
.takes_value(true) .num_args(1)
.long("endpoint-bind-addr") .long("endpoint-bind-addr")
.env("ONETUN_ENDPOINT_BIND_ADDR") .env("ONETUN_ENDPOINT_BIND_ADDR")
.help("The address (IP + port) used to bind the local UDP socket for the WireGuard tunnel. Example: 1.2.3.4:30000. Defaults to 0.0.0.0:0 for IPv4 endpoints, or [::]:0 for IPv6 endpoints."), .help("The address (IP + port) used to bind the local UDP socket for the WireGuard tunnel. Example: 1.2.3.4:30000. Defaults to 0.0.0.0:0 for IPv4 endpoints, or [::]:0 for IPv6 endpoints."),
Arg::with_name("source-peer-ip") Arg::new("source-peer-ip")
.required(true) .required(true)
.takes_value(true) .num_args(1)
.long("source-peer-ip") .long("source-peer-ip")
.env("ONETUN_SOURCE_PEER_IP") .env("ONETUN_SOURCE_PEER_IP")
.help("The source IP to identify this peer as (local). Example: 192.168.4.3"), .help("The source IP to identify this peer as (local). Example: 192.168.4.3"),
Arg::with_name("keep-alive") Arg::new("keep-alive")
.required(false) .required(false)
.takes_value(true) .num_args(1)
.long("keep-alive") .long("keep-alive")
.env("ONETUN_KEEP_ALIVE") .env("ONETUN_KEEP_ALIVE")
.help("Configures a persistent keep-alive for the WireGuard tunnel, in seconds."), .help("Configures a persistent keep-alive for the WireGuard tunnel, in seconds."),
Arg::with_name("max-transmission-unit") Arg::new("max-transmission-unit")
.required(false) .required(false)
.takes_value(true) .num_args(1)
.long("max-transmission-unit") .long("max-transmission-unit")
.env("ONETUN_MTU") .env("ONETUN_MTU")
.default_value("1420") .default_value("1420")
.help("Configures the max-transmission-unit (MTU) of the WireGuard tunnel."), .help("Configures the max-transmission-unit (MTU) of the WireGuard tunnel."),
Arg::with_name("log") Arg::new("log")
.required(false) .required(false)
.takes_value(true) .num_args(1)
.long("log") .long("log")
.env("ONETUN_LOG") .env("ONETUN_LOG")
.default_value("info") .default_value("info")
.help("Configures the log level and format."), .help("Configures the log level and format."),
Arg::with_name("pcap") Arg::new("pcap")
.required(false) .required(false)
.takes_value(true) .num_args(1)
.long("pcap") .long("pcap")
.env("ONETUN_PCAP") .env("ONETUN_PCAP")
.help("Decrypts and captures IP packets on the WireGuard tunnel to a given output file."), .help("Decrypts and captures IP packets on the WireGuard tunnel to a given output file."),
Arg::with_name("remote") Arg::new("remote")
.required(false) .required(false)
.takes_value(true) .num_args(1..)
.multiple(true)
.long("remote") .long("remote")
.short("r") .short('r')
.help("Remote port forward configurations. The format of each argument is <src_port>:<dst_host>:<dst_port>[:TCP,UDP,...], \ .help("Remote port forward configurations. The format of each argument is <src_port>:<dst_host>:<dst_port>[:TCP,UDP,...], \
where <src_port> is the port the other peers will reach the server with, <dst_host> is the IP to forward to, and <dst_port> is the port to forward to. \ where <src_port> is the port the other peers will reach the server with, <dst_host> is the IP to forward to, and <dst_port> is the port to forward to. \
The <src_port> will be bound on onetun's peer IP, as specified by --source-peer-ip. If you pass a different value for <src_host> here, it will be rejected.\n\ The <src_port> will be bound on onetun's peer IP, as specified by --source-peer-ip. If you pass a different value for <src_host> here, it will be rejected.\n\
@ -144,7 +142,7 @@ impl Config {
// Combine `PORT_FORWARD` arg and `ONETUN_PORT_FORWARD_#` envs // Combine `PORT_FORWARD` arg and `ONETUN_PORT_FORWARD_#` envs
let mut port_forward_strings = HashSet::new(); let mut port_forward_strings = HashSet::new();
if let Some(values) = matches.values_of("PORT_FORWARD") { if let Some(values) = matches.get_many::<String>("PORT_FORWARD") {
for value in values { for value in values {
port_forward_strings.insert(value.to_owned()); port_forward_strings.insert(value.to_owned());
} }
@ -169,12 +167,12 @@ impl Config {
.collect(); .collect();
// Read source-peer-ip // Read source-peer-ip
let source_peer_ip = parse_ip(matches.value_of("source-peer-ip")) let source_peer_ip = parse_ip(matches.get_one::<String>("source-peer-ip"))
.with_context(|| "Invalid source peer IP")?; .with_context(|| "Invalid source peer IP")?;
// Combined `remote` arg and `ONETUN_REMOTE_PORT_FORWARD_#` envs // Combined `remote` arg and `ONETUN_REMOTE_PORT_FORWARD_#` envs
let mut port_forward_strings = HashSet::new(); let mut port_forward_strings = HashSet::new();
if let Some(values) = matches.values_of("remote") { if let Some(values) = matches.get_many::<String>("remote") {
for value in values { for value in values {
port_forward_strings.insert(value.to_owned()); port_forward_strings.insert(value.to_owned());
} }
@ -193,7 +191,7 @@ impl Config {
.map(|s| { .map(|s| {
PortForwardConfig::from_notation( PortForwardConfig::from_notation(
&s, &s,
matches.value_of("source-peer-ip").unwrap(), matches.get_one::<String>("source-peer-ip").unwrap(),
) )
}) })
.collect(); .collect();
@ -216,7 +214,7 @@ impl Config {
// Read private key from file or CLI argument // Read private key from file or CLI argument
let (group_readable, world_readable) = matches let (group_readable, world_readable) = matches
.value_of("private-key-file") .get_one::<String>("private-key-file")
.and_then(is_file_insecurely_readable) .and_then(is_file_insecurely_readable)
.unwrap_or_default(); .unwrap_or_default();
if group_readable { if group_readable {
@ -226,7 +224,9 @@ impl Config {
warnings.push("Private key file is world-readable. This is insecure.".into()); warnings.push("Private key file is world-readable. This is insecure.".into());
} }
let private_key = if let Some(private_key_file) = matches.value_of("private-key-file") { let private_key = if let Some(private_key_file) =
matches.get_one::<String>("private-key-file")
{
read_to_string(private_key_file) read_to_string(private_key_file)
.map(|s| s.trim().to_string()) .map(|s| s.trim().to_string())
.with_context(|| "Failed to read private key file") .with_context(|| "Failed to read private key file")
@ -236,15 +236,16 @@ impl Config {
Use \"--private-key-file <file containing private key>\", or the \"ONETUN_PRIVATE_KEY\" env variable instead.".into()); Use \"--private-key-file <file containing private key>\", or the \"ONETUN_PRIVATE_KEY\" env variable instead.".into());
} }
matches matches
.value_of("private-key") .get_one::<String>("private-key")
.map(String::from) .cloned()
.with_context(|| "Missing private key") .with_context(|| "Missing private key")
}?; }?;
let endpoint_addr = parse_addr(matches.value_of("endpoint-addr")) let endpoint_addr = parse_addr(matches.get_one::<String>("endpoint-addr"))
.with_context(|| "Invalid endpoint address")?; .with_context(|| "Invalid endpoint address")?;
let endpoint_bind_addr = if let Some(addr) = matches.value_of("endpoint-bind-addr") { let endpoint_bind_addr = if let Some(addr) = matches.get_one::<String>("endpoint-bind-addr")
{
let addr = parse_addr(Some(addr)).with_context(|| "Invalid bind address")?; let addr = parse_addr(Some(addr)).with_context(|| "Invalid bind address")?;
// Make sure the bind address and endpoint address are the same IP version // Make sure the bind address and endpoint address are the same IP version
if addr.ip().is_ipv4() != endpoint_addr.ip().is_ipv4() { if addr.ip().is_ipv4() != endpoint_addr.ip().is_ipv4() {
@ -268,33 +269,37 @@ impl Config {
parse_private_key(&private_key).with_context(|| "Invalid private key")?, parse_private_key(&private_key).with_context(|| "Invalid private key")?,
), ),
endpoint_public_key: Arc::new( endpoint_public_key: Arc::new(
parse_public_key(matches.value_of("endpoint-public-key")) parse_public_key(matches.get_one::<String>("endpoint-public-key"))
.with_context(|| "Invalid endpoint public key")?, .with_context(|| "Invalid endpoint public key")?,
), ),
preshared_key: parse_preshared_key(matches.value_of("preshared-key"))?, preshared_key: parse_preshared_key(matches.get_one::<String>("preshared-key"))?,
endpoint_addr, endpoint_addr,
endpoint_bind_addr, endpoint_bind_addr,
source_peer_ip, source_peer_ip,
keepalive_seconds: parse_keep_alive(matches.value_of("keep-alive")) keepalive_seconds: parse_keep_alive(matches.get_one::<String>("keep-alive"))
.with_context(|| "Invalid keep-alive value")?, .with_context(|| "Invalid keep-alive value")?,
max_transmission_unit: parse_mtu(matches.value_of("max-transmission-unit")) max_transmission_unit: parse_mtu(matches.get_one::<String>("max-transmission-unit"))
.with_context(|| "Invalid max-transmission-unit value")?, .with_context(|| "Invalid max-transmission-unit value")?,
log: matches.value_of("log").unwrap_or_default().into(), log: matches
pcap_file: matches.value_of("pcap").map(String::from), .get_one::<String>("log")
.cloned()
.unwrap_or_default(),
pcap_file: matches.get_one::<String>("pcap").cloned(),
warnings, warnings,
}) })
} }
} }
fn parse_addr(s: Option<&str>) -> anyhow::Result<SocketAddr> { fn parse_addr<T: AsRef<str>>(s: Option<T>) -> anyhow::Result<SocketAddr> {
s.with_context(|| "Missing address")? s.with_context(|| "Missing address")?
.as_ref()
.to_socket_addrs() .to_socket_addrs()
.with_context(|| "Invalid address")? .with_context(|| "Invalid address")?
.next() .next()
.with_context(|| "Could not lookup address") .with_context(|| "Could not lookup address")
} }
fn parse_ip(s: Option<&str>) -> anyhow::Result<IpAddr> { fn parse_ip(s: Option<&String>) -> anyhow::Result<IpAddr> {
s.with_context(|| "Missing IP")? s.with_context(|| "Missing IP")?
.parse::<IpAddr>() .parse::<IpAddr>()
.with_context(|| "Invalid IP address") .with_context(|| "Invalid IP address")
@ -305,14 +310,14 @@ fn parse_private_key(s: &str) -> anyhow::Result<X25519SecretKey> {
.map_err(|e| anyhow::anyhow!("{}", e)) .map_err(|e| anyhow::anyhow!("{}", e))
} }
fn parse_public_key(s: Option<&str>) -> anyhow::Result<X25519PublicKey> { fn parse_public_key(s: Option<&String>) -> anyhow::Result<X25519PublicKey> {
s.with_context(|| "Missing public key")? s.with_context(|| "Missing public key")?
.parse::<X25519PublicKey>() .parse::<X25519PublicKey>()
.map_err(|e| anyhow::anyhow!("{}", e)) .map_err(|e| anyhow::anyhow!("{}", e))
.with_context(|| "Invalid public key") .with_context(|| "Invalid public key")
} }
fn parse_preshared_key(s: Option<&str>) -> anyhow::Result<Option<[u8; 32]>> { fn parse_preshared_key(s: Option<&String>) -> anyhow::Result<Option<[u8; 32]>> {
if let Some(s) = s { if let Some(s) = s {
let psk = base64::decode(s).with_context(|| "Invalid pre-shared key")?; let psk = base64::decode(s).with_context(|| "Invalid pre-shared key")?;
Ok(Some(psk.try_into().map_err(|_| { Ok(Some(psk.try_into().map_err(|_| {
@ -323,7 +328,7 @@ fn parse_preshared_key(s: Option<&str>) -> anyhow::Result<Option<[u8; 32]>> {
} }
} }
fn parse_keep_alive(s: Option<&str>) -> anyhow::Result<Option<u16>> { fn parse_keep_alive(s: Option<&String>) -> anyhow::Result<Option<u16>> {
if let Some(s) = s { if let Some(s) = s {
let parsed: u16 = s.parse().with_context(|| { let parsed: u16 = s.parse().with_context(|| {
format!( format!(
@ -337,14 +342,14 @@ fn parse_keep_alive(s: Option<&str>) -> anyhow::Result<Option<u16>> {
} }
} }
fn parse_mtu(s: Option<&str>) -> anyhow::Result<usize> { fn parse_mtu(s: Option<&String>) -> anyhow::Result<usize> {
s.with_context(|| "Missing MTU")? s.with_context(|| "Missing MTU")?
.parse() .parse()
.with_context(|| "Invalid MTU") .with_context(|| "Invalid MTU")
} }
#[cfg(unix)] #[cfg(unix)]
fn is_file_insecurely_readable(path: &str) -> Option<(bool, bool)> { fn is_file_insecurely_readable(path: &String) -> Option<(bool, bool)> {
use std::fs::File; use std::fs::File;
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
@ -353,7 +358,7 @@ fn is_file_insecurely_readable(path: &str) -> Option<(bool, bool)> {
} }
#[cfg(not(unix))] #[cfg(not(unix))]
fn is_file_insecurely_readable(_path: &str) -> Option<(bool, bool)> { fn is_file_insecurely_readable(_path: &String) -> Option<(bool, bool)> {
// No good way to determine permissions on non-Unix target // No good way to determine permissions on non-Unix target
None None
} }