Skip to content

Commit 5f65e16

Browse files
committed
Merge #1208: Overhaul core Tracker: extract announce handler
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
2 parents 6e47314 + 2f74caf commit 5f65e16

31 files changed

+648
-346
lines changed

packages/configuration/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module contains the configuration data structures for the
44
//! Torrust Tracker, which is a `BitTorrent` tracker server.
55
//!
6-
//! The current version for configuration is [`v2`].
6+
//! The current version for configuration is [`v2_0_0`].
77
pub mod v2_0_0;
88
pub mod validator;
99

packages/configuration/src/v2_0_0/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
//! Please refer to the documentation of each structure for more information
4040
//! about each section.
4141
//!
42-
//! - [`Core configuration`](crate::v2::Configuration)
43-
//! - [`HTTP API configuration`](crate::v2::tracker_api::HttpApi)
44-
//! - [`HTTP Tracker configuration`](crate::v2::http_tracker::HttpTracker)
45-
//! - [`UDP Tracker configuration`](crate::v2::udp_tracker::UdpTracker)
46-
//! - [`Health Check API configuration`](crate::v2::health_check_api::HealthCheckApi)
42+
//! - [`Core configuration`](crate::v2_0_0::Configuration)
43+
//! - [`HTTP API configuration`](crate::v2_0_0::tracker_api::HttpApi)
44+
//! - [`HTTP Tracker configuration`](crate::v2_0_0::http_tracker::HttpTracker)
45+
//! - [`UDP Tracker configuration`](crate::v2_0_0::udp_tracker::UdpTracker)
46+
//! - [`Health Check API configuration`](crate::v2_0_0::health_check_api::HealthCheckApi)
4747
//!
4848
//! ## Port binding
4949
//!
@@ -78,7 +78,7 @@
7878
//!
7979
//! Alternatively, you could setup a reverse proxy like Nginx or Apache to
8080
//! handle the SSL/TLS part and forward the requests to the tracker. If you do
81-
//! that, you should set [`on_reverse_proxy`](crate::v2::network::Network::on_reverse_proxy)
81+
//! that, you should set [`on_reverse_proxy`](crate::v2_0_0::network::Network::on_reverse_proxy)
8282
//! to `true` in the configuration file. It's out of scope for this
8383
//! documentation to explain in detail how to setup a reverse proxy, but the
8484
//! configuration file should be something like this:

