Admin webui with one single option

This commit is contained in:
Ethan O'Brien 2024-05-27 15:09:17 -05:00
parent 30caa814f4
commit dae95f5aa3
4 changed files with 89 additions and 8 deletions

View file

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

View file

@ -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<String> {
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))
}

View file

@ -29,6 +29,11 @@ function Login() {
e.preventDefault();
window.location.href = "/import/";
}
const adminPanel = (e) => {
e.preventDefault();
window.location.href = "/admin/";
}
return (
<div id="login-form">
@ -40,7 +45,8 @@ function Login() {
<input type="password" id="password" name="password" onChange={(event) => {password = event.target.value}} />
<input type="submit" value="Submit" onClick={handleSubmit}/>
<div id="sub_div">
<button onClick={import_user}>Import User</button>
<button onClick={import_user}>Import User</button><br/><br/>
<button hidden={!["127.0.0.1", "localhost"].includes(window.location.hostname)} onClick={adminPanel}>Admin panel</button>
{ error[0] ? <p>Error: { error[0] } </p> : <p></p> }
</div>
</form>

View file

@ -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(
<React.StrictMode>
<Elem />
</React.StrictMode>,
)
if (Elem) {
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<Elem />
</React.StrictMode>,
)
}