diff -r b3157d218ae2 -r fd3a20e9d095 rust/hedgewars-server/src/core/server.rs --- a/rust/hedgewars-server/src/core/server.rs Sat Dec 21 23:33:50 2019 +0300 +++ b/rust/hedgewars-server/src/core/server.rs Mon Dec 23 18:08:55 2019 +0300 @@ -1,4 +1,5 @@ use super::{ + anteroom::HwAnteroomClient, client::HwClient, indexslab::IndexSlab, room::HwRoom, @@ -8,10 +9,9 @@ use bitflags::_core::hint::unreachable_unchecked; use bitflags::*; -use chrono::{offset, DateTime}; use log::*; use slab::Slab; -use std::{borrow::BorrowMut, collections::HashSet, iter, mem::replace, num::NonZeroU16}; +use std::{borrow::BorrowMut, collections::HashSet, iter, mem::replace}; #[derive(Debug)] pub enum CreateRoomError { @@ -106,91 +106,6 @@ #[derive(Debug)] pub struct AccessError(); -pub struct HwAnteClient { - pub nick: Option, - pub protocol_number: Option, - pub server_salt: String, - pub is_checker: bool, - pub is_local_admin: bool, - pub is_registered: bool, - pub is_admin: bool, - pub is_contributor: bool, -} - -struct Ipv4AddrRange { - min: [u8; 4], - max: [u8; 4], -} - -impl Ipv4AddrRange { - fn contains(&self, addr: [u8; 4]) -> bool { - (0..4).all(|i| self.min[i] <= addr[i] && addr[i] <= self.max[i]) - } -} - -struct BanCollection { - ban_ips: Vec, - ban_timeouts: Vec>, - ban_reasons: Vec, -} - -impl BanCollection { - fn new() -> Self { - Self { - ban_ips: vec![], - ban_timeouts: vec![], - ban_reasons: vec![], - } - } - - fn find(&self, addr: [u8; 4]) -> Option { - let time = offset::Utc::now(); - self.ban_ips - .iter() - .enumerate() - .find(|(i, r)| r.contains(addr) && time < self.ban_timeouts[*i]) - .map(|(i, _)| self.ban_reasons[i].clone()) - } -} - -pub struct HwAnteroom { - pub clients: IndexSlab, - bans: BanCollection, -} - -impl HwAnteroom { - pub fn new(clients_limit: usize) -> Self { - let clients = IndexSlab::with_capacity(clients_limit); - HwAnteroom { - clients, - bans: BanCollection::new(), - } - } - - pub fn find_ip_ban(&self, addr: [u8; 4]) -> Option { - self.bans.find(addr) - } - - pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) { - let client = HwAnteClient { - nick: None, - protocol_number: None, - server_salt: salt, - is_checker: false, - is_local_admin, - is_registered: false, - is_admin: false, - is_contributor: false, - }; - self.clients.insert(client_id, client); - } - - pub fn remove_client(&mut self, client_id: ClientId) -> Option { - let client = self.clients.remove(client_id); - client - } -} - pub struct ServerGreetings { pub for_latest_protocol: String, pub for_old_protocols: String, @@ -212,9 +127,8 @@ } pub struct HwServer { - pub clients: IndexSlab, + clients: IndexSlab, pub rooms: Slab, - pub anteroom: HwAnteroom, pub latest_protocol: u16, pub flags: ServerFlags, pub greetings: ServerGreetings, @@ -227,7 +141,6 @@ Self { clients, rooms, - anteroom: HwAnteroom::new(clients_limit), greetings: ServerGreetings::new(), latest_protocol: 58, flags: ServerFlags::empty(), @@ -286,7 +199,7 @@ .unwrap_or(false) } - pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) { + pub fn add_client(&mut self, client_id: ClientId, data: HwAnteroomClient) { if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) { let mut client = HwClient::new(client_id, protocol.get(), nick); client.set_is_checker(data.is_checker);