Skip to content

Commit 7b9d64b

Browse files
committed
✨ DNS
1 parent edad037 commit 7b9d64b

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

src/main.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@ struct FirewallPath {
2222
rule: FirewallRule
2323
}
2424

25+
#[derive(Debug, Deserialize)]
26+
struct DNSPatch {
27+
option: String,
28+
value: String
29+
}
30+
2531
#[async_std::main]
2632
async fn main() -> tide::Result<()> {
2733
let mut app = tide::new();
2834
app.at("/api/firewall").get(get_firewall);
2935
app.at("/api/firewall/rule").put(put_firewall_rule);
3036
app.at("/api/firewall/rule").delete(delete_firewall_rule);
37+
app.at("/api/dns").get(get_dns);
38+
app.at("/api/dns").patch(patch_dns);
3139
app.at("/api/*").all(err404);
3240

3341
let cors = CorsMiddleware::new()
34-
.allow_methods("GET, PUT, DELETE, POST, OPTIONS".parse::<HeaderValue>().unwrap())
42+
.allow_methods("GET, PUT, DELETE, POST, PATCH, OPTIONS".parse::<HeaderValue>().unwrap())
3543
.allow_origin(Origin::from("*"))
3644
.allow_credentials(false);
3745
app.with(cors);
@@ -90,6 +98,29 @@ async fn delete_firewall_rule(mut req: Request<()>) -> tide::Result {
9098
Ok("{\"success\": true}".into())
9199
}
92100

101+
async fn get_dns(mut req: Request<()>) -> tide::Result {
102+
let dns_text = fs::read_to_string(Path::new(EZG_ROOT).join("dns.json")).expect("Unable to read file");
103+
println!("{}", dns_text);
104+
Ok(dns_text.into())
105+
}
106+
107+
async fn patch_dns(mut req: Request<()>) -> tide::Result {
108+
let DNSPatch { option, value } = req.body_json().await?;
109+
110+
// run bash script to add rule
111+
let output = std::process::Command::new("bash")
112+
.current_dir(EZG_ROOT)
113+
.arg(Path::new(EZG_ROOT).join("ezg").to_str().unwrap())
114+
.arg("dns")
115+
.arg("set")
116+
.arg(option)
117+
.arg(value)
118+
.output()
119+
.expect("failed to execute process");
120+
121+
Ok("{\"success\": true}".into())
122+
}
123+
93124
// async fn order_shoes(mut req: Request<()>) -> tide::Result {
94125
// let Animal { name, legs } = req.body_json().await?;
95126
// Ok(format!("Hello, {}! I've put in an order for {} shoes", name, legs).into())

0 commit comments

Comments
 (0)