author | Wuzzy <Wuzzy@disroot.org> |
Wed, 28 Jun 2023 18:57:00 +0200 | |
changeset 15967 | f362e82cddcf |
parent 15831 | 7d0f747afcb8 |
child 16002 | e915ed28726e |
permissions | -rw-r--r-- |
15163 | 1 |
use std::{ |
2 |
cmp::PartialEq, |
|
3 |
collections::HashMap, |
|
4 |
fmt::{Formatter, LowerHex}, |
|
5 |
iter::Iterator, |
|
6 |
}; |
|
12149 | 7 |
|
15074 | 8 |
use self::{ |
9 |
actions::{Destination, DestinationGroup, PendingMessage}, |
|
15075 | 10 |
inanteroom::LoginResult, |
15441 | 11 |
strings::*, |
14694 | 12 |
}; |
15804 | 13 |
use crate::handlers::actions::ToPendingMessage; |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
14 |
use crate::{ |
15074 | 15 |
core::{ |
15520 | 16 |
anteroom::HwAnteroom, |
15075 | 17 |
room::RoomSave, |
18 |
server::HwServer, |
|
15804 | 19 |
types::{ClientId, Replay, RoomId}, |
15074 | 20 |
}, |
15804 | 21 |
utils, |
22 |
}; |
|
23 |
use hedgewars_network_protocol::{ |
|
24 |
messages::{ |
|
15075 | 25 |
global_chat, server_chat, HwProtocolMessage, HwProtocolMessage::EngineMessage, |
26 |
HwServerMessage, HwServerMessage::*, |
|
15074 | 27 |
}, |
15804 | 28 |
types::{GameCfg, TeamInfo}, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
29 |
}; |
15804 | 30 |
|
14693 | 31 |
use base64::encode; |
13805 | 32 |
use log::*; |
14693 | 33 |
use rand::{thread_rng, RngCore}; |
13666 | 34 |
|
15074 | 35 |
mod actions; |
14457 | 36 |
mod checker; |
37 |
mod common; |
|
15075 | 38 |
mod inanteroom; |
15074 | 39 |
mod inlobby; |
15075 | 40 |
mod inroom; |
15439 | 41 |
mod strings; |
12149 | 42 |
|
15102 | 43 |
#[derive(PartialEq, Debug)] |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
44 |
pub struct Sha1Digest([u8; 20]); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
45 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
46 |
impl Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
47 |
pub fn new(digest: [u8; 20]) -> Self { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
48 |
Self(digest) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
49 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
50 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
51 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
52 |
impl LowerHex for Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
53 |
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
54 |
for byte in &self.0 { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
55 |
write!(f, "{:02x}", byte)?; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
56 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
57 |
Ok(()) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
58 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
59 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
60 |
|
15163 | 61 |
impl PartialEq<&str> for Sha1Digest { |
62 |
fn eq(&self, other: &&str) -> bool { |
|
63 |
if other.len() != self.0.len() * 2 { |
|
64 |
false |
|
65 |
} else { |
|
66 |
#[inline] |
|
67 |
fn convert(c: u8) -> u8 { |
|
68 |
if c > b'9' { |
|
15216 | 69 |
c.wrapping_sub(b'a').saturating_add(10) |
15163 | 70 |
} else { |
15216 | 71 |
c.wrapping_sub(b'0') |
15163 | 72 |
} |
73 |
} |
|
74 |
||
75 |
other |
|
76 |
.as_bytes() |
|
77 |
.chunks_exact(2) |
|
78 |
.zip(&self.0) |
|
79 |
.all(|(chars, byte)| { |
|
80 |
if let [hi, lo] = chars { |
|
81 |
convert(*lo) == byte & 0x0f && convert(*hi) == (byte & 0xf0) >> 4 |
|
82 |
} else { |
|
83 |
unreachable!() |
|
84 |
} |
|
85 |
}) |
|
86 |
} |
|
87 |
} |
|
88 |
} |
|
89 |
||
15520 | 90 |
pub struct ServerState { |
91 |
pub server: HwServer, |
|
92 |
pub anteroom: HwAnteroom, |
|
93 |
} |
|
94 |
||
95 |
impl ServerState { |
|
96 |
pub fn new(clients_limit: usize, rooms_limit: usize) -> Self { |
|
97 |
Self { |
|
98 |
server: HwServer::new(clients_limit, rooms_limit), |
|
99 |
anteroom: HwAnteroom::new(clients_limit), |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
||
15102 | 104 |
#[derive(Debug)] |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
105 |
pub struct AccountInfo { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
106 |
pub is_registered: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
107 |
pub is_admin: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
108 |
pub is_contributor: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
109 |
pub server_hash: Sha1Digest, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
110 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
111 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
112 |
pub enum IoTask { |
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
113 |
CheckRegistered { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
114 |
nick: String, |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
115 |
}, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
116 |
GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
117 |
nick: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
118 |
protocol: u16, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
119 |
password_hash: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
120 |
client_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
121 |
server_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
122 |
}, |
15532 | 123 |
GetCheckerAccount { |
124 |
nick: String, |
|
125 |
password: String, |
|
126 |
}, |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
127 |
GetReplay { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
128 |
id: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
129 |
}, |
14780 | 130 |
SaveRoom { |
131 |
room_id: RoomId, |
|
132 |
filename: String, |
|
133 |
contents: String, |
|
134 |
}, |
|
135 |
LoadRoom { |
|
136 |
room_id: RoomId, |
|
14781 | 137 |
filename: String, |
138 |
}, |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
139 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
140 |
|
15102 | 141 |
#[derive(Debug)] |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
142 |
pub enum IoResult { |
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
143 |
AccountRegistered(bool), |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
144 |
Account(Option<AccountInfo>), |
15532 | 145 |
CheckerAccount { is_registered: bool }, |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
146 |
Replay(Option<Replay>), |
14780 | 147 |
SaveRoom(RoomId, bool), |
14781 | 148 |
LoadRoom(RoomId, Option<String>), |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
149 |
} |
14693 | 150 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
151 |
pub struct Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
152 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
153 |
messages: Vec<PendingMessage>, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
154 |
io_tasks: Vec<IoTask>, |
14696 | 155 |
removed_clients: Vec<ClientId>, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
156 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
157 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
158 |
impl Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
159 |
pub fn new(client_id: ClientId) -> Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
160 |
Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
161 |
client_id, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
162 |
messages: vec![], |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
163 |
io_tasks: vec![], |
14696 | 164 |
removed_clients: vec![], |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
165 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
166 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
167 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
168 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
169 |
pub fn is_empty(&self) -> bool { |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
170 |
self.messages.is_empty() && self.removed_clients.is_empty() && self.io_tasks.is_empty() |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
171 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
172 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
173 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
174 |
pub fn len(&self) -> usize { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
175 |
self.messages.len() |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
176 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
177 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
178 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
179 |
pub fn client_id(&self) -> ClientId { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
180 |
self.client_id |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
181 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
182 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
183 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
184 |
pub fn add(&mut self, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
185 |
self.messages.push(message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
186 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
187 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
188 |
#[inline] |
15439 | 189 |
pub fn warn(&mut self, message: &str) { |
190 |
self.add(Warning(message.to_string()).send_self()); |
|
191 |
} |
|
192 |
||
193 |
#[inline] |
|
15441 | 194 |
pub fn error(&mut self, message: &str) { |
195 |
self.add(Error(message.to_string()).send_self()); |
|
196 |
} |
|
197 |
||
198 |
#[inline] |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
199 |
pub fn request_io(&mut self, task: IoTask) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
200 |
self.io_tasks.push(task) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
201 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
202 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
203 |
pub fn extract_messages<'a, 'b: 'a>( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
204 |
&'b mut self, |
15075 | 205 |
server: &'a HwServer, |
206 |
) -> impl Iterator<Item = (Vec<ClientId>, HwServerMessage)> + 'a { |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
207 |
let client_id = self.client_id; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
208 |
self.messages.drain(..).map(move |m| { |
14788 | 209 |
let ids = get_recipients(server, client_id, m.destination); |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
210 |
(ids, m.message) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
211 |
}) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
212 |
} |
14696 | 213 |
|
214 |
pub fn remove_client(&mut self, client_id: ClientId) { |
|
215 |
self.removed_clients.push(client_id); |
|
216 |
} |
|
217 |
||
218 |
pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ { |
|
219 |
self.removed_clients.drain(..) |
|
220 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
221 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
222 |
pub fn extract_io_tasks(&mut self) -> impl Iterator<Item = IoTask> + '_ { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
223 |
self.io_tasks.drain(..) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
224 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
225 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
226 |
|
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
227 |
impl Extend<PendingMessage> for Response { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
228 |
fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
229 |
for msg in iter { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
230 |
self.add(msg) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
231 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
232 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
233 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
234 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
235 |
fn get_recipients( |
15075 | 236 |
server: &HwServer, |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
237 |
client_id: ClientId, |
14788 | 238 |
destination: Destination, |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
239 |
) -> Vec<ClientId> { |
14788 | 240 |
match destination { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
241 |
Destination::ToSelf => vec![client_id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
242 |
Destination::ToId(id) => vec![id], |
14788 | 243 |
Destination::ToIds(ids) => ids, |
244 |
Destination::ToAll { group, skip_self } => { |
|
14789 | 245 |
let mut ids: Vec<_> = match group { |
15526
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
246 |
DestinationGroup::All => server.iter_client_ids().collect(), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
247 |
DestinationGroup::Lobby => server.lobby_client_ids().collect(), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
248 |
DestinationGroup::Protocol(proto) => server.protocol_client_ids(proto).collect(), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
249 |
DestinationGroup::Room(id) => server.room_client_ids(id).collect(), |
14788 | 250 |
}; |
251 |
||
252 |
if skip_self { |
|
253 |
if let Some(index) = ids.iter().position(|id| *id == client_id) { |
|
254 |
ids.remove(index); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
ids |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
259 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
260 |
} |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
261 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
262 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
263 |
pub fn handle( |
15520 | 264 |
state: &mut ServerState, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
265 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
266 |
response: &mut Response, |
15075 | 267 |
message: HwProtocolMessage, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
268 |
) { |
12149 | 269 |
match message { |
15075 | 270 |
HwProtocolMessage::Ping => response.add(Pong.send_self()), |
15113 | 271 |
HwProtocolMessage::Pong => (), |
14693 | 272 |
_ => { |
15520 | 273 |
if state.anteroom.clients.contains(client_id) { |
274 |
match inanteroom::handle(state, client_id, response, message) { |
|
14693 | 275 |
LoginResult::Unchanged => (), |
276 |
LoginResult::Complete => { |
|
15520 | 277 |
if let Some(client) = state.anteroom.remove_client(client_id) { |
15534 | 278 |
let is_checker = client.is_checker; |
15520 | 279 |
state.server.add_client(client_id, client); |
15534 | 280 |
if !is_checker { |
281 |
common::get_lobby_join_data(&state.server, response); |
|
282 |
} |
|
14693 | 283 |
} |
284 |
} |
|
285 |
LoginResult::Exit => { |
|
15520 | 286 |
state.anteroom.remove_client(client_id); |
14696 | 287 |
response.remove_client(client_id); |
14693 | 288 |
} |
289 |
} |
|
15520 | 290 |
} else if state.server.has_client(client_id) { |
14693 | 291 |
match message { |
15075 | 292 |
HwProtocolMessage::Quit(Some(msg)) => { |
15520 | 293 |
common::remove_client( |
294 |
&mut state.server, |
|
295 |
response, |
|
296 |
"User quit: ".to_string() + &msg, |
|
297 |
); |
|
14693 | 298 |
} |
15075 | 299 |
HwProtocolMessage::Quit(None) => { |
15520 | 300 |
common::remove_client(&mut state.server, response, "User quit".to_string()); |
14693 | 301 |
} |
15075 | 302 |
HwProtocolMessage::Info(nick) => { |
15520 | 303 |
if let Some(client) = state.server.find_client(&nick) { |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
304 |
let admin_sign = if client.is_admin() { "@" } else { "" }; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
305 |
let master_sign = if client.is_master() { "+" } else { "" }; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
306 |
let room_info = match client.room_id { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
307 |
Some(room_id) => { |
15520 | 308 |
let room = state.server.room(room_id); |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
309 |
let status = match room.game_info { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
310 |
Some(_) if client.teams_in_game == 0 => "(spectating)", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
311 |
Some(_) => "(playing)", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
312 |
None => "", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
313 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
314 |
format!( |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
315 |
"[{}{}room {}]{}", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
316 |
admin_sign, master_sign, room.name, status |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
317 |
) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
318 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
319 |
None => format!("[{}lobby]", admin_sign), |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
320 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
321 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
322 |
let info = vec![ |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
323 |
client.nick.clone(), |
14802 | 324 |
"[]".to_string(), |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
325 |
utils::protocol_version_string(client.protocol_number).to_string(), |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
326 |
room_info, |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
327 |
]; |
14802 | 328 |
response.add(Info(info).send_self()) |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
329 |
} else { |
15441 | 330 |
response.add(server_chat(USER_OFFLINE.to_string()).send_self()) |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
331 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
332 |
} |
15075 | 333 |
HwProtocolMessage::ToggleServerRegisteredOnly => { |
15520 | 334 |
if !state.server.is_admin(client_id) { |
15441 | 335 |
response.warn(ACCESS_DENIED); |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
336 |
} else { |
15520 | 337 |
state |
338 |
.server |
|
339 |
.set_is_registered_only(!state.server.is_registered_only()); |
|
340 |
let msg = if state.server.is_registered_only() { |
|
15441 | 341 |
REGISTERED_ONLY_ENABLED |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
342 |
} else { |
15441 | 343 |
REGISTERED_ONLY_DISABLED |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
344 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
345 |
response.add(server_chat(msg.to_string()).send_all()); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
346 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
347 |
} |
15075 | 348 |
HwProtocolMessage::Global(msg) => { |
15520 | 349 |
if !state.server.is_admin(client_id) { |
15441 | 350 |
response.warn(ACCESS_DENIED); |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
351 |
} else { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
352 |
response.add(global_chat(msg).send_all()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
353 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
354 |
} |
15075 | 355 |
HwProtocolMessage::SuperPower => { |
15520 | 356 |
if state.server.enable_super_power(client_id) { |
15518 | 357 |
response.add(server_chat(SUPER_POWER.to_string()).send_self()) |
358 |
} else { |
|
15441 | 359 |
response.warn(ACCESS_DENIED); |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
360 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
361 |
} |
15831 | 362 |
#[allow(unused_variables)] |
15075 | 363 |
HwProtocolMessage::Watch(id) => { |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
364 |
#[cfg(feature = "official-server")] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
365 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
366 |
response.request_io(IoTask::GetReplay { id }) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
367 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
368 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
369 |
#[cfg(not(feature = "official-server"))] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
370 |
{ |
15441 | 371 |
response.warn(REPLAY_NOT_SUPPORTED); |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
372 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
373 |
} |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
374 |
_ => match state.server.get_room_control(client_id) { |
15520 | 375 |
None => inlobby::handle(&mut state.server, client_id, response, message), |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
376 |
Some(control) => inroom::handle(control, response, message), |
14693 | 377 |
}, |
378 |
} |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
379 |
} |
14693 | 380 |
} |
12149 | 381 |
} |
382 |
} |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
383 |
|
15176 | 384 |
pub fn handle_client_accept( |
15520 | 385 |
state: &mut ServerState, |
15176 | 386 |
client_id: ClientId, |
387 |
response: &mut Response, |
|
15517 | 388 |
addr: [u8; 4], |
15176 | 389 |
is_local: bool, |
15831 | 390 |
) -> bool { |
15517 | 391 |
let ban_reason = Some(addr) |
392 |
.filter(|_| !is_local) |
|
15520 | 393 |
.and_then(|a| state.anteroom.find_ip_ban(a)); |
15517 | 394 |
if let Some(reason) = ban_reason { |
395 |
response.add(HwServerMessage::Bye(reason).send_self()); |
|
396 |
response.remove_client(client_id); |
|
15831 | 397 |
false |
15517 | 398 |
} else { |
399 |
let mut salt = [0u8; 18]; |
|
400 |
thread_rng().fill_bytes(&mut salt); |
|
14693 | 401 |
|
15520 | 402 |
state |
15517 | 403 |
.anteroom |
404 |
.add_client(client_id, encode(&salt), is_local); |
|
14693 | 405 |
|
15831 | 406 |
response.add( |
407 |
HwServerMessage::Connected(utils::SERVER_MESSAGE.to_owned(), utils::SERVER_VERSION) |
|
408 |
.send_self(), |
|
409 |
); |
|
410 |
true |
|
15517 | 411 |
} |
14693 | 412 |
} |
413 |
||
15520 | 414 |
pub fn handle_client_loss(state: &mut ServerState, client_id: ClientId, response: &mut Response) { |
415 |
if state.anteroom.remove_client(client_id).is_none() { |
|
416 |
common::remove_client(&mut state.server, response, "Connection reset".to_string()); |
|
14696 | 417 |
} |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
418 |
} |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
419 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
420 |
pub fn handle_io_result( |
15520 | 421 |
state: &mut ServerState, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
422 |
client_id: ClientId, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
423 |
response: &mut Response, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
424 |
io_result: IoResult, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
425 |
) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
426 |
match io_result { |
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
427 |
IoResult::AccountRegistered(is_registered) => { |
15520 | 428 |
if !is_registered && state.server.is_registered_only() { |
15441 | 429 |
response.add(Bye(REGISTRATION_REQUIRED.to_string()).send_self()); |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
430 |
response.remove_client(client_id); |
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
431 |
} else if is_registered { |
15532 | 432 |
let client = &state.anteroom.clients[client_id]; |
433 |
response.add(AskPassword(client.server_salt.clone()).send_self()); |
|
15520 | 434 |
} else if let Some(client) = state.anteroom.remove_client(client_id) { |
435 |
state.server.add_client(client_id, client); |
|
436 |
common::get_lobby_join_data(&state.server, response); |
|
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
437 |
} |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
438 |
} |
15532 | 439 |
IoResult::Account(None) => { |
440 |
response.add(Bye(AUTHENTICATION_FAILED.to_string()).send_self()); |
|
441 |
response.remove_client(client_id); |
|
442 |
} |
|
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
443 |
IoResult::Account(Some(info)) => { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15102
diff
changeset
|
444 |
response.add(ServerAuth(format!("{:x}", info.server_hash)).send_self()); |
15520 | 445 |
if let Some(mut client) = state.anteroom.remove_client(client_id) { |
15441 | 446 |
client.is_registered = info.is_registered; |
447 |
client.is_admin = info.is_admin; |
|
448 |
client.is_contributor = info.is_contributor; |
|
15520 | 449 |
state.server.add_client(client_id, client); |
450 |
common::get_lobby_join_data(&state.server, response); |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
451 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
452 |
} |
15532 | 453 |
IoResult::CheckerAccount { is_registered } => { |
454 |
if is_registered { |
|
455 |
if let Some(client) = state.anteroom.remove_client(client_id) { |
|
456 |
state.server.add_client(client_id, client); |
|
457 |
response.add(LogonPassed.send_self()); |
|
458 |
} |
|
459 |
} else { |
|
460 |
response.add(Bye(NO_CHECKER_RIGHTS.to_string()).send_self()); |
|
461 |
response.remove_client(client_id); |
|
462 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
463 |
} |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
464 |
IoResult::Replay(Some(replay)) => { |
15520 | 465 |
let client = state.server.client(client_id); |
15441 | 466 |
let protocol = client.protocol_number; |
14907 | 467 |
let start_msg = if protocol < 58 { |
15441 | 468 |
RoomJoined(vec![client.nick.clone()]) |
14907 | 469 |
} else { |
470 |
ReplayStart |
|
471 |
}; |
|
472 |
response.add(start_msg.send_self()); |
|
473 |
||
15543 | 474 |
common::get_room_config_impl(&replay.config, Destination::ToSelf, response); |
475 |
common::get_teams(replay.teams.iter(), Destination::ToSelf, response); |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
476 |
response.add(RunGame.send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
477 |
response.add(ForwardEngineMessage(replay.message_log).send_self()); |
14907 | 478 |
|
479 |
if protocol < 58 { |
|
480 |
response.add(Kicked.send_self()); |
|
481 |
} |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
482 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
483 |
IoResult::Replay(None) => { |
15441 | 484 |
response.warn(REPLAY_LOAD_FAILED); |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
485 |
} |
14780 | 486 |
IoResult::SaveRoom(_, true) => { |
15441 | 487 |
response.add(server_chat(ROOM_CONFIG_SAVED.to_string()).send_self()); |
14780 | 488 |
} |
489 |
IoResult::SaveRoom(_, false) => { |
|
15441 | 490 |
response.warn(ROOM_CONFIG_SAVE_FAILED); |
14780 | 491 |
} |
492 |
IoResult::LoadRoom(room_id, Some(contents)) => { |
|
15526
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
493 |
match state.server.set_room_saves(room_id, &contents) { |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
494 |
Ok(_) => response.add(server_chat(ROOM_CONFIG_LOADED.to_string()).send_self()), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
495 |
Err(e) => { |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
496 |
warn!("Error while deserializing the room configs: {}", e); |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15523
diff
changeset
|
497 |
response.warn(ROOM_CONFIG_DESERIALIZE_FAILED); |
14780 | 498 |
} |
499 |
} |
|
500 |
} |
|
14781 | 501 |
IoResult::LoadRoom(_, None) => { |
15441 | 502 |
response.warn(ROOM_CONFIG_LOAD_FAILED); |
14780 | 503 |
} |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
504 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
505 |
} |
15163 | 506 |
|
507 |
#[cfg(test)] |
|
508 |
mod test { |
|
509 |
use super::Sha1Digest; |
|
510 |
||
511 |
#[test] |
|
512 |
fn hash_cmp_test() { |
|
513 |
let hash = Sha1Digest([ |
|
514 |
0x37, 0xC4, 0x9F, 0x5C, 0xC3, 0xC9, 0xDB, 0xFC, 0x54, 0xAC, 0x22, 0x04, 0xF6, 0x12, |
|
515 |
0x9A, 0xED, 0x69, 0xB1, 0xC4, 0x5C, |
|
516 |
]); |
|
517 |
||
518 |
assert_eq!(hash, &format!("{:x}", hash)[..]); |
|
519 |
} |
|
520 |
} |