rust/hedgewars-server/src/core/anteroom.rs
author S.D.
Tue, 27 Sep 2022 14:59:03 +0300
changeset 15900 fc3cb23fd26f
parent 15543 1fcce8feace4
child 15968 ce47259d5c86
permissions -rw-r--r--
Allow to see rooms of incompatible versions in the lobby For the new clients the room version is shown in a separate column. There is also a hack for previous versions clients: the room vesion specifier is prepended to the room names for rooms of incompatible versions, and the server shows 'incompatible version' error if the client tries to join them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15543
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     1
use super::{indexslab::IndexSlab, types::ClientId};
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     2
use chrono::{offset, DateTime};
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     3
use std::{iter::Iterator, num::NonZeroU16};
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     4
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     5
pub struct HwAnteroomClient {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     6
    pub nick: Option<String>,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     7
    pub protocol_number: Option<NonZeroU16>,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     8
    pub server_salt: String,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
     9
    pub is_checker: bool,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    10
    pub is_local_admin: bool,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    11
    pub is_registered: bool,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    12
    pub is_admin: bool,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    13
    pub is_contributor: bool,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    14
}
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    15
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    16
struct Ipv4AddrRange {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    17
    min: [u8; 4],
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    18
    max: [u8; 4],
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    19
}
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    20
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    21
impl Ipv4AddrRange {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    22
    fn contains(&self, addr: [u8; 4]) -> bool {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    23
        (0..4).all(|i| self.min[i] <= addr[i] && addr[i] <= self.max[i])
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    24
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    25
}
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    26
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    27
struct BanCollection {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    28
    ban_ips: Vec<Ipv4AddrRange>,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    29
    ban_timeouts: Vec<DateTime<offset::Utc>>,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    30
    ban_reasons: Vec<String>,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    31
}
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    32
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    33
impl BanCollection {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    34
    fn new() -> Self {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    35
        Self {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    36
            ban_ips: vec![],
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    37
            ban_timeouts: vec![],
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    38
            ban_reasons: vec![],
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    39
        }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    40
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    41
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    42
    fn find(&self, addr: [u8; 4]) -> Option<String> {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    43
        let time = offset::Utc::now();
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    44
        self.ban_ips
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    45
            .iter()
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    46
            .enumerate()
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    47
            .find(|(i, r)| r.contains(addr) && time < self.ban_timeouts[*i])
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    48
            .map(|(i, _)| self.ban_reasons[i].clone())
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    49
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    50
}
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    51
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    52
pub struct HwAnteroom {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    53
    pub clients: IndexSlab<HwAnteroomClient>,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    54
    bans: BanCollection,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    55
}
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    56
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    57
impl HwAnteroom {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    58
    pub fn new(clients_limit: usize) -> Self {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    59
        let clients = IndexSlab::with_capacity(clients_limit);
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    60
        HwAnteroom {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    61
            clients,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    62
            bans: BanCollection::new(),
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    63
        }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    64
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    65
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    66
    pub fn find_ip_ban(&self, addr: [u8; 4]) -> Option<String> {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    67
        self.bans.find(addr)
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    68
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    69
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    70
    pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    71
        let client = HwAnteroomClient {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    72
            nick: None,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    73
            protocol_number: None,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    74
            server_salt: salt,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    75
            is_checker: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    76
            is_local_admin,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    77
            is_registered: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    78
            is_admin: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    79
            is_contributor: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    80
        };
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    81
        self.clients.insert(client_id, client);
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    82
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    83
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    84
    pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteroomClient> {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    85
        let client = self.clients.remove(client_id);
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    86
        client
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    87
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    88
}