From a77b3554e76cd23b845f431fb6a8abe6c32a2ab7 Mon Sep 17 00:00:00 2001 From: Denis Drakhnia Date: Sun, 28 Jan 2024 10:04:37 +0200 Subject: [PATCH] master: make the config file optional --- .gitignore | 3 --- Cargo.toml | 1 + master/config/default.toml | 39 ++++++++++++++++++++++++++++++++++++++ master/config/main.toml | 39 -------------------------------------- master/src/cli.rs | 12 ++++-------- master/src/config.rs | 31 ++++++++++++++++++++++++------ master/src/main.rs | 24 +++++++++++++++-------- 7 files changed, 85 insertions(+), 64 deletions(-) create mode 100644 master/config/default.toml delete mode 100644 master/config/main.toml diff --git a/.gitignore b/.gitignore index edfac18..61ae5aa 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,3 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb - -# a1ba: do not add my config files by accident -config/ diff --git a/Cargo.toml b/Cargo.toml index 13c4029..d8e2fcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "admin", "query", ] +default-members = ["master"] [profile.release] codegen-units = 1 diff --git a/master/config/default.toml b/master/config/default.toml new file mode 100644 index 0000000..b7967ec --- /dev/null +++ b/master/config/default.toml @@ -0,0 +1,39 @@ +# Default config file + +#[log] +## Possible values: 0-5, off, error, warn, info, debug, trace +#level = "info" + +#[server] +#ip = "0.0.0.0" +#port = 27010 +## How many servers with the same ip can be registred +#max-servers-per-ip = 14 + +#[server.timeout] +## Time in seconds while challenge is valid +#challenge = 10 +## Time in seconds while server is valid +#server = 300 +## Time in seconds before next admin request is allowed after wrong password +#admin = 10 + +#[client] +## If client version is less then show update message +#version = "0.19" +#update_title = "https://github.com/FWGS/xash3d-fwgs" +#update_map = "Update please" +##update_addr = "mentality.rip" + +#[hash] +#len = 64 +#key = "Half-Life" +#personal = "Freeman" + +##[[admin]] +##name = "gordon" +##password = "crowbar" + +##[[admin]] +##name = "gman" +##password = "time2choose" diff --git a/master/config/main.toml b/master/config/main.toml deleted file mode 100644 index 8d244b9..0000000 --- a/master/config/main.toml +++ /dev/null @@ -1,39 +0,0 @@ -# Default config file - -[log] -# Possible values: 0-5, off, error, warn, info, debug, trace -level = "info" - -[server] -ip = "0.0.0.0" -port = 27010 -# How many servers with the same ip can be registred -max-servers-per-ip = 14 - -[server.timeout] -# Time in seconds while challenge is valid -challenge = 10 -# Time in seconds while server is valid -server = 300 -# Time in seconds before next admin request is allowed after wrong password -admin = 10 - -[client] -# If client version is less then show update message -version = "0.19" -update_title = "https://github.com/FWGS/xash3d-fwgs" -update_map = "Update please" -update_addr = "mentality.rip" - -[hash] -len = 64 -key = "Half-Life" -personal = "Freeman" - -#[[admin]] -#name = "gordon" -#password = "crowbar" - -#[[admin]] -#name = "gman" -#password = "time2choose" diff --git a/master/src/cli.rs b/master/src/cli.rs index c8f2d86..e036d23 100644 --- a/master/src/cli.rs +++ b/master/src/cli.rs @@ -29,7 +29,7 @@ pub struct Cli { pub log_level: Option, pub listen_ip: Option, pub listen_port: Option, - pub config_path: Box, + pub config_path: Option>, } fn print_usage(opts: Options) { @@ -42,10 +42,7 @@ fn print_version() { } pub fn parse() -> Result { - let mut cli = Cli { - config_path: config::DEFAULT_CONFIG_PATH.to_string().into_boxed_str(), - ..Cli::default() - }; + let mut cli = Cli::default(); let args: Vec<_> = std::env::args().collect(); let mut opts = Options::new(); @@ -61,8 +58,7 @@ pub fn parse() -> Result { config::DEFAULT_MASTER_SERVER_PORT ); opts.optopt("p", "port", &port_help, "PORT"); - let config_help = format!("config path [default: {}]", cli.config_path); - opts.optopt("c", "config", &config_help, "PATH"); + opts.optopt("c", "config", "config path", "PATH"); let matches = opts.parse(&args[1..])?; @@ -95,7 +91,7 @@ pub fn parse() -> Result { } if let Some(s) = matches.opt_str("config") { - cli.config_path = s.into_boxed_str(); + cli.config_path = Some(s.into_boxed_str()); } Ok(cli) diff --git a/master/src/config.rs b/master/src/config.rs index 3d74ba7..3152ec6 100644 --- a/master/src/config.rs +++ b/master/src/config.rs @@ -12,8 +12,6 @@ use thiserror::Error; use xash3d_protocol::admin; use xash3d_protocol::filter::Version; -pub const DEFAULT_CONFIG_PATH: &str = "config/main.toml"; - pub const DEFAULT_MASTER_SERVER_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)); pub const DEFAULT_MASTER_SERVER_PORT: u16 = 27010; pub const DEFAULT_CHALLENGE_TIMEOUT: u32 = 10; @@ -44,7 +42,7 @@ pub enum Error { Io(#[from] io::Error), } -#[derive(Deserialize, Debug)] +#[derive(Default, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct Config { #[serde(default)] @@ -122,7 +120,7 @@ impl Default for TimeoutConfig { } } -#[derive(Deserialize, Default, Debug)] +#[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct ClientConfig { #[serde(default)] @@ -136,7 +134,18 @@ pub struct ClientConfig { pub update_addr: Option>, } -#[derive(Deserialize, Default, Debug)] +impl Default for ClientConfig { + fn default() -> Self { + Self { + version: Version::new(0, 19), + update_map: String::from("Update please").into_boxed_str(), + update_title: String::from("https://github.com/FWGS/xash3d-fwgs").into_boxed_str(), + update_addr: None, + } + } +} + +#[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct HashConfig { #[serde(default = "default_usize::")] @@ -147,7 +156,17 @@ pub struct HashConfig { pub personal: Box, } -#[derive(Deserialize, Default, Debug)] +impl Default for HashConfig { + fn default() -> Self { + Self { + len: DEFAULT_HASH_LEN, + key: default_hash_key(), + personal: default_hash_personal(), + } + } +} + +#[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct AdminConfig { pub name: Box, diff --git a/master/src/main.rs b/master/src/main.rs index 70957ff..3de59c4 100644 --- a/master/src/main.rs +++ b/master/src/main.rs @@ -19,7 +19,10 @@ use crate::config::Config; use crate::master_server::{Error, MasterServer}; fn load_config(cli: &Cli) -> Result { - let mut cfg = config::load(cli.config_path.as_ref())?; + let mut cfg = match cli.config_path { + Some(ref p) => config::load(p.as_ref())?, + None => Config::default(), + }; if let Some(level) = cli.log_level { cfg.log.level = level; @@ -45,7 +48,10 @@ fn run() -> Result<(), Error> { logger::init(); let cfg = load_config(&cli).unwrap_or_else(|e| { - eprintln!("Failed to load config \"{}\": {}", cli.config_path, e); + match cli.config_path.as_deref() { + Some(p) => eprintln!("Failed to load config \"{}\": {}", p, e), + None => eprintln!("{}", e), + } process::exit(1); }); @@ -57,15 +63,17 @@ fn run() -> Result<(), Error> { master.run(&sig_flag)?; if sig_flag.swap(false, Ordering::Relaxed) { - info!("Reloading config from {}", cli.config_path); + if let Some(config_path) = cli.config_path.as_deref() { + info!("Reloading config from {}", config_path); - match load_config(&cli) { - Ok(cfg) => { - if let Err(e) = master.update_config(cfg) { - error!("{}", e); + match load_config(&cli) { + Ok(cfg) => { + if let Err(e) = master.update_config(cfg) { + error!("{}", e); + } } + Err(e) => error!("failed to load config: {}", e), } - Err(e) => error!("failed to load config: {}", e), } } }