Skip to content

Commit

Permalink
Code format
Browse files Browse the repository at this point in the history
  • Loading branch information
hibo98 committed Sep 10, 2021
1 parent 51a3246 commit a0b4c19
Showing 1 changed file with 65 additions and 24 deletions.
89 changes: 65 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -33,7 +33,11 @@ fn uci_get(key: &str) -> Option<String> {
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
}
Expand All @@ -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,
));
}
}
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -107,30 +135,43 @@ 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);
return;
}

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);
}

0 comments on commit a0b4c19

Please sign in to comment.