From aa183a30bdf44d1056adb23f9aa69ab4ce41ba05 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Thu, 9 May 2024 15:22:48 +0200 Subject: [PATCH] refactor(sender, receiver, relay): changed use of .env file to a config file added support for a config file. under linux this will be under XDG_HOME/.config/caesar with name caesar.toml --- Cargo.lock | 189 +++++++++++++++++++++++++++++++- caesar-cli/Cargo.toml | 4 +- caesar-cli/src/cli/args.rs | 29 ++--- caesar-cli/src/main.rs | 36 +++--- caesar-core/src/relay/server.rs | 14 +-- 5 files changed, 226 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a804b32..5e3b3e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -413,7 +413,9 @@ dependencies = [ "axum-client-ip", "caesar-core", "clap 4.5.4", - "dotenv", + "confy", + "dotenvy", + "lazy_static", "serde", "serde_json", "tokio", @@ -543,11 +545,26 @@ dependencies = [ "atty", "bitflags 1.3.2", "strsim 0.8.0", - "textwrap", + "textwrap 0.11.0", "unicode-width", "vec_map", ] +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "atty", + "bitflags 1.3.2", + "clap_lex 0.2.4", + "indexmap 1.9.3", + "strsim 0.10.0", + "termcolor", + "textwrap 0.16.1", +] + [[package]] name = "clap" version = "4.5.4" @@ -566,7 +583,7 @@ checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" dependencies = [ "anstream", "anstyle", - "clap_lex", + "clap_lex 0.7.0", "strsim 0.11.1", ] @@ -582,6 +599,15 @@ dependencies = [ "syn 2.0.60", ] +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "clap_lex" version = "0.7.0" @@ -616,6 +642,18 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "confy" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45b1f4c00870f07dc34adcac82bb6a72cc5aabca8536ba1797e01df51d2ce9a0" +dependencies = [ + "directories", + "serde", + "thiserror", + "toml", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -780,6 +818,27 @@ dependencies = [ "subtle", ] +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dotenv" version = "0.15.0" @@ -789,6 +848,15 @@ dependencies = [ "clap 2.34.0", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +dependencies = [ + "clap 3.2.25", +] + [[package]] name = "ecdsa" version = "0.16.9" @@ -1460,6 +1528,16 @@ version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.5.0", + "libc", +] + [[package]] name = "linux-raw-sys" version = "0.4.13" @@ -1758,6 +1836,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "ordered-float" version = "4.2.0" @@ -1767,6 +1851,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + [[package]] name = "overload" version = "0.1.1" @@ -2071,6 +2161,17 @@ dependencies = [ "bitflags 2.5.0", ] +[[package]] +name = "redox_users" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.10.4" @@ -2367,6 +2468,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -2604,6 +2714,12 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "strsim" version = "0.11.1" @@ -2724,6 +2840,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -2733,6 +2858,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "textwrap" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" + [[package]] name = "thiserror" version = "1.0.59" @@ -2879,6 +3010,40 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef" +dependencies = [ + "indexmap 2.2.6", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tonic" version = "0.10.2" @@ -3331,6 +3496,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -3485,6 +3659,15 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c52e9c97a68071b23e836c9380edae937f17b9c4667bd021973efc689f618d" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.52.0" diff --git a/caesar-cli/Cargo.toml b/caesar-cli/Cargo.toml index c9bbef7..28653fb 100644 --- a/caesar-cli/Cargo.toml +++ b/caesar-cli/Cargo.toml @@ -17,7 +17,9 @@ serde_json = { version = "1.0" } serde = { version = "1.0", features = ["derive"] } tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } -dotenv = { version = "0.15.0", features = ["clap", "cli"] } clap = { version = "4.5.4", features = ["derive"] } axum = { version = "0.7.5", features = ["ws"] } axum-client-ip = "0.6.0" +confy = "0.6.1" +dotenvy = { version = "0.15.7", features = ["clap", "cli"] } +lazy_static = "1.4.0" diff --git a/caesar-cli/src/cli/args.rs b/caesar-cli/src/cli/args.rs index bbb46ea..d5b90dd 100644 --- a/caesar-cli/src/cli/args.rs +++ b/caesar-cli/src/cli/args.rs @@ -5,6 +5,8 @@ use clap::{Parser, Subcommand}; use std::{env, sync::Arc}; use tracing::debug; +use crate::GLOBAL_CONFIG; + #[derive(Parser, Debug)] #[command(version = env!("CARGO_PKG_VERSION"), about = "Send and receive files securely")] #[command(long_about = None)] @@ -61,16 +63,11 @@ impl Args { } pub async fn run(&self) -> Result<(), Box> { + let cfg = &GLOBAL_CONFIG; debug!("args: {:#?}", self); match &self.command { Some(Commands::Send { relay, files }) => { - let relay_string: String = relay - .as_deref() - .unwrap_or( - &env::var("APP_ORIGIN") - .unwrap_or("wss://caesar-transfer-iu.shuttleapp.rs/ws".to_string()), - ) - .to_string(); + let relay_string: String = relay.as_deref().unwrap_or(&cfg.app_origin).to_string(); let relay_arc = Arc::new(relay_string); let files_arc = Arc::new(files.to_vec()); sender::start_sender(relay_arc, files_arc).await; @@ -81,22 +78,20 @@ impl Args { name, }) => { println!("Receive for {name:?}"); - receiver::start_receiver( - relay.as_deref().unwrap_or( - env::var("APP_ORIGIN") - .unwrap_or("ws://0.0.0.0:8000/ws".to_string()) - .as_str(), - ), - name, - ) - .await; + receiver::start_receiver(relay.as_deref().unwrap_or(&cfg.app_origin), name).await; } Some(Commands::Serve { port, listen_address, }) => { println!("Serve with address '{listen_address:?}' and '{port:?}'"); - relay::server::start_ws(port.as_ref(), listen_address.as_ref()).await; + let address: String = listen_address + .as_deref() + .unwrap_or(&cfg.app_host) + .to_string(); + let port_value = port.unwrap_or(cfg.app_port.parse::().unwrap_or(0)); + let port: i32 = port_value; + relay::server::start_ws(&port, &address).await; } None => {} } diff --git a/caesar-cli/src/main.rs b/caesar-cli/src/main.rs index d7e56fc..3077379 100644 --- a/caesar-cli/src/main.rs +++ b/caesar-cli/src/main.rs @@ -1,33 +1,39 @@ use crate::cli::args::Args; -use dotenv::dotenv; +use dotenvy::dotenv; +use lazy_static::lazy_static; +use serde::{self, Deserialize, Serialize}; use tracing::error; use tracing_subscriber::filter::EnvFilter; mod cli; +#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)] +struct CaesarConfig { + app_environment: String, + app_host: String, + app_port: String, + app_origin: String, + app_relay: String, + rust_log: String, +} + +lazy_static! { + static ref GLOBAL_CONFIG: CaesarConfig = { + let cfg: CaesarConfig = confy::load("caesar", "caesar") + .expect("Konfigurationsdatei konnte nicht geladen werden"); + cfg + }; +} + #[tokio::main] -// This is the entrypoint of caesar. -// The #[tokio::main] attribute is required for any async code, and it -// sets up the tokio runtime. -// The async fn main() is the entrypoint of the application, and it's where -// we kick off our program. async fn main() -> Result<(), Box> { - // Load environment variables from a .env file if one is present. dotenv().ok(); - // Set up our logging subscriber. - // TheEnvFilter::from_default_env reads the env variable RUST_LOG - // and sets up the logging accordingly. - // The default is INFO level logging. tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .init(); - // Parse the command line arguments. let args = Args::new(); - // Run the commands based on the parsed arguments. - // If there is an error, print it to the console with the error! macro. if let Err(e) = args.run().await { error!("{e}"); } - // Return an Ok result, which just means that our program exited successfully. Ok(()) } diff --git a/caesar-core/src/relay/server.rs b/caesar-core/src/relay/server.rs index e1c18f0..d58ed3a 100644 --- a/caesar-core/src/relay/server.rs +++ b/caesar-core/src/relay/server.rs @@ -8,7 +8,7 @@ use axum::{ use futures_util::StreamExt; use serde_json::json; -use std::{env, net::SocketAddr, sync::Arc}; +use std::{net::SocketAddr, sync::Arc}; use tokio::{ net::TcpListener, signal, @@ -21,15 +21,9 @@ use crate::relay::client::Client; use crate::relay::transfer::TransferResponse; use crate::relay::{appstate::AppState, transfer::TransferRequest}; -pub async fn start_ws(port: Option<&i32>, listen_addr: Option<&String>) { - let app_host = match listen_addr { - Some(address) => address.to_string(), - None => env::var("APP_HOST").unwrap_or("0.0.0.0".to_string()), - }; - let app_port = match port { - Some(port) => port.to_string(), - None => env::var("APP_PORT").unwrap_or("8000".to_string()), - }; +pub async fn start_ws(port: &i32, listen_addr: &String) { + let app_host = listen_addr; + let app_port = port; debug!("Server configured to accept connections on host {app_host}...",); debug!("Server configured to listen connections on port {app_port}...",);