setting up cli opts and improving config generation

master
lalanza808 4 years ago
parent a9c4a7efa3
commit 72d8a3f2e0

@ -9,3 +9,4 @@ rocket = "0.4.4"
rocket_contrib = {version = "0.4.4", features = ["tera_templates"]}
qrcode-generator = "1.0.6"
base64 = "0.12.0"
structopt = "0.3.14"

@ -1,6 +1,8 @@
#!/bin/sh
add-apt-repository ppa:wireguard/wireguard -y
apt install wireguard -y
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
# Install WireGuard
apt-get install -y software-properties-common
add-apt-repository -y ppa:wireguard/wireguard
apt-get update
apt-get install -y "linux-headers-$(uname -r)"
apt-get install -y wireguard iptables

@ -3,12 +3,29 @@
#[macro_use] extern crate rocket_contrib;
extern crate qrcode_generator;
use rocket::State;
use rocket_contrib::templates::Template;
use rocket_contrib::json::JsonValue;
use rocket_contrib::serve::StaticFiles;
use qrcode_generator::QrCodeEcc;
use structopt::StructOpt;
use std::process::Command;
#[derive(Debug, StructOpt)]
struct Cli {
#[structopt(short = "k", long = "pubkey")]
pubkey: String,
#[structopt(short = "r", long = "route")]
route: String,
#[structopt(short = "e", long = "endpoint")]
endpoint: String,
#[structopt(short = "p", long = "port")]
port: u32,
#[structopt(short = "d", long = "dns")]
dns: String,
}
fn genkey() -> String {
let output = Command::new("wg")
.arg("genkey")
@ -25,14 +42,33 @@ fn index() -> String {
"sup dog".to_string()
}
#[get("/new")]
fn new_key() -> Template {
#[get("/generate")]
fn generate(cli_args: State<Cli>) -> Template {
let new_key = genkey();
let qr_code: String = qrcode_generator::to_svg_to_string(new_key, QrCodeEcc::Low, 256, None)
.unwrap();
let full_config = format!("[Interface]
PrivateKey = {}
Address = 10.66.66.2/32
DNS = {}
[Peer]
PublicKey = {}
AllowedIPs = {}
Endpoint = {}:{}
PersistentKeepalive = 21",
new_key.trim_end(),
cli_args.dns,
cli_args.pubkey,
cli_args.route,
cli_args.endpoint,
cli_args.port
);
let qr_code: String = qrcode_generator::to_svg_to_string(
&full_config, QrCodeEcc::Low, 256, None
).unwrap();
let qr_code: String = base64::encode(qr_code);
let context: JsonValue = json!({
"qr_code": qr_code,
"full_config": full_config
});
Template::render("new", context)
}
@ -43,9 +79,12 @@ fn not_found() -> String {
}
fn main() {
let cli_args = Cli::from_args();
println!("{:#?}", cli_args);
rocket::ignite()
.manage(cli_args)
.mount("/", routes![
index, new_key
index, generate
])
.mount("/static", StaticFiles::from("./static"))
.register(catchers![not_found])

@ -1 +1,13 @@
<html>
<body>
<div>
<p>Here's a client config for you:</p>
<pre style="color: red;">
{{ full_config }}
</pre>
</div>
<img src="data:image/svg+xml;base64,{{ qr_code }}" width=200 class="center">
</body>
</html>

Loading…
Cancel
Save