splitting code into multiple files
parent
8aaec7df33
commit
c3cd1a7ea0
@ -0,0 +1,25 @@
|
||||
use std::env;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WireGuardOptions {
|
||||
pub pubkey: String,
|
||||
pub route: String,
|
||||
pub endpoint: String,
|
||||
pub sudo: String,
|
||||
pub port: String,
|
||||
pub dns: String,
|
||||
}
|
||||
|
||||
|
||||
impl Default for WireGuardOptions {
|
||||
fn default() -> WireGuardOptions {
|
||||
WireGuardOptions {
|
||||
pubkey: env!("WGAS_PUBKEY").to_string(),
|
||||
endpoint: env!("WGAS_ENDPOINT").to_string(),
|
||||
sudo: env::var("WGAS_SUDO").unwrap_or("false".to_string()),
|
||||
dns: env::var("WGAS_DNS").unwrap_or("1.1.1.1".to_string()),
|
||||
route: env::var("WGAS_ROUTE").unwrap_or("0.0.0.0/0".to_string()),
|
||||
port: env::var("WGAS_PORT").unwrap_or(51820.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
use std::process::Command;
|
||||
|
||||
|
||||
pub fn genkey() -> String {
|
||||
let output = Command::new("./bin/wg_cmd")
|
||||
.arg("genkey")
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
let privkey = String::from_utf8(output.stdout)
|
||||
.unwrap();
|
||||
|
||||
privkey
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
#[path = "data.rs"]
|
||||
mod data;
|
||||
#[path = "helpers.rs"]
|
||||
mod helpers;
|
||||
|
||||
use data::WireGuardOptions;
|
||||
use helpers::genkey;
|
||||
use rocket_contrib::templates::Template;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use qrcode_generator::QrCodeEcc;
|
||||
|
||||
|
||||
#[get("/")]
|
||||
pub fn index() -> String {
|
||||
"sup dog".to_string()
|
||||
}
|
||||
|
||||
#[get("/generate")]
|
||||
pub fn generate() -> Template {
|
||||
let new_key = genkey();
|
||||
let state = WireGuardOptions { ..Default::default() };
|
||||
let full_config = format!("[Interface]
|
||||
PrivateKey = {}
|
||||
Address = 10.66.66.2/32
|
||||
DNS = {}
|
||||
|
||||
[Peer]
|
||||
PublicKey = {}
|
||||
AllowedIPs = {}
|
||||
Endpoint = {}:{}
|
||||
PersistentKeepalive = 21",
|
||||
new_key.trim_end(),
|
||||
state.dns,
|
||||
state.pubkey,
|
||||
state.route,
|
||||
state.endpoint,
|
||||
state.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)
|
||||
}
|
Loading…
Reference in New Issue