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