Skip to content

Commit

Permalink
Merge #934: Config overhaul: split tracker mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
josecelano committed Jul 1, 2024
2 parents 5f1fdbd + b6b841d commit 5aad462
Show file tree
Hide file tree
Showing 23 changed files with 195 additions and 239 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ 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"

Expand Down
7 changes: 4 additions & 3 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>;

Expand Down
83 changes: 45 additions & 38 deletions packages/configuration/src/v1/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::TrackerMode;

use super::network::Network;
use crate::v1::database::Database;
Expand All @@ -8,27 +7,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,
Expand All @@ -37,51 +15,80 @@ 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,

// When `true` only approved torrents can be announced in the tracker.
#[serde(default = "Core::default_listed")]
pub listed: bool,

// Network configuration.
#[serde(default = "Core::default_network")]
pub net: Network,

// When `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,

/// 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(),
listed: Self::default_listed(),
net: Self::default_network(),
private: Self::default_private(),
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_listed() -> bool {
false
}

fn default_announce_policy() -> AnnouncePolicy {
AnnouncePolicy::default()
fn default_network() -> Network {
Network::default()
}

fn default_database() -> Database {
Database::default()
fn default_private() -> bool {
false
}

fn default_network() -> Network {
Network::default()
fn default_tracker_policy() -> TrackerPolicy {
TrackerPolicy::default()
}
fn default_tracker_usage_statistics() -> bool {
true
}
}
30 changes: 16 additions & 14 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
//!
Expand Down Expand Up @@ -365,14 +366,10 @@ mod tests {
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
Expand All @@ -386,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"
"#
Expand Down
69 changes: 0 additions & 69 deletions packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,70 +62,3 @@ pub enum DatabaseDriver {
}

pub type PersistentTorrents = BTreeMap<InfoHash, u32>;

/// 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<Self, Self::Err> {
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()
}
}
1 change: 0 additions & 1 deletion packages/test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
18 changes: 9 additions & 9 deletions packages/test-helpers/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<J
}

// Load whitelisted torrents
if tracker.is_whitelisted() {
if tracker.is_listed() {
tracker
.load_whitelist_from_database()
.await
Expand All @@ -64,8 +64,8 @@ pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<J
for udp_tracker_config in udp_trackers {
if tracker.is_private() {
warn!(
"Could not start UDP tracker on: {} while in {:?}. UDP is not safe for private trackers!",
udp_tracker_config.bind_address, config.core.mode
"Could not start UDP tracker on: {} while in private mode. UDP is not safe for private trackers!",
udp_tracker_config.bind_address
);
} else {
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone(), registar.give_form()).await);
Expand Down
Loading

0 comments on commit 5aad462

Please sign in to comment.