diff --git a/src/main.rs b/src/main.rs index cf739b4..b340d21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ -use std::env; -use std::process::Command; +use qstring::QString; use regex::Regex; use serde_derive::Serialize; -use qstring::QString; +use std::env; +use std::process::Command; #[derive(Serialize)] struct Response { @@ -33,7 +33,11 @@ fn uci_get(key: &str) -> Option { let output = Command::new("uci").arg("get").arg("-q").arg(key).output(); if let Ok(output) = output { if output.status.success() { - Some(String::from_utf8(output.stdout).unwrap_or_default().replace("\n", "")) + Some( + String::from_utf8(output.stdout) + .unwrap_or_default() + .replace("\n", ""), + ) } else { None } @@ -49,16 +53,28 @@ fn register_wg(wg_backbone_path: &str, node: &str, key: &str) -> Option<(Wiregua if let Ok(node) = node { if key_check.is_match(key) { let output = Command::new("sudo") - .arg(wg_backbone_path) - .arg("accept") - .arg(node.to_string()) - .arg(key) - .output(); + .arg(wg_backbone_path) + .arg("accept") + .arg(node.to_string()) + .arg(key) + .output(); if let Ok(output) = output { if output.status.success() { - return Some((WireguardInfo{ node, key: key.to_string() }, false)); + return Some(( + WireguardInfo { + node, + key: key.to_string(), + }, + false, + )); } else if output.status.code() == Some(2) { - return Some((WireguardInfo{ node, key: key.to_string() }, true)); + return Some(( + WireguardInfo { + node, + key: key.to_string(), + }, + true, + )); } } } @@ -83,8 +99,16 @@ fn main() { let uci_restrict = uci_get("wg_cgi.uci.wg_restrict").unwrap_or_default(); let sh_wg_backbone = uci_get("wg_cgi.sh.wg_backbone_path").unwrap_or_default(); - if uci_public.is_empty() || uci_node.is_empty() || uci_restrict.is_empty() || sh_wg_backbone.is_empty() { - let response = Response{ status: Status::NotConfigured, server: None, client: None }; + if uci_public.is_empty() + || uci_node.is_empty() + || uci_restrict.is_empty() + || sh_wg_backbone.is_empty() + { + let response = Response { + status: Status::NotConfigured, + server: None, + client: None, + }; print_output_and_exit(&response); } @@ -94,7 +118,11 @@ fn main() { let server_restricted = server_restricted.trim(); if server_key.is_empty() || server_node.is_empty() || server_restricted.is_empty() { - let response = Response{ status: Status::NotConfigured, server: None, client: None }; + let response = Response { + status: Status::NotConfigured, + server: None, + client: None, + }; print_output_and_exit(&response); } @@ -107,20 +135,25 @@ fn main() { let query_string = env::var("QUERY_STRING").unwrap_or_default(); if server_restricted != "1" && request_method == "GET" && !query_string.is_empty() { - let qs = QString::from(&format!("?{}", query_string)[..]); let client_key = qs.get("key").unwrap_or_default(); let client_node = qs.get("node").unwrap_or_default(); - + let response = match register_wg(sh_wg_backbone.as_str(), client_node, client_key) { - Some((client, false)) => { - Response{ status: Status::RequestAccepted, server, client: Some(client) } + Some((client, false)) => Response { + status: Status::RequestAccepted, + server, + client: Some(client), }, - Some((_, true)) => { - Response{ status: Status::RequestAlreadyRegistered, server, client: None } + Some((_, true)) => Response { + status: Status::RequestAlreadyRegistered, + server, + client: None, }, - None => { - Response{ status: Status::RequestFailed, server, client: None } + None => Response { + status: Status::RequestFailed, + server, + client: None, }, }; print_output_and_exit(&response); @@ -128,9 +161,17 @@ fn main() { } let response = if server_restricted == "1" { - Response{ status: Status::Restricted, server, client: None } + Response { + status: Status::Restricted, + server, + client: None, + } } else { - Response{ status: Status::NotRestricted, server, client: None } + Response { + status: Status::NotRestricted, + server, + client: None, + } }; print_output_and_exit(&response); }