rust/hedgewars-server/src/handlers/inanteroom.rs
author Wuzzy <Wuzzy2@mail.ru>
Thu, 18 Jul 2019 01:46:40 +0200
changeset 15255 8dd3602a4500
parent 15108 823052e66611
permissions -rw-r--r--
Pagevideos: Create header object explicitly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13483
diff changeset
     3
use crate::{
15079
c5a6e8566425 shuffle server files
alfadur
parents: 14875
diff changeset
     4
    core::{
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     5
        client::HwClient,
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     6
        server::{HwAnteClient, HwAnteroom, HwServer},
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     7
        types::ClientId,
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
     8
    },
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     9
    protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*},
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    10
    utils::is_name_illegal,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    11
};
15079
c5a6e8566425 shuffle server files
alfadur
parents: 14875
diff changeset
    12
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    13
use log::*;
13805
0118b7412570 use openssl for password message handling
alfadur
parents: 13803
diff changeset
    14
#[cfg(feature = "official-server")]
0118b7412570 use openssl for password message handling
alfadur
parents: 13803
diff changeset
    15
use openssl::sha::sha1;
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    16
use std::{
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    17
    fmt::{Formatter, LowerHex},
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    18
    num::NonZeroU16,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    19
};
13805
0118b7412570 use openssl for password message handling
alfadur
parents: 13803
diff changeset
    20
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    21
pub enum LoginResult {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    22
    Unchanged,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    23
    Complete,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    24
    Exit,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    25
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    26
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    27
fn completion_result<'a, I>(
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    28
    mut other_clients: I,
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    29
    client: &mut HwAnteClient,
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    30
    response: &mut super::Response,
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    31
) -> LoginResult
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    32
where
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    33
    I: Iterator<Item = (ClientId, &'a HwClient)>,
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    34
{
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    35
    let has_nick_clash =
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    36
        other_clients.any(|(_, c)| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap());
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
    37
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    38
    if has_nick_clash {
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    39
        if client.protocol_number.unwrap().get() < 38 {
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    40
            response.add(Bye("User quit: Nickname is already in use".to_string()).send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    41
            LoginResult::Exit
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    42
        } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    43
            client.nick = None;
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    44
            response.add(Notice("NickAlreadyInUse".to_string()).send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    45
            LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    46
        }
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    47
    } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    48
        #[cfg(feature = "official-server")]
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    49
        {
15108
823052e66611 check for account existence before asking passwords
alfadur
parents: 15080
diff changeset
    50
            response.request_io(super::IoTask::CheckRegistered {
823052e66611 check for account existence before asking passwords
alfadur
parents: 15080
diff changeset
    51
                nick: client.nick.as_ref().unwrap().clone(),
823052e66611 check for account existence before asking passwords
alfadur
parents: 15080
diff changeset
    52
            });
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    53
            LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    54
        }
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    55
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    56
        #[cfg(not(feature = "official-server"))]
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    57
        {
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    58
            LoginResult::Complete
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    59
        }
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
    60
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
    61
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
    62
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    63
pub fn handle(
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    64
    server: &mut HwServer,
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    65
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14462
diff changeset
    66
    response: &mut super::Response,
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    67
    message: HwProtocolMessage,
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    68
) -> LoginResult {
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    69
    match message {
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    70
        HwProtocolMessage::Quit(_) => {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    71
            response.add(Bye("User quit".to_string()).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    72
            LoginResult::Exit
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    73
        }
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    74
        HwProtocolMessage::Nick(nick) => {
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    75
            let client = &mut server.anteroom.clients[client_id];
14875
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14786
diff changeset
    76
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    77
            if client.nick.is_some() {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    78
                response.add(Error("Nickname already provided.".to_string()).send_self());
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    79
                LoginResult::Unchanged
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    80
            } else if is_name_illegal(&nick) {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    81
                response.add(Bye("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()).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    82
                LoginResult::Exit
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    83
            } else {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    84
                client.nick = Some(nick.clone());
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    85
                response.add(Nick(nick).send_self());
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
    86
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    87
                if client.protocol_number.is_some() {
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    88
                    completion_result(server.clients.iter(), client, response)
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    89
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    90
                    LoginResult::Unchanged
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
    91
                }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
    92
            }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
    93
        }
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    94
        HwProtocolMessage::Proto(proto) => {
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
    95
            let client = &mut server.anteroom.clients[client_id];
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    96
            if client.protocol_number.is_some() {
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
    97
                response.add(Error("Protocol already known.".to_string()).send_self());
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
    98
                LoginResult::Unchanged
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
    99
            } else if proto == 0 {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   100
                response.add(Error("Bad number.".to_string()).send_self());
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   101
                LoginResult::Unchanged
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   102
            } else {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   103
                client.protocol_number = NonZeroU16::new(proto);
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   104
                response.add(Proto(proto).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   105
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   106
                if client.nick.is_some() {
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   107
                    completion_result(server.clients.iter(), client, response)
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   108
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   109
                    LoginResult::Unchanged
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   110
                }
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
   111
            }
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13671
diff changeset
   112
        }
4664da990556 Add official server feature to cargo
alfadur
parents: 13671
diff changeset
   113
        #[cfg(feature = "official-server")]
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   114
        HwProtocolMessage::Password(hash, salt) => {
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   115
            let client = &server.anteroom.clients[client_id];
13805
0118b7412570 use openssl for password message handling
alfadur
parents: 13803
diff changeset
   116
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   117
            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: 14701
diff changeset
   118
                response.request_io(super::IoTask::GetAccount {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   119
                    nick: nick.clone(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   120
                    protocol: protocol.get(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   121
                    server_salt: client.server_salt.clone(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   122
                    client_salt: salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   123
                    password_hash: hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   124
                });
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   125
            };
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   126
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14701
diff changeset
   127
            LoginResult::Unchanged
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
   128
        }
13805
0118b7412570 use openssl for password message handling
alfadur
parents: 13803
diff changeset
   129
        #[cfg(feature = "official-server")]
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   130
        HwProtocolMessage::Checker(protocol, nick, password) => {
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   131
            let client = &mut server.anteroom.clients[client_id];
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   132
            if protocol == 0 {
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   133
                response.add(Error("Bad number.".to_string()).send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   134
                LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   135
            } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   136
                client.protocol_number = NonZeroU16::new(protocol);
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   137
                client.nick = Some(nick);
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   138
                client.is_checker = true;
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   139
                LoginResult::Complete
01f8ab45f806 fix lobby joining
alfadur
parents: 14784
diff changeset
   140
            }
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13671
diff changeset
   141
        }
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   142
        _ => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   143
            warn!("Incorrect command in logging-in state");
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   144
            LoginResult::Unchanged
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14695
diff changeset
   145
        }
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   146
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   147
}