Skip to content

Commit

Permalink
Merge #378
Browse files Browse the repository at this point in the history
378: Add genconf sub-command r=sopium a=sopium

Generate configuration for both sides for adding a new peer to an
existing node.

bors merge

Co-authored-by: Yin Guanhao <sopium@mysterious.site>
  • Loading branch information
bors[bot] and blckngm authored Aug 2, 2021
2 parents d192efd + b2ac043 commit 2d59468
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions src/cli/real_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 = <X25519 as DH>::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,
Expand Down

0 comments on commit 2d59468

Please sign in to comment.