diff --git a/Cargo.toml b/Cargo.toml index 1f5321b..a08ff95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ actix-web = { version = "4.5.1", features = [ "openssl" ] } rusqlite = { version = "0.30.0", features = ["bundled"] } openssl = { version = "0.10", features = ["vendored"] } reqwest = { version = "0.11", features = ["blocking"] } +clap = { version = "4.4.6", features = ["derive"]} base64 = "0.21.5" json = "0.12.4" rand = "0.8.5" diff --git a/src/main.rs b/src/main.rs index e331477..02ca3c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,8 @@ use actix_web::{ }; use crate::router::global; use json::JsonValue; +use clap::Parser; +use std::sync::atomic::Ordering; fn unhandled(req: HttpRequest, body: String) -> Option { if body != String::new() { @@ -181,22 +183,6 @@ async fn js(_req: HttpRequest) -> HttpResponse { .body(include_file!("webui/dist/index.js")) } -#[actix_web::main] -async fn main() -> std::io::Result<()> { - let rv = HttpServer::new(|| App::new() - .wrap_fn(|req, srv| { - println!("Request: {}", req.path()); - srv.call(req) - }) - .app_data(web::PayloadConfig::default().limit(1024 * 1024 * 25)) - .service(css) - .service(js) - .default_service(web::route().to(request)) - ).bind(("0.0.0.0", 8080))?.run(); - println!("Server started: http://127.0.0.1:{}", 8080); - rv.await -} - #[macro_export] macro_rules! include_file { ( $s:expr ) => { @@ -215,3 +201,40 @@ pub fn decode(bytes: &[u8]) -> Vec { dec.read_to_end(&mut ret).unwrap(); ret } + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Args { + #[arg(short, long, default_value_t = 8080, help = "Port to listen on")] + port: u16, + + #[arg(long, default_value = "./", help = "Path to store database files")] + path: String, + + #[arg(long, default_value_t = false, help = "Serve gree headers with https. WILL NOT ACCEPT HTTPS REQUESTS")] + https: bool +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + let args = Args::parse(); + let port = args.port; + + router::gree::HTTPS.store(args.https, Ordering::Relaxed); + let rv = HttpServer::new(|| App::new() + .wrap_fn(|req, srv| { + println!("Request: {}", req.path()); + srv.call(req) + }) + .app_data(web::PayloadConfig::default().limit(1024 * 1024 * 25)) + .service(css) + .service(js) + .default_service(web::route().to(request)) + ).bind(("0.0.0.0", port))?.run(); + + println!("Server started: http://0.0.0.0:{}", port); + if args.https { + println!("Note: gree is set to https mode. http requests will fail on jp clients."); + } + rv.await +} diff --git a/src/router/gree.rs b/src/router/gree.rs index 1efe41a..148ca48 100644 --- a/src/router/gree.rs +++ b/src/router/gree.rs @@ -1,13 +1,14 @@ use actix_web::{HttpResponse, HttpRequest, http::header::{HeaderValue, ContentType, HeaderMap}}; use base64::{Engine as _, engine::general_purpose}; use std::collections::HashMap; -use std::env; use sha1::Sha1; use substring::Substring; use json::{object, JsonValue}; use hmac::{Hmac, Mac}; use rusqlite::params; use lazy_static::lazy_static; +use std::sync::atomic::Ordering; +use std::sync::atomic::AtomicBool; use openssl::pkey::PKey; use openssl::rsa::Rsa; @@ -332,8 +333,12 @@ pub fn migration_password_register(req: HttpRequest, body: String) -> HttpRespon send(req, resp) } -fn get_protocol() -> String { - if env::args().nth(1).unwrap_or_default() == *"https" { +lazy_static!{ + pub static ref HTTPS: AtomicBool = AtomicBool::new(false); +} + +pub fn get_protocol() -> String { + if HTTPS.load(Ordering::SeqCst) == true { return String::from("https"); } String::from("http") diff --git a/src/sql.rs b/src/sql.rs index 2d29c3c..10f1fd6 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -1,6 +1,8 @@ use rusqlite::{Connection, params, ToSql}; use std::sync::Mutex; use json::{JsonValue, array}; +use clap::Parser; +use std::fs; use crate::router::clear_rate::Live; @@ -11,7 +13,9 @@ pub struct SQLite { impl SQLite { pub fn new(path: &str, setup: fn(&SQLite)) -> SQLite { - let conn = Connection::open(path).unwrap(); + let args = crate::Args::parse(); + fs::create_dir_all(&args.path).unwrap(); + let conn = Connection::open(format!("{}/{}", args.path, path)).unwrap(); conn.execute("PRAGMA foreign_keys = ON;", ()).unwrap(); let instance = SQLite { engine: Mutex::new(conn),