author | alfadur |
Sat, 22 Feb 2025 19:39:31 +0300 | |
changeset 16120 | 5febd2bc5372 |
parent 16019 | c40f5e27aaf0 |
permissions | -rw-r--r-- |
15554 | 1 |
use super::strings::*; |
15826 | 2 |
use crate::handlers::actions::ToPendingMessage; |
13666 | 3 |
use crate::{ |
15095 | 4 |
core::{ |
15542 | 5 |
anteroom::{HwAnteroom, HwAnteroomClient}, |
15096 | 6 |
client::HwClient, |
15542 | 7 |
server::HwServer, |
15096 | 8 |
types::ClientId, |
14714 | 9 |
}, |
15096 | 10 |
utils::is_name_illegal, |
13419 | 11 |
}; |
15826 | 12 |
use hedgewars_network_protocol::messages::{ |
13 |
HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*, |
|
14 |
}; |
|
14478 | 15 |
use log::*; |
14714 | 16 |
use std::{ |
17 |
fmt::{Formatter, LowerHex}, |
|
18 |
num::NonZeroU16, |
|
19 |
}; |
|
13774 | 20 |
|
14714 | 21 |
pub enum LoginResult { |
22 |
Unchanged, |
|
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
23 |
Complete(HwAnteroomClient), |
14714 | 24 |
Exit, |
25 |
} |
|
26 |
||
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
27 |
fn get_completion_result( |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
28 |
anteroom: &mut HwAnteroom, |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
29 |
client_id: ClientId, |
14802 | 30 |
response: &mut super::Response, |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
31 |
) -> LoginResult { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
32 |
#[cfg(feature = "official-server")] |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
33 |
{ |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
34 |
let client = anteroom.get_client(client_id); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
35 |
response.request_io(super::IoTask::CheckRegistered { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
36 |
nick: client.nick.as_ref().unwrap().clone(), |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
37 |
}); |
15554 | 38 |
LoginResult::Unchanged |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
39 |
} |
14802 | 40 |
|
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
41 |
#[cfg(not(feature = "official-server"))] |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
42 |
{ |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
43 |
LoginResult::Complete(anteroom.remove_client(client_id).unwrap()) |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
44 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
45 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
46 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
47 |
pub fn handle( |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
48 |
anteroom: &mut HwAnteroom, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
49 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
50 |
response: &mut super::Response, |
15096 | 51 |
message: HwProtocolMessage, |
14714 | 52 |
) -> LoginResult { |
16019
c40f5e27aaf0
fix connection errors carrying over to new clients
alfadur
parents:
15967
diff
changeset
|
53 |
//todo!("Handle parsing of empty nicks") |
12147 | 54 |
match message { |
15096 | 55 |
HwProtocolMessage::Quit(_) => { |
14714 | 56 |
response.add(Bye("User quit".to_string()).send_self()); |
57 |
LoginResult::Exit |
|
58 |
} |
|
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
59 |
HwProtocolMessage::Nick(nick, token) => { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
60 |
if anteroom.nick_taken(&nick) { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
61 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
62 |
return LoginResult::Unchanged; |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
63 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
64 |
let reconnect = token |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
65 |
.map(|t| anteroom.get_nick_token(&nick) == Some(&t[..])) |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
66 |
.unwrap_or(false); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
67 |
let client = anteroom.get_client_mut(client_id); |
14891 | 68 |
|
14802 | 69 |
if client.nick.is_some() { |
15554 | 70 |
response.error(NICKNAME_PROVIDED); |
14714 | 71 |
LoginResult::Unchanged |
14478 | 72 |
} else if is_name_illegal(&nick) { |
15554 | 73 |
response.add(Bye(ILLEGAL_CLIENT_NAME.to_string()).send_self()); |
14714 | 74 |
LoginResult::Exit |
14478 | 75 |
} else { |
14714 | 76 |
client.nick = Some(nick.clone()); |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
77 |
let protocol_number = client.protocol_number; |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
78 |
if reconnect { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
79 |
client.is_registered = reconnect; |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
80 |
} else if let Some(token) = anteroom.register_nick_token(&nick) { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
81 |
response.add(Token(token.to_string()).send_self()); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
82 |
} |
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 |
|
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
85 |
if protocol_number.is_some() { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
86 |
get_completion_result(anteroom, client_id, 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) => { |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
93 |
let client = anteroom.get_client_mut(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() { |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
105 |
get_completion_result(anteroom, client_id, 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) => { |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
113 |
let client = anteroom.get_client(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) => { |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
129 |
let client = anteroom.get_client_mut(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()); |
|
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
148 |
anteroom.remember_nick(nick); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
16019
diff
changeset
|
149 |
LoginResult::Complete(anteroom.remove_client(client_id).unwrap()) |
15554 | 150 |
} |
14802 | 151 |
} |
13771 | 152 |
} |
14714 | 153 |
_ => { |
15554 | 154 |
warn!("Incorrect command in anteroom"); |
14714 | 155 |
LoginResult::Unchanged |
156 |
} |
|
12147 | 157 |
} |
158 |
} |