From dae95f5aa3b6db5327b85585800de7108820ff8f Mon Sep 17 00:00:00 2001 From: Ethan O'Brien Date: Mon, 27 May 2024 15:09:17 -0500 Subject: [PATCH] Admin webui with one single option --- src/main.rs | 2 ++ src/router/webui.rs | 71 +++++++++++++++++++++++++++++++++++++-- webui/src/login/Login.jsx | 8 ++++- webui/src/main.jsx | 16 ++++++--- 4 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 49126fc..876fbf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -144,6 +144,7 @@ async fn request(req: HttpRequest, body: String) -> HttpResponse { "/api/webui/startLoginbonus" => router::webui::start_loginbonus(req, body), "/api/webui/import" => router::webui::import(req, body), "/api/webui/set_time" => router::webui::set_time(req, body), + "/api/webui/admin" => router::webui::admin_post(req, body), _ => api_req(req, body) } } else { @@ -158,6 +159,7 @@ async fn request(req: HttpRequest, body: String) -> HttpResponse { "/web/announcement" => router::web::announcement(req), "/api/webui/userInfo" => router::webui::user(req), "/webui/logout" => router::webui::logout(req), + "/api/webui/admin" => router::webui::admin(req), _ => api_req(req, body) } } diff --git a/src/router/webui.rs b/src/router/webui.rs index 7ee7696..3ca851c 100644 --- a/src/router/webui.rs +++ b/src/router/webui.rs @@ -4,11 +4,30 @@ use actix_web::{ http::header::HeaderValue, http::header::ContentType }; -use json::object; +use json::{JsonValue, object}; +use std::fs; +use std::fs::File; +use std::io::Write; use crate::include_file; use crate::router::{userdata, items}; +fn get_config() -> JsonValue { + let contents = fs::read_to_string("config.json").unwrap_or(String::from("aaaaaaaaaaaaaaaaa")); + json::parse(&contents).unwrap_or(object!{ + import: true + }) +} +fn save_config(val: String) { + let mut current = get_config(); + let new = json::parse(&val).unwrap(); + for vall in new.entries() { + current[vall.0] = vall.1.clone(); + } + let mut f = File::create("config.json").unwrap(); + f.write_all(json::stringify(current).as_bytes()).unwrap(); +} + fn get_login_token(req: &HttpRequest) -> Option { let blank_header = HeaderValue::from_static(""); let cookies = req.headers().get("Cookie").unwrap_or(&blank_header).to_str().unwrap_or(""); @@ -48,6 +67,15 @@ pub fn login(_req: HttpRequest, body: String) -> HttpResponse { } pub fn import(_req: HttpRequest, body: String) -> HttpResponse { + if get_config()["import"].as_bool().unwrap() == false { + let resp = object!{ + result: "Err", + message: "Importing accounts is disabled on this server." + }; + return HttpResponse::Ok() + .insert_header(ContentType::json()) + .body(json::stringify(resp)); + } let body = json::parse(&body).unwrap(); let result = userdata::webui_import_user(body); @@ -142,7 +170,7 @@ pub fn main(req: HttpRequest) -> HttpResponse { } } } - if req.path() != "/" && req.path() != "/home/" && req.path() != "/import/" { + if req.path() != "/" && req.path() != "/home/" && req.path() != "/import/" && req.path() != "/admin/" { return HttpResponse::Found() .insert_header(("Location", "/")) .body(""); @@ -151,3 +179,42 @@ pub fn main(req: HttpRequest) -> HttpResponse { .insert_header(ContentType::html()) .body(include_file!("webui/dist/index.html")) } + + +macro_rules! check_admin { + ( $s:expr ) => { + { + if $s.peer_addr().unwrap().ip().to_string() != "127.0.0.1" { + let resp = object!{ + result: "ERR", + message: "Must be on localhost address to access admin panel." + }; + return HttpResponse::Ok() + .insert_header(ContentType::json()) + .body(json::stringify(resp)) + } + } + }; +} + +pub fn admin(req: HttpRequest) -> HttpResponse { + check_admin!(req); + let resp = object!{ + result: "OK", + data: get_config() + }; + HttpResponse::Ok() + .insert_header(ContentType::json()) + .body(json::stringify(resp)) +} + +pub fn admin_post(req: HttpRequest, body: String) -> HttpResponse { + check_admin!(req); + save_config(body); + let resp = object!{ + result: "OK" + }; + HttpResponse::Ok() + .insert_header(ContentType::json()) + .body(json::stringify(resp)) +} diff --git a/webui/src/login/Login.jsx b/webui/src/login/Login.jsx index c703506..c80262e 100644 --- a/webui/src/login/Login.jsx +++ b/webui/src/login/Login.jsx @@ -29,6 +29,11 @@ function Login() { e.preventDefault(); window.location.href = "/import/"; } + + const adminPanel = (e) => { + e.preventDefault(); + window.location.href = "/admin/"; + } return (
@@ -40,7 +45,8 @@ function Login() { {password = event.target.value}} />
- +

+ { error[0] ?

Error: { error[0] }

:

}
diff --git a/webui/src/main.jsx b/webui/src/main.jsx index 74e0193..ae91ac8 100644 --- a/webui/src/main.jsx +++ b/webui/src/main.jsx @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom/client' import Login from './login/Login.jsx' import Home from './home/Home.jsx' import Import from './import/Import.jsx' +import Admin from './admin/Admin.jsx' let Elem; switch (window.location.pathname) { @@ -15,12 +16,17 @@ switch (window.location.pathname) { case "/import/": Elem = Import; break; + case "/admin/": + Elem = Admin; + break; default: window.location.pathname = "/"; } -ReactDOM.createRoot(document.getElementById('root')).render( - - - , -) +if (Elem) { + ReactDOM.createRoot(document.getElementById('root')).render( + + + , + ) +}