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