From 26f40621c95fb04f7433463e232071dbe4596b6c Mon Sep 17 00:00:00 2001 From: blek Date: Sat, 21 Oct 2023 01:12:16 +1000 Subject: [PATCH] basic config --- filed/Cargo.lock | 79 ++++++++++++++++++++++++++++++++++- filed/Cargo.toml | 1 + filed/src/config.rs | 1 + filed/src/config/types.rs | 88 +++++++++++++++++++++++++++++++++++++++ filed/src/env.rs | 13 +++++- filed/src/main.rs | 2 + 6 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 filed/src/config.rs create mode 100644 filed/src/config/types.rs diff --git a/filed/Cargo.lock b/filed/Cargo.lock index 88cd89f..03f39c7 100644 --- a/filed/Cargo.lock +++ b/filed/Cargo.lock @@ -276,7 +276,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "874c6e2d19f8d4a285083b11a3241bfbe01ac3ed85f26e1e6b34888d960552bd" dependencies = [ "derive_more", - "indexmap", + "indexmap 1.9.3", "nom", ] @@ -325,6 +325,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "erased-serde" version = "0.3.31" @@ -374,6 +380,7 @@ dependencies = [ "sha2", "static_dir", "tokio", + "toml", "urlencoding 2.1.3", "warp", ] @@ -486,7 +493,7 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap", + "indexmap 1.9.3", "slab", "tokio", "tokio-util", @@ -509,6 +516,12 @@ dependencies = [ "bumpalo", ] +[[package]] +name = "hashbrown" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" + [[package]] name = "headers" version = "0.3.9" @@ -678,6 +691,16 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "indexmap" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" +dependencies = [ + "equivalent", + "hashbrown 0.14.2", +] + [[package]] name = "itoa" version = "1.0.9" @@ -1136,6 +1159,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1424,6 +1456,40 @@ dependencies = [ "tracing", ] +[[package]] +name = "toml" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" +dependencies = [ + "indexmap 2.0.2", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower-service" version = "0.3.2" @@ -1791,3 +1857,12 @@ name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "winnow" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3b801d0e0a6726477cc207f60162da452f3a95adb368399bef20a946e06f65c" +dependencies = [ + "memchr", +] diff --git a/filed/Cargo.toml b/filed/Cargo.toml index 297b2c8..8b883e3 100644 --- a/filed/Cargo.toml +++ b/filed/Cargo.toml @@ -24,6 +24,7 @@ serde_json = "1.0.107" sha2 = "0.10.8" static_dir = "0.2.0" tokio = { version = "1.32.0", features = ["rt", "macros", "rt-multi-thread"] } +toml = "0.8.2" urlencoding = "2.1.3" warp = "0.3.6" diff --git a/filed/src/config.rs b/filed/src/config.rs new file mode 100644 index 0000000..dd198c6 --- /dev/null +++ b/filed/src/config.rs @@ -0,0 +1 @@ +pub mod types; \ No newline at end of file diff --git a/filed/src/config/types.rs b/filed/src/config/types.rs new file mode 100644 index 0000000..b169081 --- /dev/null +++ b/filed/src/config/types.rs @@ -0,0 +1,88 @@ +use serde::{Serialize, Deserialize}; +use std::{error::Error, fs}; + +use crate::env::Env; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FilesPolicy { + /// Whether the uploads are enabled + #[serde(default)] + allow_uploads: bool, + + /// Allow custom names + #[serde(default)] + allow_custom_names: bool, + + /// Allow password protection + #[serde(default)] + allow_pass_protection: bool, + + /// Max uploads for IP (doesn't include deleted uploads) + #[serde(default)] + max_per_ip: usize, + + /// Default time for file to be deleted + #[serde(default)] + file_del_timeout: usize, + + /// Whitelisted file types + #[serde(default)] + type_whitelist: Option>, + + /// Backlisted file types + #[serde(default)] + type_blacklist: Option>, + + /// Instance name + #[serde(default)] + instance_name: String, + + /// Instance URL (not the bind URL). Must be Some(...) + #[serde(default)] + instance_url: Option +} + +impl Default for FilesPolicy { + fn default() -> Self { + FilesPolicy { + allow_uploads: true, + allow_custom_names: true, + allow_pass_protection: true, + max_per_ip: 8, + file_del_timeout: 1800, + type_whitelist: None, + type_blacklist: None, + instance_name: "blek! File".into(), + instance_url: None + } + } +} + +impl FilesPolicy { + pub fn validate(self: &Self) -> Result<(), String> { + if self.instance_url.is_none() { + return Err("Instance url must not be empty!".into()); + } + Ok(()) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Config { + files: FilesPolicy +} + +impl Config { + + pub fn validate(self: &Self) -> Result<(), String> { + self.files.validate()?; + Ok(()) + } + + pub fn load(env: Env) -> Result> { + let raw = fs::read_to_string(env.confpath.clone())?; + let conf: Config = toml::from_str(&raw)?; + conf.validate()?; + Ok(conf) + } +} \ No newline at end of file diff --git a/filed/src/env.rs b/filed/src/env.rs index 7bcc11c..427f7de 100644 --- a/filed/src/env.rs +++ b/filed/src/env.rs @@ -20,7 +20,8 @@ pub struct Env { pub redis: Redis, pub filedir: String, pub instanceurl: String, - pub uploadspath: String + pub uploadspath: String, + pub confpath: String } fn get_var, O: From>(name: T) -> Result { @@ -55,7 +56,15 @@ pub fn loadenv() -> Result> { spath }, instanceurl: get_var("INSTANCE_URL")?, - uploadspath: get_var("UPLOADS_PATH")? + uploadspath: get_var("UPLOADS_PATH")?, + confpath: { + let spath: String = get_var("CONF_FILE").unwrap_or("/etc/filed".into()); + let path = Path::new(&spath); + if ! path.is_file() { + return Err(format!("CONF_FILE is {}, which is not a file!", spath).into()) + } + spath + } } ) } diff --git a/filed/src/main.rs b/filed/src/main.rs index 3176c0a..56ae7d1 100644 --- a/filed/src/main.rs +++ b/filed/src/main.rs @@ -6,6 +6,7 @@ mod files; mod env; mod web; mod db; +mod config; pub mod security; @@ -13,6 +14,7 @@ pub mod security; async fn main() { dotenvy::dotenv().unwrap(); let envy = env::loadenv().map_err(|err| format!("Could not load env: {err}")).unwrap(); + let conf = config::types::Config::load(envy.clone()).unwrap(); // set up logging if envy.logging {