update routes to use Strings and finish /save-peer

master
lalanza808 4 years ago
parent 2c066fc934
commit f5fb5f6e7e

@ -21,7 +21,7 @@ fn main() {
println!("{:#?}", wg_opts);
rocket::ignite()
.mount("/", routes![
routes::home, routes::add_peer, routes::save_peer_config,
routes::home, routes::add_peer, routes::save_peer,
])
.mount("/static", StaticFiles::from("./static"))
.register(catchers![not_found])

@ -14,18 +14,18 @@ use std::fs;
#[get("/")]
pub fn home() -> Template {
let show_config = wg_cmd(&["show"]);
let whoami = sh_cmd("whoami");
let uptime = sh_cmd("uptime");
let hostname = sh_cmd("hostname");
let netstat_info = sh_cmd("netstat -tan | grep \"ESTABLISHED\\|CLOSE_WAIT\"");
let show_config_cmd = wg_cmd(vec!["show".to_string()]);
let whoami = sh_cmd("whoami".to_string());
let uptime = sh_cmd("uptime".to_string());
let hostname = sh_cmd("hostname".to_string());
let netstat_info = sh_cmd("netstat -tan | grep \"ESTABLISHED\\|CLOSE_WAIT\"".to_string());
let shell_ps1 = format!(
"{}@{} $",
whoami.trim_end(),
hostname.trim_end()
);
let context: JsonValue = json!({
"show_config": show_config.trim_end(),
"show_config": show_config_cmd.0.trim_end(),
"uptime": uptime.trim_end(),
"netstat_info": netstat_info,
"shell_ps1": shell_ps1,
@ -36,10 +36,13 @@ pub fn home() -> Template {
#[get("/add-peer")]
pub fn add_peer() -> Template {
let new_key = wg_cmd(&["genkey"]);
let privkey_cmd = wg_cmd(vec!["genkey".to_string()]);
let generate_pubkey = format!("echo '{}' | wg pubkey", privkey_cmd.0.trim_end());
let pubkey_cmd = sh_cmd(generate_pubkey);
let state = WireGuardOptions { ..Default::default() };
let context: JsonValue = json!({
"privkey": new_key.trim_end(),
"privkey": privkey_cmd.0.trim_end(),
"pubkey": pubkey_cmd.trim_end(),
"state": state,
});
@ -47,24 +50,29 @@ pub fn add_peer() -> Template {
}
#[post("/save-peer", data = "<peer_config>")]
pub fn save_peer_config(peer_config: Json<PeerConfig>) -> JsonValue {
println!("{:#?}", peer_config);
// let peer_config = format!("[Peer]
// # name = {}
// PublicKey = {}
// AllowedIPs = {}/32")
// let mut file = File::create("/tmp/wgas.conf").unwrap();
// let conf_str = serde_json::to_string(&input.into_inner()).unwrap();
// file.write_all().unwrap();
// let wg_set = wg_cmda(&["set", "wg0", "private-key", "/tmp/wgas.conf"]); // todo - randomize
// let file_removed = match fs::remove_file("/tmp/wgas.conf"){
// Ok(_) => true,
// Err(_) => false
// };
pub fn save_peer(peer_config: Json<PeerConfig>) -> JsonValue {
let state = WireGuardOptions { ..Default::default() };
let new_peer_config = format!(
"[Peer]\n# name = {}\nPublicKey = {}\nAllowedIPs = {}/32",
peer_config.name,
peer_config.pubkey,
peer_config.ipaddr,
);
let file_path = format!(
"./etc/{}-{}-wgas.conf",
peer_config.name,
peer_config.ipaddr,
);
let mut file = File::create(&file_path).unwrap();
file.write_all(&new_peer_config.as_bytes()).unwrap();
let wg_set = wg_cmd(vec!["addconf".to_string(), state.interface, file_path.clone()]);
let file_removed = match fs::remove_file(file_path){
Ok(_) => true,
Err(_) => false
};
json!({
"eyo": "hello",
"config_data": "asd",
// "response": wg_set.trim_end(),
// "key_cleared": file_removed
"command_stdout": wg_set.0.trim_end(),
"command_exit": wg_set.1,
"file_removed": file_removed
})
}

Loading…
Cancel
Save