author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 29 Apr 2019 21:24:42 +0200 | |
changeset 14870 | 24df603fbcd2 |
parent 14806 | a1077e8d26f4 |
child 15095 | c5a6e8566425 |
permissions | -rw-r--r-- |
14477 | 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 | 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 | 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 | 21 |
struct ServerStatistics { |
22 |
rooms: u32, |
|
23 |
players: u32, |
|
24 |
} |
|
25 |
||
26 |
struct Achievements {} |
|
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 | 29 |
pool: Option<mysql::Pool>, |
30 |
} |
|
31 |
||
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 | 34 |
Self { pool: None } |
35 |
} |
|
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 | 38 |
self.pool = Some(mysql::Pool::new(url)?); |
39 |
||
40 |
Ok(()) |
|
41 |
} |
|
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 | 79 |
} |
80 |
} |
|
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 | 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 | 85 |
stmt.execute(params! { |
86 |
"players" => stats.players, |
|
87 |
"rooms" => stats.rooms, |
|
88 |
})?; |
|
89 |
} |
|
14477 | 90 |
Ok(()) |
91 |
} else { |
|
92 |
Err(DriverError::SetupError.into()) |
|
93 |
} |
|
94 |
} |
|
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 | 97 |
Ok(()) |
98 |
} |
|
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 | 113 |
} |
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 |
} |