Browse Source

master: resolve addr for update_addr config

Set required client version to 0.19 and newer.
Set game server challenge timeout to 10 seconds.
ipv6
Denis Drakhnia 1 year ago
parent
commit
e30d1c2a65
  1. 6
      master/config/main.toml
  2. 13
      master/src/config.rs
  3. 45
      master/src/master_server.rs

6
master/config/main.toml

@ -10,7 +10,7 @@ port = 27010 @@ -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 @@ -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

13
master/src/config.rs

@ -3,7 +3,7 @@ @@ -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"; @@ -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 { @@ -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 { @@ -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 { @@ -112,7 +113,7 @@ pub struct ClientConfig {
#[serde(default)]
pub update_title: Box<str>,
#[serde(default)]
pub update_addr: Option<SocketAddrV4>,
pub update_addr: Option<Box<str>>,
}
#[derive(Deserialize, Default, Debug)]
@ -145,6 +146,10 @@ fn default_server_port() -> u16 { @@ -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
}

45
master/src/master_server.rs

@ -124,18 +124,51 @@ struct MasterServer { @@ -124,18 +124,51 @@ struct MasterServer {
blocklist: HashSet<Ipv4Addr>,
}
fn resolve_socket_addr<A>(addr: A) -> io::Result<Option<SocketAddrV4>>
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<Self, Error> {
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,
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,

Loading…
Cancel
Save