Skip to content

Commit

Permalink
Built some Dash api support
Browse files Browse the repository at this point in the history
  • Loading branch information
strawmelonjuice committed Feb 16, 2024
1 parent 76cae08 commit f9f3481
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 97 deletions.
29 changes: 28 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ tar = "0.4.40"
fs_extra = "1.3.0"
mime = "0.3.17"
time = { version = "0.3.11", features = ["formatting"] }
rand = "0.8.5"
urlencoding = "2.1.3"
normalize-path = "0.2.1"
random-string = "1.1.0"
54 changes: 54 additions & 0 deletions src/dashfunctions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use normalize_path::NormalizePath;
use random_string::generate_rng;
use std::fs;
use std::fs::File;
use std::io::{Error, Write};
use std::path::PathBuf;
use std::sync::Mutex;
use actix_web::{post, HttpResponse, web};
use actix_web::web::Data;
use serde::Deserialize;
use crate::logger::logger;
use crate::structs::PluginMeta;

#[derive(Deserialize)]
struct DashAPIData {
passkey: String,
command: String,
subcommand: String,
params: String,
}
#[post("/dashapi/")]
pub(crate) async fn dashserver(data: web::Form<DashAPIData>, _pluginsmex: Data<Mutex<Vec<PluginMeta>>>) -> HttpResponse {
if data.passkey != (passkey().unwrap_or(String::from(""))) {
logger(15, String::from("An unauthorized external entity just tried performing an action on this instance."));
return HttpResponse::Forbidden().body(String::from("Wrong passkey entered. A report has been sent to the server owner."));
}

match data.command.as_str() {
"log" => {
logger(data.subcommand.parse().unwrap_or(1), format!("{{CynthiaDash}} {}",data.params.clone()));
}
_ => {
return HttpResponse::BadRequest().body(String::from("Invalid command."));
}
}

HttpResponse::Ok().body(String::from("OK!"))

}

pub fn passkey() -> Result<String, Error> {
let fl = PathBuf::from("./.cynthiaTemp/dashpasskeys/");
fs::create_dir_all(&fl).unwrap();
let filepath = fl.join(format!("{}.TXT", std::process::id())).normalize();
if filepath.exists() {
let pk = fs::read_to_string(filepath)?;
Ok(pk)
} else {
let pk: String = generate_rng(15..70, random_string::charsets::ALPHANUMERIC);
let mut fi: File = File::create(filepath.clone())?;
write!(fi, "{}", pk)?;
Ok(pk)
}
}
6 changes: 2 additions & 4 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::structs::CynthiaCacheIndexObject;
use colored::Colorize;
use dotenv::dotenv;
use normalize_path::NormalizePath;
use rand::Rng;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use random_string::generate_rng;

