From dd7cf1152a2fd67398533220b68eb292ee10a87f Mon Sep 17 00:00:00 2001 From: Yin Guanhao Date: Tue, 3 Aug 2021 00:57:16 +0800 Subject: [PATCH 1/2] Add genconf sub-command Generate configuration for both sides for adding a new peer to an existing node --- src/cli/real_main.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/cli/real_main.rs b/src/cli/real_main.rs index 9aa246f..23bf124 100644 --- a/src/cli/real_main.rs +++ b/src/cli/real_main.rs @@ -205,6 +205,13 @@ enum Cmd { Pubkey, #[structopt(about = "Generate preshared key")] Genpsk, + #[structopt(about = "Generate configuration")] + Genconf { + #[structopt(long)] + peer_pubkey: String, + #[structopt(long)] + self_ip: String, + }, #[structopt(about = "Transform wg config files to TOML")] Transform { #[structopt(long)] @@ -263,6 +270,63 @@ impl Cmd { ); } } + Cmd::Genconf { + peer_pubkey, + self_ip, + } => { + use ansi_term::{Color, Style}; + + let self_private_key = X25519::genkey(); + let self_private_key_base64 = base64::encode(self_private_key.as_slice()); + let self_public_key = ::pubkey(&self_private_key); + let self_public_key_base64 = base64::encode(&self_public_key); + + let self_ip = &*self_ip; + let self_config = toml::toml! { + [Interface] + PrivateKey = self_private_key_base64 + Address = self_ip + + [[Peer]] + PublicKey = peer_pubkey + }; + let peer_config = toml::toml! { + [[Peer]] + PublicKey = self_public_key_base64 + AllowedIPs = [self_ip] + }; + + let is_tty = atty::is(atty::Stream::Stdout); + + macro_rules! if_tty { + ($s:expr) => { + if is_tty { + $s + } else { + Style::new() + } + }; + } + + let yellow = if_tty!(Color::Yellow.bold()); + let cyan = if_tty!(Color::Cyan.bold()); + + print!( + r#"=========================================================== +Self config: +=========================================================== +{}=========================================================== + + +=========================================================== +Add this to peer's config: +=========================================================== +{}=========================================================== +"#, + yellow.paint(toml::to_string(&self_config)?), + cyan.paint(toml::to_string(&peer_config)?) + ); + } Cmd::Transform { overwrite, config_file, From b2ac043bd553749220f06772d394b73d50690ba9 Mon Sep 17 00:00:00 2001 From: Yin Guanhao Date: Tue, 3 Aug 2021 01:54:09 +0800 Subject: [PATCH 2/2] Bump version to v0.3.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- fuzz/Cargo.lock | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a8cfa7b..c9a171c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1740,7 +1740,7 @@ dependencies = [ [[package]] name = "titun" -version = "0.3.0" +version = "0.3.1" dependencies = [ "ansi_term 0.12.1", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 79cc3fa..d3714ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ authors = ["sopium"] license = "GPL-3.0" name = "titun" repository = "https://github.com/sopium/titun" -version = "0.3.0" +version = "0.3.1" autobenches = false [workspace] diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 2bef91c..9c8307c 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -1511,7 +1511,7 @@ dependencies = [ [[package]] name = "titun" -version = "0.3.0" +version = "0.3.1" dependencies = [ "ansi_term 0.12.1", "anyhow",