forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
283 lines (231 loc) · 7.68 KB
/
Copy pathmod.rs
File metadata and controls
283 lines (231 loc) · 7.68 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use core::panic;
use std::env;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use reqwest::Response;
use torrust_tracker::config::Configuration;
use torrust_tracker::jobs::tracker_api;
use torrust_tracker::protocol::clock::DurationSinceUnixEpoch;
use torrust_tracker::protocol::info_hash::InfoHash;
use torrust_tracker::tracker::peer::{self, Peer};
use torrust_tracker::tracker::statistics::Keeper;
use torrust_tracker::{ephemeral_instance_keys, logging, static_time, tracker};
use crate::common::ephemeral_random_port;
pub fn sample_peer() -> peer::Peer {
peer::Peer {
peer_id: peer::Id(*b"-qB00000000000000000"),
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
uploaded: NumberOfBytes(0),
downloaded: NumberOfBytes(0),
left: NumberOfBytes(0),
event: AnnounceEvent::Started,
}
}
pub fn tracker_configuration() -> Arc<Configuration> {
let mut config = Configuration {
log_level: Some("off".to_owned()),
..Default::default()
};
// Ephemeral socket address
let port = ephemeral_random_port();
config.http_api.bind_address = format!("127.0.0.1:{}", &port);
// Ephemeral database
let temp_directory = env::temp_dir();
let temp_file = temp_directory.join(format!("data_{}.db", &port));
config.db_path = temp_file.to_str().unwrap().to_owned();
Arc::new(config)
}
#[derive(Clone)]
pub struct ConnectionInfo {
pub bind_address: String,
pub api_token: Option<String>,
}
impl ConnectionInfo {
pub fn authenticated(bind_address: &str, api_token: &str) -> Self {
Self {
bind_address: bind_address.to_string(),
api_token: Some(api_token.to_string()),
}
}
pub fn anonymous(bind_address: &str) -> Self {
Self {
bind_address: bind_address.to_string(),
api_token: None,
}
}
}
pub async fn start_default_api_server() -> Server {
let configuration = tracker_configuration();
start_custom_api_server(configuration.clone()).await
}
pub async fn start_custom_api_server(configuration: Arc<Configuration>) -> Server {
start(configuration).await
}
async fn start(configuration: Arc<Configuration>) -> Server {
let connection_info = ConnectionInfo::authenticated(
&configuration.http_api.bind_address.clone(),
&configuration.http_api.access_tokens.get_key_value("admin").unwrap().1.clone(),
);
// Set the time of Torrust app starting
lazy_static::initialize(&static_time::TIME_AT_APP_START);
// Initialize the Ephemeral Instance Random Seed
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
// Initialize stats tracker
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();
// Initialize Torrust tracker
let tracker = match tracker::Tracker::new(&configuration.clone(), Some(stats_event_sender), stats_repository) {
Ok(tracker) => Arc::new(tracker),
Err(error) => {
panic!("{}", error)
}
};
// Initialize logging
logging::setup(&configuration);
// Start the HTTP API job
tracker_api::start_job(&configuration.http_api, tracker.clone()).await;
Server {
tracker,
connection_info,
}
}
pub struct Server {
pub tracker: Arc<tracker::Tracker>,
pub connection_info: ConnectionInfo,
}
impl Server {
pub fn get_connection_info(&self) -> ConnectionInfo {
self.connection_info.clone()
}
pub fn get_bind_address(&self) -> String {
self.connection_info.bind_address.clone()
}
/// Add a torrent to the tracker
pub async fn add_torrent(&self, info_hash: &InfoHash, peer: &Peer) {
self.tracker.update_torrent_with_peer_and_get_stats(info_hash, peer).await;
}
}
pub struct Client {
connection_info: ConnectionInfo,
}
type ReqwestQuery = Vec<ReqwestQueryParam>;
type ReqwestQueryParam = (String, String);
#[derive(Default, Debug)]
pub struct Query {
params: Vec<QueryParam>,
}
impl Query {
pub fn empty() -> Self {
Self { params: vec![] }
}
pub fn params(params: Vec<QueryParam>) -> Self {
Self { params }
}
pub fn add_param(&mut self, param: QueryParam) {
self.params.push(param);
}
fn with_token(token: &str) -> Self {
Self {
params: vec![QueryParam::new("token", token)],
}
}
}
impl From<Query> for ReqwestQuery {
fn from(url_search_params: Query) -> Self {
url_search_params
.params
.iter()
.map(|param| ReqwestQueryParam::from((*param).clone()))
.collect()
}
}
#[derive(Clone, Debug)]
pub struct QueryParam {
name: String,
value: String,
}
impl QueryParam {
pub fn new(name: &str, value: &str) -> Self {
Self {
name: name.to_string(),
value: value.to_string(),
}
}
}
impl From<QueryParam> for ReqwestQueryParam {
fn from(param: QueryParam) -> Self {
(param.name, param.value)
}
}
impl Client {
pub fn new(connection_info: ConnectionInfo) -> Self {
Self { connection_info }
}
pub async fn generate_auth_key(&self, seconds_valid: i32) -> Response {
self.post(&format!("key/{}", &seconds_valid)).await
}
pub async fn delete_auth_key(&self, key: &str) -> Response {
self.delete(&format!("key/{}", &key)).await
}
pub async fn reload_keys(&self) -> Response {
self.get("keys/reload", Query::default()).await
}
pub async fn whitelist_a_torrent(&self, info_hash: &str) -> Response {
self.post(&format!("whitelist/{}", &info_hash)).await
}
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Response {
self.delete(&format!("whitelist/{}", &info_hash)).await
}
pub async fn reload_whitelist(&self) -> Response {
self.get("whitelist/reload", Query::default()).await
}
pub async fn get_torrent(&self, info_hash: &str) -> Response {
self.get(&format!("torrent/{}", &info_hash), Query::default()).await
}
pub async fn get_torrents(&self, params: Query) -> Response {
self.get("torrents", params).await
}
pub async fn get_tracker_statistics(&self) -> Response {
self.get("stats", Query::default()).await
}
async fn get(&self, path: &str, params: Query) -> Response {
let mut query: Query = params;
if let Some(token) = &self.connection_info.api_token {
query.add_param(QueryParam::new("token", token));
};
reqwest::Client::builder()
.build()
.unwrap()
.get(self.base_url(path))
.query(&ReqwestQuery::from(query))
.send()
.await
.unwrap()
}
async fn post(&self, path: &str) -> Response {
reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.send()
.await
.unwrap()
}
async fn delete(&self, path: &str) -> Response {
reqwest::Client::new()
.delete(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.send()
.await
.unwrap()
}
fn base_url(&self, path: &str) -> String {
format!("http://{}/api/{path}", &self.connection_info.bind_address)
}
fn query_with_token(&self) -> Query {
match &self.connection_info.api_token {
Some(token) => Query::with_token(token),
None => Query::default(),
}
}
}