Browse Source

master: make the config file optional

ipv6
Denis Drakhnia 10 months ago
parent
commit
a77b3554e7
  1. 3
      .gitignore
  2. 1
      Cargo.toml
  3. 39
      master/config/default.toml
  4. 39
      master/config/main.toml
  5. 12
      master/src/cli.rs
  6. 31
      master/src/config.rs
  7. 14
      master/src/main.rs

3
.gitignore vendored

@ -13,6 +13,3 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information # MSVC Windows builds of rustc generate these, which store debugging information
*.pdb *.pdb
# a1ba: do not add my config files by accident
config/

1
Cargo.toml

@ -6,6 +6,7 @@ members = [
"admin", "admin",
"query", "query",
] ]
default-members = ["master"]
[profile.release] [profile.release]
codegen-units = 1 codegen-units = 1

39
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"

39
master/config/main.toml

@ -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"

12
master/src/cli.rs

@ -29,7 +29,7 @@ pub struct Cli {
pub log_level: Option<LevelFilter>, pub log_level: Option<LevelFilter>,
pub listen_ip: Option<IpAddr>, pub listen_ip: Option<IpAddr>,
pub listen_port: Option<u16>, pub listen_port: Option<u16>,
pub config_path: Box<str>, pub config_path: Option<Box<str>>,
} }
fn print_usage(opts: Options) { fn print_usage(opts: Options) {
@ -42,10 +42,7 @@ fn print_version() {
} }
pub fn parse() -> Result<Cli, Error> { pub fn parse() -> Result<Cli, Error> {
let mut cli = Cli { let mut cli = Cli::default();
config_path: config::DEFAULT_CONFIG_PATH.to_string().into_boxed_str(),
..Cli::default()
};
let args: Vec<_> = std::env::args().collect(); let args: Vec<_> = std::env::args().collect();
let mut opts = Options::new(); let mut opts = Options::new();
@ -61,8 +58,7 @@ pub fn parse() -> Result<Cli, Error> {
config::DEFAULT_MASTER_SERVER_PORT config::DEFAULT_MASTER_SERVER_PORT
); );
opts.optopt("p", "port", &port_help, "PORT"); opts.optopt("p", "port", &port_help, "PORT");
let config_help = format!("config path [default: {}]", cli.config_path); opts.optopt("c", "config", "config path", "PATH");
opts.optopt("c", "config", &config_help, "PATH");
let matches = opts.parse(&args[1..])?; let matches = opts.parse(&args[1..])?;
@ -95,7 +91,7 @@ pub fn parse() -> Result<Cli, Error> {
} }
if let Some(s) = matches.opt_str("config") { 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) Ok(cli)

31
master/src/config.rs

@ -12,8 +12,6 @@ use thiserror::Error;
use xash3d_protocol::admin; use xash3d_protocol::admin;
use xash3d_protocol::filter::Version; 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_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0));
pub const DEFAULT_MASTER_SERVER_PORT: u16 = 27010; pub const DEFAULT_MASTER_SERVER_PORT: u16 = 27010;
pub const DEFAULT_CHALLENGE_TIMEOUT: u32 = 10; pub const DEFAULT_CHALLENGE_TIMEOUT: u32 = 10;
@ -44,7 +42,7 @@ pub enum Error {
Io(#[from] io::Error), Io(#[from] io::Error),
} }
#[derive(Deserialize, Debug)] #[derive(Default, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct Config { pub struct Config {
#[serde(default)] #[serde(default)]
@ -122,7 +120,7 @@ impl Default for TimeoutConfig {
} }
} }
#[derive(Deserialize, Default, Debug)] #[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct ClientConfig { pub struct ClientConfig {
#[serde(default)] #[serde(default)]
@ -136,7 +134,18 @@ pub struct ClientConfig {
pub update_addr: Option<Box<str>>, pub update_addr: Option<Box<str>>,
} }
#[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)] #[serde(deny_unknown_fields)]
pub struct HashConfig { pub struct HashConfig {
#[serde(default = "default_usize::<DEFAULT_HASH_LEN>")] #[serde(default = "default_usize::<DEFAULT_HASH_LEN>")]
@ -147,7 +156,17 @@ pub struct HashConfig {
pub personal: Box<str>, pub personal: Box<str>,
} }
#[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)] #[serde(deny_unknown_fields)]
pub struct AdminConfig { pub struct AdminConfig {
pub name: Box<str>, pub name: Box<str>,

14
master/src/main.rs

@ -19,7 +19,10 @@ use crate::config::Config;
use crate::master_server::{Error, MasterServer}; use crate::master_server::{Error, MasterServer};
fn load_config(cli: &Cli) -> Result<Config, config::Error> { fn load_config(cli: &Cli) -> Result<Config, config::Error> {
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 { if let Some(level) = cli.log_level {
cfg.log.level = level; cfg.log.level = level;
@ -45,7 +48,10 @@ fn run() -> Result<(), Error> {
logger::init(); logger::init();
let cfg = load_config(&cli).unwrap_or_else(|e| { 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); process::exit(1);
}); });
@ -57,7 +63,8 @@ fn run() -> Result<(), Error> {
master.run(&sig_flag)?; master.run(&sig_flag)?;
if sig_flag.swap(false, Ordering::Relaxed) { 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) { match load_config(&cli) {
Ok(cfg) => { Ok(cfg) => {
@ -70,6 +77,7 @@ fn run() -> Result<(), Error> {
} }
} }
} }
}
fn main() { fn main() {
if let Err(e) = run() { if let Err(e) = run() {

Loading…
Cancel
Save