author | alfadur <mail@none> |
Wed, 06 Feb 2019 00:14:04 +0300 | |
changeset 14691 | 9f98086de1b6 |
parent 14688 | 932ff7683653 |
child 14695 | f61ce544d436 |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13671 | 3 |
use crate::{ |
14462 | 4 |
protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
13671 | 5 |
server::{ |
14462 | 6 |
actions::{Action, Action::*}, |
13805 | 7 |
client::HWClient, |
14379 | 8 |
core::HWServer, |
13671 | 9 |
coretypes::ClientId, |
10 |
}, |
|
14462 | 11 |
utils::is_name_illegal, |
13424 | 12 |
}; |
14462 | 13 |
use log::*; |
13805 | 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 { |
|
14462 | 32 |
let s = format!( |
33 |
"{}{}{}{}{}", |
|
34 |
salt1, salt2, client.web_password, client.protocol_number, "!hedgewars" |
|
35 |
); |
|
13805 | 36 |
Sha1Digest(sha1(s.as_bytes())) |
37 |
} |
|
12152 | 38 |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
39 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
40 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
41 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
42 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
43 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
44 |
) { |
12152 | 45 |
match message { |
13421 | 46 |
HWProtocolMessage::Nick(nick) => { |
13671 | 47 |
let client = &mut server.clients[client_id]; |
48 |
debug!("{} {}", nick, is_name_illegal(&nick)); |
|
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
49 |
if client.room_id != None { |
13671 | 50 |
unreachable!() |
14462 | 51 |
} else if !client.nick.is_empty() { |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
52 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14462 | 53 |
} else if is_name_illegal(&nick) { |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
54 |
super::common::remove_client(server, response, "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()) |
14462 | 55 |
} else { |
13671 | 56 |
client.nick = nick.clone(); |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
57 |
response.add(Nick(nick).send_self()); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
58 |
|
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
59 |
if client.protocol_number > 0 { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14678
diff
changeset
|
60 |
super::common::process_login(server, response); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
61 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
62 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
63 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
64 |
HWProtocolMessage::Proto(proto) => { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
65 |
let client = &mut server.clients[client_id]; |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
66 |
if client.protocol_number != 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
67 |
response.add(Error("Protocol already known.".to_string()).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
68 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
69 |
response.add(Error("Bad number.".to_string()).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
70 |
} else { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
71 |
client.protocol_number = proto; |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
72 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
73 |
|
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
74 |
if client.nick != "" { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14678
diff
changeset
|
75 |
super::common::process_login(server, response); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
76 |
} |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
77 |
} |
13803 | 78 |
} |
79 |
#[cfg(feature = "official-server")] |
|
13805 | 80 |
HWProtocolMessage::Password(hash, salt) => { |
81 |
let c = &server.clients[client_id]; |
|
82 |
||
83 |
let client_hash = get_hash(c, &salt, &c.server_salt); |
|
84 |
let server_hash = get_hash(c, &c.server_salt, &salt); |
|
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
85 |
if client_hash == server_hash { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
86 |
response.add(ServerAuth(format!("{:x}", server_hash)).send_self()); |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14678
diff
changeset
|
87 |
//TODO enter lobby |
13805 | 88 |
} else { |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
89 |
super::common::remove_client(server, response, "Authentication failed".to_string()) |
14691
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
90 |
} |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
91 |
} |
13805 | 92 |
#[cfg(feature = "official-server")] |
13803 | 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 |
} |
|
12152 | 99 |
_ => warn!("Incorrect command in logging-in state"), |
100 |
} |
|
101 |
} |