diff --git a/master/config/main.toml b/master/config/main.toml index 37b3a99..bb47014 100644 --- a/master/config/main.toml +++ b/master/config/main.toml @@ -10,7 +10,7 @@ port = 27010 [server.timeout] # Time in seconds while challenge is valid -challenge = 300 +challenge = 10 # Time in seconds while server is valid server = 300 # TIme in seconds before next admin request is allowed after wrong password @@ -18,10 +18,10 @@ admin = 10 [client] # If client version is less then show update message -version = "0.20" +version = "0.19" update_title = "https://github.com/FWGS/xash3d-fwgs" update_map = "Update please" -update_addr = "127.0.0.1:27010" +update_addr = "mentality.rip:27010" [hash] len = 64 diff --git a/master/src/config.rs b/master/src/config.rs index 5ca6641..afa52b5 100644 --- a/master/src/config.rs +++ b/master/src/config.rs @@ -3,7 +3,7 @@ use std::fs; use std::io; -use std::net::{IpAddr, Ipv4Addr, SocketAddrV4}; +use std::net::{IpAddr, Ipv4Addr}; use std::path::Path; use log::LevelFilter; @@ -16,6 +16,7 @@ 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; pub const DEFAULT_TIMEOUT: u32 = 300; pub const DEFAULT_ADMIN_TIMEOUT: u32 = 10; @@ -83,7 +84,7 @@ impl Default for ServerConfig { #[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct TimeoutConfig { - #[serde(default = "default_timeout")] + #[serde(default = "default_challenge_timeout")] pub challenge: u32, #[serde(default = "default_timeout")] pub server: u32, @@ -94,7 +95,7 @@ pub struct TimeoutConfig { impl Default for TimeoutConfig { fn default() -> Self { Self { - challenge: default_timeout(), + challenge: default_challenge_timeout(), server: default_timeout(), admin: default_admin_timeout(), } @@ -112,7 +113,7 @@ pub struct ClientConfig { #[serde(default)] pub update_title: Box, #[serde(default)] - pub update_addr: Option, + pub update_addr: Option>, } #[derive(Deserialize, Default, Debug)] @@ -145,6 +146,10 @@ fn default_server_port() -> u16 { DEFAULT_MASTER_SERVER_PORT } +fn default_challenge_timeout() -> u32 { + DEFAULT_CHALLENGE_TIMEOUT +} + fn default_timeout() -> u32 { DEFAULT_TIMEOUT } diff --git a/master/src/master_server.rs b/master/src/master_server.rs index 41708d4..e6de3c9 100644 --- a/master/src/master_server.rs +++ b/master/src/master_server.rs @@ -124,18 +124,51 @@ struct MasterServer { blocklist: HashSet, } +fn resolve_socket_addr(addr: A) -> io::Result> +where + A: ToSocketAddrs, +{ + for i in addr.to_socket_addrs()? { + match i { + SocketAddr::V4(i) => return Ok(Some(i)), + SocketAddr::V6(_) => {} + } + } + Ok(None) +} + impl MasterServer { fn new(cfg: Config) -> Result { let addr = SocketAddr::new(cfg.server.ip, cfg.server.port); info!("Listen address: {}", addr); let sock = UdpSocket::bind(addr).map_err(Error::BindSocket)?; - let update_addr = - cfg.client - .update_addr - .unwrap_or_else(|| match sock.local_addr().unwrap() { - SocketAddr::V4(addr) => addr, - _ => todo!(), - }); + + let update_addr = { + let mut addr = None; + + if let Some(update_addr) = cfg.client.update_addr { + addr = match resolve_socket_addr(&*update_addr) { + Ok(None) => { + error!( + "update address: failed to resolve IPv4 for \"{}\"", + update_addr + ); + None + } + Err(e) => { + error!("update address: {}", e); + None + } + Ok(addr) => addr, + } + }; + + // fallback to local address + addr.unwrap_or_else(|| match sock.local_addr().unwrap() { + SocketAddr::V4(a) => a, + _ => todo!(), + }) + }; Ok(Self { sock,