diff --git a/.gitignore b/.gitignore index 0b745e2..68b3e7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target -.env \ No newline at end of file + +.env +config.toml \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index e529cc9..a3d0865 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -413,6 +413,7 @@ dependencies = [ "rocket", "serde", "sha", + "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4c56fc5..7ca0d8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ regex = "1.9.4" rocket = "=0.5.0-rc.3" serde = { version = "1.0.188", features = ["derive"] } sha = "1.0.3" +toml = "0.7.6" diff --git a/config.example.toml b/config.example.toml new file mode 100644 index 0000000..277ab04 --- /dev/null +++ b/config.example.toml @@ -0,0 +1,19 @@ +[general] +# if this path is encountered during path traversal, +# it will be removed. this is useful for instances +# where your absolute domain path is not long enough +# to replace boomlings.com, because you can then point +# it at a different, longer path to fill the gap +# +# example: +# boomlings.com/database/ +# example.com/aaaaaaaaaa/ +# ^^^^^^^^^^^ +# leaving blank will disable this +append_path = "" +# where can your server be accessible? +port = 8000 + +[accounts] +# allow new accounts to be created +allow_registration = true \ No newline at end of file diff --git a/readme.md b/readme.md index 5072b14..2fc460a 100644 --- a/readme.md +++ b/readme.md @@ -34,5 +34,5 @@ _these features are implemented_ ## todo -- cache hashed passwords -- our passwords are a little insecure (`argon2(sha1(password + "mI29fmAnxgTs"))`) and there isnt anything we can do about this because gpj2 is forced like that!! thanks robtop!! (try and find a fix anyway lul) \ No newline at end of file +- make the `append_path` option do something (atm doesnt work) +- move authorization logic to (./src/helpers/accounts.rs)[./src/helpers/accounts.rs] + cache authorization \ No newline at end of file diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..805c622 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,34 @@ +use serde::Deserialize; +use std::fs; +use std::sync::LazyLock; + +#[derive(Deserialize)] +pub struct Config { + pub general: ConfigGeneral, + pub accounts: ConfigAccounts +} + +#[derive(Deserialize)] +pub struct ConfigGeneral { + pub append_path: String, + pub port: u16 +} + +#[derive(Deserialize)] +pub struct ConfigAccounts { + pub allow_registration: bool +} + +impl Config { + pub fn load_from_file(file_path: &str) -> Self { + let toml_str = fs::read_to_string(file_path).expect("Error finding toml config:"); + let config: Config = toml::from_str(toml_str.as_str()).expect("Error parsing toml config:"); + + return config; + } +} + +pub static CONFIG: LazyLock = LazyLock::new(|| { + let config = Config::load_from_file("config.toml"); + config +}); \ No newline at end of file diff --git a/src/endpoints/accounts/register_account.rs b/src/endpoints/accounts/register_account.rs index e4e5898..3623a54 100644 --- a/src/endpoints/accounts/register_account.rs +++ b/src/endpoints/accounts/register_account.rs @@ -5,6 +5,7 @@ use rocket::response::status; use diesel::prelude::*; use diesel::result::Error; +use crate::CONFIG; use crate::helpers; use crate::db; @@ -18,6 +19,10 @@ pub struct FormRegisterAccount { #[post("/memaddrefix/accounts/registerGJAccount.php", data = "")] pub fn register_account(input: Form) -> status::Custom<&'static str> { let connection = &mut db::establish_connection_pg(); + + if CONFIG.accounts.allow_registration == false { + return status::Custom(Status::Ok, "-1") + } if input.userName != helpers::clean::clean(input.userName.as_ref()) { return status::Custom(Status::Ok, "-4") diff --git a/src/main.rs b/src/main.rs index aca3f2b..013d063 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![feature(decl_macro)] +#![feature(lazy_cell)] #[macro_use] extern crate rocket; @@ -11,17 +12,22 @@ use helpers::*; mod endpoints; use endpoints::*; +mod config; +use config::*; + #[get("/")] fn index() -> String { - return String::from("index | coming soon to a localhost:8000 near u"); + return String::from("gdps-server | https://git.reidlab.online/reidlab/gdps-server"); } #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![ - index, - - endpoints::accounts::login_account::login_account, - endpoints::accounts::register_account::register_account - ]) + rocket::build() + .configure(rocket::Config::figment().merge(("port", CONFIG.general.port))) + .mount("/", routes![ + index, + + endpoints::accounts::login_account::login_account, + endpoints::accounts::register_account::register_account + ]) } \ No newline at end of file