Skip to content

Commit 5aad462

Browse files
committed
Merge #934: Config overhaul: split tracker mode
b6b841d chore: remove crate from ignore list in cargo machete (Jose Celano) f61c7c3 docs: add commments to core::Tracker struct fields (Jose Celano) 5a16ea1 refactor: [#932] make all Tracker fields private (Jose Celano) a5b9e14 refactor: inject the core config to the core tracker (Jose Celano) ca31c83 feat: [#932] replace `mode` core config option with `private` and `listed` flags (Jose Celano) f5d8dc6 refactor: [#932] WIP. Add new core config options: private and listed (Jose Celano) 2186809 refactor: [#932] sort config core section fields (Jose Celano) Pull request description: Replace `mode` core config option with `private` and `listed` flags: From: ```toml [core] mode = "public" tracker_usage_statistics = true inactive_peer_cleanup_interval = 600 ``` To: ```toml [core] inactive_peer_cleanup_interval = 600 listed = false private = false tracker_usage_statistics = true ``` ACKs for top commit: josecelano: ACK b6b841d Tree-SHA512: 66f5cb32bf13f19a539581d948faf9e13bf4e6c70b92d3866f3a8405f507dfa8f0da32048873f6e12d1f93d9f204fc9dd51692d77d46948e695060a7c4b754d0
2 parents 5f1fdbd + b6b841d commit 5aad462

23 files changed

Lines changed: 195 additions & 239 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ tower-http = { version = "0", features = ["compression-full", "cors", "propagate
8080
trace = "0"
8181
tracing = "0"
8282
tracing-subscriber = { version = "0.3.18", features = ["json"] }
83-
url = {version = "2", features = ["serde"] }
83+
url = { version = "2", features = ["serde"] }
8484
uuid = { version = "1", features = ["v4"] }
8585
zerocopy = "0.7.33"
8686

packages/configuration/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ const ENV_VAR_CONFIG_TOML: &str = "TORRUST_TRACKER_CONFIG_TOML";
3535
pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_TRACKER_CONFIG_TOML_PATH";
3636

3737
pub type Configuration = v1::Configuration;
38-
pub type UdpTracker = v1::udp_tracker::UdpTracker;
39-
pub type HttpTracker = v1::http_tracker::HttpTracker;
40-
pub type HttpApi = v1::tracker_api::HttpApi;
38+
pub type Core = v1::core::Core;
4139
pub type HealthCheckApi = v1::health_check_api::HealthCheckApi;
40+
pub type HttpApi = v1::tracker_api::HttpApi;
41+
pub type HttpTracker = v1::http_tracker::HttpTracker;
42+
pub type UdpTracker = v1::udp_tracker::UdpTracker;
4243

4344
pub type AccessTokens = HashMap<String, String>;
4445

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use serde::{Deserialize, Serialize};
2-
use torrust_tracker_primitives::TrackerMode;
32

43
use super::network::Network;
54
use crate::v1::database::Database;
@@ -8,27 +7,6 @@ use crate::{AnnouncePolicy, TrackerPolicy};
87
#[allow(clippy::struct_excessive_bools)]
98
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
109
pub struct Core {
11-
/// Tracker mode. See [`TrackerMode`] for more information.
12-
#[serde(default = "Core::default_mode")]
13-
pub mode: TrackerMode,
14-
15-
/// Weather the tracker should collect statistics about tracker usage.
16-
/// If enabled, the tracker will collect statistics like the number of
17-
/// connections handled, the number of announce requests handled, etc.
18-
/// Refer to the [`Tracker`](https://docs.rs/torrust-tracker) for more
19-
/// information about the collected metrics.
20-
#[serde(default = "Core::default_tracker_usage_statistics")]
21-
pub tracker_usage_statistics: bool,
22-
23-
/// Interval in seconds that the cleanup job will run to remove inactive
24-
/// peers from the torrent peer list.
25-
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
26-
pub inactive_peer_cleanup_interval: u64,
27-
28-
// Tracker policy configuration.
29-
#[serde(default = "Core::default_tracker_policy")]
30-
pub tracker_policy: TrackerPolicy,
31-
3210
// Announce policy configuration.
3311
#[serde(default = "Core::default_announce_policy")]
3412
pub announce_policy: AnnouncePolicy,
@@ -37,51 +15,80 @@ pub struct Core {
3715
#[serde(default = "Core::default_database")]
3816
pub database: Database,
3917

18+
/// Interval in seconds that the cleanup job will run to remove inactive
19+
/// peers from the torrent peer list.
20+
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
21+
pub inactive_peer_cleanup_interval: u64,
22+
23+
// When `true` only approved torrents can be announced in the tracker.
24+
#[serde(default = "Core::default_listed")]
25+
pub listed: bool,
26+
4027
// Network configuration.
4128
#[serde(default = "Core::default_network")]
4229
pub net: Network,
30+
31+
// When `true` clients require a key to connect and use the tracker.
32+
#[serde(default = "Core::default_private")]
33+
pub private: bool,
34+
35+
// Tracker policy configuration.
36+
#[serde(default = "Core::default_tracker_policy")]
37+
pub tracker_policy: TrackerPolicy,
38+
39+
/// Weather the tracker should collect statistics about tracker usage.
40+
/// If enabled, the tracker will collect statistics like the number of
41+
/// connections handled, the number of announce requests handled, etc.
42+
/// Refer to the [`Tracker`](https://docs.rs/torrust-tracker) for more
43+
/// information about the collected metrics.
44+
#[serde(default = "Core::default_tracker_usage_statistics")]
45+
pub tracker_usage_statistics: bool,
4346
}
4447

4548
impl Default for Core {
4649
fn default() -> Self {
4750
Self {
48-
mode: Self::default_mode(),
49-
tracker_usage_statistics: Self::default_tracker_usage_statistics(),
50-
inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(),
51-
tracker_policy: Self::default_tracker_policy(),
5251
announce_policy: Self::default_announce_policy(),
5352
database: Self::default_database(),
53+
inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(),
54+
listed: Self::default_listed(),
5455
net: Self::default_network(),
56+
private: Self::default_private(),
57+
tracker_policy: Self::default_tracker_policy(),
58+
tracker_usage_statistics: Self::default_tracker_usage_statistics(),
5559
}
5660
}
5761
}
5862

5963
impl Core {
60-
fn default_mode() -> TrackerMode {
61-
TrackerMode::Public
64+
fn default_announce_policy() -> AnnouncePolicy {
65+
AnnouncePolicy::default()
6266
}
6367

64-
fn default_tracker_usage_statistics() -> bool {
65-
true
68+
fn default_database() -> Database {
69+
Database::default()
6670
}
6771

6872
fn default_inactive_peer_cleanup_interval() -> u64 {
6973
600
7074
}
7175

72-
fn default_tracker_policy() -> TrackerPolicy {
73-
TrackerPolicy::default()
76+
fn default_listed() -> bool {
77+
false
7478
}
7579

76-
fn default_announce_policy() -> AnnouncePolicy {
77-
AnnouncePolicy::default()
80+
fn default_network() -> Network {
81+
Network::default()
7882
}
7983

80-
fn default_database() -> Database {
81-
Database::default()
84+
fn default_private() -> bool {
85+
false
8286
}
8387

84-
fn default_network() -> Network {
85-
Network::default()
88+
fn default_tracker_policy() -> TrackerPolicy {
89+
TrackerPolicy::default()
90+
}
91+
fn default_tracker_usage_statistics() -> bool {
92+
true
8693
}
8794
}

packages/configuration/src/v1/mod.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,10 @@
199199
//! log_level = "info"
200200
//!
201201
//! [core]
202-
//! mode = "public"
203-
//! tracker_usage_statistics = true
204202
//! inactive_peer_cleanup_interval = 600
205-
//!
206-
//! [core.tracker_policy]
207-
//! max_peer_timeout = 900
208-
//! persistent_torrent_completed_stat = false
209-
//! remove_peerless_torrents = true
203+
//! listed = false
204+
//! private = false
205+
//! tracker_usage_statistics = true
210206
//!
211207
//! [core.announce_policy]
212208
//! interval = 120
@@ -220,6 +216,11 @@
220216
//! external_ip = "0.0.0.0"
221217
//! on_reverse_proxy = false
222218
//!
219+
//! [core.tracker_policy]
220+
//! max_peer_timeout = 900
221+
//! persistent_torrent_completed_stat = false
222+
//! remove_peerless_torrents = true
223+
//!
223224
//! [http_api]
224225
//! bind_address = "127.0.0.1:1212"
225226
//!
@@ -365,14 +366,10 @@ mod tests {
365366
log_level = "info"
366367
367368
[core]
368-
mode = "public"
369-
tracker_usage_statistics = true
370369
inactive_peer_cleanup_interval = 600
371-
372-
[core.tracker_policy]
373-
max_peer_timeout = 900
374-
persistent_torrent_completed_stat = false
375-
remove_peerless_torrents = true
370+
listed = false
371+
private = false
372+
tracker_usage_statistics = true
376373
377374
[core.announce_policy]
378375
interval = 120
@@ -386,6 +383,11 @@ mod tests {
386383
external_ip = "0.0.0.0"
387384
on_reverse_proxy = false
388385
386+
[core.tracker_policy]
387+
max_peer_timeout = 900
388+
persistent_torrent_completed_stat = false
389+
remove_peerless_torrents = true
390+
389391
[health_check_api]
390392
bind_address = "127.0.0.1:1313"
391393
"#

packages/primitives/src/lib.rs

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
//! by the tracker server crate, but also by other crates in the Torrust
66
//! ecosystem.
77
use std::collections::BTreeMap;
8-
use std::fmt;
9-
use std::str::FromStr;
108
use std::time::Duration;
119

1210
use info_hash::InfoHash;
@@ -64,70 +62,3 @@ pub enum DatabaseDriver {
6462
}
6563

6664
pub type PersistentTorrents = BTreeMap<InfoHash, u32>;
67-
68-
/// The mode the tracker will run in.
69-
///
70-
/// Refer to [Torrust Tracker Configuration](https://docs.rs/torrust-tracker-configuration)
71-
/// to know how to configure the tracker to run in each mode.
72-
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
73-
pub enum TrackerMode {
74-
/// Will track every new info hash and serve every peer.
75-
#[serde(rename = "public")]
76-
Public,
77-
78-
/// Will only track whitelisted info hashes.
79-
#[serde(rename = "listed")]
80-
Listed,
81-
82-
/// Will only serve authenticated peers
83-
#[serde(rename = "private")]
84-
Private,
85-
86-
/// Will only track whitelisted info hashes and serve authenticated peers
87-
#[serde(rename = "private_listed")]
88-
PrivateListed,
89-
}
90-
91-
impl Default for TrackerMode {
92-
fn default() -> Self {
93-
Self::Public
94-
}
95-
}
96-
97-
impl fmt::Display for TrackerMode {
98-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99-
let display_str = match self {
100-
TrackerMode::Public => "public",
101-
TrackerMode::Listed => "listed",
102-
TrackerMode::Private => "private",
103-
TrackerMode::PrivateListed => "private_listed",
104-
};
105-
write!(f, "{display_str}")
106-
}
107-
}
108-
109-
impl FromStr for TrackerMode {
110-
type Err = String;
111-
112-
fn from_str(s: &str) -> Result<Self, Self::Err> {
113-
match s.to_lowercase().as_str() {
114-
"public" => Ok(TrackerMode::Public),
115-
"listed" => Ok(TrackerMode::Listed),
116-
"private" => Ok(TrackerMode::Private),
117-
"private_listed" => Ok(TrackerMode::PrivateListed),
118-
_ => Err(format!("Unknown tracker mode: {s}")),
119-
}
120-
}
121-
}
122-
123-
impl TrackerMode {
124-
#[must_use]
125-
pub fn is_open(&self) -> bool {
126-
matches!(self, TrackerMode::Public | TrackerMode::Listed)
127-
}
128-
129-
#[must_use]
130-
pub fn is_close(&self) -> bool {
131-
!self.is_open()
132-
}
133-
}

packages/test-helpers/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ version.workspace = true
1717
[dependencies]
1818
rand = "0"
1919
torrust-tracker-configuration = { version = "3.0.0-alpha.12-develop", path = "../configuration" }
20-
torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../primitives" }

packages/test-helpers/src/configuration.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::env;
33
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
44

55
use torrust_tracker_configuration::{Configuration, HttpApi, HttpTracker, LogLevel, UdpTracker};
6-
use torrust_tracker_primitives::TrackerMode;
76

87
use crate::random;
98

@@ -86,40 +85,41 @@ pub fn ephemeral_without_reverse_proxy() -> Configuration {
8685

8786
/// Ephemeral configuration with `public` mode.
8887
#[must_use]
89-
pub fn ephemeral_mode_public() -> Configuration {
88+
pub fn ephemeral_public() -> Configuration {
9089
let mut cfg = ephemeral();
9190

92-
cfg.core.mode = TrackerMode::Public;
91+
cfg.core.private = false;
9392

9493
cfg
9594
}
9695

9796
/// Ephemeral configuration with `private` mode.
9897
#[must_use]
99-
pub fn ephemeral_mode_private() -> Configuration {
98+
pub fn ephemeral_private() -> Configuration {
10099
let mut cfg = ephemeral();
101100

102-
cfg.core.mode = TrackerMode::Private;
101+
cfg.core.private = true;
103102

104103
cfg
105104
}
106105

107106
/// Ephemeral configuration with `listed` mode.
108107
#[must_use]
109-
pub fn ephemeral_mode_whitelisted() -> Configuration {
108+
pub fn ephemeral_listed() -> Configuration {
110109
let mut cfg = ephemeral();
111110

112-
cfg.core.mode = TrackerMode::Listed;
111+
cfg.core.listed = true;
113112

114113
cfg
115114
}
116115

117116
/// Ephemeral configuration with `private_listed` mode.
118117
#[must_use]
119-
pub fn ephemeral_mode_private_whitelisted() -> Configuration {
118+
pub fn ephemeral_private_and_listed() -> Configuration {
120119
let mut cfg = ephemeral();
121120

122-
cfg.core.mode = TrackerMode::PrivateListed;
121+
cfg.core.private = true;
122+
cfg.core.listed = true;
123123

124124
cfg
125125
}

src/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<J
5151
}
5252

5353
// Load whitelisted torrents
54-
if tracker.is_whitelisted() {
54+
if tracker.is_listed() {
5555
tracker
5656
.load_whitelist_from_database()
5757
.await
@@ -64,8 +64,8 @@ pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<J
6464
for udp_tracker_config in udp_trackers {
6565
if tracker.is_private() {
6666
warn!(
67-
"Could not start UDP tracker on: {} while in {:?}. UDP is not safe for private trackers!",
68-
udp_tracker_config.bind_address, config.core.mode
67+
"Could not start UDP tracker on: {} while in private mode. UDP is not safe for private trackers!",
68+
udp_tracker_config.bind_address
6969
);
7070
} else {
7171
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone(), registar.give_form()).await);

0 commit comments

Comments
 (0)