mirror of
https://git.ethanthesleepy.one/ethanaobrien/ew.git
synced 2025-05-13 11:37:33 -05:00
Admin webui with one single option
This commit is contained in:
parent
30caa814f4
commit
dae95f5aa3
4 changed files with 89 additions and 8 deletions
|
@ -144,6 +144,7 @@ async fn request(req: HttpRequest, body: String) -> HttpResponse {
|
||||||
"/api/webui/startLoginbonus" => router::webui::start_loginbonus(req, body),
|
"/api/webui/startLoginbonus" => router::webui::start_loginbonus(req, body),
|
||||||
"/api/webui/import" => router::webui::import(req, body),
|
"/api/webui/import" => router::webui::import(req, body),
|
||||||
"/api/webui/set_time" => router::webui::set_time(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)
|
_ => api_req(req, body)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -158,6 +159,7 @@ async fn request(req: HttpRequest, body: String) -> HttpResponse {
|
||||||
"/web/announcement" => router::web::announcement(req),
|
"/web/announcement" => router::web::announcement(req),
|
||||||
"/api/webui/userInfo" => router::webui::user(req),
|
"/api/webui/userInfo" => router::webui::user(req),
|
||||||
"/webui/logout" => router::webui::logout(req),
|
"/webui/logout" => router::webui::logout(req),
|
||||||
|
"/api/webui/admin" => router::webui::admin(req),
|
||||||
_ => api_req(req, body)
|
_ => api_req(req, body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,30 @@ use actix_web::{
|
||||||
http::header::HeaderValue,
|
http::header::HeaderValue,
|
||||||
http::header::ContentType
|
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::include_file;
|
||||||
use crate::router::{userdata, items};
|
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> {
|
fn get_login_token(req: &HttpRequest) -> Option<String> {
|
||||||
let blank_header = HeaderValue::from_static("");
|
let blank_header = HeaderValue::from_static("");
|
||||||
let cookies = req.headers().get("Cookie").unwrap_or(&blank_header).to_str().unwrap_or("");
|
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 {
|
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 body = json::parse(&body).unwrap();
|
||||||
|
|
||||||
let result = userdata::webui_import_user(body);
|
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()
|
return HttpResponse::Found()
|
||||||
.insert_header(("Location", "/"))
|
.insert_header(("Location", "/"))
|
||||||
.body("");
|
.body("");
|
||||||
|
@ -151,3 +179,42 @@ pub fn main(req: HttpRequest) -> HttpResponse {
|
||||||
.insert_header(ContentType::html())
|
.insert_header(ContentType::html())
|
||||||
.body(include_file!("webui/dist/index.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))
|
||||||
|
}
|
||||||
|
|
|
@ -30,6 +30,11 @@ function Login() {
|
||||||
window.location.href = "/import/";
|
window.location.href = "/import/";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const adminPanel = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
window.location.href = "/admin/";
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id="login-form">
|
<div id="login-form">
|
||||||
<h1>Login</h1>
|
<h1>Login</h1>
|
||||||
|
@ -40,7 +45,8 @@ function Login() {
|
||||||
<input type="password" id="password" name="password" onChange={(event) => {password = event.target.value}} />
|
<input type="password" id="password" name="password" onChange={(event) => {password = event.target.value}} />
|
||||||
<input type="submit" value="Submit" onClick={handleSubmit}/>
|
<input type="submit" value="Submit" onClick={handleSubmit}/>
|
||||||
<div id="sub_div">
|
<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> }
|
{ error[0] ? <p>Error: { error[0] } </p> : <p></p> }
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -3,6 +3,7 @@ import ReactDOM from 'react-dom/client'
|
||||||
import Login from './login/Login.jsx'
|
import Login from './login/Login.jsx'
|
||||||
import Home from './home/Home.jsx'
|
import Home from './home/Home.jsx'
|
||||||
import Import from './import/Import.jsx'
|
import Import from './import/Import.jsx'
|
||||||
|
import Admin from './admin/Admin.jsx'
|
||||||
|
|
||||||
let Elem;
|
let Elem;
|
||||||
switch (window.location.pathname) {
|
switch (window.location.pathname) {
|
||||||
|
@ -15,12 +16,17 @@ switch (window.location.pathname) {
|
||||||
case "/import/":
|
case "/import/":
|
||||||
Elem = Import;
|
Elem = Import;
|
||||||
break;
|
break;
|
||||||
|
case "/admin/":
|
||||||
|
Elem = Admin;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
window.location.pathname = "/";
|
window.location.pathname = "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById('root')).render(
|
if (Elem) {
|
||||||
<React.StrictMode>
|
ReactDOM.createRoot(document.getElementById('root')).render(
|
||||||
<Elem />
|
<React.StrictMode>
|
||||||
</React.StrictMode>,
|
<Elem />
|
||||||
)
|
</React.StrictMode>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue