author | alfadur <mail@none> |
Wed, 06 Feb 2019 22:29:02 +0300 | |
changeset 14695 | f61ce544d436 |
parent 14691 | 9f98086de1b6 |
child 14698 | 6a2e13e36b7f |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13671 | 3 |
use crate::{ |
14462 | 4 |
protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
14695
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
5 |
server::{client::HWClient, core::HWServer, coretypes::ClientId}, |
14462 | 6 |
utils::is_name_illegal, |
13424 | 7 |
}; |
14462 | 8 |
use log::*; |
13805 | 9 |
#[cfg(feature = "official-server")] |
10 |
use openssl::sha::sha1; |
|
11 |
use std::fmt::{Formatter, LowerHex}; |
|
12 |
||
13 |
#[derive(PartialEq)] |
|
14 |
struct Sha1Digest([u8; 20]); |
|
15 |
||
16 |
impl LowerHex for Sha1Digest { |
|
17 |
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { |
|
18 |
for byte in &self.0 { |
|
19 |
write!(f, "{:02x}", byte)?; |
|
20 |
} |
|
21 |
Ok(()) |
|
22 |
} |
|
23 |
} |
|
24 |
||
25 |
#[cfg(feature = "official-server")] |
|
26 |
fn get_hash(client: &HWClient, salt1: &str, salt2: &str) -> Sha1Digest { |
|
14462 | 27 |
let s = format!( |
28 |
"{}{}{}{}{}", |
|
29 |
salt1, salt2, client.web_password, client.protocol_number, "!hedgewars" |
|
30 |
); |
|
13805 | 31 |
Sha1Digest(sha1(s.as_bytes())) |
32 |
} |
|
12152 | 33 |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
34 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
35 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
36 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
37 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
38 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
39 |
) { |
12152 | 40 |
match message { |
13421 | 41 |
HWProtocolMessage::Nick(nick) => { |
13671 | 42 |
let client = &mut server.clients[client_id]; |
43 |
debug!("{} {}", nick, is_name_illegal(&nick)); |
|
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
44 |
if client.room_id != None { |
13671 | 45 |
unreachable!() |
14462 | 46 |
} else if !client.nick.is_empty() { |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
47 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14462 | 48 |
} else if is_name_illegal(&nick) { |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
49 |
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 | 50 |
} else { |
13671 | 51 |
client.nick = nick.clone(); |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
52 |
response.add(Nick(nick).send_self()); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
53 |
|
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
54 |
if client.protocol_number > 0 { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14678
diff
changeset
|
55 |
super::common::process_login(server, response); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
56 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
57 |
} |
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 |
HWProtocolMessage::Proto(proto) => { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
60 |
let client = &mut server.clients[client_id]; |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
61 |
if client.protocol_number != 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
62 |
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
|
63 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
64 |
response.add(Error("Bad number.".to_string()).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
65 |
} else { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
66 |
client.protocol_number = proto; |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
67 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
68 |
|
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
69 |
if client.nick != "" { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14678
diff
changeset
|
70 |
super::common::process_login(server, response); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
71 |
} |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
72 |
} |
13803 | 73 |
} |
74 |
#[cfg(feature = "official-server")] |
|
13805 | 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); |
|
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
80 |
if client_hash == server_hash { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
81 |
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
|
82 |
//TODO enter lobby |
13805 | 83 |
} else { |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
84 |
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
|
85 |
} |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
86 |
} |
13805 | 87 |
#[cfg(feature = "official-server")] |
13803 | 88 |
HWProtocolMessage::Checker(protocol, nick, password) => { |
89 |
let c = &mut server.clients[client_id]; |
|
90 |
c.nick = nick; |
|
91 |
c.web_password = password; |
|
92 |
c.set_is_checker(true); |
|
93 |
} |
|
12152 | 94 |
_ => warn!("Incorrect command in logging-in state"), |
95 |
} |
|
96 |
} |