Skip to content

Commit

Permalink
fix: [torrust#488] torrent without tracker keys for open tracker
Browse files Browse the repository at this point in the history
When the tracker is open (public or public whitelisted) thre tracker
user's keys should not be included in the downloaded torrent file.
  • Loading branch information
josecelano committed Feb 23, 2024
1 parent 70329ea commit 8cab267
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ impl Default for TrackerMode {
}
}

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

/// Configuration for the associated tracker.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tracker {
Expand Down
18 changes: 16 additions & 2 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde_derive::{Deserialize, Serialize};

use super::category::DbCategoryRepository;
use super::user::DbUserRepository;
use crate::config::Configuration;
use crate::config::{Configuration, TrackerMode};
use crate::databases::database::{Database, Error, Sorting};
use crate::errors::ServiceError;
use crate::models::category::CategoryId;
Expand Down Expand Up @@ -258,21 +258,30 @@ impl Index {

let tracker_url = self.get_tracker_url().await;

// Add personal tracker url or default tracker url
if self.get_tracker_mode().await.is_open() {
// We dont' include tracker keys
torrent.announce = Some(tracker_url);
return Ok(torrent);
}

match opt_user_id {
Some(user_id) => {
// Authenticated user -> include tracker keys
let personal_announce_url = self
.tracker_service
.get_personal_announce_url(user_id)
.await
.unwrap_or(tracker_url);

torrent.announce = Some(personal_announce_url.clone());

if let Some(list) = &mut torrent.announce_list {
let vec = vec![personal_announce_url];
list.insert(0, vec);
}
}
None => {
// Unauthenticated user -> dont' include tracker keys
torrent.announce = Some(tracker_url);
}
}
Expand Down Expand Up @@ -513,6 +522,11 @@ impl Index {
let settings = self.configuration.settings.read().await;
settings.tracker.url.clone()
}

async fn get_tracker_mode(&self) -> TrackerMode {
let settings = self.configuration.settings.read().await;
settings.tracker.mode.clone()
}
}

pub struct DbTorrentRepository {
Expand Down

0 comments on commit 8cab267

Please sign in to comment.