splitting code into multiple files

master
lalanza808 4 years ago
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
}

@ -1,77 +1,13 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene, decl_macro, plugin)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
extern crate qrcode_generator;
mod routes;
mod data;
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")
.output()
.expect("failed to execute process");
let stdout = output.stdout;
let privkey = String::from_utf8(stdout)
.unwrap();
return privkey;
}
#[get("/")]
fn index() -> String {
"sup dog".to_string()
}
#[get("/generate")]
fn generate(cli_args: State<Cli>) -> Template {
let new_key = genkey();
let full_config = format!("[Interface]
PrivateKey = {}
Address = 10.66.66.2/32
DNS = {}
use rocket_contrib::templates::Template;
[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)
}
#[catch(404)]
fn not_found() -> String {
@ -79,12 +15,13 @@ fn not_found() -> String {
}
fn main() {
let cli_args = Cli::from_args();
println!("{:#?}", cli_args);
let wg_opts = data::WireGuardOptions {
..Default::default()
};
println!("{:#?}", wg_opts);
rocket::ignite()
.manage(cli_args)
.mount("/", routes![
index, generate
routes::index, routes::generate
])
.mount("/static", StaticFiles::from("./static"))
.register(catchers![not_found])

@ -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…
Cancel
Save