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
147 lines (128 loc) · 4.59 KB
/
database.rs
File metadata and controls
147 lines (128 loc) · 4.59 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
use crate::key_manager::AuthKey;
use crate::{InfoHash, AUTH_KEY_LENGTH};
use log::debug;
use r2d2::Pool;
use r2d2_sqlite::rusqlite::NO_PARAMS;
use r2d2_sqlite::{rusqlite, SqliteConnectionManager};
use std::convert::TryInto;
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.");
SqliteDatabase::create_database_tables(&sqlite_pool)?;
Ok(SqliteDatabase { pool: sqlite_pool })
}
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
);";
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
);
let conn = pool.get().unwrap();
conn.execute(create_whitelist_table, NO_PARAMS)
.and_then(|updated| {
conn.execute(&create_keys_table, NO_PARAMS)
.map(|updated2| updated + updated2)
})
.map_err(trace_debug)
}
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();
conn.execute(
"INSERT INTO whitelist (info_hash) VALUES (?)",
&[info_hash.to_string()],
)
.map_err(trace_debug)
.and_then(validate_updated)
}
pub async fn remove_info_hash_from_whitelist(
&self,
info_hash: InfoHash,
) -> Result<usize, rusqlite::Error> {
let conn = self.pool.get().unwrap();
conn.execute(
"DELETE FROM whitelist WHERE info_hash = ?",
&[info_hash.to_string()],
)
.map_err(trace_debug)
.and_then(validate_updated)
}
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.try_into().unwrap()),
})
} 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();
conn.execute(
"INSERT INTO keys (key, valid_until) VALUES (?1, ?2)",
&[
auth_key.key.to_string(),
auth_key.valid_until.unwrap().to_string(),
],
)
.map_err(trace_debug)
.and_then(validate_updated)
}
pub async fn remove_key_from_keys(&self, key: String) -> Result<usize, rusqlite::Error> {
let conn = self.pool.get().unwrap();
conn.execute("DELETE FROM keys WHERE key = ?", &[key])
.map_err(trace_debug)
.and_then(validate_updated)
}
}
fn trace_debug<T: std::fmt::Debug>(value: T) -> T {
debug!("{:?}", value);
value
}
fn validate_updated(updated: usize) -> Result<usize, rusqlite::Error> {
if updated > 0 {
Ok(updated)
} else {
Err(rusqlite::Error::ExecuteReturnedResults)
}
}