Skip to content

Commit 36c7ed1

Browse files
josecelanoda2ce7
authored andcommitted
feat!: [torrust#878] extract net section in core config section
1 parent 4a39d65 commit 36c7ed1

File tree

8 files changed

+99
-63
lines changed

8 files changed

+99
-63
lines changed

packages/configuration/src/v1/core.rs

+19-36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::net::{IpAddr, Ipv4Addr};
2-
31
use serde::{Deserialize, Serialize};
42
use torrust_tracker_primitives::TrackerMode;
53

4+
use super::network::Network;
65
use crate::v1::database::Database;
76
use crate::AnnouncePolicy;
87

@@ -13,10 +12,6 @@ pub struct Core {
1312
#[serde(default = "Core::default_mode")]
1413
pub mode: TrackerMode,
1514

16-
// Database configuration.
17-
#[serde(default = "Core::default_database")]
18-
pub database: Database,
19-
2015
/// See [`AnnouncePolicy::interval`]
2116
#[serde(default = "AnnouncePolicy::default_interval")]
2217
pub announce_interval: u32,
@@ -25,20 +20,6 @@ pub struct Core {
2520
#[serde(default = "AnnouncePolicy::default_interval_min")]
2621
pub min_announce_interval: u32,
2722

28-
/// Weather the tracker is behind a reverse proxy or not.
29-
/// If the tracker is behind a reverse proxy, the `X-Forwarded-For` header
30-
/// sent from the proxy will be used to get the client's IP address.
31-
#[serde(default = "Core::default_on_reverse_proxy")]
32-
pub on_reverse_proxy: bool,
33-
34-
/// The external IP address of the tracker. If the client is using a
35-
/// loopback IP address, this IP address will be used instead. If the peer
36-
/// is using a loopback IP address, the tracker assumes that the peer is
37-
/// in the same network as the tracker and will use the tracker's IP
38-
/// address instead.
39-
#[serde(default = "Core::default_external_ip")]
40-
pub external_ip: Option<IpAddr>,
41-
4223
/// Weather the tracker should collect statistics about tracker usage.
4324
/// If enabled, the tracker will collect statistics like the number of
4425
/// connections handled, the number of announce requests handled, etc.
@@ -71,6 +52,14 @@ pub struct Core {
7152
/// enabled.
7253
#[serde(default = "Core::default_remove_peerless_torrents")]
7354
pub remove_peerless_torrents: bool,
55+
56+
// Database configuration.
57+
#[serde(default = "Core::default_database")]
58+
pub database: Database,
59+
60+
// Network configuration.
61+
#[serde(default = "Core::default_network")]
62+
pub net: Network,
7463
}
7564

7665
impl Default for Core {
@@ -79,16 +68,15 @@ impl Default for Core {
7968

8069
Self {
8170
mode: Self::default_mode(),
82-
database: Self::default_database(),
8371
announce_interval: announce_policy.interval,
8472
min_announce_interval: announce_policy.interval_min,
8573
max_peer_timeout: Self::default_max_peer_timeout(),
86-
on_reverse_proxy: Self::default_on_reverse_proxy(),
87-
external_ip: Self::default_external_ip(),
8874
tracker_usage_statistics: Self::default_tracker_usage_statistics(),
8975
persistent_torrent_completed_stat: Self::default_persistent_torrent_completed_stat(),
9076
inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(),
9177
remove_peerless_torrents: Self::default_remove_peerless_torrents(),
78+
database: Self::default_database(),
79+
net: Self::default_network(),
9280
}
9381
}
9482
}
@@ -98,19 +86,6 @@ impl Core {
9886
TrackerMode::Public
9987
}
10088

101-
fn default_database() -> Database {
102-
Database::default()
103-
}
104-
105-
fn default_on_reverse_proxy() -> bool {
106-
false
107-
}
108-
109-
#[allow(clippy::unnecessary_wraps)]
110-
fn default_external_ip() -> Option<IpAddr> {
111-
Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))
112-
}
113-
11489
fn default_tracker_usage_statistics() -> bool {
11590
true
11691
}
@@ -130,4 +105,12 @@ impl Core {
130105
fn default_remove_peerless_torrents() -> bool {
131106
true
132107
}
108+
109+
fn default_database() -> Database {
110+
Database::default()
111+
}
112+
113+
fn default_network() -> Network {
114+
Network::default()
115+
}
133116
}

packages/configuration/src/v1/mod.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,19 @@
200200
//! mode = "public"
201201
//! announce_interval = 120
202202
//! min_announce_interval = 120
203-
//! on_reverse_proxy = false
204-
//! external_ip = "0.0.0.0"
205203
//! tracker_usage_statistics = true
206204
//! persistent_torrent_completed_stat = false
207205
//! max_peer_timeout = 900
208206
//! inactive_peer_cleanup_interval = 600
209207
//! remove_peerless_torrents = true
210208
//!
211-
//! [core.database]
212-
//! driver = "Sqlite3"
213-
//! path = "./storage/tracker/lib/database/sqlite3.db"
209+
//! [core.database]
210+
//! driver = "Sqlite3"
211+
//! path = "./storage/tracker/lib/database/sqlite3.db"
212+
//!
213+
//! [core.net]
214+
//! external_ip = "0.0.0.0"
215+
//! on_reverse_proxy = false
214216
//!
215217
//! [[udp_trackers]]
216218
//! enabled = false
@@ -240,6 +242,7 @@ pub mod database;
240242
pub mod health_check_api;
241243
pub mod http_tracker;
242244
pub mod logging;
245+
pub mod network;
243246
pub mod tracker_api;
244247
pub mod udp_tracker;
245248

@@ -307,7 +310,7 @@ impl Configuration {
307310
/// and `None` otherwise.
308311
#[must_use]
309312
pub fn get_ext_ip(&self) -> Option<IpAddr> {
310-
self.core.external_ip.as_ref().map(|external_ip| *external_ip)
313+
self.core.net.external_ip.as_ref().map(|external_ip| *external_ip)
311314
}
312315

313316
/// Saves the default configuration at the given path.
@@ -387,18 +390,20 @@ mod tests {
387390
mode = "public"
388391
announce_interval = 120
389392
min_announce_interval = 120
390-
on_reverse_proxy = false
391-
external_ip = "0.0.0.0"
392393
tracker_usage_statistics = true
393394
persistent_torrent_completed_stat = false
394395
max_peer_timeout = 900
395396
inactive_peer_cleanup_interval = 600
396397
remove_peerless_torrents = true
397398
398-
[core.database]
399-
driver = "Sqlite3"
400-
path = "./storage/tracker/lib/database/sqlite3.db"
401-
399+
[core.database]
400+
driver = "Sqlite3"
401+
path = "./storage/tracker/lib/database/sqlite3.db"
402+
403+
[core.net]
404+
external_ip = "0.0.0.0"
405+
on_reverse_proxy = false
406+
402407
[[udp_trackers]]
403408
enabled = false
404409
bind_address = "0.0.0.0:6969"
@@ -443,7 +448,10 @@ mod tests {
443448
fn configuration_should_contain_the_external_ip() {
444449
let configuration = Configuration::default();
445450

446-
assert_eq!(configuration.core.external_ip, Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
451+
assert_eq!(
452+
configuration.core.net.external_ip,
453+
Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))
454+
);
447455
}
448456

449457
#[test]
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::net::{IpAddr, Ipv4Addr};
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
#[allow(clippy::struct_excessive_bools)]
6+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
7+
pub struct Network {
8+
/// The external IP address of the tracker. If the client is using a
9+
/// loopback IP address, this IP address will be used instead. If the peer
10+
/// is using a loopback IP address, the tracker assumes that the peer is
11+
/// in the same network as the tracker and will use the tracker's IP
12+
/// address instead.
13+
#[serde(default = "Network::default_external_ip")]
14+
pub external_ip: Option<IpAddr>,
15+
16+
/// Weather the tracker is behind a reverse proxy or not.
17+
/// If the tracker is behind a reverse proxy, the `X-Forwarded-For` header
18+
/// sent from the proxy will be used to get the client's IP address.
19+
#[serde(default = "Network::default_on_reverse_proxy")]
20+
pub on_reverse_proxy: bool,
21+
}
22+
23+
impl Default for Network {
24+
fn default() -> Self {
25+
Self {
26+
external_ip: Self::default_external_ip(),
27+
on_reverse_proxy: Self::default_on_reverse_proxy(),
28+
}
29+
}
30+
}
31+
32+
impl Network {
33+
#[allow(clippy::unnecessary_wraps)]
34+
fn default_external_ip() -> Option<IpAddr> {
35+
Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))
36+
}
37+
38+
fn default_on_reverse_proxy() -> bool {
39+
false
40+
}
41+
}

packages/test-helpers/src/configuration.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn ephemeral() -> Configuration {
6464
pub fn ephemeral_with_reverse_proxy() -> Configuration {
6565
let mut cfg = ephemeral();
6666

67-
cfg.core.on_reverse_proxy = true;
67+
cfg.core.net.on_reverse_proxy = true;
6868

6969
cfg
7070
}
@@ -74,7 +74,7 @@ pub fn ephemeral_with_reverse_proxy() -> Configuration {
7474
pub fn ephemeral_without_reverse_proxy() -> Configuration {
7575
let mut cfg = ephemeral();
7676

77-
cfg.core.on_reverse_proxy = false;
77+
cfg.core.net.on_reverse_proxy = false;
7878

7979
cfg
8080
}
@@ -124,7 +124,7 @@ pub fn ephemeral_mode_private_whitelisted() -> Configuration {
124124
pub fn ephemeral_with_external_ip(ip: IpAddr) -> Configuration {
125125
let mut cfg = ephemeral();
126126

127-
cfg.core.external_ip = Some(ip);
127+
cfg.core.net.external_ip = Some(ip);
128128

129129
cfg
130130
}

src/core/mod.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,18 @@
320320
//! announce_interval = 120
321321
//! min_announce_interval = 120
322322
//! max_peer_timeout = 900
323-
//! on_reverse_proxy = false
324-
//! external_ip = "2.137.87.41"
325323
//! tracker_usage_statistics = true
326324
//! persistent_torrent_completed_stat = true
327325
//! inactive_peer_cleanup_interval = 600
328326
//! remove_peerless_torrents = false
329327
//!
330-
//! [core.database]
331-
//! driver = "Sqlite3"
332-
//! path = "./storage/tracker/lib/database/sqlite3.db"
328+
//! [core.database]
329+
//! driver = "Sqlite3"
330+
//! path = "./storage/tracker/lib/database/sqlite3.db"
331+
//!
332+
//! [core.net]
333+
//! on_reverse_proxy = false
334+
//! external_ip = "2.137.87.41"
333335
//! ```
334336
//!
335337
//! Refer to the [`configuration` module documentation](https://docs.rs/torrust-tracker-configuration) to get more information about all options.
@@ -566,13 +568,13 @@ impl Tracker {
566568
stats_event_sender,
567569
stats_repository,
568570
database,
569-
external_ip: config.external_ip,
571+
external_ip: config.net.external_ip,
570572
policy: TrackerPolicy::new(
571573
config.remove_peerless_torrents,
572574
config.max_peer_timeout,
573575
config.persistent_torrent_completed_stat,
574576
),
575-
on_reverse_proxy: config.on_reverse_proxy,
577+
on_reverse_proxy: config.net.on_reverse_proxy,
576578
})
577579
}
578580

src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,10 @@
172172
//!
173173
//! [core]
174174
//! announce_interval = 120
175-
//! external_ip = "0.0.0.0"
176175
//! inactive_peer_cleanup_interval = 600
177176
//! max_peer_timeout = 900
178177
//! min_announce_interval = 120
179178
//! mode = "public"
180-
//! on_reverse_proxy = false
181179
//! persistent_torrent_completed_stat = false
182180
//! remove_peerless_torrents = true
183181
//! tracker_usage_statistics = true
@@ -186,6 +184,10 @@
186184
//! driver = "Sqlite3"
187185
//! path = "./storage/tracker/lib/database/sqlite3.db"
188186
//!
187+
//! [core.net]
188+
//! external_ip = "0.0.0.0"
189+
//! on_reverse_proxy = false
190+
//!
189191
//! [[udp_trackers]]
190192
//! bind_address = "0.0.0.0:6969"
191193
//! enabled = false

src/servers/http/v1/services/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ mod tests {
151151

152152
fn tracker_with_an_ipv6_external_ip(stats_event_sender: Box<dyn statistics::EventSender>) -> Tracker {
153153
let mut configuration = configuration::ephemeral();
154-
configuration.core.external_ip = Some(IpAddr::V6(Ipv6Addr::new(
154+
configuration.core.net.external_ip = Some(IpAddr::V6(Ipv6Addr::new(
155155
0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969,
156156
)));
157157

src/servers/udp/v0/handlers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ mod tests {
425425
}
426426

427427
pub fn with_external_ip(mut self, external_ip: &str) -> Self {
428-
self.configuration.core.external_ip = Some(external_ip.to_owned().parse().expect("valid IP address"));
428+
self.configuration.core.net.external_ip = Some(external_ip.to_owned().parse().expect("valid IP address"));
429429
self
430430
}
431431

0 commit comments

Comments
 (0)