packages/http-protocol/src/v1/requests/announce.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ impl fmt::Display for Event {
185185
/// Depending on the value of this param, the tracker will return a different
186186
/// response:
187187
///
188-
/// - [`Normal`](crate::servers::http::v1::responses::announce::Normal), i.e. a `non-compact` response.
189-
/// - [`Compact`](crate::servers::http::v1::responses::announce::Compact) response.
188+
/// - [`Normal`](crate::v1::responses::announce::Normal), i.e. a `non-compact` response.
189+
/// - [`Compact`](crate::v1::responses::announce::Compact) response.
190190
///
191191
/// Refer to [BEP 23. Tracker Returns Compact Peer Lists](https://www.bittorrent.org/beps/bep_0023.html)
192192
#[derive(PartialEq, Debug)]
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
//! HTTP requests for the HTTP tracker.
2-
//!
3-
//! Refer to the generic [HTTP server documentation](crate::servers::http) for
4-
//! more information about the HTTP tracker.
52
pub mod announce;
63
pub mod scrape;

packages/http-protocol/src/v1/responses/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! `Announce` response for the HTTP tracker [`announce`](bittorrent_http_protocol::v1::requests::announce::Announce) request.
1+
//! `Announce` response for the HTTP tracker [`announce`](crate::v1::requests::announce::Announce) request.
22
//!
33
//! Data structures and logic to build the `announce` response.
44
use std::io::Write;

packages/http-protocol/src/v1/responses/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! `Error` response for the [`HTTP tracker`](crate::servers::http).
1+
//! `Error` response for the HTTP tracker.
22
//!
33
//! Data structures and logic to build the error responses.
44
//!
@@ -15,7 +15,7 @@ use serde::Serialize;
1515

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

18-
/// `Error` response for the [`HTTP tracker`](crate::servers::http).
18+
/// `Error` response for the HTTP tracker.
1919
#[derive(Serialize, Debug, PartialEq)]
2020
pub struct Error {
2121
/// Human readable string which explains why the request failed.

packages/http-protocol/src/v1/responses/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
//! HTTP responses for the HTTP tracker.
2-
//!
3-
//! Refer to the generic [HTTP server documentation](crate::servers::http) for
4-
//! more information about the HTTP tracker.
52
pub mod announce;
63
pub mod error;
74
pub mod scrape;

packages/http-protocol/src/v1/responses/scrape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! `Scrape` response for the HTTP tracker [`scrape`](bittorrent_http_protocol::v1::requests::scrape::Scrape) request.
1+
//! `Scrape` response for the HTTP tracker [`scrape`](crate::v1::requests::scrape::Scrape) request.
22
//!
33
//! Data structures and logic to build the `scrape` response.
44
use std::borrow::Cow;

src/app.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
5050
let registar = Registar::default();
5151

5252
// Load peer keys
53-
if app_container.tracker.is_private() {
53+
if config.core.private {
5454
app_container
5555
.keys_handler
5656
.load_keys_from_database()
@@ -59,7 +59,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
5959
}
6060

6161
// Load whitelisted torrents
62-
if app_container.tracker.is_listed() {
62+
if config.core.listed {
6363
app_container
6464
.whitelist_manager
6565
.load_whitelist_from_database()
@@ -70,7 +70,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
7070
// Start the UDP blocks
7171
if let Some(udp_trackers) = &config.udp_trackers {
7272
for udp_tracker_config in udp_trackers {
73-
if app_container.tracker.is_private() {
73+
if config.core.private {
7474
tracing::warn!(
7575
"Could not start UDP tracker on: {} while in private mode. UDP is not safe for private trackers!",
7676
udp_tracker_config.bind_address
@@ -80,6 +80,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
8080
udp_tracker::start_job(
8181
udp_tracker_config,
8282
app_container.tracker.clone(),
83+
app_container.announce_handler.clone(),
8384
app_container.scrape_handler.clone(),
8485
app_container.whitelist_authorization.clone(),
8586
app_container.stats_event_sender.clone(),
@@ -100,6 +101,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
100101
if let Some(job) = http_tracker::start_job(
101102
http_tracker_config,
102103
app_container.tracker.clone(),
104+
app_container.announce_handler.clone(),
103105
app_container.scrape_handler.clone(),
104106
app_container.authentication_service.clone(),
105107
app_container.whitelist_authorization.clone(),

src/bootstrap/app.rs

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tracing::instrument;
2222
use super::config::initialize_configuration;
2323
use crate::bootstrap;
2424
use crate::container::AppContainer;
25+
use crate::core::announce_handler::AnnounceHandler;
2526
use crate::core::authentication::handler::KeysHandler;
2627
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
2728
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
@@ -121,11 +122,18 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
121122
&db_torrent_repository,
122123
));
123124

125+
let announce_handler = Arc::new(AnnounceHandler::new(
126+
&configuration.core,
127+
&in_memory_torrent_repository,
128+
&db_torrent_repository,
129+
));
130+
124131
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));
125132

126133
AppContainer {
127134
database,
128135
tracker,
136+
announce_handler,
129137
scrape_handler,
130138
keys_handler,
131139
authentication_service,

src/bootstrap/jobs/http_tracker.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use torrust_tracker_configuration::HttpTracker;
1919
use tracing::instrument;
2020

2121
use super::make_rust_tls;
22+
use crate::core::announce_handler::AnnounceHandler;
2223
use crate::core::authentication::service::AuthenticationService;
2324
use crate::core::scrape_handler::ScrapeHandler;
2425
use crate::core::statistics::event::sender::Sender;
@@ -39,6 +40,7 @@ use crate::servers::registar::ServiceRegistrationForm;
3940
#[instrument(skip(
4041
config,
4142
tracker,
43+
announce_handler,
4244
scrape_handler,
4345
authentication_service,
4446
whitelist_authorization,
@@ -48,6 +50,7 @@ use crate::servers::registar::ServiceRegistrationForm;
4850
pub async fn start_job(
4951
config: &HttpTracker,
5052
tracker: Arc<core::Tracker>,
53+
announce_handler: Arc<AnnounceHandler>,
5154
scrape_handler: Arc<ScrapeHandler>,
5255
authentication_service: Arc<AuthenticationService>,
5356
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
@@ -67,6 +70,7 @@ pub async fn start_job(
6770
socket,
6871
tls,
6972
tracker.clone(),
73+
announce_handler.clone(),
7074
scrape_handler.clone(),
7175
authentication_service.clone(),
7276
whitelist_authorization.clone(),
@@ -80,11 +84,21 @@ pub async fn start_job(
8084

8185
#[allow(clippy::too_many_arguments)]
8286
#[allow(clippy::async_yields_async)]
83-
#[instrument(skip(socket, tls, tracker, scrape_handler, whitelist_authorization, stats_event_sender, form))]
87+
#[instrument(skip(
88+
socket,
89+
tls,
90+
tracker,
91+
announce_handler,
92+
scrape_handler,
93+
whitelist_authorization,
94+
stats_event_sender,
95+
form
96+
))]
8497
async fn start_v1(
8598
socket: SocketAddr,
8699
tls: Option<RustlsConfig>,
87100
tracker: Arc<core::Tracker>,
101+
announce_handler: Arc<AnnounceHandler>,
88102
scrape_handler: Arc<ScrapeHandler>,
89103
authentication_service: Arc<AuthenticationService>,
90104
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
@@ -94,6 +108,7 @@ async fn start_v1(
94108
let server = HttpServer::new(Launcher::new(socket, tls))
95109
.start(
96110
tracker,
111+
announce_handler,
97112
scrape_handler,
98113
authentication_service,
99114
whitelist_authorization,
@@ -142,6 +157,7 @@ mod tests {
142157
start_job(
143158
config,
144159
app_container.tracker,
160+
app_container.announce_handler,
145161
app_container.scrape_handler,
146162
app_container.authentication_service,
147163
app_container.whitelist_authorization,

src/bootstrap/jobs/udp_tracker.rs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use tokio::task::JoinHandle;
1313
use torrust_tracker_configuration::UdpTracker;
1414
use tracing::instrument;
1515

16+
use crate::core::announce_handler::AnnounceHandler;
1617
use crate::core::scrape_handler::ScrapeHandler;
1718
use crate::core::statistics::event::sender::Sender;
1819
use crate::core::{self, whitelist};
@@ -32,10 +33,12 @@ use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
3233
/// It will panic if it is unable to start the UDP service.
3334
/// It will panic if the task did not finish successfully.
3435
#[must_use]
36+
#[allow(clippy::too_many_arguments)]
3537
#[allow(clippy::async_yields_async)]
3638
#[instrument(skip(
3739
config,
3840
tracker,
41+
announce_handler,
3942
scrape_handler,
4043
whitelist_authorization,
4144
stats_event_sender,
@@ -45,6 +48,7 @@ use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
4548
pub async fn start_job(
4649
config: &UdpTracker,
4750
tracker: Arc<core::Tracker>,
51+
announce_handler: Arc<AnnounceHandler>,
4852
scrape_handler: Arc<ScrapeHandler>,
4953
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
5054
stats_event_sender: Arc<Option<Box<dyn Sender>>>,
@@ -57,6 +61,7 @@ pub async fn start_job(
5761
let server = Server::new(Spawner::new(bind_to))
5862
.start(
5963
tracker,
64+
announce_handler,
6065
scrape_handler,
6166
whitelist_authorization,
6267
stats_event_sender,

src/container.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use tokio::sync::RwLock;
44

5+
use crate::core::announce_handler::AnnounceHandler;
56
use crate::core::authentication::handler::KeysHandler;
67
use crate::core::authentication::service::AuthenticationService;
78
use crate::core::databases::Database;
@@ -18,6 +19,7 @@ use crate::servers::udp::server::banning::BanService;
1819
pub struct AppContainer {
1920
pub database: Arc<Box<dyn Database>>,
2021
pub tracker: Arc<Tracker>,
22+
pub announce_handler: Arc<AnnounceHandler>,
2123
pub scrape_handler: Arc<ScrapeHandler>,
2224
pub keys_handler: Arc<KeysHandler>,
2325
pub authentication_service: Arc<AuthenticationService>,

0 commit comments

Comments
 (0)