author | alfadur <mail@none> |
Sat, 02 Feb 2019 15:06:39 +0300 | |
changeset 14692 | 455865ccd36c |
parent 14478 | 98ef2913ec73 |
child 14693 | 6e6632068a33 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13666 | 3 |
use crate::{ |
14478 | 4 |
protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
13666 | 5 |
server::{ |
14478 | 6 |
actions::{Action, Action::*}, |
13774 | 7 |
client::HWClient, |
14395 | 8 |
core::HWServer, |
13666 | 9 |
coretypes::ClientId, |
10 |
}, |
|
14478 | 11 |
utils::is_name_illegal, |
13419 | 12 |
}; |
14478 | 13 |
use log::*; |
13774 | 14 |
#[cfg(feature = "official-server")] |
15 |
use openssl::sha::sha1; |
|
16 |
use std::fmt::{Formatter, LowerHex}; |
|
17 |
||
18 |
#[derive(PartialEq)] |
|
19 |
struct Sha1Digest([u8; 20]); |
|
20 |
||
21 |
impl LowerHex for Sha1Digest { |
|
22 |
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { |
|
23 |
for byte in &self.0 { |
|
24 |
write!(f, "{:02x}", byte)?; |
|
25 |
} |
|
26 |
Ok(()) |
|
27 |
} |
|
28 |
} |
|
29 |
||
30 |
#[cfg(feature = "official-server")] |
|
31 |
fn get_hash(client: &HWClient, salt1: &str, salt2: &str) -> Sha1Digest { |
|
14478 | 32 |
let s = format!( |
33 |
"{}{}{}{}{}", |
|
34 |
salt1, salt2, client.web_password, client.protocol_number, "!hedgewars" |
|
35 |
); |
|
13774 | 36 |
Sha1Digest(sha1(s.as_bytes())) |
37 |
} |
|
12147 | 38 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
39 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
40 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
41 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
42 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
43 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
44 |
) { |
12147 | 45 |
match message { |
13416 | 46 |
HWProtocolMessage::Nick(nick) => { |
13666 | 47 |
let client = &mut server.clients[client_id]; |
48 |
debug!("{} {}", nick, is_name_illegal(&nick)); |
|
49 |
let actions = if client.room_id != None { |
|
50 |
unreachable!() |
|
14478 | 51 |
} else if !client.nick.is_empty() { |
13666 | 52 |
vec![ProtocolError("Nickname already provided.".to_string())] |
14478 | 53 |
} else if is_name_illegal(&nick) { |
13666 | 54 |
vec![ByeClient("Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())] |
14478 | 55 |
} else { |
13666 | 56 |
client.nick = nick.clone(); |
14478 | 57 |
vec![Nick(nick).send_self().action(), CheckRegistered] |
13666 | 58 |
}; |
59 |
||
13419 | 60 |
server.react(client_id, actions); |
13771 | 61 |
} |
12147 | 62 |
HWProtocolMessage::Proto(proto) => { |
13666 | 63 |
let client = &mut server.clients[client_id]; |
64 |
let actions = if client.protocol_number != 0 { |
|
65 |
vec![ProtocolError("Protocol already known.".to_string())] |
|
14478 | 66 |
} else if proto == 0 { |
13666 | 67 |
vec![ProtocolError("Bad number.".to_string())] |
14478 | 68 |
} else { |
13666 | 69 |
client.protocol_number = proto; |
14478 | 70 |
vec![Proto(proto).send_self().action(), CheckRegistered] |
13666 | 71 |
}; |
13419 | 72 |
server.react(client_id, actions); |
13771 | 73 |
} |
74 |
#[cfg(feature = "official-server")] |
|
13774 | 75 |
HWProtocolMessage::Password(hash, salt) => { |
76 |
let c = &server.clients[client_id]; |
|
77 |
||
78 |
let client_hash = get_hash(c, &salt, &c.server_salt); |
|
79 |
let server_hash = get_hash(c, &c.server_salt, &salt); |
|
80 |
let actions = if client_hash == server_hash { |
|
14478 | 81 |
vec![ |
82 |
ServerAuth(format!("{:x}", server_hash)) |
|
83 |
.send_self() |
|
84 |
.action(), |
|
85 |
JoinLobby, |
|
86 |
] |
|
13774 | 87 |
} else { |
88 |
vec![ByeClient("Authentication failed".to_string())] |
|
89 |
}; |
|
90 |
server.react(client_id, actions); |
|
91 |
} |
|
92 |
#[cfg(feature = "official-server")] |
|
13771 | 93 |
HWProtocolMessage::Checker(protocol, nick, password) => { |
94 |
let c = &mut server.clients[client_id]; |
|
95 |
c.nick = nick; |
|
96 |
c.web_password = password; |
|
97 |
c.set_is_checker(true); |
|
98 |
} |
|
12147 | 99 |
_ => warn!("Incorrect command in logging-in state"), |
100 |
} |
|
101 |
} |