author | alfadur <mail@none> |
Mon, 30 Dec 2019 17:25:44 +0300 | |
changeset 15556 | bb93e9642b5b |
parent 15554 | f1205f33bf5b |
child 15565 | 02f648f7cbe1 |
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 |
}, |
15554 | 118 |
GetCheckerAccount { |
119 |
nick: String, |
|
120 |
password: String, |
|
121 |
}, |
|
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
122 |
GetReplay { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
123 |
id: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
124 |
}, |
14801 | 125 |
SaveRoom { |
126 |
room_id: RoomId, |
|
127 |
filename: String, |
|
128 |
contents: String, |
|
129 |
}, |
|
130 |
LoadRoom { |
|
131 |
room_id: RoomId, |
|
14802 | 132 |
filename: String, |
133 |
}, |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
134 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
135 |
|
15123 | 136 |
#[derive(Debug)] |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
137 |
pub enum IoResult { |
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15123
diff
changeset
|
138 |
AccountRegistered(bool), |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
139 |
Account(Option<AccountInfo>), |
15554 | 140 |
CheckerAccount { is_registered: bool }, |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
141 |
Replay(Option<Replay>), |
14801 | 142 |
SaveRoom(RoomId, bool), |
14802 | 143 |
LoadRoom(RoomId, Option<String>), |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
144 |
} |
14714 | 145 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
146 |
pub struct Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
147 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
148 |
messages: Vec<PendingMessage>, |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
149 |
io_tasks: Vec<IoTask>, |
14717 | 150 |
removed_clients: Vec<ClientId>, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
151 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
152 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
153 |
impl Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
154 |
pub fn new(client_id: ClientId) -> Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
155 |
Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
156 |
client_id, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
157 |
messages: vec![], |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
158 |
io_tasks: vec![], |
14717 | 159 |
removed_clients: vec![], |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
160 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
161 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
162 |
|
14693
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 is_empty(&self) -> bool { |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
165 |
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
|
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] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
169 |
pub fn len(&self) -> usize { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
170 |
self.messages.len() |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
171 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
172 |
|
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 client_id(&self) -> ClientId { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
175 |
self.client_id |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
176 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
177 |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
178 |
#[inline] |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
179 |
pub fn add(&mut self, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
180 |
self.messages.push(message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
181 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
182 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
183 |
#[inline] |
15463 | 184 |
pub fn warn(&mut self, message: &str) { |
185 |
self.add(Warning(message.to_string()).send_self()); |
|
186 |
} |
|
187 |
||
188 |
#[inline] |
|
15465 | 189 |
pub fn error(&mut self, message: &str) { |
190 |
self.add(Error(message.to_string()).send_self()); |
|
191 |
} |
|
192 |
||
193 |
#[inline] |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
194 |
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
|
195 |
self.io_tasks.push(task) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
196 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
197 |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
198 |
pub fn extract_messages<'a, 'b: 'a>( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
199 |
&'b mut self, |
15096 | 200 |
server: &'a HwServer, |
201 |
) -> impl Iterator<Item = (Vec<ClientId>, HwServerMessage)> + 'a { |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
202 |
let client_id = self.client_id; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
203 |
self.messages.drain(..).map(move |m| { |
14809 | 204 |
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
|
205 |
(ids, m.message) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
206 |
}) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
207 |
} |
14717 | 208 |
|
209 |
pub fn remove_client(&mut self, client_id: ClientId) { |
|
210 |
self.removed_clients.push(client_id); |
|
211 |
} |
|
212 |
||
213 |
pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ { |
|
214 |
self.removed_clients.drain(..) |
|
215 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
216 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
217 |
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
|
218 |
self.io_tasks.drain(..) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
219 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
220 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
221 |
|
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
222 |
impl Extend<PendingMessage> for Response { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
223 |
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
|
224 |
for msg in iter { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
225 |
self.add(msg) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
226 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
227 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
228 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
229 |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
230 |
fn get_recipients( |
15096 | 231 |
server: &HwServer, |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
232 |
client_id: ClientId, |
14809 | 233 |
destination: Destination, |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
234 |
) -> Vec<ClientId> { |
14809 | 235 |
match destination { |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
236 |
Destination::ToSelf => vec![client_id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
237 |
Destination::ToId(id) => vec![id], |
14809 | 238 |
Destination::ToIds(ids) => ids, |
239 |
Destination::ToAll { group, skip_self } => { |
|
14810 | 240 |
let mut ids: Vec<_> = match group { |
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
241 |
DestinationGroup::All => server.iter_client_ids().collect(), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
242 |
DestinationGroup::Lobby => server.lobby_client_ids().collect(), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
243 |
DestinationGroup::Protocol(proto) => server.protocol_client_ids(proto).collect(), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
244 |
DestinationGroup::Room(id) => server.room_client_ids(id).collect(), |
14809 | 245 |
}; |
246 |
||
247 |
if skip_self { |
|
248 |
if let Some(index) = ids.iter().position(|id| *id == client_id) { |
|
249 |
ids.remove(index); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
ids |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
254 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
255 |
} |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
256 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
257 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
258 |
pub fn handle( |
15542 | 259 |
state: &mut ServerState, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
260 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
261 |
response: &mut Response, |
15096 | 262 |
message: HwProtocolMessage, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
263 |
) { |
12149 | 264 |
match message { |
15096 | 265 |
HwProtocolMessage::Ping => response.add(Pong.send_self()), |
15134 | 266 |
HwProtocolMessage::Pong => (), |
14714 | 267 |
_ => { |
15542 | 268 |
if state.anteroom.clients.contains(client_id) { |
269 |
match inanteroom::handle(state, client_id, response, message) { |
|
14714 | 270 |
LoginResult::Unchanged => (), |
271 |
LoginResult::Complete => { |
|
15542 | 272 |
if let Some(client) = state.anteroom.remove_client(client_id) { |
15556 | 273 |
let is_checker = client.is_checker; |
15542 | 274 |
state.server.add_client(client_id, client); |
15556 | 275 |
if !is_checker { |
276 |
common::get_lobby_join_data(&state.server, response); |
|
277 |
} |
|
14714 | 278 |
} |
279 |
} |
|
280 |
LoginResult::Exit => { |
|
15542 | 281 |
state.anteroom.remove_client(client_id); |
14717 | 282 |
response.remove_client(client_id); |
14714 | 283 |
} |
284 |
} |
|
15542 | 285 |
} else if state.server.has_client(client_id) { |
14714 | 286 |
match message { |
15096 | 287 |
HwProtocolMessage::Quit(Some(msg)) => { |
15542 | 288 |
common::remove_client( |
289 |
&mut state.server, |
|
290 |
response, |
|
291 |
"User quit: ".to_string() + &msg, |
|
292 |
); |
|
14714 | 293 |
} |
15096 | 294 |
HwProtocolMessage::Quit(None) => { |
15542 | 295 |
common::remove_client(&mut state.server, response, "User quit".to_string()); |
14714 | 296 |
} |
15096 | 297 |
HwProtocolMessage::Info(nick) => { |
15542 | 298 |
if let Some(client) = state.server.find_client(&nick) { |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
299 |
let admin_sign = if client.is_admin() { "@" } else { "" }; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
300 |
let master_sign = if client.is_master() { "+" } else { "" }; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
301 |
let room_info = match client.room_id { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
302 |
Some(room_id) => { |
15542 | 303 |
let room = state.server.room(room_id); |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
304 |
let status = match room.game_info { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
305 |
Some(_) if client.teams_in_game == 0 => "(spectating)", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
306 |
Some(_) => "(playing)", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
307 |
None => "", |
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 |
format!( |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
310 |
"[{}{}room {}]{}", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
311 |
admin_sign, master_sign, room.name, status |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
312 |
) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
313 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
314 |
None => format!("[{}lobby]", admin_sign), |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
315 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
316 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
317 |
let info = vec![ |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
318 |
client.nick.clone(), |
14823 | 319 |
"[]".to_string(), |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
320 |
utils::protocol_version_string(client.protocol_number).to_string(), |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
321 |
room_info, |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
322 |
]; |
14823 | 323 |
response.add(Info(info).send_self()) |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
324 |
} else { |
15465 | 325 |
response.add(server_chat(USER_OFFLINE.to_string()).send_self()) |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
326 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
327 |
} |
15096 | 328 |
HwProtocolMessage::ToggleServerRegisteredOnly => { |
15542 | 329 |
if !state.server.is_admin(client_id) { |
15465 | 330 |
response.warn(ACCESS_DENIED); |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
331 |
} else { |
15542 | 332 |
state |
333 |
.server |
|
334 |
.set_is_registered_only(!state.server.is_registered_only()); |
|
335 |
let msg = if state.server.is_registered_only() { |
|
15465 | 336 |
REGISTERED_ONLY_ENABLED |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
337 |
} else { |
15465 | 338 |
REGISTERED_ONLY_DISABLED |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
339 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
340 |
response.add(server_chat(msg.to_string()).send_all()); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
341 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
342 |
} |
15096 | 343 |
HwProtocolMessage::Global(msg) => { |
15542 | 344 |
if !state.server.is_admin(client_id) { |
15465 | 345 |
response.warn(ACCESS_DENIED); |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
346 |
} else { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
347 |
response.add(global_chat(msg).send_all()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
348 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
349 |
} |
15096 | 350 |
HwProtocolMessage::SuperPower => { |
15542 | 351 |
if state.server.enable_super_power(client_id) { |
15540 | 352 |
response.add(server_chat(SUPER_POWER.to_string()).send_self()) |
353 |
} else { |
|
15465 | 354 |
response.warn(ACCESS_DENIED); |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
355 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
356 |
} |
15096 | 357 |
HwProtocolMessage::Watch(id) => { |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
358 |
#[cfg(feature = "official-server")] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
359 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
360 |
response.request_io(IoTask::GetReplay { id }) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
361 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
362 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
363 |
#[cfg(not(feature = "official-server"))] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
364 |
{ |
15465 | 365 |
response.warn(REPLAY_NOT_SUPPORTED); |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
366 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
367 |
} |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
368 |
_ => match state.server.get_room_control(client_id) { |
15542 | 369 |
None => inlobby::handle(&mut state.server, client_id, response, message), |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
370 |
Some(control) => inroom::handle(control, response, message), |
14714 | 371 |
}, |
372 |
} |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
373 |
} |
14714 | 374 |
} |
12149 | 375 |
} |
376 |
} |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
377 |
|
15197 | 378 |
pub fn handle_client_accept( |
15542 | 379 |
state: &mut ServerState, |
15197 | 380 |
client_id: ClientId, |
381 |
response: &mut Response, |
|
15539 | 382 |
addr: [u8; 4], |
15197 | 383 |
is_local: bool, |
384 |
) { |
|
15539 | 385 |
let ban_reason = Some(addr) |
386 |
.filter(|_| !is_local) |
|
15542 | 387 |
.and_then(|a| state.anteroom.find_ip_ban(a)); |
15539 | 388 |
if let Some(reason) = ban_reason { |
389 |
response.add(HwServerMessage::Bye(reason).send_self()); |
|
390 |
response.remove_client(client_id); |
|
391 |
} else { |
|
392 |
let mut salt = [0u8; 18]; |
|
393 |
thread_rng().fill_bytes(&mut salt); |
|
14714 | 394 |
|
15542 | 395 |
state |
15539 | 396 |
.anteroom |
397 |
.add_client(client_id, encode(&salt), is_local); |
|
14714 | 398 |
|
15539 | 399 |
response.add(HwServerMessage::Connected(utils::SERVER_VERSION).send_self()); |
400 |
} |
|
14714 | 401 |
} |
402 |
||
15542 | 403 |
pub fn handle_client_loss(state: &mut ServerState, client_id: ClientId, response: &mut Response) { |
404 |
if state.anteroom.remove_client(client_id).is_none() { |
|
405 |
common::remove_client(&mut state.server, response, "Connection reset".to_string()); |
|
14717 | 406 |
} |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
407 |
} |
14800
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 |
pub fn handle_io_result( |
15542 | 410 |
state: &mut ServerState, |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
411 |
client_id: ClientId, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
412 |
response: &mut Response, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
413 |
io_result: IoResult, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
414 |
) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
415 |
match io_result { |
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15123
diff
changeset
|
416 |
IoResult::AccountRegistered(is_registered) => { |
15542 | 417 |
if !is_registered && state.server.is_registered_only() { |
15465 | 418 |
response.add(Bye(REGISTRATION_REQUIRED.to_string()).send_self()); |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
419 |
response.remove_client(client_id); |
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15123
diff
changeset
|
420 |
} else if is_registered { |
15554 | 421 |
let client = &state.anteroom.clients[client_id]; |
422 |
response.add(AskPassword(client.server_salt.clone()).send_self()); |
|
15542 | 423 |
} else if let Some(client) = state.anteroom.remove_client(client_id) { |
424 |
state.server.add_client(client_id, client); |
|
425 |
common::get_lobby_join_data(&state.server, response); |
|
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15123
diff
changeset
|
426 |
} |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15123
diff
changeset
|
427 |
} |
15554 | 428 |
IoResult::Account(None) => { |
429 |
response.add(Bye(AUTHENTICATION_FAILED.to_string()).send_self()); |
|
430 |
response.remove_client(client_id); |
|
431 |
} |
|
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15123
diff
changeset
|
432 |
IoResult::Account(Some(info)) => { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15123
diff
changeset
|
433 |
response.add(ServerAuth(format!("{:x}", info.server_hash)).send_self()); |
15542 | 434 |
if let Some(mut client) = state.anteroom.remove_client(client_id) { |
15465 | 435 |
client.is_registered = info.is_registered; |
436 |
client.is_admin = info.is_admin; |
|
437 |
client.is_contributor = info.is_contributor; |
|
15542 | 438 |
state.server.add_client(client_id, client); |
439 |
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
|
440 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
441 |
} |
15554 | 442 |
IoResult::CheckerAccount { is_registered } => { |
443 |
if is_registered { |
|
444 |
if let Some(client) = state.anteroom.remove_client(client_id) { |
|
445 |
state.server.add_client(client_id, client); |
|
446 |
response.add(LogonPassed.send_self()); |
|
447 |
} |
|
448 |
} else { |
|
449 |
response.add(Bye(NO_CHECKER_RIGHTS.to_string()).send_self()); |
|
450 |
response.remove_client(client_id); |
|
451 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
452 |
} |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
453 |
IoResult::Replay(Some(replay)) => { |
15542 | 454 |
let client = state.server.client(client_id); |
15465 | 455 |
let protocol = client.protocol_number; |
14928 | 456 |
let start_msg = if protocol < 58 { |
15465 | 457 |
RoomJoined(vec![client.nick.clone()]) |
14928 | 458 |
} else { |
459 |
ReplayStart |
|
460 |
}; |
|
461 |
response.add(start_msg.send_self()); |
|
462 |
||
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
463 |
common::get_room_config_impl(&replay.config, client_id, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
464 |
common::get_teams(replay.teams.iter(), client_id, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
465 |
response.add(RunGame.send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
466 |
response.add(ForwardEngineMessage(replay.message_log).send_self()); |
14928 | 467 |
|
468 |
if protocol < 58 { |
|
469 |
response.add(Kicked.send_self()); |
|
470 |
} |
|
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
471 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
472 |
IoResult::Replay(None) => { |
15465 | 473 |
response.warn(REPLAY_LOAD_FAILED); |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
474 |
} |
14801 | 475 |
IoResult::SaveRoom(_, true) => { |
15465 | 476 |
response.add(server_chat(ROOM_CONFIG_SAVED.to_string()).send_self()); |
14801 | 477 |
} |
478 |
IoResult::SaveRoom(_, false) => { |
|
15465 | 479 |
response.warn(ROOM_CONFIG_SAVE_FAILED); |
14801 | 480 |
} |
481 |
IoResult::LoadRoom(room_id, Some(contents)) => { |
|
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
482 |
match state.server.set_room_saves(room_id, &contents) { |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
483 |
Ok(_) => response.add(server_chat(ROOM_CONFIG_LOADED.to_string()).send_self()), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
484 |
Err(e) => { |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
485 |
warn!("Error while deserializing the room configs: {}", e); |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15545
diff
changeset
|
486 |
response.warn(ROOM_CONFIG_DESERIALIZE_FAILED); |
14801 | 487 |
} |
488 |
} |
|
489 |
} |
|
14802 | 490 |
IoResult::LoadRoom(_, None) => { |
15465 | 491 |
response.warn(ROOM_CONFIG_LOAD_FAILED); |
14801 | 492 |
} |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
493 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
494 |
} |
15184 | 495 |
|
496 |
#[cfg(test)] |
|
497 |
mod test { |
|
498 |
use super::Sha1Digest; |
|
499 |
||
500 |
#[test] |
|
501 |
fn hash_cmp_test() { |
|
502 |
let hash = Sha1Digest([ |
|
503 |
0x37, 0xC4, 0x9F, 0x5C, 0xC3, 0xC9, 0xDB, 0xFC, 0x54, 0xAC, 0x22, 0x04, 0xF6, 0x12, |
|
504 |
0x9A, 0xED, 0x69, 0xB1, 0xC4, 0x5C, |
|
505 |
]); |
|
506 |
||
507 |
assert_eq!(hash, &format!("{:x}", hash)[..]); |
|
508 |
} |
|
509 |
} |