diff --git a/.gitignore b/.gitignore index 6ef783c..fe026e3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ main.exe main.pdb .idea/ restarts.log -nohup.out \ No newline at end of file +nohup.out +server.log +control_panel.log \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 4933522..c86c789 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,18 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "VelNetServerRust" -version = "0.2.0" -dependencies = [ - "async-notify", - "async-std", - "chrono", - "futures", - "serde", - "serde_json", -] - [[package]] name = "async-attributes" version = "1.1.2" @@ -205,7 +193,7 @@ dependencies = [ "libc", "num-integer", "num-traits", - "time", + "time 0.1.44", "winapi", ] @@ -468,6 +456,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "once_cell" version = "1.10.0" @@ -560,6 +557,17 @@ dependencies = [ "serde", ] +[[package]] +name = "simplelog" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48dfff04aade74dd495b007c831cd6f4e0cee19c344dd9dc0884c0289b70a786" +dependencies = [ + "log", + "termcolor", + "time 0.3.11", +] + [[package]] name = "slab" version = "0.4.5" @@ -587,6 +595,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "time" version = "0.1.44" @@ -598,6 +615,24 @@ dependencies = [ "winapi", ] +[[package]] +name = "time" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c91f41dcb2f096c05f0873d667dceec1087ce5bcf984ec8ffb19acddbb3217" +dependencies = [ + "itoa", + "libc", + "num_threads", + "time-macros", +] + +[[package]] +name = "time-macros" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792" + [[package]] name = "unicode-xid" version = "0.2.2" @@ -614,6 +649,20 @@ dependencies = [ "version_check", ] +[[package]] +name = "velnet_server" +version = "0.2.0" +dependencies = [ + "async-notify", + "async-std", + "chrono", + "futures", + "log", + "serde", + "serde_json", + "simplelog", +] + [[package]] name = "version_check" version = "0.9.4" @@ -733,6 +782,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 8f52e53..823b4a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "VelNetServerRust" +name = "velnet_server" version = "0.2.0" edition = "2021" @@ -11,4 +11,6 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" async-std = { version = "1.6", features = ["attributes"]} futures = "*" -async-notify = "*" \ No newline at end of file +async-notify = "*" +simplelog = "^0.12.0" +log = "*" \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..4b32a32 --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "port": 5000, + "tcp_timeout": 30, + "log_file": "server.log" +} diff --git a/config.txt b/config.txt deleted file mode 100644 index 81bd2ee..0000000 --- a/config.txt +++ /dev/null @@ -1,4 +0,0 @@ -{ -"port":5000, -"tcp_timeout":30 -} diff --git a/control-panel/Cargo.lock b/control-panel/Cargo.lock index 2884cfc..79200c4 100644 --- a/control-panel/Cargo.lock +++ b/control-panel/Cargo.lock @@ -19,6 +19,29 @@ dependencies = [ "tokio-util 0.7.0", ] +[[package]] +name = "actix-files" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e04dcf7654254676d434b0285e2298d577ed4826f67f536e7a39bb0f64721164" +dependencies = [ + "actix-http", + "actix-service", + "actix-utils", + "actix-web", + "askama_escape", + "bitflags", + "bytes", + "derive_more", + "futures-core", + "http-range", + "log", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", +] + [[package]] name = "actix-http" version = "3.0.0" @@ -62,8 +85,8 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "465a6172cf69b960917811022d8f29bc0b7fa1398bc4f78b3c466673db1213b6" dependencies = [ - "quote", - "syn", + "quote 1.0.15", + "syn 1.0.86", ] [[package]] @@ -176,9 +199,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7525bedf54704abb1d469e88d7e7e9226df73778798a69cea5022d53b2ae91bc" dependencies = [ "actix-router", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.36", + "quote 1.0.15", + "syn 1.0.86", ] [[package]] @@ -222,6 +245,12 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "askama_escape" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" + [[package]] name = "autocfg" version = "1.1.0" @@ -333,16 +362,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "control-panel" -version = "0.1.0" -dependencies = [ - "actix-web", - "handlebars", - "serde", - "serde_json", -] - [[package]] name = "convert_case" version = "0.4.0" @@ -388,6 +407,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "default-env" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f753eb82d29277e79efc625e84aecacfd4851ee50e05a8573a4740239a77bfd3" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "syn 0.15.44", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -395,10 +425,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ "convert_case", - "proc-macro2", - "quote", + "proc-macro2 1.0.36", + "quote 1.0.15", "rustc_version", - "syn", + "syn 1.0.86", ] [[package]] @@ -420,6 +450,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "either" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be" + [[package]] name = "encoding_rs" version = "0.8.30" @@ -569,6 +605,12 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -589,6 +631,12 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-range" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" + [[package]] name = "httparse" version = "1.6.0" @@ -622,6 +670,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "itertools" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.1" @@ -715,6 +772,16 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "miniz_oxide" version = "0.4.4" @@ -849,9 +916,9 @@ checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55" dependencies = [ "pest", "pest_meta", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.36", + "quote 1.0.15", + "syn 1.0.86", ] [[package]] @@ -883,13 +950,22 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +dependencies = [ + "unicode-xid 0.1.0", +] + [[package]] name = "proc-macro2" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ - "unicode-xid", + "unicode-xid 0.2.2", ] [[package]] @@ -898,13 +974,22 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +dependencies = [ + "proc-macro2 0.4.30", +] + [[package]] name = "quote" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.36", ] [[package]] @@ -972,6 +1057,12 @@ dependencies = [ "semver", ] +[[package]] +name = "rustversion" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0a5f7c728f5d284929a1cccb5bc19884422bfe6ef4d6c409da2c41838983fcf" + [[package]] name = "ryu" version = "1.0.9" @@ -1014,9 +1105,9 @@ version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.36", + "quote 1.0.15", + "syn 1.0.86", ] [[package]] @@ -1074,6 +1165,17 @@ dependencies = [ "libc", ] +[[package]] +name = "simplelog" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48dfff04aade74dd495b007c831cd6f4e0cee19c344dd9dc0884c0289b70a786" +dependencies = [ + "log", + "termcolor", + "time", +] + [[package]] name = "slab" version = "0.4.5" @@ -1096,15 +1198,66 @@ dependencies = [ "winapi", ] +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" + +[[package]] +name = "strum_macros" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4faebde00e8ff94316c01800f9054fd2ba77d30d9e922541913051d1d978918b" +dependencies = [ + "heck", + "proc-macro2 1.0.36", + "quote 1.0.15", + "rustversion", + "syn 1.0.86", +] + +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid 0.1.0", +] + [[package]] name = "syn" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", + "proc-macro2 1.0.36", + "quote 1.0.15", + "unicode-xid 0.2.2", +] + +[[package]] +name = "systemctl" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3134cbab79000bee5ed4d71331d2c9071db897f8369e3e3089b6be85cc781d1" +dependencies = [ + "default-env", + "itertools", + "strum", + "strum_macros", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", ] [[package]] @@ -1218,6 +1371,15 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.7" @@ -1233,6 +1395,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" + [[package]] name = "unicode-xid" version = "0.2.2" @@ -1251,6 +1419,20 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "velnet_control_panel" +version = "0.2.0" +dependencies = [ + "actix-files", + "actix-web", + "handlebars", + "log", + "serde", + "serde_json", + "simplelog", + "systemctl", +] + [[package]] name = "version_check" version = "0.9.4" diff --git a/control-panel/Cargo.toml b/control-panel/Cargo.toml index 77992fe..118ad7d 100644 --- a/control-panel/Cargo.toml +++ b/control-panel/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "control-panel" -version = "0.1.0" +name = "velnet_control_panel" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -9,4 +9,8 @@ edition = "2021" actix-web = "4" handlebars = { version = "4.2.1", features = ["dir_source"] } serde_json = "1.0" -serde = { version = "1.0", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0", features = ["derive"] } +simplelog = "^0.12.0" +log = "*" +systemctl = "0.1.7" +actix-files = "0.6.1" \ No newline at end of file diff --git a/control-panel/compile_server.sh b/control-panel/compile_server.sh new file mode 100644 index 0000000..e88d724 --- /dev/null +++ b/control-panel/compile_server.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +cd .. +sudo -u "$1" bash -c "~/.cargo/bin/cargo build --release" diff --git a/control-panel/config.json b/control-panel/config.json index 9a7f6c0..4147840 100644 --- a/control-panel/config.json +++ b/control-panel/config.json @@ -1,5 +1,8 @@ { - "port": 8080, - "log_file": "/home/ubuntu/VelNetServerRust2/nohup.out", - "server_dir": "/home/ubuntu/VelNetServerRust2/" + "port": 8080, + "user": "ntsfranz", + "server_log_file": "../server.log", + "control_panel_log_file": "control_panel.log", + "server_dir": "../", + "handlebars_dev_mode": true } diff --git a/control-panel/git_pull.sh b/control-panel/git_pull.sh index 5535a8b..db50a60 100644 --- a/control-panel/git_pull.sh +++ b/control-panel/git_pull.sh @@ -1 +1 @@ -sudo -u ubuntu bash -c "git pull" +sudo -u "$1" bash -c "git pull" diff --git a/control-panel/onefetch_file.sh b/control-panel/onefetch_file.sh index 9e241f2..5aedc64 100755 --- a/control-panel/onefetch_file.sh +++ b/control-panel/onefetch_file.sh @@ -1 +1,3 @@ -sudo -u ubuntu bash -c "onefetch > onefetch.out" +#!/bin/bash + +sudo -u "$1" bash -c "~/.cargo/bin/onefetch > onefetch.out" diff --git a/control-panel/src/main.rs b/control-panel/src/main.rs index b1e14af..e2bf4f7 100644 --- a/control-panel/src/main.rs +++ b/control-panel/src/main.rs @@ -9,18 +9,22 @@ use serde_json::json; use std::io; use std::io::{prelude::*, BufReader}; use std::fs; -use std::fs::File; +use std::fs::{File, OpenOptions}; use std::path::Path; use serde::{Serialize, Deserialize}; -use std::process::{Command, Stdio}; -use std::env; +use std::process::{Command}; use std::cmp; +use std::error::Error; +use simplelog; #[derive(Serialize, Deserialize)] -struct Config { +struct ControlPanelConfig { port: u16, - log_file: String, + user: String, + server_log_file: String, + control_panel_log_file: String, server_dir: String, + handlebars_dev_mode: bool, } @@ -38,15 +42,12 @@ fn lines_from_file(filename: impl AsRef) -> Vec { #[get("/")] async fn index(hb: web::Data>) -> HttpResponse { - - //read the config file - let file = fs::read_to_string("config.json").unwrap(); - let config: Config = serde_json::from_str(&file).unwrap(); + let config = read_config_file().unwrap(); //read the log file - let log_file = lines_from_file(config.log_file); + let log_file = lines_from_file(config.server_log_file); - let restarts_log = lines_from_file("../restarts.log"); + // let restarts_log = lines_from_file(config.restarts_log_file); let uptime = Command::new("uptime") .output() @@ -54,6 +55,7 @@ async fn index(hb: web::Data>) -> HttpResponse { let _onefetch = Command::new("sh") .arg("onefetch_file.sh") + .arg(config.user) .output() .expect("failed"); @@ -61,10 +63,10 @@ async fn index(hb: web::Data>) -> HttpResponse { let data = json!({ "log_output": &log_file[(cmp::max((log_file.len() as i64) - 1000, 0) as usize)..], - "restarts_output": &restarts_log[(cmp::max((restarts_log.len() as i64) - 1000, 0) as usize)..], + // "restarts_output": &restarts_log[(cmp::max((restarts_log.len() as i64) - 1000, 0) as usize)..], "uptime": format!("{}", String::from_utf8_lossy(&uptime.stdout)), //"onefetch": format!("{}", String::from_utf8_lossy(&onefetch.stdout)) - "onefetch": onefetch + "onefetch": onefetch.trim_end() }); let body = hb.render("index", &data).unwrap(); @@ -75,21 +77,33 @@ async fn index(hb: web::Data>) -> HttpResponse { async fn restart_server() -> HttpResponse { // Restart the process - let _output = Command::new("sh") - .arg("../run.sh") - .stdin(Stdio::null()) - //.stdout(Stdio::null()) - //.stderr(Stdio::null()) - .spawn(); + // let _output = Command::new("sh") + // .arg("../restart_server.sh") + // .stdin(Stdio::null()) + // .spawn(); + // let _output = Command::new("systemd") + // .arg("restart") + // .arg("velnet") + // .stdin(Stdio::null()) + // .spawn(); - HttpResponse::Ok().body("DONE") + systemctl::restart("velnet.service").unwrap(); + + let ret = systemctl::status("velnet.service").unwrap(); + + HttpResponse::Ok().body(ret) } #[get("/git_pull")] async fn git_pull() -> HttpResponse { + + //read the config file + let config = read_config_file().unwrap(); + let output = Command::new("sh") .arg("git_pull.sh") + .arg(config.user) .output() .expect("failed to execute process"); @@ -101,37 +115,63 @@ async fn git_pull() -> HttpResponse { async fn compile() -> HttpResponse { //read the config file - let file = fs::read_to_string("config.json").unwrap(); - let config: Config = serde_json::from_str(&file).unwrap(); + let config = read_config_file().unwrap(); - let orig_dir = std::env::current_dir().unwrap(); + log::debug!("before"); - let root = Path::new(&config.server_dir); - let _new_dir = env::set_current_dir(&root); - - print!("before"); - - let output = Command::new("cargo") - .arg("build") - .arg("--release") + let output = Command::new("sh") + .arg("compile_server.sh") + .arg(config.user) .output() .expect("failed to execute process"); - print!("after"); - - let _new_dir = env::set_current_dir(orig_dir); + log::debug!("after"); HttpResponse::Ok().body(output.stdout) } +fn read_config_file() -> Result> { + // Open the file in read-only mode with buffer. + let file = File::open("config.json")?; + let reader = BufReader::new(file); + + let config = serde_json::from_reader(reader)?; + + Ok(config) +} + #[actix_web::main] async fn main() -> io::Result<()> { + + //read the config file + let config: ControlPanelConfig = read_config_file().unwrap(); + + let f = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open(config.control_panel_log_file) + .unwrap(); + simplelog::CombinedLogger::init( + vec![ + simplelog::TermLogger::new(simplelog::LevelFilter::Info, simplelog::Config::default(), simplelog::TerminalMode::Mixed, simplelog::ColorChoice::Auto), + simplelog::WriteLogger::new(simplelog::LevelFilter::Debug, simplelog::Config::default(), f), + ] + ).unwrap(); + + + log::info!("Starting control panel server."); + + let mut handlebars = Handlebars::new(); - handlebars.set_dev_mode(true); + handlebars.set_dev_mode(config.handlebars_dev_mode); handlebars.register_templates_directory(".hbs", "./static/templates").unwrap(); let handlebars_ref = web::Data::new(handlebars); + + log::info!("http://127.0.0.1:{}", config.port); + HttpServer::new(move || { App::new() .wrap(error_handlers()) @@ -140,8 +180,9 @@ async fn main() -> io::Result<()> { .service(restart_server) .service(git_pull) .service(compile) + .service(actix_files::Files::new("/static", "./static")) }) - .bind(("127.0.0.1", 8080))? + .bind(("0.0.0.0", config.port))? .run() .await } diff --git a/control-panel/src/main.rs.bak b/control-panel/src/main.rs.bak new file mode 100644 index 0000000..5cb203b --- /dev/null +++ b/control-panel/src/main.rs.bak @@ -0,0 +1,223 @@ +use actix_web::body::BoxBody; +use actix_web::dev::ServiceResponse; +use actix_web::http::header::ContentType; +use actix_web::http::StatusCode; +use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers}; +use actix_web::{get, web, App, HttpResponse, HttpServer, Result}; +use handlebars::Handlebars; +use serde_json::json; +use std::io; +use std::io::{prelude::*, BufReader}; +use std::fs; +use std::fs::File; +use std::path::Path; +use serde::{Serialize, Deserialize}; +use std::process::{Command, Stdio}; +use std::env; +use std::cmp; + +#[derive(Serialize, Deserialize)] +struct Config { + port: u16, + log_file: String, + server_dir: String, +} + + +fn lines_from_file(filename: impl AsRef) -> Vec { + let file = File::open(filename).expect("no such file"); + let buf = BufReader::new(file); + buf.lines() + .map(|l| l.expect("Could not parse line")) + .collect() +} + +#[get("/")] +async fn index(hb: web::Data>) -> HttpResponse { + + //read the config file + let file = fs::read_to_string("config.json").unwrap(); + let config: Config = serde_json::from_str(&file).unwrap(); + + //read the log file + let log_file = lines_from_file(config.log_file); + + let restarts_log = lines_from_file("../restarts.log"); + + let uptime = Command::new("uptime") + .output() + .expect("failed to execute process"); + + let _onefetch = Command::new("sh") + .arg("onefetch_file.sh") + .output() + .expect("failed"); + + let onefetch = fs::read_to_string("onefetch.out").unwrap(); + + let data = json!({ + "log_output": log_file, + "restarts_output": restarts_log, + "uptime": format!("{}", String::from_utf8_lossy(&uptime.stdout)), + //"onefetch": format!("{}", String::from_utf8_lossy(&onefetch.stdout)) + "onefetch": onefetch + }); + let body = hb.render("index", &data).unwrap(); + + HttpResponse::Ok().body(body) +} + +#[get("/nohup.out")] +async fn nohup() -> HttpResponse { + + //read the config file + let file = fs::read_to_string("config.json").unwrap(); + let config: Config = serde_json::from_str(&file).unwrap(); + + //read the log file + let log_file = lines_from_file(config.log_file); + + // get the last 100 lines + let all_lines = log_file.as_slice()[cmp::max(log_file.len()-100, 0)..].to_vec().join("\n"); + + HttpResponse::Ok().body(all_lines) +} + +#[get("/restarts.log2")] +async fn restarts() -> HttpResponse { + + //read the log file + let log_file = lines_from_file("../restarts.log"); + + // get the last 100 lines + let all_lines = log_file.as_slice()[cmp::max(log_file.len()-100, 0)..].to_vec().join("\n"); + + HttpResponse::Ok().body(all_lines) +} + +#[get("/restart_server")] +async fn restart_server() -> HttpResponse { + + // Restart the process + let _output = Command::new("sh") + .arg("../run.sh") + .stdin(Stdio::null()) + //.stdout(Stdio::null()) + //.stderr(Stdio::null()) + .spawn(); + + HttpResponse::Ok().body("DONE") +} + + + +#[get("/git_pull")] +async fn git_pull() -> HttpResponse { + + let output = Command::new("git") + .arg("pull") + .output() + .expect("failed to execute process"); + + HttpResponse::Ok().body(output.stdout) +} + + +#[get("/compile")] +async fn compile() -> HttpResponse { + + //read the config file + let file = fs::read_to_string("config.json").unwrap(); + let config: Config = serde_json::from_str(&file).unwrap(); + + let orig_dir = std::env::current_dir().unwrap(); + + let root = Path::new(&config.server_dir); + let _new_dir = env::set_current_dir(&root); + + print!("before"); + + let output = Command::new("cargo") + .arg("build") + .arg("--release") + .output() + .expect("failed to execute process"); + + print!("after"); + + let _new_dir = env::set_current_dir(orig_dir); + + HttpResponse::Ok().body(output.stdout) +} + + +#[actix_web::main] +async fn main() -> io::Result<()> { + let mut handlebars = Handlebars::new(); + handlebars.set_dev_mode(true); + handlebars.register_templates_directory(".hbs", "./static/templates").unwrap(); + let handlebars_ref = web::Data::new(handlebars); + + HttpServer::new(move || { + App::new() + .wrap(error_handlers()) + .app_data(handlebars_ref.clone()) + .service(index) + .service(restart_server) + .service(git_pull) + .service(compile) + .service(nohup) + .service(restarts) + }) + .bind(("127.0.0.1", 8080))? + .run() + .await +} + +// Custom error handlers, to return HTML responses when an error occurs. +fn error_handlers() -> ErrorHandlers { + ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found) +} + +// Error handler for a 404 Page not found error. +fn not_found(res: ServiceResponse) -> Result> { + let response = get_error_response(&res, "Page not found"); + Ok(ErrorHandlerResponse::Response(ServiceResponse::new( + res.into_parts().0, + response.map_into_left_body(), + ))) +} + +// Generic error handler. +fn get_error_response(res: &ServiceResponse, error: &str) -> HttpResponse { + let request = res.request(); + + // Provide a fallback to a simple plain text response in case an error occurs during the + // rendering of the error page. + let fallback = |e: &str| { + HttpResponse::build(res.status()) + .content_type(ContentType::plaintext()) + .body(e.to_string()) + }; + + let hb = request + .app_data::>() + .map(|t| t.get_ref()); + match hb { + Some(hb) => { + let data = json!({ + "error": error, + "status_code": res.status().as_str() + }); + let body = hb.render("error", &data); + + match body { + Ok(body) => HttpResponse::build(res.status()) + .content_type(ContentType::html()) + .body(body), + Err(_) => fallback(error), + } + } + None => fallback(error), + } +} diff --git a/control-panel/static/favicons/android-chrome-192x192.png b/control-panel/static/favicons/android-chrome-192x192.png new file mode 100644 index 0000000..b3cd171 Binary files /dev/null and b/control-panel/static/favicons/android-chrome-192x192.png differ diff --git a/control-panel/static/favicons/android-chrome-256x256.png b/control-panel/static/favicons/android-chrome-256x256.png new file mode 100644 index 0000000..bfceabe Binary files /dev/null and b/control-panel/static/favicons/android-chrome-256x256.png differ diff --git a/control-panel/static/favicons/apple-touch-icon.png b/control-panel/static/favicons/apple-touch-icon.png new file mode 100644 index 0000000..ef6f509 Binary files /dev/null and b/control-panel/static/favicons/apple-touch-icon.png differ diff --git a/control-panel/static/favicons/browserconfig.xml b/control-panel/static/favicons/browserconfig.xml new file mode 100644 index 0000000..e8b57e5 --- /dev/null +++ b/control-panel/static/favicons/browserconfig.xml @@ -0,0 +1,9 @@ + + + + + + #b91d47 + + + diff --git a/control-panel/static/favicons/favicon-16x16.png b/control-panel/static/favicons/favicon-16x16.png new file mode 100644 index 0000000..c9c768a Binary files /dev/null and b/control-panel/static/favicons/favicon-16x16.png differ diff --git a/control-panel/static/favicons/favicon-32x32.png b/control-panel/static/favicons/favicon-32x32.png new file mode 100644 index 0000000..fd5a65e Binary files /dev/null and b/control-panel/static/favicons/favicon-32x32.png differ diff --git a/control-panel/static/favicons/favicon.ico b/control-panel/static/favicons/favicon.ico new file mode 100644 index 0000000..aa13620 Binary files /dev/null and b/control-panel/static/favicons/favicon.ico differ diff --git a/control-panel/static/favicons/mstile-150x150.png b/control-panel/static/favicons/mstile-150x150.png new file mode 100644 index 0000000..145dcc2 Binary files /dev/null and b/control-panel/static/favicons/mstile-150x150.png differ diff --git a/control-panel/static/favicons/safari-pinned-tab.svg b/control-panel/static/favicons/safari-pinned-tab.svg new file mode 100644 index 0000000..1cd7cf2 --- /dev/null +++ b/control-panel/static/favicons/safari-pinned-tab.svg @@ -0,0 +1,20 @@ + + + + +Created by potrace 1.14, written by Peter Selinger 2001-2017 + + + + + + + + + diff --git a/control-panel/static/favicons/site.webmanifest b/control-panel/static/favicons/site.webmanifest new file mode 100644 index 0000000..de65106 --- /dev/null +++ b/control-panel/static/favicons/site.webmanifest @@ -0,0 +1,19 @@ +{ + "name": "", + "short_name": "", + "icons": [ + { + "src": "/android-chrome-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/android-chrome-256x256.png", + "sizes": "256x256", + "type": "image/png" + } + ], + "theme_color": "#ffffff", + "background_color": "#ffffff", + "display": "standalone" +} diff --git a/control-panel/static/templates/index.hbs b/control-panel/static/templates/index.hbs index bfbb296..ea32a4c 100644 --- a/control-panel/static/templates/index.hbs +++ b/control-panel/static/templates/index.hbs @@ -1,15 +1,24 @@ - + + + + + + + + + + VelNet Control Panel