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