Skip to content

Commit

Permalink
refactor: [#1268] move scrape logic from udp server to udp_tracker_co…
Browse files Browse the repository at this point in the history
…re package
  • Loading branch information
josecelano committed Feb 14, 2025
1 parent c0fc390 commit eca5c59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/packages/udp_tracker_core/services/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,29 @@
use std::net::SocketAddr;
use std::sync::Arc;

use aquatic_udp_protocol::ScrapeRequest;
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;

/// It handles the `Scrape` request.
pub async fn handle_scrape(
remote_addr: SocketAddr,
request: &ScrapeRequest,
scrape_handler: &Arc<ScrapeHandler>,
opt_udp_stats_event_sender: &Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>,
) -> ScrapeData {
// Convert from aquatic infohashes
let mut info_hashes: Vec<InfoHash> = vec![];
for info_hash in &request.info_hashes {
info_hashes.push((*info_hash).into());
}

invoke(scrape_handler, opt_udp_stats_event_sender, &info_hashes, remote_addr).await
}

pub async fn invoke(
scrape_handler: &Arc<ScrapeHandler>,
opt_udp_stats_event_sender: &Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>,
Expand Down
13 changes: 5 additions & 8 deletions src/servers/udp/handlers/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ use std::sync::Arc;
use aquatic_udp_protocol::{
NumberOfDownloads, NumberOfPeers, Response, ScrapeRequest, ScrapeResponse, TorrentScrapeStatistics, TransactionId,
};
use bittorrent_primitives::info_hash::InfoHash;
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
use tracing::{instrument, Level};
use zerocopy::network_endian::I32;

use crate::packages::udp_tracker_core;
use crate::packages::udp_tracker_core::services;
use crate::servers::udp::connection_cookie::check;
use crate::servers::udp::error::Error;
use crate::servers::udp::handlers::gen_remote_fingerprint;
Expand All @@ -37,20 +35,19 @@ pub async fn handle_scrape(

tracing::trace!("handle scrape");

// todo: move authentication to `udp_tracker_core::services::scrape::handle_scrape`

check(
&request.connection_id,
gen_remote_fingerprint(&remote_addr),
cookie_valid_range,
)
.map_err(|e| (e, request.transaction_id))?;

// Convert from aquatic infohashes
let mut info_hashes: Vec<InfoHash> = vec![];
for info_hash in &request.info_hashes {
info_hashes.push((*info_hash).into());
}
let scrape_data =
udp_tracker_core::services::scrape::handle_scrape(remote_addr, request, scrape_handler, opt_udp_stats_event_sender).await;

let scrape_data = services::scrape::invoke(scrape_handler, opt_udp_stats_event_sender, &info_hashes, remote_addr).await;
// todo: extract `build_response` function.

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

Expand Down

0 comments on commit eca5c59

Please sign in to comment.