From 2186809608de314d915aba8c5738a4ecb40fd10c Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 1 Jul 2024 10:26:02 +0100 Subject: [PATCH 1/7] refactor: [#932] sort config core section fields When you format a toml file with a linter it sorts the keys alphabetically. It's good to have the same order in the code. It's also a common practice for JSON. This helps to make serialization deterministic. --- packages/configuration/src/v1/core.rs | 75 +++++++++++++-------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/packages/configuration/src/v1/core.rs b/packages/configuration/src/v1/core.rs index 49fdf2a80..31da85915 100644 --- a/packages/configuration/src/v1/core.rs +++ b/packages/configuration/src/v1/core.rs @@ -8,27 +8,6 @@ use crate::{AnnouncePolicy, TrackerPolicy}; #[allow(clippy::struct_excessive_bools)] #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] pub struct Core { - /// Tracker mode. See [`TrackerMode`] for more information. - #[serde(default = "Core::default_mode")] - pub mode: TrackerMode, - - /// Weather the tracker should collect statistics about tracker usage. - /// If enabled, the tracker will collect statistics like the number of - /// connections handled, the number of announce requests handled, etc. - /// Refer to the [`Tracker`](https://docs.rs/torrust-tracker) for more - /// information about the collected metrics. - #[serde(default = "Core::default_tracker_usage_statistics")] - pub tracker_usage_statistics: bool, - - /// Interval in seconds that the cleanup job will run to remove inactive - /// peers from the torrent peer list. - #[serde(default = "Core::default_inactive_peer_cleanup_interval")] - pub inactive_peer_cleanup_interval: u64, - - // Tracker policy configuration. - #[serde(default = "Core::default_tracker_policy")] - pub tracker_policy: TrackerPolicy, - // Announce policy configuration. #[serde(default = "Core::default_announce_policy")] pub announce_policy: AnnouncePolicy, @@ -37,51 +16,71 @@ pub struct Core { #[serde(default = "Core::default_database")] pub database: Database, + /// Interval in seconds that the cleanup job will run to remove inactive + /// peers from the torrent peer list. + #[serde(default = "Core::default_inactive_peer_cleanup_interval")] + pub inactive_peer_cleanup_interval: u64, + + /// Tracker mode. See [`TrackerMode`] for more information. + #[serde(default = "Core::default_mode")] + pub mode: TrackerMode, + // Network configuration. #[serde(default = "Core::default_network")] pub net: Network, + + // Tracker policy configuration. + #[serde(default = "Core::default_tracker_policy")] + pub tracker_policy: TrackerPolicy, + + /// Weather the tracker should collect statistics about tracker usage. + /// If enabled, the tracker will collect statistics like the number of + /// connections handled, the number of announce requests handled, etc. + /// Refer to the [`Tracker`](https://docs.rs/torrust-tracker) for more + /// information about the collected metrics. + #[serde(default = "Core::default_tracker_usage_statistics")] + pub tracker_usage_statistics: bool, } impl Default for Core { fn default() -> Self { Self { - mode: Self::default_mode(), - tracker_usage_statistics: Self::default_tracker_usage_statistics(), - inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(), - tracker_policy: Self::default_tracker_policy(), announce_policy: Self::default_announce_policy(), database: Self::default_database(), + inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(), + mode: Self::default_mode(), net: Self::default_network(), + tracker_policy: Self::default_tracker_policy(), + tracker_usage_statistics: Self::default_tracker_usage_statistics(), } } } impl Core { - fn default_mode() -> TrackerMode { - TrackerMode::Public + fn default_announce_policy() -> AnnouncePolicy { + AnnouncePolicy::default() } - fn default_tracker_usage_statistics() -> bool { - true + fn default_database() -> Database { + Database::default() } fn default_inactive_peer_cleanup_interval() -> u64 { 600 } - fn default_tracker_policy() -> TrackerPolicy { - TrackerPolicy::default() + fn default_mode() -> TrackerMode { + TrackerMode::Public } - fn default_announce_policy() -> AnnouncePolicy { - AnnouncePolicy::default() + fn default_network() -> Network { + Network::default() } - fn default_database() -> Database { - Database::default() + fn default_tracker_policy() -> TrackerPolicy { + TrackerPolicy::default() } - - fn default_network() -> Network { - Network::default() + fn default_tracker_usage_statistics() -> bool { + true } } From f5d8dc6679e349578d69ed56251f30cc7f23f976 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 1 Jul 2024 10:40:08 +0100 Subject: [PATCH 2/7] refactor: [#932] WIP. Add new core config options: private and listed --- packages/configuration/src/v1/core.rs | 18 ++++++++++++++++++ packages/configuration/src/v1/mod.rs | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/configuration/src/v1/core.rs b/packages/configuration/src/v1/core.rs index 31da85915..9f3af36b6 100644 --- a/packages/configuration/src/v1/core.rs +++ b/packages/configuration/src/v1/core.rs @@ -21,6 +21,10 @@ pub struct Core { #[serde(default = "Core::default_inactive_peer_cleanup_interval")] pub inactive_peer_cleanup_interval: u64, + // Whe `true` only approved torrents can be announced in the tracker. + #[serde(default = "Core::default_listed")] + pub listed: bool, + /// Tracker mode. See [`TrackerMode`] for more information. #[serde(default = "Core::default_mode")] pub mode: TrackerMode, @@ -29,6 +33,10 @@ pub struct Core { #[serde(default = "Core::default_network")] pub net: Network, + // Whe `true` clients require a key to connect and use the tracker. + #[serde(default = "Core::default_private")] + pub private: bool, + // Tracker policy configuration. #[serde(default = "Core::default_tracker_policy")] pub tracker_policy: TrackerPolicy, @@ -48,8 +56,10 @@ impl Default for Core { announce_policy: Self::default_announce_policy(), database: Self::default_database(), inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(), + listed: Self::default_listed(), mode: Self::default_mode(), net: Self::default_network(), + private: Self::default_private(), tracker_policy: Self::default_tracker_policy(), tracker_usage_statistics: Self::default_tracker_usage_statistics(), } @@ -69,6 +79,10 @@ impl Core { 600 } + fn default_listed() -> bool { + false + } + fn default_mode() -> TrackerMode { TrackerMode::Public } @@ -77,6 +91,10 @@ impl Core { Network::default() } + fn default_private() -> bool { + false + } + fn default_tracker_policy() -> TrackerPolicy { TrackerPolicy::default() } diff --git a/packages/configuration/src/v1/mod.rs b/packages/configuration/src/v1/mod.rs index 546f55b6e..080edde70 100644 --- a/packages/configuration/src/v1/mod.rs +++ b/packages/configuration/src/v1/mod.rs @@ -366,8 +366,10 @@ mod tests { [core] mode = "public" - tracker_usage_statistics = true inactive_peer_cleanup_interval = 600 + listed = false + private = false + tracker_usage_statistics = true [core.tracker_policy] max_peer_timeout = 900 From ca31c835ed08503f89c8470eddcc84381847e959 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 1 Jul 2024 11:22:06 +0100 Subject: [PATCH 3/7] feat: [#932] 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 ``` --- Cargo.lock | 1 - Cargo.toml | 4 +- packages/configuration/src/v1/core.rs | 14 +---- packages/configuration/src/v1/mod.rs | 26 ++++---- packages/primitives/src/lib.rs | 69 ---------------------- packages/test-helpers/Cargo.toml | 1 - packages/test-helpers/src/configuration.rs | 18 +++--- src/app.rs | 6 +- src/bootstrap/jobs/http_tracker.rs | 4 +- src/bootstrap/jobs/tracker_apis.rs | 4 +- src/core/mod.rs | 41 ++++++------- src/lib.rs | 13 ++-- src/servers/apis/server.rs | 4 +- src/servers/http/server.rs | 4 +- src/servers/http/v1/handlers/announce.rs | 4 +- src/servers/http/v1/handlers/scrape.rs | 4 +- src/servers/http/v1/services/announce.rs | 2 +- src/servers/http/v1/services/scrape.rs | 2 +- src/servers/udp/handlers.rs | 6 +- src/servers/udp/server/mod.rs | 6 +- tests/servers/http/v1/contract.rs | 62 +++++++++---------- 21 files changed, 108 insertions(+), 187 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f977949ee..3a403332a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4054,7 +4054,6 @@ version = "3.0.0-alpha.12-develop" dependencies = [ "rand", "torrust-tracker-configuration", - "torrust-tracker-primitives", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index a65c2a74d..3eca9934d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,12 +80,12 @@ tower-http = { version = "0", features = ["compression-full", "cors", "propagate trace = "0" tracing = "0" tracing-subscriber = { version = "0.3.18", features = ["json"] } -url = {version = "2", features = ["serde"] } +url = { version = "2", features = ["serde"] } uuid = { version = "1", features = ["v4"] } zerocopy = "0.7.33" [package.metadata.cargo-machete] -ignored = ["crossbeam-skiplist", "dashmap", "figment", "parking_lot", "serde_bytes"] +ignored = ["crossbeam-skiplist", "dashmap", "figment", "parking_lot", "serde_bytes", "torrust-tracker-primitives"] [dev-dependencies] local-ip-address = "0" diff --git a/packages/configuration/src/v1/core.rs b/packages/configuration/src/v1/core.rs index 9f3af36b6..1f0a0f957 100644 --- a/packages/configuration/src/v1/core.rs +++ b/packages/configuration/src/v1/core.rs @@ -1,5 +1,4 @@ use serde::{Deserialize, Serialize}; -use torrust_tracker_primitives::TrackerMode; use super::network::Network; use crate::v1::database::Database; @@ -21,19 +20,15 @@ pub struct Core { #[serde(default = "Core::default_inactive_peer_cleanup_interval")] pub inactive_peer_cleanup_interval: u64, - // Whe `true` only approved torrents can be announced in the tracker. + // When `true` only approved torrents can be announced in the tracker. #[serde(default = "Core::default_listed")] pub listed: bool, - /// Tracker mode. See [`TrackerMode`] for more information. - #[serde(default = "Core::default_mode")] - pub mode: TrackerMode, - // Network configuration. #[serde(default = "Core::default_network")] pub net: Network, - // Whe `true` clients require a key to connect and use the tracker. + // When `true` clients require a key to connect and use the tracker. #[serde(default = "Core::default_private")] pub private: bool, @@ -57,7 +52,6 @@ impl Default for Core { database: Self::default_database(), inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(), listed: Self::default_listed(), - mode: Self::default_mode(), net: Self::default_network(), private: Self::default_private(), tracker_policy: Self::default_tracker_policy(), @@ -83,10 +77,6 @@ impl Core { false } - fn default_mode() -> TrackerMode { - TrackerMode::Public - } - fn default_network() -> Network { Network::default() } diff --git a/packages/configuration/src/v1/mod.rs b/packages/configuration/src/v1/mod.rs index 080edde70..c5e0f9f7a 100644 --- a/packages/configuration/src/v1/mod.rs +++ b/packages/configuration/src/v1/mod.rs @@ -199,14 +199,10 @@ //! log_level = "info" //! //! [core] -//! mode = "public" -//! tracker_usage_statistics = true //! inactive_peer_cleanup_interval = 600 -//! -//! [core.tracker_policy] -//! max_peer_timeout = 900 -//! persistent_torrent_completed_stat = false -//! remove_peerless_torrents = true +//! listed = false +//! private = false +//! tracker_usage_statistics = true //! //! [core.announce_policy] //! interval = 120 @@ -220,6 +216,11 @@ //! external_ip = "0.0.0.0" //! on_reverse_proxy = false //! +//! [core.tracker_policy] +//! max_peer_timeout = 900 +//! persistent_torrent_completed_stat = false +//! remove_peerless_torrents = true +//! //! [http_api] //! bind_address = "127.0.0.1:1212" //! @@ -365,17 +366,11 @@ mod tests { log_level = "info" [core] - mode = "public" inactive_peer_cleanup_interval = 600 listed = false private = false tracker_usage_statistics = true - [core.tracker_policy] - max_peer_timeout = 900 - persistent_torrent_completed_stat = false - remove_peerless_torrents = true - [core.announce_policy] interval = 120 interval_min = 120 @@ -388,6 +383,11 @@ mod tests { external_ip = "0.0.0.0" on_reverse_proxy = false + [core.tracker_policy] + max_peer_timeout = 900 + persistent_torrent_completed_stat = false + remove_peerless_torrents = true + [health_check_api] bind_address = "127.0.0.1:1313" "# diff --git a/packages/primitives/src/lib.rs b/packages/primitives/src/lib.rs index 454635e8d..7ad1d35b4 100644 --- a/packages/primitives/src/lib.rs +++ b/packages/primitives/src/lib.rs @@ -5,8 +5,6 @@ //! by the tracker server crate, but also by other crates in the Torrust //! ecosystem. use std::collections::BTreeMap; -use std::fmt; -use std::str::FromStr; use std::time::Duration; use info_hash::InfoHash; @@ -64,70 +62,3 @@ pub enum DatabaseDriver { } pub type PersistentTorrents = BTreeMap; - -/// The mode the tracker will run in. -/// -/// Refer to [Torrust Tracker Configuration](https://docs.rs/torrust-tracker-configuration) -/// to know how to configure the tracker to run in each mode. -#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] -pub enum TrackerMode { - /// Will track every new info hash and serve every peer. - #[serde(rename = "public")] - Public, - - /// Will only track whitelisted info hashes. - #[serde(rename = "listed")] - Listed, - - /// Will only serve authenticated peers - #[serde(rename = "private")] - Private, - - /// Will only track whitelisted info hashes and serve authenticated peers - #[serde(rename = "private_listed")] - PrivateListed, -} - -impl Default for TrackerMode { - fn default() -> Self { - Self::Public - } -} - -impl fmt::Display for TrackerMode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let display_str = match self { - TrackerMode::Public => "public", - TrackerMode::Listed => "listed", - TrackerMode::Private => "private", - TrackerMode::PrivateListed => "private_listed", - }; - write!(f, "{display_str}") - } -} - -impl FromStr for TrackerMode { - type Err = String; - - fn from_str(s: &str) -> Result { - match s.to_lowercase().as_str() { - "public" => Ok(TrackerMode::Public), - "listed" => Ok(TrackerMode::Listed), - "private" => Ok(TrackerMode::Private), - "private_listed" => Ok(TrackerMode::PrivateListed), - _ => Err(format!("Unknown tracker mode: {s}")), - } - } -} - -impl TrackerMode { - #[must_use] - pub fn is_open(&self) -> bool { - matches!(self, TrackerMode::Public | TrackerMode::Listed) - } - - #[must_use] - pub fn is_close(&self) -> bool { - !self.is_open() - } -} diff --git a/packages/test-helpers/Cargo.toml b/packages/test-helpers/Cargo.toml index 2f10c6a0f..4fed6bc42 100644 --- a/packages/test-helpers/Cargo.toml +++ b/packages/test-helpers/Cargo.toml @@ -17,4 +17,3 @@ version.workspace = true [dependencies] rand = "0" torrust-tracker-configuration = { version = "3.0.0-alpha.12-develop", path = "../configuration" } -torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../primitives" } diff --git a/packages/test-helpers/src/configuration.rs b/packages/test-helpers/src/configuration.rs index 646617b32..65d9d9144 100644 --- a/packages/test-helpers/src/configuration.rs +++ b/packages/test-helpers/src/configuration.rs @@ -3,7 +3,6 @@ use std::env; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; use torrust_tracker_configuration::{Configuration, HttpApi, HttpTracker, LogLevel, UdpTracker}; -use torrust_tracker_primitives::TrackerMode; use crate::random; @@ -86,40 +85,41 @@ pub fn ephemeral_without_reverse_proxy() -> Configuration { /// Ephemeral configuration with `public` mode. #[must_use] -pub fn ephemeral_mode_public() -> Configuration { +pub fn ephemeral_public() -> Configuration { let mut cfg = ephemeral(); - cfg.core.mode = TrackerMode::Public; + cfg.core.private = false; cfg } /// Ephemeral configuration with `private` mode. #[must_use] -pub fn ephemeral_mode_private() -> Configuration { +pub fn ephemeral_private() -> Configuration { let mut cfg = ephemeral(); - cfg.core.mode = TrackerMode::Private; + cfg.core.private = true; cfg } /// Ephemeral configuration with `listed` mode. #[must_use] -pub fn ephemeral_mode_whitelisted() -> Configuration { +pub fn ephemeral_listed() -> Configuration { let mut cfg = ephemeral(); - cfg.core.mode = TrackerMode::Listed; + cfg.core.listed = true; cfg } /// Ephemeral configuration with `private_listed` mode. #[must_use] -pub fn ephemeral_mode_private_whitelisted() -> Configuration { +pub fn ephemeral_private_and_listed() -> Configuration { let mut cfg = ephemeral(); - cfg.core.mode = TrackerMode::PrivateListed; + cfg.core.private = true; + cfg.core.listed = true; cfg } diff --git a/src/app.rs b/src/app.rs index f6a909002..2d70a6dde 100644 --- a/src/app.rs +++ b/src/app.rs @@ -51,7 +51,7 @@ pub async fn start(config: &Configuration, tracker: Arc) -> Vec) -> Vec>, - mode: TrackerMode, + private: bool, + listed: bool, policy: TrackerPolicy, keys: tokio::sync::RwLock>, whitelist: tokio::sync::RwLock>, @@ -558,12 +560,11 @@ impl Tracker { ) -> Result { let database = Arc::new(databases::driver::build(&config.database.driver, &config.database.path)?); - let mode = config.mode.clone(); - Ok(Tracker { //config, announce_policy: config.announce_policy, - mode, + private: config.private, + listed: config.listed, keys: tokio::sync::RwLock::new(std::collections::HashMap::new()), whitelist: tokio::sync::RwLock::new(std::collections::HashSet::new()), torrents: Arc::default(), @@ -578,17 +579,17 @@ impl Tracker { /// Returns `true` is the tracker is in public mode. pub fn is_public(&self) -> bool { - self.mode == TrackerMode::Public + !self.private } /// Returns `true` is the tracker is in private mode. pub fn is_private(&self) -> bool { - self.mode == TrackerMode::Private || self.mode == TrackerMode::PrivateListed + self.private } /// Returns `true` is the tracker is in whitelisted mode. - pub fn is_whitelisted(&self) -> bool { - self.mode == TrackerMode::Listed || self.mode == TrackerMode::PrivateListed + pub fn is_listed(&self) -> bool { + self.listed } /// Returns `true` if the tracker requires authentication. @@ -869,7 +870,7 @@ impl Tracker { /// Will return an error if the tracker is running in `listed` mode /// and the infohash is not whitelisted. pub async fn authorize(&self, info_hash: &InfoHash) -> Result<(), Error> { - if !self.is_whitelisted() { + if !self.is_listed() { return Ok(()); } @@ -1028,15 +1029,15 @@ mod tests { use crate::shared::bit_torrent::info_hash::fixture::gen_seeded_infohash; fn public_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_public()) + tracker_factory(&configuration::ephemeral_public()) } fn private_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_private()) + tracker_factory(&configuration::ephemeral_private()) } fn whitelisted_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_whitelisted()) + tracker_factory(&configuration::ephemeral_listed()) } pub fn tracker_persisting_torrents_in_database() -> Tracker { diff --git a/src/lib.rs b/src/lib.rs index cf2834418..e5362259f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,14 +172,10 @@ //! //! [core] //! inactive_peer_cleanup_interval = 600 -//! mode = "public" +//! listed = false +//! private = false //! tracker_usage_statistics = true //! -//! [core.tracker_policy] -//! max_peer_timeout = 900 -//! persistent_torrent_completed_stat = false -//! remove_peerless_torrents = true -//! //! [core.announce_policy] //! interval = 120 //! interval_min = 120 @@ -192,6 +188,11 @@ //! external_ip = "0.0.0.0" //! on_reverse_proxy = false //! +//! [core.tracker_policy] +//! max_peer_timeout = 900 +//! persistent_torrent_completed_stat = false +//! remove_peerless_torrents = true +//! //! [health_check_api] //! bind_address = "127.0.0.1:1313" //!``` diff --git a/src/servers/apis/server.rs b/src/servers/apis/server.rs index 967080bd5..39a68a856 100644 --- a/src/servers/apis/server.rs +++ b/src/servers/apis/server.rs @@ -266,7 +266,7 @@ impl Launcher { mod tests { use std::sync::Arc; - use torrust_tracker_test_helpers::configuration::ephemeral_mode_public; + use torrust_tracker_test_helpers::configuration::ephemeral_public; use crate::bootstrap::app::initialize_with_configuration; use crate::bootstrap::jobs::make_rust_tls; @@ -275,7 +275,7 @@ mod tests { #[tokio::test] async fn it_should_be_able_to_start_and_stop() { - let cfg = Arc::new(ephemeral_mode_public()); + let cfg = Arc::new(ephemeral_public()); let config = &cfg.http_api.clone().unwrap(); let tracker = initialize_with_configuration(&cfg); diff --git a/src/servers/http/server.rs b/src/servers/http/server.rs index 87f0e945b..faedaf921 100644 --- a/src/servers/http/server.rs +++ b/src/servers/http/server.rs @@ -226,7 +226,7 @@ pub fn check_fn(binding: &SocketAddr) -> ServiceHealthCheckJob { mod tests { use std::sync::Arc; - use torrust_tracker_test_helpers::configuration::ephemeral_mode_public; + use torrust_tracker_test_helpers::configuration::ephemeral_public; use crate::bootstrap::app::initialize_with_configuration; use crate::bootstrap::jobs::make_rust_tls; @@ -235,7 +235,7 @@ mod tests { #[tokio::test] async fn it_should_be_able_to_start_and_stop() { - let cfg = Arc::new(ephemeral_mode_public()); + let cfg = Arc::new(ephemeral_public()); let tracker = initialize_with_configuration(&cfg); let http_trackers = cfg.http_trackers.clone().expect("missing HTTP trackers configuration"); let config = &http_trackers[0]; diff --git a/src/servers/http/v1/handlers/announce.rs b/src/servers/http/v1/handlers/announce.rs index 0b009f700..0514a9f71 100644 --- a/src/servers/http/v1/handlers/announce.rs +++ b/src/servers/http/v1/handlers/announce.rs @@ -181,11 +181,11 @@ mod tests { use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources; fn private_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_private()) + tracker_factory(&configuration::ephemeral_private()) } fn whitelisted_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_whitelisted()) + tracker_factory(&configuration::ephemeral_listed()) } fn tracker_on_reverse_proxy() -> Tracker { diff --git a/src/servers/http/v1/handlers/scrape.rs b/src/servers/http/v1/handlers/scrape.rs index 172607637..eb8875a58 100644 --- a/src/servers/http/v1/handlers/scrape.rs +++ b/src/servers/http/v1/handlers/scrape.rs @@ -121,11 +121,11 @@ mod tests { use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources; fn private_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_private()) + tracker_factory(&configuration::ephemeral_private()) } fn whitelisted_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_whitelisted()) + tracker_factory(&configuration::ephemeral_listed()) } fn tracker_on_reverse_proxy() -> Tracker { diff --git a/src/servers/http/v1/services/announce.rs b/src/servers/http/v1/services/announce.rs index eee5e4688..47175817d 100644 --- a/src/servers/http/v1/services/announce.rs +++ b/src/servers/http/v1/services/announce.rs @@ -57,7 +57,7 @@ mod tests { use crate::core::Tracker; fn public_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_public()) + tracker_factory(&configuration::ephemeral_public()) } fn sample_info_hash() -> InfoHash { diff --git a/src/servers/http/v1/services/scrape.rs b/src/servers/http/v1/services/scrape.rs index bf9fbd933..ee7814194 100644 --- a/src/servers/http/v1/services/scrape.rs +++ b/src/servers/http/v1/services/scrape.rs @@ -70,7 +70,7 @@ mod tests { use crate::core::Tracker; fn public_tracker() -> Tracker { - tracker_factory(&configuration::ephemeral_mode_public()) + tracker_factory(&configuration::ephemeral_public()) } fn sample_info_hashes() -> Vec { diff --git a/src/servers/udp/handlers.rs b/src/servers/udp/handlers.rs index 12ae6a250..f1f61ee6b 100644 --- a/src/servers/udp/handlers.rs +++ b/src/servers/udp/handlers.rs @@ -339,15 +339,15 @@ mod tests { } fn public_tracker() -> Arc { - initialized_tracker(&configuration::ephemeral_mode_public()) + initialized_tracker(&configuration::ephemeral_public()) } fn private_tracker() -> Arc { - initialized_tracker(&configuration::ephemeral_mode_private()) + initialized_tracker(&configuration::ephemeral_private()) } fn whitelisted_tracker() -> Arc { - initialized_tracker(&configuration::ephemeral_mode_whitelisted()) + initialized_tracker(&configuration::ephemeral_listed()) } fn initialized_tracker(configuration: &Configuration) -> Arc { diff --git a/src/servers/udp/server/mod.rs b/src/servers/udp/server/mod.rs index 034f71beb..e3321f157 100644 --- a/src/servers/udp/server/mod.rs +++ b/src/servers/udp/server/mod.rs @@ -47,7 +47,7 @@ mod tests { use std::sync::Arc; use std::time::Duration; - use torrust_tracker_test_helpers::configuration::ephemeral_mode_public; + use torrust_tracker_test_helpers::configuration::ephemeral_public; use super::spawner::Spawner; use super::Server; @@ -56,7 +56,7 @@ mod tests { #[tokio::test] async fn it_should_be_able_to_start_and_stop() { - let cfg = Arc::new(ephemeral_mode_public()); + let cfg = Arc::new(ephemeral_public()); let tracker = initialize_with_configuration(&cfg); let udp_trackers = cfg.udp_trackers.clone().expect("missing UDP trackers configuration"); let config = &udp_trackers[0]; @@ -79,7 +79,7 @@ mod tests { #[tokio::test] async fn it_should_be_able_to_start_and_stop_with_wait() { - let cfg = Arc::new(ephemeral_mode_public()); + let cfg = Arc::new(ephemeral_public()); let tracker = initialize_with_configuration(&cfg); let config = &cfg.udp_trackers.as_ref().unwrap().first().unwrap(); let bind_to = config.bind_address; diff --git a/tests/servers/http/v1/contract.rs b/tests/servers/http/v1/contract.rs index a7962db0f..cdffead99 100644 --- a/tests/servers/http/v1/contract.rs +++ b/tests/servers/http/v1/contract.rs @@ -107,7 +107,7 @@ mod for_all_config_modes { #[tokio::test] async fn it_should_start_and_stop() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; env.stop().await; } @@ -376,7 +376,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_return_no_peers_if_the_announced_peer_is_the_first_one() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let response = Client::new(*env.bind_address()) .announce( @@ -405,7 +405,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_return_the_list_of_previously_announced_peers() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -447,7 +447,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_return_the_list_of_previously_announced_peers_including_peers_using_ipv4_and_ipv6() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -499,7 +499,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_consider_two_peers_to_be_the_same_when_they_have_the_same_peer_id_even_if_the_ip_is_different() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); let peer = PeerBuilder::default().build(); @@ -526,7 +526,7 @@ mod for_all_config_modes { // Tracker Returns Compact Peer Lists // https://www.bittorrent.org/beps/bep_0023.html - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -567,7 +567,7 @@ mod for_all_config_modes { // code-review: the HTTP tracker does not return the compact response by default if the "compact" // param is not provided in the announce URL. The BEP 23 suggest to do so. - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -605,7 +605,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_increase_the_number_of_tcp4_connections_handled_in_statistics() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; Client::new(*env.bind_address()) .announce(&QueryBuilder::default().query()) @@ -648,7 +648,7 @@ mod for_all_config_modes { async fn should_not_increase_the_number_of_tcp6_connections_handled_if_the_client_is_not_using_an_ipv6_ip() { // The tracker ignores the peer address in the request param. It uses the client remote ip address. - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; Client::new(*env.bind_address()) .announce( @@ -669,7 +669,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_increase_the_number_of_tcp4_announce_requests_handled_in_statistics() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; Client::new(*env.bind_address()) .announce(&QueryBuilder::default().query()) @@ -712,7 +712,7 @@ mod for_all_config_modes { async fn should_not_increase_the_number_of_tcp6_announce_requests_handled_if_the_client_is_not_using_an_ipv6_ip() { // The tracker ignores the peer address in the request param. It uses the client remote ip address. - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; Client::new(*env.bind_address()) .announce( @@ -733,7 +733,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_assign_to_the_peer_ip_the_remote_client_ip_instead_of_the_peer_address_in_the_request_param() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); let client_ip = local_ip().unwrap(); @@ -905,7 +905,7 @@ mod for_all_config_modes { //#[tokio::test] #[allow(dead_code)] async fn should_fail_when_the_request_is_empty() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let response = Client::new(*env.bind_address()).get("scrape").await; assert_missing_query_params_for_scrape_request_error_response(response).await; @@ -915,7 +915,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_fail_when_the_info_hash_param_is_invalid() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let mut params = QueryBuilder::default().query().params(); @@ -932,7 +932,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_return_the_file_with_the_incomplete_peer_when_there_is_one_peer_with_bytes_pending_to_download() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -971,7 +971,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_return_the_file_with_the_complete_peer_when_there_is_one_peer_with_no_bytes_pending_to_download() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1010,7 +1010,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_return_a_file_with_zeroed_values_when_there_are_no_peers() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1029,7 +1029,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_accept_multiple_infohashes() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash1 = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); let info_hash2 = InfoHash::from_str("3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0").unwrap(); @@ -1055,7 +1055,7 @@ mod for_all_config_modes { #[tokio::test] async fn should_increase_the_number_ot_tcp4_scrape_requests_handled_in_statistics() { - let env = Started::new(&configuration::ephemeral_mode_public().into()).await; + let env = Started::new(&configuration::ephemeral_public().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1123,7 +1123,7 @@ mod configured_as_whitelisted { #[tokio::test] async fn should_fail_if_the_torrent_is_not_in_the_whitelist() { - let env = Started::new(&configuration::ephemeral_mode_whitelisted().into()).await; + let env = Started::new(&configuration::ephemeral_listed().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1138,7 +1138,7 @@ mod configured_as_whitelisted { #[tokio::test] async fn should_allow_announcing_a_whitelisted_torrent() { - let env = Started::new(&configuration::ephemeral_mode_whitelisted().into()).await; + let env = Started::new(&configuration::ephemeral_listed().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1172,7 +1172,7 @@ mod configured_as_whitelisted { #[tokio::test] async fn should_return_the_zeroed_file_when_the_requested_file_is_not_whitelisted() { - let env = Started::new(&configuration::ephemeral_mode_whitelisted().into()).await; + let env = Started::new(&configuration::ephemeral_listed().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1202,7 +1202,7 @@ mod configured_as_whitelisted { #[tokio::test] async fn should_return_the_file_stats_when_the_requested_file_is_whitelisted() { - let env = Started::new(&configuration::ephemeral_mode_whitelisted().into()).await; + let env = Started::new(&configuration::ephemeral_listed().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1263,7 +1263,7 @@ mod configured_as_private { #[tokio::test] async fn should_respond_to_authenticated_peers() { - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; let expiring_key = env.tracker.generate_auth_key(Duration::from_secs(60)).await.unwrap(); @@ -1278,7 +1278,7 @@ mod configured_as_private { #[tokio::test] async fn should_fail_if_the_peer_has_not_provided_the_authentication_key() { - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1293,7 +1293,7 @@ mod configured_as_private { #[tokio::test] async fn should_fail_if_the_key_query_param_cannot_be_parsed() { - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; let invalid_key = "INVALID_KEY"; @@ -1308,7 +1308,7 @@ mod configured_as_private { #[tokio::test] async fn should_fail_if_the_peer_cannot_be_authenticated_with_the_provided_key() { - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; // The tracker does not have this key let unregistered_key = Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(); @@ -1341,7 +1341,7 @@ mod configured_as_private { #[tokio::test] async fn should_fail_if_the_key_query_param_cannot_be_parsed() { - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; let invalid_key = "INVALID_KEY"; @@ -1356,7 +1356,7 @@ mod configured_as_private { #[tokio::test] async fn should_return_the_zeroed_file_when_the_client_is_not_authenticated() { - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1386,7 +1386,7 @@ mod configured_as_private { #[tokio::test] async fn should_return_the_real_file_stats_when_the_client_is_authenticated() { - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); @@ -1430,7 +1430,7 @@ mod configured_as_private { // There is not authentication error // code-review: should this really be this way? - let env = Started::new(&configuration::ephemeral_mode_private().into()).await; + let env = Started::new(&configuration::ephemeral_private().into()).await; let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); From a5b9e14a47e1496ce8fddfec836f21651ad9ca5b Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 1 Jul 2024 12:40:39 +0100 Subject: [PATCH 4/7] refactor: inject the core config to the core tracker --- packages/configuration/src/lib.rs | 7 +++--- src/core/mod.rs | 41 +++++++++++-------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/packages/configuration/src/lib.rs b/packages/configuration/src/lib.rs index ca008a49a..8a544b6e2 100644 --- a/packages/configuration/src/lib.rs +++ b/packages/configuration/src/lib.rs @@ -35,10 +35,11 @@ const ENV_VAR_CONFIG_TOML: &str = "TORRUST_TRACKER_CONFIG_TOML"; pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_TRACKER_CONFIG_TOML_PATH"; pub type Configuration = v1::Configuration; -pub type UdpTracker = v1::udp_tracker::UdpTracker; -pub type HttpTracker = v1::http_tracker::HttpTracker; -pub type HttpApi = v1::tracker_api::HttpApi; +pub type Core = v1::core::Core; pub type HealthCheckApi = v1::health_check_api::HealthCheckApi; +pub type HttpApi = v1::tracker_api::HttpApi; +pub type HttpTracker = v1::http_tracker::HttpTracker; +pub type UdpTracker = v1::udp_tracker::UdpTracker; pub type AccessTokens = HashMap; diff --git a/src/core/mod.rs b/src/core/mod.rs index 20b7c81f4..a9fe2a8a6 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -456,8 +456,7 @@ use std::time::Duration; use derive_more::Constructor; use tokio::sync::mpsc::error::SendError; use torrust_tracker_clock::clock::Time; -use torrust_tracker_configuration::v1::core::Core; -use torrust_tracker_configuration::{AnnouncePolicy, TrackerPolicy, TORRENT_PEERS_LIMIT}; +use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT}; use torrust_tracker_primitives::info_hash::InfoHash; use torrust_tracker_primitives::peer; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; @@ -482,20 +481,15 @@ use crate::CurrentClock; /// > Typically, the `Tracker` is used by a higher application service that handles /// > the network layer. pub struct Tracker { - announce_policy: AnnouncePolicy, + config: Core, /// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite) /// or [`MySQL`](crate::core::databases::mysql) pub database: Arc>, - private: bool, - listed: bool, - policy: TrackerPolicy, keys: tokio::sync::RwLock>, whitelist: tokio::sync::RwLock>, pub torrents: Arc, stats_event_sender: Option>, stats_repository: statistics::Repo, - external_ip: Option, - on_reverse_proxy: bool, } /// Structure that holds the data returned by the `announce` request. @@ -561,35 +555,29 @@ impl Tracker { let database = Arc::new(databases::driver::build(&config.database.driver, &config.database.path)?); Ok(Tracker { - //config, - announce_policy: config.announce_policy, - private: config.private, - listed: config.listed, + config: config.clone(), keys: tokio::sync::RwLock::new(std::collections::HashMap::new()), whitelist: tokio::sync::RwLock::new(std::collections::HashSet::new()), torrents: Arc::default(), stats_event_sender, stats_repository, database, - external_ip: config.net.external_ip, - policy: config.tracker_policy.clone(), - on_reverse_proxy: config.net.on_reverse_proxy, }) } /// Returns `true` is the tracker is in public mode. pub fn is_public(&self) -> bool { - !self.private + !self.config.private } /// Returns `true` is the tracker is in private mode. pub fn is_private(&self) -> bool { - self.private + self.config.private } /// Returns `true` is the tracker is in whitelisted mode. pub fn is_listed(&self) -> bool { - self.listed + self.config.listed } /// Returns `true` if the tracker requires authentication. @@ -599,15 +587,15 @@ impl Tracker { /// Returns `true` is the tracker is in whitelisted mode. pub fn is_behind_reverse_proxy(&self) -> bool { - self.on_reverse_proxy + self.config.net.on_reverse_proxy } pub fn get_announce_policy(&self) -> AnnouncePolicy { - self.announce_policy + self.config.announce_policy } pub fn get_maybe_external_ip(&self) -> Option { - self.external_ip + self.config.net.external_ip } /// It handles an announce request. @@ -632,7 +620,7 @@ impl Tracker { // responsibility into another authentication service. debug!("Before: {peer:?}"); - peer.change_ip(&assign_ip_address_to_peer(remote_client_ip, self.external_ip)); + peer.change_ip(&assign_ip_address_to_peer(remote_client_ip, self.config.net.external_ip)); debug!("After: {peer:?}"); let stats = self.upsert_peer_and_get_stats(info_hash, peer).await; @@ -735,7 +723,7 @@ impl Tracker { /// /// # Context: Tracker async fn persist_stats(&self, info_hash: &InfoHash, swarm_metadata: &SwarmMetadata) { - if self.policy.persistent_torrent_completed_stat { + if self.config.tracker_policy.persistent_torrent_completed_stat { let completed = swarm_metadata.downloaded; let info_hash = *info_hash; @@ -759,11 +747,12 @@ impl Tracker { /// # Context: Tracker pub fn cleanup_torrents(&self) { // If we don't need to remove torrents we will use the faster iter - if self.policy.remove_peerless_torrents { - self.torrents.remove_peerless_torrents(&self.policy); + if self.config.tracker_policy.remove_peerless_torrents { + self.torrents.remove_peerless_torrents(&self.config.tracker_policy); } else { let current_cutoff = - CurrentClock::now_sub(&Duration::from_secs(u64::from(self.policy.max_peer_timeout))).unwrap_or_default(); + CurrentClock::now_sub(&Duration::from_secs(u64::from(self.config.tracker_policy.max_peer_timeout))) + .unwrap_or_default(); self.torrents.remove_inactive_peers(current_cutoff); } } From 5a16ea10a8d474a444eadd27b17ebfb4e9a150f3 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 1 Jul 2024 12:53:22 +0100 Subject: [PATCH 5/7] refactor: [#932] make all Tracker fields private --- src/core/mod.rs | 16 ++++++++++++++-- tests/servers/api/mod.rs | 7 +++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/core/mod.rs b/src/core/mod.rs index a9fe2a8a6..4136966d2 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -481,13 +481,14 @@ use crate::CurrentClock; /// > Typically, the `Tracker` is used by a higher application service that handles /// > the network layer. pub struct Tracker { + // The tracker configuration. config: Core, /// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite) /// or [`MySQL`](crate::core::databases::mysql) - pub database: Arc>, + database: Arc>, keys: tokio::sync::RwLock>, whitelist: tokio::sync::RwLock>, - pub torrents: Arc, + torrents: Arc, stats_event_sender: Option>, stats_repository: statistics::Repo, } @@ -987,6 +988,17 @@ impl Tracker { Some(stats_event_sender) => stats_event_sender.send_event(event).await, } } + + /// It drops the database tables. + /// + /// # Errors + /// + /// Will return `Err` if unable to drop tables. + pub fn drop_database_tables(&self) -> Result<(), databases::error::Error> { + // todo: this is only used for testing. WE have to pass the database + // reference directly to the tests instead of via the tracker. + self.database.drop_database_tables() + } } #[must_use] diff --git a/tests/servers/api/mod.rs b/tests/servers/api/mod.rs index 9c30e316a..38df46e9b 100644 --- a/tests/servers/api/mod.rs +++ b/tests/servers/api/mod.rs @@ -11,7 +11,10 @@ pub type Started = environment::Environment; /// It forces a database error by dropping all tables. /// That makes any query fail. -/// code-review: alternatively we could inject a database mock in the future. +/// code-review: +/// Alternatively we could: +/// - Inject a database mock in the future. +/// - Inject directly the database reference passed to the Tracker type. pub fn force_database_error(tracker: &Arc) { - tracker.database.drop_database_tables().unwrap(); + tracker.drop_database_tables().unwrap(); } From f61c7c36cb3592c1989aba71b0959f6477d6d8a9 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 1 Jul 2024 13:05:11 +0100 Subject: [PATCH 6/7] docs: add commments to core::Tracker struct fields --- src/core/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/mod.rs b/src/core/mod.rs index 4136966d2..9a64826c9 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -481,15 +481,26 @@ use crate::CurrentClock; /// > Typically, the `Tracker` is used by a higher application service that handles /// > the network layer. pub struct Tracker { - // The tracker configuration. + /// The tracker configuration. config: Core, + /// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite) /// or [`MySQL`](crate::core::databases::mysql) database: Arc>, + + /// Tracker users' keys. Only for private trackers. keys: tokio::sync::RwLock>, + + /// The list of allowed torrents. Only for listed trackers. whitelist: tokio::sync::RwLock>, + + /// The in-memory torrents repository. torrents: Arc, + + /// Service to send stats events. stats_event_sender: Option>, + + /// The in-memory stats repo. stats_repository: statistics::Repo, } From b6b841d6fe244b8deaa62629791d100623a508ce Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 1 Jul 2024 13:31:30 +0100 Subject: [PATCH 7/7] chore: remove crate from ignore list in cargo machete It was accidentaly added. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3eca9934d..41afb1538 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,7 +85,7 @@ uuid = { version = "1", features = ["v4"] } zerocopy = "0.7.33" [package.metadata.cargo-machete] -ignored = ["crossbeam-skiplist", "dashmap", "figment", "parking_lot", "serde_bytes", "torrust-tracker-primitives"] +ignored = ["crossbeam-skiplist", "dashmap", "figment", "parking_lot", "serde_bytes"] [dev-dependencies] local-ip-address = "0"