author | unc0rr |
Wed, 23 Jun 2021 23:41:51 +0200 | |
changeset 15826 | 747278149393 |
parent 15562 | 479911540e17 |
child 15853 | 7d0f747afcb8 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
15554 | 3 |
use super::strings::*; |
15826 | 4 |
use crate::handlers::actions::ToPendingMessage; |
13666 | 5 |
use crate::{ |
15095 | 6 |
core::{ |
15542 | 7 |
anteroom::{HwAnteroom, HwAnteroomClient}, |
15096 | 8 |
client::HwClient, |
15542 | 9 |
server::HwServer, |
15096 | 10 |
types::ClientId, |
14714 | 11 |
}, |
15096 | 12 |
utils::is_name_illegal, |
13419 | 13 |
}; |
15826 | 14 |
use hedgewars_network_protocol::messages::{ |
15 |
HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*, |
|
16 |
}; |
|
14478 | 17 |
use log::*; |
13774 | 18 |
#[cfg(feature = "official-server")] |
19 |
use openssl::sha::sha1; |
|
14714 | 20 |
use std::{ |
21 |
fmt::{Formatter, LowerHex}, |
|
22 |
num::NonZeroU16, |
|
23 |
}; |
|
13774 | 24 |
|
14714 | 25 |
pub enum LoginResult { |
26 |
Unchanged, |
|
27 |
Complete, |
|
28 |
Exit, |
|
29 |
} |
|
30 |
||
14802 | 31 |
fn completion_result<'a, I>( |
32 |
mut other_clients: I, |
|
15542 | 33 |
client: &mut HwAnteroomClient, |
14802 | 34 |
response: &mut super::Response, |
35 |
) -> LoginResult |
|
36 |
where |
|
15542 | 37 |
I: Iterator<Item = &'a HwClient>, |
14802 | 38 |
{ |
15554 | 39 |
let has_nick_clash = other_clients.any(|c| c.nick == *client.nick.as_ref().unwrap()); |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
40 |
|
14802 | 41 |
if has_nick_clash { |
15554 | 42 |
client.nick = None; |
43 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
|
44 |
LoginResult::Unchanged |
|
14802 | 45 |
} else { |
46 |
#[cfg(feature = "official-server")] |
|
47 |
{ |
|
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
48 |
response.request_io(super::IoTask::CheckRegistered { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
49 |
nick: client.nick.as_ref().unwrap().clone(), |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
50 |
}); |
14802 | 51 |
LoginResult::Unchanged |
52 |
} |
|
53 |
||
54 |
#[cfg(not(feature = "official-server"))] |
|
55 |
{ |
|
56 |
LoginResult::Complete |
|
57 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
58 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
59 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
60 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
61 |
pub fn handle( |
15542 | 62 |
server_state: &mut super::ServerState, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
63 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
64 |
response: &mut super::Response, |
15096 | 65 |
message: HwProtocolMessage, |
14714 | 66 |
) -> LoginResult { |
12147 | 67 |
match message { |
15096 | 68 |
HwProtocolMessage::Quit(_) => { |
14714 | 69 |
response.add(Bye("User quit".to_string()).send_self()); |
70 |
LoginResult::Exit |
|
71 |
} |
|
15096 | 72 |
HwProtocolMessage::Nick(nick) => { |
15542 | 73 |
let client = &mut server_state.anteroom.clients[client_id]; |
14891 | 74 |
|
14802 | 75 |
if client.nick.is_some() { |
15554 | 76 |
response.error(NICKNAME_PROVIDED); |
14714 | 77 |
LoginResult::Unchanged |
14478 | 78 |
} else if is_name_illegal(&nick) { |
15554 | 79 |
response.add(Bye(ILLEGAL_CLIENT_NAME.to_string()).send_self()); |
14714 | 80 |
LoginResult::Exit |
14478 | 81 |
} else { |
14714 | 82 |
client.nick = Some(nick.clone()); |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
83 |
response.add(Nick(nick).send_self()); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
84 |
|
14714 | 85 |
if client.protocol_number.is_some() { |
15542 | 86 |
completion_result(server_state.server.iter_clients(), client, response) |
14714 | 87 |
} else { |
88 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
89 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
90 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
91 |
} |
15096 | 92 |
HwProtocolMessage::Proto(proto) => { |
15542 | 93 |
let client = &mut server_state.anteroom.clients[client_id]; |
14714 | 94 |
if client.protocol_number.is_some() { |
15554 | 95 |
response.error(PROTOCOL_PROVIDED); |
14714 | 96 |
LoginResult::Unchanged |
15562 | 97 |
} else if proto < 48 { |
15554 | 98 |
response.add(Bye(PROTOCOL_TOO_OLD.to_string()).send_self()); |
99 |
LoginResult::Exit |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
100 |
} else { |
14714 | 101 |
client.protocol_number = NonZeroU16::new(proto); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
102 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
103 |
|
14714 | 104 |
if client.nick.is_some() { |
15542 | 105 |
completion_result(server_state.server.iter_clients(), client, response) |
14714 | 106 |
} else { |
107 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
108 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
109 |
} |
13771 | 110 |
} |
111 |
#[cfg(feature = "official-server")] |
|
15096 | 112 |
HwProtocolMessage::Password(hash, salt) => { |
15542 | 113 |
let client = &server_state.anteroom.clients[client_id]; |
13774 | 114 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
115 |
if let (Some(nick), Some(protocol)) = (client.nick.as_ref(), client.protocol_number) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
116 |
response.request_io(super::IoTask::GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
117 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
118 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
119 |
server_salt: client.server_salt.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
120 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
121 |
password_hash: hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
122 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
123 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
124 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
125 |
LoginResult::Unchanged |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
126 |
} |
13774 | 127 |
#[cfg(feature = "official-server")] |
15096 | 128 |
HwProtocolMessage::Checker(protocol, nick, password) => { |
15542 | 129 |
let client = &mut server_state.anteroom.clients[client_id]; |
14802 | 130 |
if protocol == 0 { |
15554 | 131 |
response.error("Bad number."); |
14802 | 132 |
LoginResult::Unchanged |
133 |
} else { |
|
134 |
client.protocol_number = NonZeroU16::new(protocol); |
|
135 |
client.is_checker = true; |
|
15554 | 136 |
#[cfg(not(feature = "official-server"))] |
137 |
{ |
|
138 |
response.request_io(super::IoTask::GetCheckerAccount { |
|
139 |
nick: nick, |
|
140 |
password: password, |
|
141 |
}); |
|
142 |
LoginResult::Unchanged |
|
143 |
} |
|
144 |
||
145 |
#[cfg(feature = "official-server")] |
|
146 |
{ |
|
147 |
response.add(LogonPassed.send_self()); |
|
148 |
LoginResult::Complete |
|
149 |
} |
|
14802 | 150 |
} |
13771 | 151 |
} |
14714 | 152 |
_ => { |
15554 | 153 |
warn!("Incorrect command in anteroom"); |
14714 | 154 |
LoginResult::Unchanged |
155 |
} |
|
12147 | 156 |
} |
157 |
} |