15521
|
1 |
use super::{indexslab::IndexSlab, types::ClientId};
|
|
2 |
use chrono::{offset, DateTime};
|
|
3 |
use std::{iter::Iterator, num::NonZeroU16};
|
|
4 |
|
|
5 |
pub struct HwAnteroomClient {
|
|
6 |
pub nick: Option<String>,
|
|
7 |
pub protocol_number: Option<NonZeroU16>,
|
|
8 |
pub server_salt: String,
|
|
9 |
pub is_checker: bool,
|
|
10 |
pub is_local_admin: bool,
|
|
11 |
pub is_registered: bool,
|
|
12 |
pub is_admin: bool,
|
|
13 |
pub is_contributor: bool,
|
|
14 |
}
|
|
15 |
|
|
16 |
struct Ipv4AddrRange {
|
|
17 |
min: [u8; 4],
|
|
18 |
max: [u8; 4],
|
|
19 |
}
|
|
20 |
|
|
21 |
impl Ipv4AddrRange {
|
|
22 |
fn contains(&self, addr: [u8; 4]) -> bool {
|
|
23 |
(0..4).all(|i| self.min[i] <= addr[i] && addr[i] <= self.max[i])
|
|
24 |
}
|
|
25 |
}
|
|
26 |
|
|
27 |
struct BanCollection {
|
|
28 |
ban_ips: Vec<Ipv4AddrRange>,
|
|
29 |
ban_timeouts: Vec<DateTime<offset::Utc>>,
|
|
30 |
ban_reasons: Vec<String>,
|
|
31 |
}
|
|
32 |
|
|
33 |
impl BanCollection {
|
|
34 |
fn new() -> Self {
|
15989
|
35 |
//todo!("add nick bans");
|
15521
|
36 |
Self {
|
|
37 |
ban_ips: vec![],
|
|
38 |
ban_timeouts: vec![],
|
|
39 |
ban_reasons: vec![],
|
|
40 |
}
|
|
41 |
}
|
|
42 |
|
|
43 |
fn find(&self, addr: [u8; 4]) -> Option<String> {
|
|
44 |
let time = offset::Utc::now();
|
|
45 |
self.ban_ips
|
|
46 |
.iter()
|
|
47 |
.enumerate()
|
|
48 |
.find(|(i, r)| r.contains(addr) && time < self.ban_timeouts[*i])
|
|
49 |
.map(|(i, _)| self.ban_reasons[i].clone())
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
pub struct HwAnteroom {
|
|
54 |
pub clients: IndexSlab<HwAnteroomClient>,
|
|
55 |
bans: BanCollection,
|
|
56 |
}
|
|
57 |
|
|
58 |
impl HwAnteroom {
|
|
59 |
pub fn new(clients_limit: usize) -> Self {
|
|
60 |
let clients = IndexSlab::with_capacity(clients_limit);
|
|
61 |
HwAnteroom {
|
|
62 |
clients,
|
|
63 |
bans: BanCollection::new(),
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
pub fn find_ip_ban(&self, addr: [u8; 4]) -> Option<String> {
|
|
68 |
self.bans.find(addr)
|
|
69 |
}
|
|
70 |
|
|
71 |
pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) {
|
|
72 |
let client = HwAnteroomClient {
|
|
73 |
nick: None,
|
|
74 |
protocol_number: None,
|
|
75 |
server_salt: salt,
|
|
76 |
is_checker: false,
|
|
77 |
is_local_admin,
|
|
78 |
is_registered: false,
|
|
79 |
is_admin: false,
|
|
80 |
is_contributor: false,
|
|
81 |
};
|
|
82 |
self.clients.insert(client_id, client);
|
|
83 |
}
|
|
84 |
|
|
85 |
pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteroomClient> {
|
|
86 |
let client = self.clients.remove(client_id);
|
|
87 |
client
|
|
88 |
}
|
|
89 |
}
|