rust/hedgewars-server/src/server/database.rs
author unC0Rr
Sat, 28 Sep 2024 22:27:13 +0200
changeset 16066 2b4f361e3891
parent 15967 e514ceb5e7d6
permissions -rw-r--r--
Wrap chat lines into div
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
     1
use mysql_async::{self, from_row_opt, params, prelude::*, Pool};
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
     2
use sha1::{Digest, Sha1};
15967
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
     3
use tokio::sync::mpsc::{channel, Receiver, Sender};
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
     4
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
     5
use crate::handlers::{AccountInfo, Sha1Digest};
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     6
15124
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
     7
const CHECK_ACCOUNT_EXISTS_QUERY: &str =
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
     8
    r"SELECT 1 FROM users WHERE users.name = :username LIMIT 1";
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
     9
15539
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15184
diff changeset
    10
const GET_ACCOUNT_QUERY: &str = r"SELECT CASE WHEN users.status = 1 THEN users.pass ELSE '' END,
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    11
     (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: 14478
diff changeset
    12
     (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: 14478
diff changeset
    13
     FROM users WHERE users.name = :username";
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    14
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    15
const STORE_STATS_QUERY: &str = r"INSERT INTO gameserver_stats
15124
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
    16
      (players, rooms, last_update)
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
    17
      VALUES
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
    18
      (:players, :rooms, UNIX_TIMESTAMP())";
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    19
14806
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14800
diff changeset
    20
const GET_REPLAY_NAME_QUERY: &str = r"SELECT filename FROM achievements WHERE id = :id";
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14800
diff changeset
    21
15142
1a43b570cbe4 Fix build errors in certain configurations
unc0rr
parents: 15131
diff changeset
    22
pub struct ServerStatistics {
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    23
    rooms: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    24
    players: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    25
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    26
15142
1a43b570cbe4 Fix build errors in certain configurations
unc0rr
parents: 15131
diff changeset
    27
pub struct Achievements {}
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    28
15967
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    29
pub enum DatabaseQuery {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    30
    CheckRegistered {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    31
        nick: String,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    32
    },
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    33
    GetAccount {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    34
        nick: String,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    35
        protocol: u16,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    36
        password_hash: String,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    37
        client_salt: String,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    38
        server_salt: String,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    39
    },
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    40
    GetCheckerAccount {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    41
        nick: String,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    42
        password: String,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    43
    },
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    44
    GetReplayFilename {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    45
        id: u32,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    46
    },
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    47
}
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    48
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    49
pub enum DatabaseResponse {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    50
    AccountRegistered(bool),
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    51
    Account(Option<AccountInfo>),
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    52
    CheckerAccount { is_registered: bool },
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    53
}
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    54
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
    55
pub struct Database {
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
    56
    pool: Pool,
15967
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    57
    query_rx: Receiver<DatabaseQuery>,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    58
    response_tx: Sender<DatabaseResponse>,
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    59
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    60
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    61
impl Database {
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
    62
    pub fn new(url: &str) -> Self {
15967
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    63
        let (query_tx, query_rx) = channel(32);
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    64
        let (response_tx, response_rx) = channel(32);
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
    65
        Self {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
    66
            pool: Pool::new(url),
15967
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    67
            query_rx,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    68
            response_tx,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    69
        }
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    70
    }
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    71
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    72
    pub async fn run(&mut self) {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    73
        use DatabaseResponse::*;
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    74
        loop {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    75
            let query = self.query_rx.recv().await;
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    76
            if let Some(query) = query {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    77
                match query {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    78
                    DatabaseQuery::CheckRegistered { nick } => {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    79
                        let is_registered = self.get_is_registered(&nick).await.unwrap_or(false);
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    80
                        self.response_tx
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    81
                            .send(AccountRegistered(is_registered))
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    82
                            .await;
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    83
                    }
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    84
                    DatabaseQuery::GetAccount {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    85
                        nick,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    86
                        protocol,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    87
                        password_hash,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    88
                        client_salt,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    89
                        server_salt,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    90
                    } => {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    91
                        let account = self
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    92
                            .get_account(
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    93
                                &nick,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    94
                                protocol,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    95
                                &password_hash,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    96
                                &client_salt,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    97
                                &server_salt,
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    98
                            )
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
    99
                            .await
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   100
                            .unwrap_or(None);
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   101
                        self.response_tx.send(Account(account)).await;
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   102
                    }
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   103
                    DatabaseQuery::GetCheckerAccount { nick, password } => {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   104
                        let is_registered = self
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   105
                            .get_checker_account(&nick, &password)
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   106
                            .await
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   107
                            .unwrap_or(false);
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   108
                        self.response_tx
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   109
                            .send(CheckerAccount { is_registered })
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   110
                            .await;
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   111
                    }
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   112
                    DatabaseQuery::GetReplayFilename { id } => {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   113
                        let filename = self.get_replay_name(id).await;
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   114
                    }
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   115
                };
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   116
            } else {
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   117
                break;
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   118
            }
15124
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
   119
        }
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
   120
    }
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
   121
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   122
    pub async fn get_is_registered(&mut self, nick: &str) -> mysql_async::Result<bool> {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   123
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   124
        let result = CHECK_ACCOUNT_EXISTS_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   125
            .with(params! { "username" => nick })
15967
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   126
            .first::<u32, _>(&mut connection)
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   127
            .await?;
15967
e514ceb5e7d6 separate out DB task
alfadur
parents: 15870
diff changeset
   128
        Ok(!result.is_some())
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   129
    }
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   130
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   131
    pub async fn get_account(
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   132
        &mut self,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   133
        nick: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   134
        protocol: u16,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   135
        password_hash: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   136
        client_salt: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   137
        server_salt: &str,
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   138
    ) -> mysql_async::Result<Option<AccountInfo>> {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   139
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   140
        if let Some((mut password, is_admin, is_contributor)) = GET_ACCOUNT_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   141
            .with(params! { "username" => nick })
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   142
            .first::<(String, i32, i32), _>(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   143
            .await?
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   144
        {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   145
            let client_hash = get_hash(protocol, &password, &client_salt, &server_salt);
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   146
            let server_hash = get_hash(protocol, &password, &server_salt, &client_salt);
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   147
            password.replace_range(.., "🦔🦔🦔🦔🦔🦔🦔🦔");
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   148
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   149
            if client_hash == password_hash {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   150
                Ok(Some(AccountInfo {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   151
                    is_registered: true,
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   152
                    is_admin: is_admin == 1,
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   153
                    is_contributor: is_contributor == 1,
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   154
                    server_hash,
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   155
                }))
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   156
            } else {
15131
6a8c294f49c9 fix hash comparison fix
alfadur
parents: 15130
diff changeset
   157
                Ok(None)
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   158
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   159
        } else {
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   160
            Ok(None)
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   161
        }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   162
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   163
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   164
    pub async fn get_checker_account(
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   165
        &mut self,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   166
        nick: &str,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   167
        checker_password: &str,
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   168
    ) -> mysql_async::Result<bool> {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   169
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   170
        if let Some((password, _, _)) = GET_ACCOUNT_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   171
            .with(params! { "username" => nick })
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   172
            .first::<(String, i32, i32), _>(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   173
            .await?
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   174
        {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   175
            Ok(checker_password == password)
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   176
        } else {
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   177
            Ok(false)
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   178
        }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   179
    }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   180
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   181
    pub async fn store_stats(&mut self, stats: &ServerStatistics) -> mysql_async::Result<()> {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   182
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   183
        STORE_STATS_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   184
            .with(params! {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   185
                "players" => stats.players,
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   186
                "rooms" => stats.rooms,
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   187
            })
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   188
            .ignore(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   189
            .await
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   190
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   191
15870
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15855
diff changeset
   192
    pub async fn store_achievements(
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15855
diff changeset
   193
        &mut self,
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15855
diff changeset
   194
        achievements: &Achievements,
3d05bada4799 Implement appropriate routing of checker messages
unC0Rr
parents: 15855
diff changeset
   195
    ) -> mysql_async::Result<()> {
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   196
        Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   197
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   198
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   199
    pub async fn get_replay_name(&mut self, replay_id: u32) -> mysql_async::Result<Option<String>> {
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   200
        let mut connection = self.pool.get_conn().await?;
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   201
        GET_REPLAY_NAME_QUERY
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   202
            .with(params! { "id" => replay_id })
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   203
            .first::<String, _>(&mut connection)
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   204
            .await
14477
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   205
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   206
}
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   207
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   208
fn get_hash(protocol_number: u16, web_password: &str, salt1: &str, salt2: &str) -> Sha1Digest {
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   209
    let data = format!(
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   210
        "{}{}{}{}{}",
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   211
        salt1, salt2, web_password, protocol_number, "!hedgewars"
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   212
    );
15855
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   213
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   214
    let mut sha1 = Sha1::new();
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   215
    sha1.update(&data);
3511bacbd763 update to mysql_async
alfadur
parents: 15817
diff changeset
   216
    Sha1Digest::new(sha1.finalize().try_into().unwrap())
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14478
diff changeset
   217
}