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