Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul core Tracker: review whitelist functionality #1269

Prev Previous commit
Next Next commit
refactor: [#1268] extract servers::udp::services::scrape service
josecelano committed Feb 14, 2025

Verified

This commit was signed with the committer’s verified signature.
josecelano Jose Celano
commit dec742e2326a9e9861a1b26907c1cbfe0c127838
18 changes: 2 additions & 16 deletions src/servers/udp/handlers/scrape.rs
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ use crate::packages::udp_tracker_core;
use crate::servers::udp::connection_cookie::check;
use crate::servers::udp::error::Error;
use crate::servers::udp::handlers::gen_remote_fingerprint;
use crate::servers::udp::services;

/// It handles the `Scrape` request. Refer to [`Scrape`](crate::servers::udp#scrape)
/// request for more information.
@@ -49,7 +50,7 @@ pub async fn handle_scrape(
info_hashes.push((*info_hash).into());
}

let scrape_data = scrape_handler.scrape(&info_hashes).await;
let scrape_data = services::scrape::invoke(scrape_handler, opt_udp_stats_event_sender, &info_hashes, remote_addr).await;

let mut torrent_stats: Vec<TorrentScrapeStatistics> = Vec::new();

@@ -68,21 +69,6 @@ pub async fn handle_scrape(
torrent_stats.push(scrape_entry);
}

if let Some(udp_stats_event_sender) = opt_udp_stats_event_sender.as_deref() {
match remote_addr {
SocketAddr::V4(_) => {
udp_stats_event_sender
.send_event(udp_tracker_core::statistics::event::Event::Udp4Scrape)
.await;
}
SocketAddr::V6(_) => {
udp_stats_event_sender
.send_event(udp_tracker_core::statistics::event::Event::Udp6Scrape)
.await;
}
}
}

let response = ScrapeResponse {
transaction_id: request.transaction_id,
torrent_stats,
2 changes: 1 addition & 1 deletion src/servers/udp/services/announce.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
//! It delegates the `announce` logic to the [`AnnounceHandler`] and it returns
//! the [`AnnounceData`].
//!
//! It also sends an [`http_tracker_core::statistics::event::Event`]
//! It also sends an [`udp_tracker_core::statistics::event::Event`]
//! because events are specific for the HTTP tracker.
use std::net::IpAddr;
use std::sync::Arc;
43 changes: 43 additions & 0 deletions src/servers/udp/services/scrape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! The `scrape` service.
//!
//! The service is responsible for handling the `scrape` requests.
//!
//! It delegates the `scrape` logic to the [`ScrapeHandler`] and it returns the
//! [`ScrapeData`].
//!
//! It also sends an [`udp_tracker_core::statistics::event::Event`]
//! because events are specific for the UDP tracker.
use std::net::SocketAddr;
use std::sync::Arc;

use bittorrent_primitives::info_hash::InfoHash;
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
use torrust_tracker_primitives::core::ScrapeData;

use crate::packages::udp_tracker_core;

pub async fn invoke(
scrape_handler: &Arc<ScrapeHandler>,
opt_udp_stats_event_sender: &Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>,
info_hashes: &Vec<InfoHash>,
remote_addr: SocketAddr,
) -> ScrapeData {
let scrape_data = scrape_handler.scrape(info_hashes).await;

if let Some(udp_stats_event_sender) = opt_udp_stats_event_sender.as_deref() {
match remote_addr {
SocketAddr::V4(_) => {
udp_stats_event_sender
.send_event(udp_tracker_core::statistics::event::Event::Udp4Scrape)
.await;
}
SocketAddr::V6(_) => {
udp_stats_event_sender
.send_event(udp_tracker_core::statistics::event::Event::Udp6Scrape)
.await;
}
}
}

scrape_data
}