rust/hedgewars-server/src/server/database.rs
author unC0Rr
Thu, 29 Sep 2022 16:30:02 +0200
changeset 15882 f185e7367dd3
parent 15848 3d05bada4799
child 15937 e514ceb5e7d6
permissions -rw-r--r--
Add some work towards having checker work with the new server
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
     1
use mysql_async::{self, from_row_opt, params, prelude::*, Pool};
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
     2
use sha1::{Digest, Sha1};
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
     3
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     4
use crate::handlers::{AccountInfo, Sha1Digest};
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     5
15103
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
     6
const CHECK_ACCOUNT_EXISTS_QUERY: &str =
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
     7
    r"SELECT 1 FROM users WHERE users.name = :username LIMIT 1";
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
     8
15517
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15163
diff changeset
     9
const GET_ACCOUNT_QUERY: &str = r"SELECT CASE WHEN users.status = 1 THEN users.pass ELSE '' END,
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    10
     (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 3),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    11
     (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 13)
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    12
     FROM users WHERE users.name = :username";
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    13
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    14
const STORE_STATS_QUERY: &str = r"INSERT INTO gameserver_stats
15103
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    15
      (players, rooms, last_update)
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    16
      VALUES
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    17
      (:players, :rooms, UNIX_TIMESTAMP())";
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    18
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14779
diff changeset
    19
const GET_REPLAY_NAME_QUERY: &str = r"SELECT filename FROM achievements WHERE id = :id";
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14779
diff changeset
    20
15121
1a43b570cbe4 Fix build errors in certain configurations
unc0rr
parents: 15110
diff changeset
    21
pub struct ServerStatistics {
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    22
    rooms: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    23
    players: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    24
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    25
15121
1a43b570cbe4 Fix build errors in certain configurations
unc0rr
parents: 15110
diff changeset
    26
pub struct Achievements {}
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    27
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    28
pub struct Database {
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    29
    pool: Pool,
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    30
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    31
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    32
impl Database {
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    33
    pub fn new(url: &str) -> Self {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    34
        Self {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    35
            pool: Pool::new(url),
15103
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    36
        }
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    37
    }
823052e66611 check for account existence before asking passwords
alfadur
parents: 15075
diff changeset
    38
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    39
    pub async fn get_is_registered(&mut self, nick: &str) -> mysql_async::Result<bool> {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    40
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    41
        let result = CHECK_ACCOUNT_EXISTS_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    42
            .with(params! { "username" => nick })
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    43
            .first(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    44
            .await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    45
        Ok(!result.is_empty())
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    46
    }
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    47
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    48
    pub async fn get_account(
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    49
        &mut self,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    50
        nick: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    51
        protocol: u16,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    52
        password_hash: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    53
        client_salt: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    54
        server_salt: &str,
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    55
    ) -> mysql_async::Result<Option<AccountInfo>> {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    56
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    57
        if let Some((mut password, is_admin, is_contributor)) = GET_ACCOUNT_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    58
            .with(params! { "username" => nick })
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    59
            .first::<(String, i32, i32), _>(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    60
            .await?
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    61
        {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    62
            let client_hash = get_hash(protocol, &password, &client_salt, &server_salt);
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    63
            let server_hash = get_hash(protocol, &password, &server_salt, &client_salt);
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    64
            password.replace_range(.., "🦔🦔🦔🦔🦔🦔🦔🦔");
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    65
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    66
            if client_hash == password_hash {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    67
                Ok(Some(AccountInfo {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    68
                    is_registered: true,
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    69
                    is_admin: is_admin == 1,
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    70
                    is_contributor: is_contributor == 1,
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    71
                    server_hash,
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    72
                }))
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    73
            } else {
15110
6a8c294f49c9 fix hash comparison fix
alfadur
parents: 15109
diff changeset
    74
                Ok(None)
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    75
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    76
        } else {
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    77
            Ok(None)
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    78
        }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    79
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    80
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    81
    pub async fn get_checker_account(
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15531
diff changeset
    82
        &mut self,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15531
diff changeset
    83
        nick: &str,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15531
diff changeset
    84
        checker_password: &str,
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    85
    ) -> mysql_async::Result<bool> {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    86
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    87
        if let Some((password, _, _)) = GET_ACCOUNT_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    88
            .with(params! { "username" => nick })
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    89
            .first::<(String, i32, i32), _>(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    90
            .await?
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    91
        {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    92
            Ok(checker_password == password)
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15531
diff changeset
    93
        } else {
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    94
            Ok(false)
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15531
diff changeset
    95
        }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15531
diff changeset
    96
    }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15531
diff changeset
    97
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    98
    pub async fn store_stats(&mut self, stats: &ServerStatistics) -> mysql_async::Result<()> {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
    99
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   100
        STORE_STATS_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   101
            .with(params! {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   102
                "players" => stats.players,
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   103
                "rooms" => stats.rooms,
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   104
            })
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   105
            .ignore(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   106
            .await
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   107
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   108
15848
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15833
diff changeset
   109
    pub async fn store_achievements(
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15833
diff changeset
   110
        &mut self,
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15833
diff changeset
   111
        achievements: &Achievements,
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15833
diff changeset
   112
    ) -> mysql_async::Result<()> {
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   113
        Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   114
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   115
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   116
    pub async fn get_replay_name(&mut self, replay_id: u32) -> mysql_async::Result<Option<String>> {
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   117
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   118
        GET_REPLAY_NAME_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   119
            .with(params! { "id" => replay_id })
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   120
            .first::<String, _>(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   121
            .await
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   122
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   123
}
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   124
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   125
fn get_hash(protocol_number: u16, web_password: &str, salt1: &str, salt2: &str) -> Sha1Digest {
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   126
    let data = format!(
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   127
        "{}{}{}{}{}",
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   128
        salt1, salt2, web_password, protocol_number, "!hedgewars"
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   129
    );
15833
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   130
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   131
    let mut sha1 = Sha1::new();
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   132
    sha1.update(&data);
3511bacbd763 update to mysql_async
alfadur
parents: 15795
diff changeset
   133
    Sha1Digest::new(sha1.finalize().try_into().unwrap())
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   134
}