rust/hedgewars-server/src/server/database.rs
author alfadur
Tue, 28 May 2019 21:28:32 +0300
changeset 15080 e935b1ad23f3
parent 15079 c5a6e8566425
child 15108 823052e66611
permissions -rw-r--r--
normalize type names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     1
use mysql;
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
     2
use mysql::{error::DriverError, error::Error, from_row_opt, params};
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
     3
use openssl::sha::sha1;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
     4
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     5
use crate::handlers::{AccountInfo, Sha1Digest};
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     6
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
     7
const GET_ACCOUNT_QUERY: &str =
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
     8
    r"SELECT CASE WHEN users.status = 1 THEN users.pass ELSE '' END,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
     9
     (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: 14462
diff changeset
    10
     (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: 14462
diff changeset
    11
     FROM users WHERE users.name = :username";
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    12
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    13
const STORE_STATS_QUERY: &str = r"INSERT INTO gameserver_stats
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    14
            (players, rooms, last_update)
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    15
            VALUES
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    16
            (:players, :rooms, UNIX_TIMESTAMP())";
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    17
14790
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
    18
const GET_REPLAY_NAME_QUERY: &str = r"SELECT filename FROM achievements WHERE id = :id";
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
    19
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    20
struct ServerStatistics {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    21
    rooms: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    22
    players: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    23
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    24
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    25
struct Achievements {}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    26
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    27
pub struct Database {
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    28
    pool: Option<mysql::Pool>,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    29
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    30
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    31
impl Database {
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    32
    pub fn new() -> Self {
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    33
        Self { pool: None }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    34
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    35
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    36
    pub fn connect(&mut self, url: &str) -> Result<(), Error> {
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    37
        self.pool = Some(mysql::Pool::new(url)?);
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    38
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    39
        Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    40
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    41
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    42
    pub fn get_account(
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    43
        &mut self,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    44
        nick: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    45
        protocol: u16,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    46
        password_hash: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    47
        client_salt: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    48
        server_salt: &str,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    49
    ) -> Result<Option<AccountInfo>, Error> {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    50
        if let Some(pool) = &self.pool {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    51
            if let Some(row) = pool.first_exec(GET_ACCOUNT_QUERY, params! { "username" => nick })? {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    52
                let (mut password, is_admin, is_contributor) =
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    53
                    from_row_opt::<(String, i32, i32)>(row)?;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    54
                let client_hash = get_hash(protocol, &password, &client_salt, &server_salt);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    55
                let server_hash = get_hash(protocol, &password, &server_salt, &client_salt);
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    56
                password.replace_range(.., "🦔🦔🦔🦔🦔🦔🦔🦔");
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    57
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    58
                if server_hash == client_hash {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    59
                    Ok(Some(AccountInfo {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    60
                        is_registered: true,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    61
                        is_admin: is_admin == 1,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    62
                        is_contributor: is_contributor == 1,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    63
                        server_hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    64
                    }))
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    65
                } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    66
                    Ok(None)
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    67
                }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    68
            } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    69
                Ok(Some(AccountInfo {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    70
                    is_registered: false,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    71
                    is_admin: false,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    72
                    is_contributor: false,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    73
                    server_hash: Sha1Digest::new([0; 20]),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    74
                }))
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    75
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    76
        } else {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    77
            Err(DriverError::SetupError.into())
14461
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
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    81
    pub fn store_stats(&mut self, stats: &ServerStatistics) -> Result<(), Error> {
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    82
        if let Some(pool) = &self.pool {
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    83
            for mut stmt in pool.prepare(STORE_STATS_QUERY).into_iter() {
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14461
diff changeset
    84
                stmt.execute(params! {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14461
diff changeset
    85
                    "players" => stats.players,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14461
diff changeset
    86
                    "rooms" => stats.rooms,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14461
diff changeset
    87
                })?;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14461
diff changeset
    88
            }
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    89
            Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    90
        } else {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    91
            Err(DriverError::SetupError.into())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    92
        }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    93
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    94
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
    95
    pub fn store_achievements(&mut self, achievements: &Achievements) -> Result<(), ()> {
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    96
        Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    97
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    98
14790
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
    99
    pub fn get_replay_name(&mut self, replay_id: u32) -> Result<Option<String>, Error> {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   100
        if let Some(pool) = &self.pool {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   101
            if let Some(row) =
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   102
                pool.first_exec(GET_REPLAY_NAME_QUERY, params! { "id" => replay_id })?
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   103
            {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   104
                let (filename) = from_row_opt::<(String)>(row)?;
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   105
                Ok(Some(filename))
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   106
            } else {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   107
                Ok(None)
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   108
            }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   109
        } else {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   110
            Err(DriverError::SetupError.into())
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   111
        }
14461
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   112
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
   113
}
14784
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   114
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   115
fn get_hash(protocol_number: u16, web_password: &str, salt1: &str, salt2: &str) -> Sha1Digest {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   116
    let s = format!(
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   117
        "{}{}{}{}{}",
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   118
        salt1, salt2, web_password, protocol_number, "!hedgewars"
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   119
    );
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   120
    Sha1Digest::new(sha1(s.as_bytes()))
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14462
diff changeset
   121
}