master: make the config file optional

This commit is contained in:
Denis Drakhnia 2024-01-28 10:04:37 +02:00
parent 6adb2c98c3
commit a77b3554e7
7 changed files with 85 additions and 64 deletions

3
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

@ -29,7 +29,7 @@ pub struct Cli {
pub log_level: Option<LevelFilter>,
pub listen_ip: Option<IpAddr>,
pub listen_port: Option<u16>,
pub config_path: Box<str>,
pub config_path: Option<Box<str>>,
}
fn print_usage(opts: Options) {
@ -42,10 +42,7 @@ fn print_version() {
}
pub fn parse() -> Result<Cli, Error> {
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<Cli, Error> {
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<Cli, Error> {
}
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)

View File

@ -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<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)]
pub struct HashConfig {
#[serde(default = "default_usize::<DEFAULT_HASH_LEN>")]
@ -147,7 +156,17 @@ pub struct HashConfig {
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)]
pub struct AdminConfig {
pub name: Box<str>,

View File

@ -19,7 +19,10 @@ use crate::config::Config;
use crate::master_server::{Error, MasterServer};
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 {
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),
}
}
}