From fd0814c4a5886eaabe4a9ff218453100b201ee6c Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 12 Jun 2024 16:50:00 +0100 Subject: [PATCH] feat: [#591] config v2, tracker mode from tracker Normalize TrackerMode enum to use the same values as in the Tracker. ```rust 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, } ``` That will enable to use the TrackerMode defined in https://crates.io/crates/torrust-tracker-primitives for TrackerMode in the future when a new version of that crate is released. --- .../index.private.e2e.container.sqlite3.toml | 2 +- src/config/mod.rs | 80 +++++++++---------- src/lib.rs | 4 +- .../api/server/v1/contexts/settings/mod.rs | 6 +- tests/common/contexts/settings/mod.rs | 2 +- 5 files changed, 44 insertions(+), 50 deletions(-) diff --git a/share/default/config/index.private.e2e.container.sqlite3.toml b/share/default/config/index.private.e2e.container.sqlite3.toml index fe781f5b..cc934a98 100644 --- a/share/default/config/index.private.e2e.container.sqlite3.toml +++ b/share/default/config/index.private.e2e.container.sqlite3.toml @@ -2,7 +2,7 @@ log_level = "info" [tracker] api_url = "http://tracker:1212" -mode = "Private" +mode = "private" url = "http://tracker:7070" [database] diff --git a/src/config/mod.rs b/src/config/mod.rs index 8ff9c08a..ccdff762 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -2,9 +2,9 @@ pub mod v1; pub mod validator; -use std::env; use std::str::FromStr; use std::sync::Arc; +use std::{env, fmt}; use camino::Utf8PathBuf; use figment::providers::{Env, Format, Serialized, Toml}; @@ -119,45 +119,29 @@ impl From for Error { } } -/* todo: +// todo: use https://crates.io/crates/torrust-tracker-primitives for TrackerMode. -Use https://crates.io/crates/torrust-tracker-primitives for TrackerMode. - -Enum variants: - - In Index In Tracker -- `Public` -> `Public` -- `Private` -> `Private` -- `Whitelisted` -> `Listed` -- `PrivateWhitelisted` -> `PrivateListed` - -Enum serialized values: - - In Index In Tracker -- `Public` -> `public` -- `Private` -> `private` -- `Whitelisted` -> `listed` -- `PrivateWhitelisted` -> `private_listed` - -It's a breaking change for the toml config file en the API. - -*/ - -/// See `TrackerMode` in [`torrust-tracker-primitives`](https://docs.rs/torrust-tracker-primitives) -/// crate for more information. -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +/// 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 serve authenticated peers. - Private, - /// Will only track whitelisted info hashes. - Whitelisted, + #[serde(rename = "listed")] + Listed, + + /// Will only serve authenticated peers + #[serde(rename = "private")] + Private, - /// Will only track whitelisted info hashes and serve authenticated peers. - PrivateWhitelisted, + /// Will only track whitelisted info hashes and serve authenticated peers + #[serde(rename = "private_listed")] + PrivateListed, } impl Default for TrackerMode { @@ -166,18 +150,28 @@ impl Default for TrackerMode { } } +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 { - "Public" => Ok(TrackerMode::Public), - "Private" => Ok(TrackerMode::Private), - "Whitelisted" => Ok(TrackerMode::Whitelisted), - "PrivateWhitelisted" => Ok(TrackerMode::PrivateWhitelisted), - _ => Err(format!( - "{s} is not a valid tracker mode. Valid values: 'Public', 'Private', 'Whitelisted', 'PrivateWhitelisted' " - )), + 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}")), } } } @@ -185,7 +179,7 @@ impl FromStr for TrackerMode { impl TrackerMode { #[must_use] pub fn is_open(&self) -> bool { - matches!(self, TrackerMode::Public | TrackerMode::Whitelisted) + matches!(self, TrackerMode::Public | TrackerMode::Listed) } #[must_use] @@ -336,7 +330,7 @@ mod tests { [tracker] url = "udp://localhost:6969" - mode = "Public" + mode = "public" api_url = "http://localhost:1212/" token = "MyAccessToken" token_valid_seconds = 7257600 diff --git a/src/lib.rs b/src/lib.rs index c3148e86..0a26c192 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,7 @@ //! ```toml //! [tracker] //! url = "udp://localhost:6969" -//! mode = "Public" +//! mode = "public" //! api_url = "http://localhost:1212/" //! token = "MyAccessToken" //! token_valid_seconds = 7257600 @@ -171,7 +171,7 @@ //! //! [tracker] //! url = "udp://localhost:6969" -//! mode = "Public" +//! mode = "public" //! api_url = "http://localhost:1212/" //! token = "MyAccessToken" //! token_valid_seconds = 7257600 diff --git a/src/web/api/server/v1/contexts/settings/mod.rs b/src/web/api/server/v1/contexts/settings/mod.rs index fe9aeab3..089137e6 100644 --- a/src/web/api/server/v1/contexts/settings/mod.rs +++ b/src/web/api/server/v1/contexts/settings/mod.rs @@ -35,7 +35,7 @@ //! }, //! "tracker": { //! "url": "udp://localhost:6969", -//! "mode": "Public", +//! "mode": "public", //! "api_url": "http://localhost:1212/", //! "token": "MyAccessToken", //! "token_valid_seconds": 7257600 @@ -102,7 +102,7 @@ //! --header "Content-Type: application/json" \ //! --header "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI" \ //! --request POST \ -//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"Public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"email_on_signup":"Optional","min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"email_verification_enabled":false,"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \ +//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"email_on_signup":"Optional","min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"email_verification_enabled":false,"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \ //! "http://127.0.0.1:3001/v1/settings" //! ``` //! @@ -158,7 +158,7 @@ //! "data": { //! "website_name": "Torrust", //! "tracker_url": "udp://localhost:6969", -//! "tracker_mode": "Public", +//! "tracker_mode": "public", //! "email_on_signup": "Optional" //! } //! } diff --git a/tests/common/contexts/settings/mod.rs b/tests/common/contexts/settings/mod.rs index b72ca7d5..a3e1b080 100644 --- a/tests/common/contexts/settings/mod.rs +++ b/tests/common/contexts/settings/mod.rs @@ -113,7 +113,7 @@ impl From for Tracker { fn from(tracker: DomainTracker) -> Self { Self { url: tracker.url, - mode: format!("{:?}", tracker.mode), + mode: tracker.mode.to_string(), api_url: tracker.api_url, token: tracker.token, token_valid_seconds: tracker.token_valid_seconds,