fn cachefolder() -> PathBuf {
let fl = PathBuf::from("./.cynthiaTemp/cache/")
Expand Down Expand Up @@ -60,9 +60,7 @@ pub(crate) fn cacheplacer(fileid: String, contents: String) -> String {
};
let cachepath = cachefolder()
.join(
rand::thread_rng()
.gen_range(10000000..999999999)
.to_string(),
generate_rng(3..7, random_string::charsets::ALPHANUMERIC),
)
.normalize();

Expand Down
8 changes: 4 additions & 4 deletions src/jsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub const NODEJSR: &str = "node.exe";
pub const NODEJSR: &str = "node";
// Bun:
#[cfg(windows)]
pub const BUNJSR: &str = "bun.exe";
pub const BUNJSR: &str = "bun.exe-disabled";
#[cfg(not(windows))]
pub const BUNJSR: &str = "bun";

Expand All @@ -20,13 +20,13 @@ pub const NODE_NPM: &str = "npm.cmd";
pub const NODE_NPM: &str = "npm";
// Bun:
#[cfg(windows)]
pub const BUN_NPM: &str = "bun.exe";
pub const BUN_NPM: &str = "bun.exe-disabled";
#[cfg(windows)]
pub const BUN_NPM_EX: &str = "bunx.exe";
pub const BUN_NPM_EX: &str = "bunx.exe-disabled";
#[cfg(not(windows))]
pub const BUN_NPM: &str = "bun";
#[cfg(not(windows))]
pub const BUN_NPM_EX: &str = "bunx";
pub const BUN_NPM_EX: &str = "bunx-disabled";

// NodeJS:
#[cfg(windows)]
Expand Down
72 changes: 50 additions & 22 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use colored::Colorize;
use time::{format_description, OffsetDateTime};

const DATE_FORMAT_STR: &str = "[year]-[month]-[day]-[hour]:[minute]:[second]:[subsecond digits:3]";
const SPACES: usize = 32;
const DIVVER: &str = "\t";

pub(crate) fn logger(act: i32, msg: String) {
/*
Expand All @@ -22,39 +24,54 @@ pub(crate) fn logger(act: i32, msg: String) {
12: Error in JSR
15: Warning!
88: Silly
5000: FATAL ERROR
*/
let spaces: usize = 32;

let tabs: String = "\t\t".to_string();
let dt1: OffsetDateTime = SystemTime::now().into();
let dt_fmt = format_description::parse(DATE_FORMAT_STR).unwrap();
let times = dt1.format(&dt_fmt).unwrap();
match act {
200 | 2 => {
let name = format!("[{} - [CynGET/OK]", times);
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
let title = format!("{}", name.bold().yellow());
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
println!("{0}👍\t{1}", preq, msg);
println!("{0}👍{DIVVER}{1}", preq, msg);
}
3 | 404 => {
let name = format!("[{} - [CynGET/404]", times);
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
let title = format!("{}", name.bold().yellow());
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
println!("{0}👎\t{1}", preq, msg);
println!("{0}👎{DIVVER}{1}", preq, msg);
}
5 => {
let name = format!("[{} - [ERROR]", times);
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
let title = format!("{}", name.bold().red().on_bright_yellow());
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
eprintln!("{0}{1}", preq, msg.bright_red());
}
5000 => {
let name = format!("[{} - [FATAL ERROR]", times);
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
Expand All @@ -64,19 +81,19 @@ pub(crate) fn logger(act: i32, msg: String) {
}
15 => {
let name = format!("[{} - [WARN]", times);
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
let title = format!("{}", name.bold().black().on_bright_yellow());
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
eprintln!("{0}⚠\t{1}", preq, msg.on_bright_magenta().black());
eprintln!("{0}⚠{DIVVER}{1}", preq, msg.on_bright_magenta().black());
}
12 => {
let name = "[JS/ERROR]";
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
Expand All @@ -86,30 +103,41 @@ pub(crate) fn logger(act: i32, msg: String) {
}
10 => {
let name = format!("[{} - [NOTE]", times);
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
let title = format!("{}", name.bold().bright_magenta());
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
println!("{0}❕\t{1}", preq, msg.bright_green());
println!("{0}❕{DIVVER}{1}", preq, msg.bright_green());
}
31 => {
let name = format!("[{} - [CACHE]", times);
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
let title = format!("{}", name.white());
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
println!("{0}♻️\t{1}", preq, msg.bright_white().italic());
println!("{0}♻️{DIVVER}{1}", preq, msg.bright_white().italic());
}
88 => {
let name = String::from("[SILLY]");
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
let title = format!("{}", name.bold().bright_magenta());
let preq = format!("{0}{2}{1}", title, " ".repeat(spaceleft), tabs);
println!("{0}❕{DIVVER}{1}", preq, msg.bright_green());
}
_ => {
let name = format!("[{} - [LOG]", times).blue();
let spaceleft = if name.chars().count() < spaces {
spaces - name.chars().count()
let spaceleft = if name.chars().count() < SPACES {
SPACES - name.chars().count()
} else {
0
};
Expand Down
Loading

0 comments on commit f9f3481

Please sign in to comment.