Skip to content

Commit

Permalink
Merge #1208: Overhaul core Tracker: extract announce handler
Browse files Browse the repository at this point in the history
2f74caf chore: fix linting errors (Jose Celano)
209b52c refactor: [#1207] inline methods (Jose Celano)
026f957 refactor: [#1207] extract AnnounceHandler (Jose Celano)
401c228 refactor: [#1207] make method upsert_peer_and_get_stats private (Jose Celano)
f741d06 refactor: [#1207] use InMemoryTorrentRepository in tests to add torrents (Jose Celano)
f23a3fc chore: [#1207] remove deprecated comment (Jose Celano)

Pull request description:

  Overhaul core Tracker: extract announce handler.

ACKs for top commit:
  josecelano:
    ACK 2f74caf

Tree-SHA512: 8b8c863bf822391104afa37ae1a23adf1609323d9e18caab0c843121f249e371465775962c795b53c0a7b7e6ad759a33b8244a975fe12d9048307ac59b59fae6
  • Loading branch information
josecelano committed Jan 27, 2025
2 parents 6e47314 + 2f74caf commit 5f65e16
Show file tree
Hide file tree
Showing 31 changed files with 648 additions and 346 deletions.
2 changes: 1 addition & 1 deletion packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This module contains the configuration data structures for the
//! Torrust Tracker, which is a `BitTorrent` tracker server.
//!
//! The current version for configuration is [`v2`].
//! The current version for configuration is [`v2_0_0`].
pub mod v2_0_0;
pub mod validator;

Expand Down
12 changes: 6 additions & 6 deletions packages/configuration/src/v2_0_0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
//! Please refer to the documentation of each structure for more information
//! about each section.
//!
//! - [`Core configuration`](crate::v2::Configuration)
//! - [`HTTP API configuration`](crate::v2::tracker_api::HttpApi)
//! - [`HTTP Tracker configuration`](crate::v2::http_tracker::HttpTracker)
//! - [`UDP Tracker configuration`](crate::v2::udp_tracker::UdpTracker)
//! - [`Health Check API configuration`](crate::v2::health_check_api::HealthCheckApi)
//! - [`Core configuration`](crate::v2_0_0::Configuration)
//! - [`HTTP API configuration`](crate::v2_0_0::tracker_api::HttpApi)
//! - [`HTTP Tracker configuration`](crate::v2_0_0::http_tracker::HttpTracker)
//! - [`UDP Tracker configuration`](crate::v2_0_0::udp_tracker::UdpTracker)
//! - [`Health Check API configuration`](crate::v2_0_0::health_check_api::HealthCheckApi)
//!
//! ## Port binding
//!
Expand Down Expand Up @@ -78,7 +78,7 @@
//!
//! Alternatively, you could setup a reverse proxy like Nginx or Apache to
//! handle the SSL/TLS part and forward the requests to the tracker. If you do
//! that, you should set [`on_reverse_proxy`](crate::v2::network::Network::on_reverse_proxy)
//! that, you should set [`on_reverse_proxy`](crate::v2_0_0::network::Network::on_reverse_proxy)
//! to `true` in the configuration file. It's out of scope for this
//! documentation to explain in detail how to setup a reverse proxy, but the
//! configuration file should be something like this:
Expand Down
4 changes: 2 additions & 2 deletions packages/http-protocol/src/v1/requests/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ impl fmt::Display for Event {
/// Depending on the value of this param, the tracker will return a different
/// response:
///
/// - [`Normal`](crate::servers::http::v1::responses::announce::Normal), i.e. a `non-compact` response.
/// - [`Compact`](crate::servers::http::v1::responses::announce::Compact) response.
/// - [`Normal`](crate::v1::responses::announce::Normal), i.e. a `non-compact` response.
/// - [`Compact`](crate::v1::responses::announce::Compact) response.
///
/// Refer to [BEP 23. Tracker Returns Compact Peer Lists](https://www.bittorrent.org/beps/bep_0023.html)
#[derive(PartialEq, Debug)]
Expand Down
3 changes: 0 additions & 3 deletions packages/http-protocol/src/v1/requests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//! HTTP requests for the HTTP tracker.
//!
//! Refer to the generic [HTTP server documentation](crate::servers::http) for
//! more information about the HTTP tracker.
pub mod announce;
pub mod scrape;
2 changes: 1 addition & 1 deletion packages/http-protocol/src/v1/responses/announce.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `Announce` response for the HTTP tracker [`announce`](bittorrent_http_protocol::v1::requests::announce::Announce) request.
//! `Announce` response for the HTTP tracker [`announce`](crate::v1::requests::announce::Announce) request.
//!
//! Data structures and logic to build the `announce` response.
use std::io::Write;
Expand Down
4 changes: 2 additions & 2 deletions packages/http-protocol/src/v1/responses/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `Error` response for the [`HTTP tracker`](crate::servers::http).
//! `Error` response for the HTTP tracker.
//!
//! Data structures and logic to build the error responses.
//!
Expand All @@ -15,7 +15,7 @@ use serde::Serialize;

use crate::v1::services::peer_ip_resolver::PeerIpResolutionError;

/// `Error` response for the [`HTTP tracker`](crate::servers::http).
/// `Error` response for the HTTP tracker.
#[derive(Serialize, Debug, PartialEq)]
pub struct Error {
/// Human readable string which explains why the request failed.
Expand Down
3 changes: 0 additions & 3 deletions packages/http-protocol/src/v1/responses/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//! HTTP responses for the HTTP tracker.
//!
//! Refer to the generic [HTTP server documentation](crate::servers::http) for
//! more information about the HTTP tracker.
pub mod announce;
pub mod error;
pub mod scrape;
Expand Down
2 changes: 1 addition & 1 deletion packages/http-protocol/src/v1/responses/scrape.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `Scrape` response for the HTTP tracker [`scrape`](bittorrent_http_protocol::v1::requests::scrape::Scrape) request.
//! `Scrape` response for the HTTP tracker [`scrape`](crate::v1::requests::scrape::Scrape) request.
//!
//! Data structures and logic to build the `scrape` response.
use std::borrow::Cow;
Expand Down
8 changes: 5 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
let registar = Registar::default();

// Load peer keys
if app_container.tracker.is_private() {
if config.core.private {
app_container
.keys_handler
.load_keys_from_database()
Expand All @@ -59,7 +59,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
}

// Load whitelisted torrents
if app_container.tracker.is_listed() {
if config.core.listed {
app_container
.whitelist_manager
.load_whitelist_from_database()
Expand All @@ -70,7 +70,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
// Start the UDP blocks
if let Some(udp_trackers) = &config.udp_trackers {
for udp_tracker_config in udp_trackers {
if app_container.tracker.is_private() {
if config.core.private {
tracing::warn!(
"Could not start UDP tracker on: {} while in private mode. UDP is not safe for private trackers!",
udp_tracker_config.bind_address
Expand All @@ -80,6 +80,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
udp_tracker::start_job(
udp_tracker_config,
app_container.tracker.clone(),
app_container.announce_handler.clone(),
app_container.scrape_handler.clone(),
app_container.whitelist_authorization.clone(),
app_container.stats_event_sender.clone(),
Expand All @@ -100,6 +101,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
if let Some(job) = http_tracker::start_job(
http_tracker_config,
app_container.tracker.clone(),
app_container.announce_handler.clone(),
app_container.scrape_handler.clone(),
app_container.authentication_service.clone(),
app_container.whitelist_authorization.clone(),
Expand Down
8 changes: 8 additions & 0 deletions src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use tracing::instrument;
use super::config::initialize_configuration;
use crate::bootstrap;
use crate::container::AppContainer;
use crate::core::announce_handler::AnnounceHandler;
use crate::core::authentication::handler::KeysHandler;
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
Expand Down Expand Up @@ -121,11 +122,18 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
&db_torrent_repository,
));

let announce_handler = Arc::new(AnnounceHandler::new(
&configuration.core,
&in_memory_torrent_repository,
&db_torrent_repository,
));

let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));

AppContainer {
database,
tracker,
announce_handler,
scrape_handler,
keys_handler,
authentication_service,
Expand Down
18 changes: 17 additions & 1 deletion src/bootstrap/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use torrust_tracker_configuration::HttpTracker;
use tracing::instrument;

use super::make_rust_tls;
use crate::core::announce_handler::AnnounceHandler;
use crate::core::authentication::service::AuthenticationService;
use crate::core::scrape_handler::ScrapeHandler;
use crate::core::statistics::event::sender::Sender;
Expand All @@ -39,6 +40,7 @@ use crate::servers::registar::ServiceRegistrationForm;
#[instrument(skip(
config,
tracker,
announce_handler,
scrape_handler,
authentication_service,
whitelist_authorization,
Expand All @@ -48,6 +50,7 @@ use crate::servers::registar::ServiceRegistrationForm;
pub async fn start_job(
config: &HttpTracker,
tracker: Arc<core::Tracker>,
announce_handler: Arc<AnnounceHandler>,
scrape_handler: Arc<ScrapeHandler>,
authentication_service: Arc<AuthenticationService>,
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
Expand All @@ -67,6 +70,7 @@ pub async fn start_job(
socket,
tls,
tracker.clone(),
announce_handler.clone(),
scrape_handler.clone(),
authentication_service.clone(),
whitelist_authorization.clone(),
Expand All @@ -80,11 +84,21 @@ pub async fn start_job(

#[allow(clippy::too_many_arguments)]
#[allow(clippy::async_yields_async)]
#[instrument(skip(socket, tls, tracker, scrape_handler, whitelist_authorization, stats_event_sender, form))]
#[instrument(skip(
socket,
tls,
tracker,
announce_handler,
scrape_handler,
whitelist_authorization,
stats_event_sender,
form
))]
async fn start_v1(
socket: SocketAddr,
tls: Option<RustlsConfig>,
tracker: Arc<core::Tracker>,
announce_handler: Arc<AnnounceHandler>,
scrape_handler: Arc<ScrapeHandler>,
authentication_service: Arc<AuthenticationService>,
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
Expand All @@ -94,6 +108,7 @@ async fn start_v1(
let server = HttpServer::new(Launcher::new(socket, tls))
.start(
tracker,
announce_handler,
scrape_handler,
authentication_service,
whitelist_authorization,
Expand Down Expand Up @@ -142,6 +157,7 @@ mod tests {
start_job(
config,
app_container.tracker,
app_container.announce_handler,
app_container.scrape_handler,
app_container.authentication_service,
app_container.whitelist_authorization,
Expand Down
5 changes: 5 additions & 0 deletions src/bootstrap/jobs/udp_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tokio::task::JoinHandle;
use torrust_tracker_configuration::UdpTracker;
use tracing::instrument;

use crate::core::announce_handler::AnnounceHandler;
use crate::core::scrape_handler::ScrapeHandler;
use crate::core::statistics::event::sender::Sender;
use crate::core::{self, whitelist};
Expand All @@ -32,10 +33,12 @@ use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
/// It will panic if it is unable to start the UDP service.
/// It will panic if the task did not finish successfully.
#[must_use]
#[allow(clippy::too_many_arguments)]
#[allow(clippy::async_yields_async)]
#[instrument(skip(
config,
tracker,
announce_handler,
scrape_handler,
whitelist_authorization,
stats_event_sender,
Expand All @@ -45,6 +48,7 @@ use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
pub async fn start_job(
config: &UdpTracker,
tracker: Arc<core::Tracker>,
announce_handler: Arc<AnnounceHandler>,
scrape_handler: Arc<ScrapeHandler>,
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
stats_event_sender: Arc<Option<Box<dyn Sender>>>,
Expand All @@ -57,6 +61,7 @@ pub async fn start_job(
let server = Server::new(Spawner::new(bind_to))
.start(
tracker,
announce_handler,
scrape_handler,
whitelist_authorization,
stats_event_sender,
Expand Down
2 changes: 2 additions & 0 deletions src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use tokio::sync::RwLock;

use crate::core::announce_handler::AnnounceHandler;
use crate::core::authentication::handler::KeysHandler;
use crate::core::authentication::service::AuthenticationService;
use crate::core::databases::Database;
Expand All @@ -18,6 +19,7 @@ use crate::servers::udp::server::banning::BanService;
pub struct AppContainer {
pub database: Arc<Box<dyn Database>>,
pub tracker: Arc<Tracker>,
pub announce_handler: Arc<AnnounceHandler>,
pub scrape_handler: Arc<ScrapeHandler>,
pub keys_handler: Arc<KeysHandler>,
pub authentication_service: Arc<AuthenticationService>,
Expand Down
Loading

0 comments on commit 5f65e16

Please sign in to comment.