rust/hedgewars-server/src/core/anteroom.rs
author alfadur
Sat, 25 Mar 2023 04:00:30 +0300
changeset 15938 ce47259d5c86
parent 15521 1fcce8feace4
child 15989 fb389df02e3e
permissions -rw-r--r--
add some server todos
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15521
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 {
15938
ce47259d5c86 add some server todos
alfadur
parents: 15521
diff changeset
    35
        todo!("add nick bans");
15521
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    36
        Self {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    37
            ban_ips: vec![],
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    38
            ban_timeouts: vec![],
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    39
            ban_reasons: vec![],
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
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    43
    fn find(&self, addr: [u8; 4]) -> Option<String> {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    44
        let time = offset::Utc::now();
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    45
        self.ban_ips
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    46
            .iter()
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    47
            .enumerate()
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    48
            .find(|(i, r)| r.contains(addr) && time < self.ban_timeouts[*i])
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    49
            .map(|(i, _)| self.ban_reasons[i].clone())
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
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    53
pub struct HwAnteroom {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    54
    pub clients: IndexSlab<HwAnteroomClient>,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    55
    bans: BanCollection,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    56
}
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    57
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    58
impl HwAnteroom {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    59
    pub fn new(clients_limit: usize) -> Self {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    60
        let clients = IndexSlab::with_capacity(clients_limit);
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    61
        HwAnteroom {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    62
            clients,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    63
            bans: BanCollection::new(),
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
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    67
    pub fn find_ip_ban(&self, addr: [u8; 4]) -> Option<String> {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    68
        self.bans.find(addr)
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    69
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    70
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    71
    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
    72
        let client = HwAnteroomClient {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    73
            nick: None,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    74
            protocol_number: None,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    75
            server_salt: salt,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    76
            is_checker: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    77
            is_local_admin,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    78
            is_registered: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    79
            is_admin: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    80
            is_contributor: false,
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    81
        };
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    82
        self.clients.insert(client_id, client);
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    83
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    84
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    85
    pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteroomClient> {
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    86
        let client = self.clients.remove(client_id);
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    87
        client
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    88
    }
1fcce8feace4 lost the anteroom
alfadur <mail@none>
parents:
diff changeset
    89
}