From 3b5622f49f9f0aee5fcf940fd0373ca5ca1a6301 Mon Sep 17 00:00:00 2001 From: cab Date: Fri, 6 May 2022 20:20:12 +0400 Subject: [PATCH] embedded genkey/pubkey impl, library version bump --- Cargo.lock | 255 ++++++++++++++++++++++++++++++++------------ Cargo.toml | 11 +- src/configs/mod.rs | 2 +- src/main.rs | 44 ++++---- src/wg_tools/mod.rs | 56 ++++++---- 5 files changed, 245 insertions(+), 123 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 29b1889..893f2f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "cfg-if" version = "1.0.0" @@ -54,19 +60,50 @@ checksum = "17cc5e6b5ab06331c33589842070416baa137e8b0eb912b008cfd4a78ada7919" [[package]] name = "clap" -version = "3.0.14" +version = "3.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b63edc3f163b3c71ec8aa23f9bd6070f77edbf3d1d198b164afa90ff00e4ec62" +checksum = "f52d4f8e4a1419219935762e32913b4430f37cb0c0200ad17a89ee18c0188a9f" dependencies = [ "atty", "bitflags", + "clap_lex", "indexmap", - "os_str_bytes", "strsim", "termcolor", "textwrap", ] +[[package]] +name = "clap_lex" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "curve25519-dalek" +version = "3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90f9d052967f590a76e62eb387bd0bbb1b000182c3cefe5364db6b7211651bc0" +dependencies = [ + "byteorder", + "digest", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + [[package]] name = "env_logger" version = "0.7.1" @@ -90,15 +127,36 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "generic-array" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "getrandom" -version = "0.2.4" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" +checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.10.2+wasi-snapshot-preview1", ] [[package]] @@ -109,12 +167,9 @@ checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" [[package]] name = "heck" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "hermit-abi" @@ -147,9 +202,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.8.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee" dependencies = [ "autocfg", "hashbrown", @@ -157,9 +212,9 @@ dependencies = [ [[package]] name = "ipnetwork" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4088d739b183546b239688ddbc79891831df421773df95e236daf7867866d355" +checksum = "1f84f1612606f3753f205a4e9a2efd6fe5b4c573a6269b2cc6c3003d44a0d127" dependencies = [ "serde", ] @@ -172,15 +227,15 @@ checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] name = "libc" -version = "0.2.117" +version = "0.2.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" +checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" [[package]] name = "log" -version = "0.4.14" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", ] @@ -193,18 +248,15 @@ checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] name = "memchr" -version = "2.4.1" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "os_str_bytes" version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" -dependencies = [ - "memchr", -] [[package]] name = "percent-encoding" @@ -230,9 +282,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.36" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" dependencies = [ "unicode-xid", ] @@ -254,23 +306,22 @@ checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" -version = "1.0.15" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" dependencies = [ "proc-macro2", ] [[package]] name = "rand" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", - "rand_core", - "rand_hc", + "rand_core 0.6.3", ] [[package]] @@ -280,32 +331,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.3", ] [[package]] name = "rand_core" -version = "0.6.3" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom", + "getrandom 0.1.16", ] [[package]] -name = "rand_hc" -version = "0.3.1" +name = "rand_core" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ - "rand_core", + "getrandom 0.2.6", ] [[package]] name = "regex" -version = "1.5.4" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" dependencies = [ "aho-corasick", "memchr", @@ -332,18 +383,18 @@ checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" [[package]] name = "serde" -version = "1.0.136" +version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.136" +version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" dependencies = [ "proc-macro2", "quote", @@ -352,9 +403,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.79" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" +checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c" dependencies = [ "itoa", "ryu", @@ -369,15 +420,15 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "strum" -version = "0.23.0" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cae14b91c7d11c9a851d3fbc80a963198998c2a64eec840477fa92d8ce9b70bb" +checksum = "e96acfc1b70604b8b2f1ffa4c57e59176c7dbb05d556c71ecd2f5498a1dee7f8" [[package]] name = "strum_macros" -version = "0.23.1" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bb0dc7ee9c15cea6199cde9a127fa16a4c5819af85395457ad72d68edc85a38" +checksum = "6878079b17446e4d3eba6192bb0a2950d5b14f0ed8424b852310e5a94345d0ef" dependencies = [ "heck", "proc-macro2", @@ -386,37 +437,55 @@ dependencies = [ "syn", ] +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + [[package]] name = "syn" -version = "1.0.86" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ff7c592601f11445996a06f8ad0c27f094a58857c2f89e97974ab9235b92c52" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "synstructure" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ "proc-macro2", "quote", + "syn", "unicode-xid", ] [[package]] name = "termcolor" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" dependencies = [ "winapi-util", ] [[package]] name = "textwrap" -version = "0.14.2" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" [[package]] name = "tinyvec" -version = "1.5.1" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" dependencies = [ "tinyvec_macros", ] @@ -427,11 +496,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + [[package]] name = "unicode-bidi" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f" +checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" [[package]] name = "unicode-normalization" @@ -442,17 +517,11 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" - [[package]] name = "unicode-xid" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" [[package]] name = "url" @@ -466,6 +535,18 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" @@ -483,11 +564,13 @@ dependencies = [ "pretty_env_logger", "qrcode", "rand", + "rand_core 0.5.1", "serde", "serde_json", "strum", "strum_macros", "url", + "x25519-dalek", ] [[package]] @@ -520,3 +603,35 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "x25519-dalek" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2392b6b94a576b4e2bf3c5b2757d63f10ada8020a2e4d08ac849ebcf6ea8e077" +dependencies = [ + "curve25519-dalek", + "rand_core 0.5.1", + "zeroize", +] + +[[package]] +name = "zeroize" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4756f7db3f7b5574938c3eb1c117038b8e07f95ee6718c0efad4ac21508f1efd" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f8f187641dad4f680d25c4bfc4225b418165984179f26ca76ec4fb6441d3a17" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] diff --git a/Cargo.toml b/Cargo.toml index d5efb01..6bf17b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,10 +22,13 @@ base64 = "0.13.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -strum = "0.23.0" -strum_macros = "0.23.1" +strum = "0.24.0" +strum_macros = "0.24.0" + +rand_core = {version = "0.5", default-features = false, features = ["getrandom"]} +x25519-dalek = "1" qrcode = { version = "0.12", default-features = false } -ipnetwork = "0.18.0" -clap = "3.0.14" +ipnetwork = "0.19.0" +clap = "3.1.16" pretty_env_logger = "0.4" diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 585263a..953c4ae 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -144,7 +144,7 @@ pub struct Peer { pub struct ProxyConfig { /// Which networks to proxy pub networks: Vec, - /// Whether to proxy whole internet, except [local networks](http://link-to-wikipedia-list) + /// Whether to proxy whole internet, except [local networks](https://en.wikipedia.org/wiki/Private_network) /// Useful on mobile devices. Can be redundant. /// See also [`GLOBAL_NET_V4`] and [`GLOBAL_NET_V6`] pub use_global_networks: bool, diff --git a/src/main.rs b/src/main.rs index d38db69..0eb44b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ extern crate serde_json; use crate::configs::check_endpoint; use crate::configs::nix::KeyFileExportConfig; use crate::configs::ConfigType; -use clap::AppSettings::SubcommandRequired; use ipnetwork::IpNetwork; use std::io::Write; use std::net::IpAddr; @@ -144,7 +143,7 @@ fn command_new_peer(cfg: &mut configs::WireguardNetworkInfo, matches: &clap::Arg name, endpoint: None, id: peer_id, - private_key: wg_tools::gen_private_key()?, + private_key: wg_tools::gen_private_key(), flags: vec![], }; @@ -248,7 +247,7 @@ fn command_export_secrets( Ok(()) } -fn edit_params<'a>(subcommand: clap::App<'a>) -> clap::App<'a> { +fn edit_params<'a>(subcommand: clap::Command<'a>) -> clap::Command<'a> { subcommand .arg(clap::Arg::new("endpoint") .short('e') @@ -264,7 +263,7 @@ fn edit_params<'a>(subcommand: clap::App<'a>) -> clap::App<'a> { .long("dns") .help("DNS for a peer") .value_name("DNS_1,DNS_2") - .use_delimiter(true) + .use_value_delimiter(true) .validator(|f| IpAddr::from_str(f) .map(|_| ()) .map_err(|f|f.to_string()) @@ -311,7 +310,7 @@ fn edit_params<'a>(subcommand: clap::App<'a>) -> clap::App<'a> { ) } -fn export_params<'a>(subcommand: clap::App<'a>) -> clap::App<'a> { +fn export_params<'a>(subcommand: clap::Command<'a>) -> clap::Command<'a> { subcommand .arg( clap::Arg::new("name") @@ -331,12 +330,12 @@ fn main() { pretty_env_logger::init(); // std::panic::set_hook(Box::new(panic_hook)); - let args = clap::App::new("wg-bond") + let args = clap::Command::new("wg-bond") .version("0.3.0") .about("Wireguard configuration manager") .author("Vladimir Serov ") .long_about("Wireguard configuration manager.\nSources: https://gitlab.com/cab404/wg-bond.") - .setting(SubcommandRequired) + .subcommand_required(true) .arg( clap::Arg::new("config") .short('c') @@ -345,10 +344,10 @@ fn main() { .value_name("FILE") .default_value("./wg-bond.json") .takes_value(true) - .use_delimiter(false), + .use_value_delimiter(false), ) .subcommand( - clap::App::new("init") + clap::Command::new("init") .about("Initializes a config file") .arg(clap::Arg::new("name").help("Network name").required(true)) .arg( @@ -363,12 +362,12 @@ fn main() { .map_err(|e| e.to_string()) }) .default_value("10.0.0.0/24") - .use_delimiter(false) + .use_value_delimiter(false) .takes_value(true), ), ) .subcommand( - edit_params(clap::App::new("add")) + edit_params(clap::Command::new("add")) .about("Adds a new peer to the network") .arg( clap::Arg::new("name") @@ -376,9 +375,9 @@ fn main() { .required(true), ), ) - .subcommand(clap::App::new("list").about("Lists all added peers")) + .subcommand(clap::Command::new("list").about("Lists all added peers")) .subcommand( - edit_params(clap::App::new("edit")) + edit_params(clap::Command::new("edit")) .about("Edits existing peer") .arg( clap::Arg::new("name") @@ -387,7 +386,7 @@ fn main() { ), ) .subcommand( - export_params(clap::App::new("nix")) + export_params(clap::Command::new("nix")) .arg( clap::Arg::new("separate-secrets") .long("separate-secrets") @@ -398,9 +397,9 @@ fn main() { ) .about("Generates Nix configs"), ) - .subcommand(clap::App::new("nixops").about("Generates NixOps config for all peers")) + .subcommand(clap::Command::new("nixops").about("Generates NixOps config for all peers")) .subcommand( - clap::App::new("secrets") + clap::Command::new("secrets") .about("Generates secret files for all peers") .arg( clap::Arg::new("target") @@ -408,16 +407,16 @@ fn main() { .default_value("./secrets"), ), ) - .subcommand(clap::App::new("hosts").about("Generates /etc/hosts for all peers")) + .subcommand(clap::Command::new("hosts").about("Generates /etc/hosts for all peers")) .subcommand( - clap::App::new("rm").about("Deletes a peer").arg( + clap::Command::new("rm").about("Deletes a peer").arg( clap::Arg::new("name") .help("Name of a new peer") .required(true), ), ) - .subcommand(export_params(clap::App::new("qr")).about("Generates QR code with config")) - .subcommand(export_params(clap::App::new("conf")).about("Generates wg-quick configs")) + .subcommand(export_params(clap::Command::new("qr")).about("Generates QR code with config")) + .subcommand(export_params(clap::Command::new("conf")).about("Generates wg-quick configs")) .get_matches(); let cfg_file = args.value_of("config").unwrap(); @@ -494,8 +493,3 @@ fn main() { Err(e) => println!("{}", e), } } - -// fn panic_hook(info: &std::panic::PanicInfo<'_>) { -// println!("We panicked."); -// println!("mowmow : {:?}", info.payload()); -// } diff --git a/src/wg_tools/mod.rs b/src/wg_tools/mod.rs index 17b07d0..472bb05 100644 --- a/src/wg_tools/mod.rs +++ b/src/wg_tools/mod.rs @@ -1,5 +1,7 @@ -use std::io::Write; -use std::process::{Command, Stdio}; +use base64; +use rand_core::OsRng; +use std::convert::TryFrom; +use x25519_dalek::{PublicKey, StaticSecret}; fn read_key(from: &Vec) -> String { String::from_utf8(from.to_owned()) @@ -8,31 +10,39 @@ fn read_key(from: &Vec) -> String { .to_string() } -pub fn gen_private_key() -> Result { - let key_bytes = Command::new("wg") - .arg("genkey") - .output() - .map_err(|e| format!("Failed to run 'wg genkey': {}", e))? - .stdout; +pub fn gen_private_key() -> String { + base64::encode(StaticSecret::new(OsRng).to_bytes()) +} - Ok(read_key(&key_bytes)) +#[test] +pub fn test_on_regular_keys() { + assert_eq!( + gen_public_key("2JhyJzhRgEE9+lU7zPA8iLNvSwkJpHA2eTOndYR9BVs="), + Ok("AM5SumUi+GKqTpHJM2lANpDwP0B0i1Ks+0aCCgnV0nU=".to_string()) + ); + assert_eq!( + gen_public_key("dGVzdAo="), + Err("Expected key size of 32, got 5".to_string()) + ); + assert_eq!( + gen_public_key("JhyJzhRgEE9+lU7zPA8iLNvSwkJpHA2eTOndYR9BVs="), + Err("Cannot decode base64".to_string()) + ); } pub fn gen_public_key(private_key: &str) -> Result { - let mut child = Command::new("wg") - .arg("pubkey") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .map_err(|e| format!("Failed to run 'wg pubkey': {}", e))?; + let private_base64 = base64::decode(private_key).map_err(|_| "Cannot decode base64")?; - child - .stdin - .as_mut() - .unwrap() - .write_all(private_key.as_bytes()) - .unwrap(); + if private_base64.len() != 32 { + return Err(format!( + "Expected key size of 32, got {}", + private_base64.len() + )); + } + let mut private_sized: [u8; 32] = [0; 32]; + private_sized.clone_from_slice(&private_base64[..]); + + let secret = StaticSecret::try_from(private_sized).map_err(|_| "failed to convert keys?")?; - let out = child.wait_with_output().unwrap(); - Ok(read_key(&out.stdout)) + Ok(base64::encode(PublicKey::from(&secret).as_bytes())) }