forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
344 lines (292 loc) · 10.7 KB
/
lib.rs
File metadata and controls
344 lines (292 loc) · 10.7 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! Configuration data structures for [Torrust Tracker](https://docs.rs/torrust-tracker).
//!
//! This module contains the configuration data structures for the
//! Torrust Tracker, which is a `BitTorrent` tracker server.
//!
//! The current version for configuration is [`v2_0_0`].
pub mod logging;
pub mod v2_0_0;
pub mod validator;
use std::collections::HashMap;
use std::env;
use std::sync::Arc;
use std::time::Duration;
use camino::Utf8PathBuf;
use derive_more::{Constructor, Display};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use thiserror::Error;
use torrust_tracker_located_error::{DynError, LocatedError};
/// The maximum number of returned peers for a torrent.
pub const TORRENT_PEERS_LIMIT: usize = 74;
/// Default timeout for sending and receiving packets. And waiting for sockets
/// to be readable and writable.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
// Environment variables
/// The whole `tracker.toml` file content. It has priority over the config file.
/// Even if the file is not on the default path.
const ENV_VAR_CONFIG_TOML: &str = "TORRUST_TRACKER_CONFIG_TOML";
/// The `tracker.toml` file location.
pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_TRACKER_CONFIG_TOML_PATH";
pub type Configuration = v2_0_0::Configuration;
pub type Core = v2_0_0::core::Core;
pub type Logging = v2_0_0::logging::Logging;
pub type HealthCheckApi = v2_0_0::health_check_api::HealthCheckApi;
pub type HttpApi = v2_0_0::tracker_api::HttpApi;
pub type HttpTracker = v2_0_0::http_tracker::HttpTracker;
pub type UdpTracker = v2_0_0::udp_tracker::UdpTracker;
pub type Database = v2_0_0::database::Database;
pub type Driver = v2_0_0::database::Driver;
pub type Threshold = v2_0_0::logging::Threshold;
pub type AccessTokens = HashMap<String, String>;
pub const LATEST_VERSION: &str = "2.0.0";
/// Info about the configuration specification.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[display("Metadata(app: {app}, purpose: {purpose}, schema_version: {schema_version})")]
pub struct Metadata {
/// The application this configuration is valid for.
#[serde(default = "Metadata::default_app")]
app: App,
/// The purpose of this parsed file.
#[serde(default = "Metadata::default_purpose")]
purpose: Purpose,
/// The schema version for the configuration.
#[serde(default = "Metadata::default_schema_version")]
#[serde(flatten)]
schema_version: Version,
}
impl Default for Metadata {
fn default() -> Self {
Self {
app: Self::default_app(),
purpose: Self::default_purpose(),
schema_version: Self::default_schema_version(),
}
}
}
impl Metadata {
fn default_app() -> App {
App::TorrustTracker
}
fn default_purpose() -> Purpose {
Purpose::Configuration
}
fn default_schema_version() -> Version {
Version::latest()
}
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[serde(rename_all = "kebab-case")]
pub enum App {
TorrustTracker,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[serde(rename_all = "lowercase")]
pub enum Purpose {
Configuration,
}
/// The configuration version.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display, Clone)]
#[serde(rename_all = "lowercase")]
pub struct Version {
#[serde(default = "Version::default_semver")]
schema_version: String,
}
impl Default for Version {
fn default() -> Self {
Self {
schema_version: Self::default_semver(),
}
}
}
impl Version {
fn new(semver: &str) -> Self {
Self {
schema_version: semver.to_owned(),
}
}
fn latest() -> Self {
Self {
schema_version: LATEST_VERSION.to_string(),
}
}
fn default_semver() -> String {
LATEST_VERSION.to_string()
}
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Constructor)]
pub struct TrackerPolicy {
// Cleanup job configuration
/// Maximum time in seconds that a peer can be inactive before being
/// considered an inactive peer. If a peer is inactive for more than this
/// time, it will be removed from the torrent peer list.
#[serde(default = "TrackerPolicy::default_max_peer_timeout")]
pub max_peer_timeout: u32,
/// If enabled the tracker will persist the number of completed downloads.
/// That's how many times a torrent has been downloaded completely.
#[serde(default = "TrackerPolicy::default_persistent_torrent_completed_stat")]
pub persistent_torrent_completed_stat: bool,
/// If enabled, the tracker will remove torrents that have no peers.
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
/// seconds and it removes inactive peers. Eventually, the peer list of a
/// torrent could be empty and the torrent will be removed if this option is
/// enabled.
#[serde(default = "TrackerPolicy::default_remove_peerless_torrents")]
pub remove_peerless_torrents: bool,
}
impl Default for TrackerPolicy {
fn default() -> Self {
Self {
max_peer_timeout: Self::default_max_peer_timeout(),
persistent_torrent_completed_stat: Self::default_persistent_torrent_completed_stat(),
remove_peerless_torrents: Self::default_remove_peerless_torrents(),
}
}
}
impl TrackerPolicy {
fn default_max_peer_timeout() -> u32 {
900
}
fn default_persistent_torrent_completed_stat() -> bool {
false
}
fn default_remove_peerless_torrents() -> bool {
true
}
}
/// Information required for loading config
#[derive(Debug, Default, Clone)]
pub struct Info {
config_toml: Option<String>,
config_toml_path: String,
}
impl Info {
/// Build Configuration Info
///
/// # Errors
///
/// Will return `Err` if unable to obtain a configuration.
///
#[allow(clippy::needless_pass_by_value)]
pub fn new(default_config_toml_path: String) -> Result<Self, Error> {
let env_var_config_toml = ENV_VAR_CONFIG_TOML.to_string();
let env_var_config_toml_path = ENV_VAR_CONFIG_TOML_PATH.to_string();
let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) {
println!("Loading extra configuration from environment variable:\n {config_toml}");
Some(config_toml)
} else {
None
};
let config_toml_path = if let Ok(config_toml_path) = env::var(env_var_config_toml_path) {
println!("Loading extra configuration from file: `{config_toml_path}` ...");
config_toml_path
} else {
println!("Loading extra configuration from default configuration file: `{default_config_toml_path}` ...");
default_config_toml_path
};
Ok(Self {
config_toml,
config_toml_path,
})
}
}
/// Announce policy
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy, Constructor)]
pub struct AnnouncePolicy {
/// Interval in seconds that the client should wait between sending regular
/// announce requests to the tracker.
///
/// It's a **recommended** wait time between announcements.
///
/// This is the standard amount of time that clients should wait between
/// sending consecutive announcements to the tracker. This value is set by
/// the tracker and is typically provided in the tracker's response to a
/// client's initial request. It serves as a guideline for clients to know
/// how often they should contact the tracker for updates on the peer list,
/// while ensuring that the tracker is not overwhelmed with requests.
#[serde(default = "AnnouncePolicy::default_interval")]
pub interval: u32,
/// Minimum announce interval. Clients must not reannounce more frequently
/// than this.
///
/// It establishes the shortest allowed wait time.
///
/// This is an optional parameter in the protocol that the tracker may
/// provide in its response. It sets a lower limit on the frequency at which
/// clients are allowed to send announcements. Clients should respect this
/// value to prevent sending too many requests in a short period, which
/// could lead to excessive load on the tracker or even getting banned by
/// the tracker for not adhering to the rules.
#[serde(default = "AnnouncePolicy::default_interval_min")]
pub interval_min: u32,
}
impl Default for AnnouncePolicy {
fn default() -> Self {
Self {
interval: Self::default_interval(),
interval_min: Self::default_interval_min(),
}
}
}
impl AnnouncePolicy {
fn default_interval() -> u32 {
120
}
fn default_interval_min() -> u32 {
120
}
}
/// Errors that can occur when loading the configuration.
#[derive(Error, Debug)]
pub enum Error {
/// Unable to load the configuration from the environment variable.
/// This error only occurs if there is no configuration file and the
/// `TORRUST_TRACKER_CONFIG_TOML` environment variable is not set.
#[error("Unable to load from Environmental Variable: {source}")]
UnableToLoadFromEnvironmentVariable {
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
},
#[error("Unable to load from Config File: {source}")]
UnableToLoadFromConfigFile {
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
},
/// Unable to load the configuration from the configuration file.
#[error("Failed processing the configuration: {source}")]
ConfigError {
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
},
#[error("The error for errors that can never happen.")]
Infallible,
#[error("Unsupported configuration version: {version}")]
UnsupportedVersion { version: Version },
#[error("Missing mandatory configuration option. Option path: {path}")]
MissingMandatoryOption { path: String },
}
impl From<figment::Error> for Error {
#[track_caller]
fn from(err: figment::Error) -> Self {
Self::ConfigError {
source: (Arc::new(err) as DynError).into(),
}
}
}
#[serde_as]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)]
pub struct TslConfig {
/// Path to the SSL certificate file.
#[serde(default = "TslConfig::default_ssl_cert_path")]
pub ssl_cert_path: Utf8PathBuf,
/// Path to the SSL key file.
#[serde(default = "TslConfig::default_ssl_key_path")]
pub ssl_key_path: Utf8PathBuf,
}
impl TslConfig {
#[allow(clippy::unnecessary_wraps)]
fn default_ssl_cert_path() -> Utf8PathBuf {
Utf8PathBuf::new()
}
#[allow(clippy::unnecessary_wraps)]
fn default_ssl_key_path() -> Utf8PathBuf {
Utf8PathBuf::new()
}
}