forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.rs
More file actions
150 lines (133 loc) · 5.22 KB
/
Copy pathdatabase.rs
File metadata and controls
150 lines (133 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{InfoHash, AUTH_KEY_LENGTH};
use log::debug;
use r2d2_sqlite::{SqliteConnectionManager, rusqlite};
use r2d2::{Pool};
use r2d2_sqlite::rusqlite::NO_PARAMS;
use crate::key_manager::AuthKey;
use std::str::FromStr;
pub struct SqliteDatabase {
pool: Pool<SqliteConnectionManager>
}
impl SqliteDatabase {
pub fn new(db_path: &str) -> Result<SqliteDatabase, rusqlite::Error> {
let sqlite_connection_manager = SqliteConnectionManager::file(db_path);
let sqlite_pool = r2d2::Pool::new(sqlite_connection_manager).expect("Failed to create r2d2 SQLite connection pool.");
let sqlite_database = SqliteDatabase {
pool: sqlite_pool
};
if let Err(error) = SqliteDatabase::create_database_tables(&sqlite_database.pool) {
return Err(error)
};
Ok(sqlite_database)
}
pub fn create_database_tables(pool: &Pool<SqliteConnectionManager>) -> Result<usize, rusqlite::Error> {
let create_whitelist_table = "
CREATE TABLE IF NOT EXISTS whitelist (
id integer PRIMARY KEY AUTOINCREMENT,
info_hash VARCHAR(20) NOT NULL UNIQUE
);".to_string();
let create_keys_table = format!("
CREATE TABLE IF NOT EXISTS keys (
id integer PRIMARY KEY AUTOINCREMENT,
key VARCHAR({}) NOT NULL UNIQUE,
valid_until INT(10) NOT NULL
);", AUTH_KEY_LENGTH as i8);
let conn = pool.get().unwrap();
match conn.execute(&create_whitelist_table, NO_PARAMS) {
Ok(updated) => {
match conn.execute(&create_keys_table, NO_PARAMS) {
Ok(updated2) => Ok(updated + updated2),
Err(e) => {
debug!("{:?}", e);
Err(e)
}
}
}
Err(e) => {
debug!("{:?}", e);
Err(e)
}
}
}
pub async fn get_info_hash_from_whitelist(&self, info_hash: &str) -> Result<InfoHash, rusqlite::Error> {
let conn = self.pool.get().unwrap();
let mut stmt = conn.prepare("SELECT info_hash FROM whitelist WHERE info_hash = ?")?;
let mut rows = stmt.query(&[info_hash])?;
if let Some(row) = rows.next()? {
let info_hash: String = row.get(0).unwrap();
// should never be able to fail
Ok(InfoHash::from_str(&info_hash).unwrap())
} else {
Err(rusqlite::Error::QueryReturnedNoRows)
}
}
pub async fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, rusqlite::Error> {
let conn = self.pool.get().unwrap();
match conn.execute("INSERT INTO whitelist (info_hash) VALUES (?)", &[info_hash.to_string()]) {
Ok(updated) => {
if updated > 0 { return Ok(updated) }
Err(rusqlite::Error::ExecuteReturnedResults)
},
Err(e) => {
debug!("{:?}", e);
Err(e)
}
}
}
pub async fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, rusqlite::Error> {
let conn = self.pool.get().unwrap();
match conn.execute("DELETE FROM whitelist WHERE info_hash = ?", &[info_hash.to_string()]) {
Ok(updated) => {
if updated > 0 { return Ok(updated) }
Err(rusqlite::Error::ExecuteReturnedResults)
},
Err(e) => {
debug!("{:?}", e);
Err(e)
}
}
}
pub async fn get_key_from_keys(&self, key: &str) -> Result<AuthKey, rusqlite::Error> {
let conn = self.pool.get().unwrap();
let mut stmt = conn.prepare("SELECT key, valid_until FROM keys WHERE key = ?")?;
let mut rows = stmt.query(&[key.to_string()])?;
if let Some(row) = rows.next()? {
let key: String = row.get(0).unwrap();
let valid_until_i64: i64 = row.get(1).unwrap();
Ok(AuthKey {
key,
valid_until: Some(valid_until_i64 as u64)
})
} else {
Err(rusqlite::Error::QueryReturnedNoRows)
}
}
pub async fn add_key_to_keys(&self, auth_key: &AuthKey) -> Result<usize, rusqlite::Error> {
let conn = self.pool.get().unwrap();
match conn.execute("INSERT INTO keys (key, valid_until) VALUES (?1, ?2)",
&[auth_key.key.to_string(), auth_key.valid_until.unwrap().to_string()]
) {
Ok(updated) => {
if updated > 0 { return Ok(updated) }
Err(rusqlite::Error::ExecuteReturnedResults)
},
Err(e) => {
debug!("{:?}", e);
Err(e)
}
}
}
pub async fn remove_key_from_keys(&self, key: String) -> Result<usize, rusqlite::Error> {
let conn = self.pool.get().unwrap();
match conn.execute("DELETE FROM keys WHERE key = ?", &[key]) {
Ok(updated) => {
if updated > 0 { return Ok(updated) }
Err(rusqlite::Error::ExecuteReturnedResults)
},
Err(e) => {
debug!("{:?}", e);
Err(e)
}
}
}
}