rust/hedgewars-server/src/server/handlers/loggingin.rs
author Wuzzy <Wuzzy2@mail.ru>
Fri, 24 May 2019 14:55:48 +0200
changeset 15038 01bb1d7ca14f
parent 14870 ce98c37826a7
permissions -rw-r--r--
Add drill rocket animation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
     3
use crate::protocol::messages::HWProtocolMessage::LoadRoom;
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
     4
use crate::server::client::HWClient;
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
     5
use crate::server::core::HWServer;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13478
diff changeset
     6
use crate::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     7
    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
     8
    server::{
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
     9
        core::{HWAnteClient, HWAnteroom},
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    10
        coretypes::ClientId,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    11
    },
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    12
    utils::is_name_illegal,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    13
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    14
use log::*;
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    15
#[cfg(feature = "official-server")]
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    16
use openssl::sha::sha1;
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    17
use std::{
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    18
    fmt::{Formatter, LowerHex},
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    19
    num::NonZeroU16,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    20
};
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    21
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    22
pub enum LoginResult {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    23
    Unchanged,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    24
    Complete,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    25
    Exit,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    26
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    27
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    28
fn completion_result<'a, I>(
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    29
    mut other_clients: I,
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    30
    client: &mut HWAnteClient,
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    31
    response: &mut super::Response,
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    32
) -> LoginResult
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    33
where
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    34
    I: Iterator<Item = (ClientId, &'a HWClient)>,
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    35
{
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    36
    let has_nick_clash =
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    37
        other_clients.any(|(_, c)| !c.is_checker() && 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
    38
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    39
    if has_nick_clash {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    40
        if client.protocol_number.unwrap().get() < 38 {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    41
            response.add(Bye("User quit: Nickname is already in use".to_string()).send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    42
            LoginResult::Exit
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    43
        } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    44
            client.nick = None;
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    45
            response.add(Notice("NickAlreadyInUse".to_string()).send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    46
            LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    47
        }
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    48
    } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    49
        #[cfg(feature = "official-server")]
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    50
        {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    51
            response.add(AskPassword(client.server_salt.clone()).send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    52
            LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    53
        }
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    54
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    55
        #[cfg(not(feature = "official-server"))]
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    56
        {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    57
            LoginResult::Complete
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    58
        }
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    59
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    60
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
    61
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    62
pub fn handle(
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    63
    server: &mut HWServer,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    64
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    65
    response: &mut super::Response,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    66
    message: HWProtocolMessage,
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    67
) -> LoginResult {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    68
    match message {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    69
        HWProtocolMessage::Quit(_) => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    70
            response.add(Bye("User quit".to_string()).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    71
            LoginResult::Exit
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    72
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    73
        HWProtocolMessage::Nick(nick) => {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    74
            let client = &mut server.anteroom.clients[client_id];
14870
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14781
diff changeset
    75
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    76
            if client.nick.is_some() {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    77
                response.add(Error("Nickname already provided.".to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    78
                LoginResult::Unchanged
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    79
            } else if is_name_illegal(&nick) {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    80
                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: 14690
diff changeset
    81
                LoginResult::Exit
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    82
            } else {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    83
                client.nick = Some(nick.clone());
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    84
                response.add(Nick(nick).send_self());
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    85
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    86
                if client.protocol_number.is_some() {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    87
                    completion_result(server.clients.iter(), client, response)
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    88
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    89
                    LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    90
                }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    91
            }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    92
        }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    93
        HWProtocolMessage::Proto(proto) => {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    94
            let client = &mut server.anteroom.clients[client_id];
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    95
            if client.protocol_number.is_some() {
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    96
                response.add(Error("Protocol already known.".to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    97
                LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    98
            } else if proto == 0 {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    99
                response.add(Error("Bad number.".to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   100
                LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   101
            } else {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   102
                client.protocol_number = NonZeroU16::new(proto);
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   103
                response.add(Proto(proto).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   104
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   105
                if client.nick.is_some() {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   106
                    completion_result(server.clients.iter(), client, response)
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   107
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   108
                    LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
   109
                }
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   110
            }
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   111
        }
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   112
        #[cfg(feature = "official-server")]
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   113
        HWProtocolMessage::Password(hash, salt) => {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   114
            let client = &server.anteroom.clients[client_id];
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   115
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   116
            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
   117
                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
   118
                    nick: nick.clone(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   119
                    protocol: protocol.get(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   120
                    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
   121
                    client_salt: salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   122
                    password_hash: hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   123
                });
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   124
            };
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   125
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14696
diff changeset
   126
            LoginResult::Unchanged
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   127
        }
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   128
        #[cfg(feature = "official-server")]
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   129
        HWProtocolMessage::Checker(protocol, nick, password) => {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   130
            let client = &mut server.anteroom.clients[client_id];
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   131
            if protocol == 0 {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   132
                response.add(Error("Bad number.".to_string()).send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   133
                LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   134
            } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   135
                client.protocol_number = NonZeroU16::new(protocol);
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   136
                client.nick = Some(nick);
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   137
                client.is_checker = true;
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   138
                LoginResult::Complete
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
   139
            }
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   140
        }
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   141
        _ => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   142
            warn!("Incorrect command in logging-in state");
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   143
            LoginResult::Unchanged
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   144
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   145
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   146
}