Skip to content

Commit

Permalink
feat: [torrust#591] config v2, tracker mode from tracker
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
josecelano committed Jun 12, 2024
1 parent 32542e9 commit fd0814c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ log_level = "info"

[tracker]
api_url = "http://tracker:1212"
mode = "Private"
mode = "private"
url = "http://tracker:7070"

[database]
Expand Down
80 changes: 37 additions & 43 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -119,45 +119,29 @@ impl From<figment::Error> 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 {
Expand All @@ -166,26 +150,36 @@ 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<Self, Self::Err> {
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}")),
}
}
}

impl TrackerMode {
#[must_use]
pub fn is_open(&self) -> bool {
matches!(self, TrackerMode::Public | TrackerMode::Whitelisted)
matches!(self, TrackerMode::Public | TrackerMode::Listed)
}

#[must_use]
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -171,7 +171,7 @@
//!
//! [tracker]
//! url = "udp://localhost:6969"
//! mode = "Public"
//! mode = "public"
//! api_url = "http://localhost:1212/"
//! token = "MyAccessToken"
//! token_valid_seconds = 7257600
Expand Down
6 changes: 3 additions & 3 deletions src/web/api/server/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//! },
//! "tracker": {
//! "url": "udp://localhost:6969",
//! "mode": "Public",
//! "mode": "public",
//! "api_url": "http://localhost:1212/",
//! "token": "MyAccessToken",
//! "token_valid_seconds": 7257600
Expand Down Expand Up @@ -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":"[email protected]","reply_to":"[email protected]","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":"[email protected]","reply_to":"[email protected]","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"
//! ```
//!
Expand Down Expand Up @@ -158,7 +158,7 @@
//! "data": {
//! "website_name": "Torrust",
//! "tracker_url": "udp://localhost:6969",
//! "tracker_mode": "Public",
//! "tracker_mode": "public",
//! "email_on_signup": "Optional"
//! }
//! }
Expand Down
2 changes: 1 addition & 1 deletion tests/common/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl From<DomainTracker> 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,
Expand Down

0 comments on commit fd0814c

Please sign in to comment.