rust/hedgewars-server/src/handlers/inanteroom.rs
author Wuzzy <Wuzzy@disroot.org>
Wed, 28 Jun 2023 18:57:00 +0200
changeset 15967 f362e82cddcf
parent 15937 e514ceb5e7d6
child 15990 c40f5e27aaf0
permissions -rw-r--r--
CS: Disable game flags incompatible with this script
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
     1
use super::strings::*;
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15540
diff changeset
     2
use crate::handlers::actions::ToPendingMessage;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13478
diff changeset
     3
use crate::{
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14870
diff changeset
     4
    core::{
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
     5
        anteroom::{HwAnteroom, HwAnteroomClient},
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     6
        client::HwClient,
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
     7
        server::HwServer,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     8
        types::ClientId,
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
     9
    },
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    10
    utils::is_name_illegal,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    11
};
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15540
diff changeset
    12
use hedgewars_network_protocol::messages::{
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15540
diff changeset
    13
    HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*,
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15540
diff changeset
    14
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    15
use log::*;
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    16
use std::{
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    17
    fmt::{Formatter, LowerHex},
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    18
    num::NonZeroU16,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    19
};
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    20
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    21
pub enum LoginResult {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    22
    Unchanged,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    23
    Complete,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    24
    Exit,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    25
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    26
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    27
fn completion_result<'a, I>(
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    28
    mut other_clients: I,
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
    29
    client: &mut HwAnteroomClient,
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    30
    response: &mut super::Response,
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    31
) -> LoginResult
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    32
where
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
    33
    I: Iterator<Item = &'a HwClient>,
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    34
{
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    35
    let has_nick_clash = other_clients.any(|c| c.nick == *client.nick.as_ref().unwrap());
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    36
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    37
    if has_nick_clash {
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    38
        client.nick = None;
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    39
        response.add(Notice("NickAlreadyInUse".to_string()).send_self());
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    40
        LoginResult::Unchanged
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    41
    } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    42
        #[cfg(feature = "official-server")]
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    43
        {
15103
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    44
            response.request_io(super::IoTask::CheckRegistered {
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    45
                nick: client.nick.as_ref().unwrap().clone(),
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    46
            });
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    47
            LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    48
        }
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    49
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    50
        #[cfg(not(feature = "official-server"))]
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    51
        {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    52
            LoginResult::Complete
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    53
        }
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    54
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    55
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    56
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    57
pub fn handle(
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
    58
    server_state: &mut super::ServerState,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    59
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    60
    response: &mut super::Response,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    61
    message: HwProtocolMessage,
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    62
) -> LoginResult {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    63
    match message {
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    64
        HwProtocolMessage::Quit(_) => {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    65
            response.add(Bye("User quit".to_string()).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    66
            LoginResult::Exit
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    67
        }
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    68
        HwProtocolMessage::Nick(nick) => {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
    69
            let client = &mut server_state.anteroom.clients[client_id];
14870
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14781
diff changeset
    70
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    71
            if client.nick.is_some() {
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    72
                response.error(NICKNAME_PROVIDED);
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    73
                LoginResult::Unchanged
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    74
            } else if is_name_illegal(&nick) {
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    75
                response.add(Bye(ILLEGAL_CLIENT_NAME.to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    76
                LoginResult::Exit
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    77
            } else {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    78
                client.nick = Some(nick.clone());
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    79
                response.add(Nick(nick).send_self());
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    80
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    81
                if client.protocol_number.is_some() {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
    82
                    completion_result(server_state.server.iter_clients(), client, response)
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    83
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    84
                    LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    85
                }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    86
            }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    87
        }
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    88
        HwProtocolMessage::Proto(proto) => {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
    89
            let client = &mut server_state.anteroom.clients[client_id];
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    90
            if client.protocol_number.is_some() {
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    91
                response.error(PROTOCOL_PROVIDED);
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    92
                LoginResult::Unchanged
15540
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15536
diff changeset
    93
            } else if proto < 48 {
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    94
                response.add(Bye(PROTOCOL_TOO_OLD.to_string()).send_self());
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
    95
                LoginResult::Exit
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    96
            } else {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    97
                client.protocol_number = NonZeroU16::new(proto);
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    98
                response.add(Proto(proto).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    99
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   100
                if client.nick.is_some() {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
   101
                    completion_result(server_state.server.iter_clients(), client, response)
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   102
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   103
                    LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   104
                }
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   105
            }
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   106
        }
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   107
        #[cfg(feature = "official-server")]
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   108
        HwProtocolMessage::Password(hash, salt) => {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
   109
            let client = &server_state.anteroom.clients[client_id];
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   110
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   111
            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: 14696
diff changeset
   112
                response.request_io(super::IoTask::GetAccount {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   113
                    nick: nick.clone(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   114
                    protocol: protocol.get(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   115
                    server_salt: client.server_salt.clone(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   116
                    client_salt: salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   117
                    password_hash: hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   118
                });
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   119
            };
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   120
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   121
            LoginResult::Unchanged
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   122
        }
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   123
        #[cfg(feature = "official-server")]
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   124
        HwProtocolMessage::Checker(protocol, nick, password) => {
15520
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15103
diff changeset
   125
            let client = &mut server_state.anteroom.clients[client_id];
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   126
            if protocol == 0 {
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   127
                response.error("Bad number.");
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   128
                LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   129
            } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   130
                client.protocol_number = NonZeroU16::new(protocol);
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   131
                client.is_checker = true;
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   132
                #[cfg(not(feature = "official-server"))]
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   133
                {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   134
                    response.request_io(super::IoTask::GetCheckerAccount {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   135
                        nick: nick,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   136
                        password: password,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   137
                    });
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   138
                    LoginResult::Unchanged
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   139
                }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   140
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   141
                #[cfg(feature = "official-server")]
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   142
                {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   143
                    response.add(LogonPassed.send_self());
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   144
                    LoginResult::Complete
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   145
                }
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   146
            }
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   147
        }
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   148
        _ => {
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15520
diff changeset
   149
            warn!("Incorrect command in anteroom");
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   150
            LoginResult::Unchanged
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   151
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   152
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   153
}