diff --git a/src/app.rs b/src/app.rs index aafae5ebf..54ccbc60c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -21,6 +21,8 @@ //! - UDP trackers: the user can enable multiple UDP tracker on several ports. //! - HTTP trackers: the user can enable multiple HTTP tracker on several ports. //! - Tracker REST API: the tracker API can be enabled/disabled. +use std::sync::Arc; + use tokio::task::JoinHandle; use torrust_tracker_configuration::Configuration; use tracing::instrument; @@ -78,6 +80,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec< } else { jobs.push( udp_tracker::start_job( + Arc::new(config.core.clone()), udp_tracker_config, app_container.tracker.clone(), app_container.announce_handler.clone(), @@ -100,6 +103,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec< for http_tracker_config in http_trackers { if let Some(job) = http_tracker::start_job( http_tracker_config, + Arc::new(config.core.clone()), app_container.tracker.clone(), app_container.announce_handler.clone(), app_container.scrape_handler.clone(), diff --git a/src/bootstrap/jobs/http_tracker.rs b/src/bootstrap/jobs/http_tracker.rs index 5da7da739..5767f30ce 100644 --- a/src/bootstrap/jobs/http_tracker.rs +++ b/src/bootstrap/jobs/http_tracker.rs @@ -15,7 +15,7 @@ use std::sync::Arc; use axum_server::tls_rustls::RustlsConfig; use tokio::task::JoinHandle; -use torrust_tracker_configuration::HttpTracker; +use torrust_tracker_configuration::{Core, HttpTracker}; use tracing::instrument; use super::make_rust_tls; @@ -49,6 +49,7 @@ use crate::servers::registar::ServiceRegistrationForm; ))] pub async fn start_job( config: &HttpTracker, + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -69,6 +70,7 @@ pub async fn start_job( start_v1( socket, tls, + core_config.clone(), tracker.clone(), announce_handler.clone(), scrape_handler.clone(), @@ -97,6 +99,7 @@ pub async fn start_job( async fn start_v1( socket: SocketAddr, tls: Option, + config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -107,6 +110,7 @@ async fn start_v1( ) -> JoinHandle<()> { let server = HttpServer::new(Launcher::new(socket, tls)) .start( + config, tracker, announce_handler, scrape_handler, @@ -156,6 +160,7 @@ mod tests { start_job( config, + Arc::new(cfg.core.clone()), app_container.tracker, app_container.announce_handler, app_container.scrape_handler, diff --git a/src/bootstrap/jobs/udp_tracker.rs b/src/bootstrap/jobs/udp_tracker.rs index d43c1c930..36f3cd7b0 100644 --- a/src/bootstrap/jobs/udp_tracker.rs +++ b/src/bootstrap/jobs/udp_tracker.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use tokio::sync::RwLock; use tokio::task::JoinHandle; -use torrust_tracker_configuration::UdpTracker; +use torrust_tracker_configuration::{Core, UdpTracker}; use tracing::instrument; use crate::core::announce_handler::AnnounceHandler; @@ -46,6 +46,7 @@ use crate::servers::udp::UDP_TRACKER_LOG_TARGET; form ))] pub async fn start_job( + core_config: Arc, config: &UdpTracker, tracker: Arc, announce_handler: Arc, @@ -60,6 +61,7 @@ pub async fn start_job( let server = Server::new(Spawner::new(bind_to)) .start( + core_config, tracker, announce_handler, scrape_handler, diff --git a/src/core/mod.rs b/src/core/mod.rs index d30c47c6d..43a2aa11d 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -451,12 +451,11 @@ pub mod whitelist; pub mod peer_tests; -use std::net::IpAddr; use std::sync::Arc; use torrent::repository::in_memory::InMemoryTorrentRepository; use torrent::repository::persisted::DatabasePersistentTorrentRepository; -use torrust_tracker_configuration::{AnnouncePolicy, Core}; +use torrust_tracker_configuration::Core; /// The domain layer tracker service. /// @@ -469,7 +468,7 @@ use torrust_tracker_configuration::{AnnouncePolicy, Core}; /// > the network layer. pub struct Tracker { /// The tracker configuration. - config: Core, + _core_config: Core, /// The in-memory torrents repository. _in_memory_torrent_repository: Arc, @@ -485,38 +484,16 @@ impl Tracker { /// /// Will return a `databases::error::Error` if unable to connect to database. The `Tracker` is responsible for the persistence. pub fn new( - config: &Core, + core_config: &Core, in_memory_torrent_repository: &Arc, db_torrent_repository: &Arc, ) -> Result { Ok(Tracker { - config: config.clone(), + _core_config: core_config.clone(), _in_memory_torrent_repository: in_memory_torrent_repository.clone(), _db_torrent_repository: db_torrent_repository.clone(), }) } - - /// Returns `true` if the tracker requires authentication. - #[must_use] - pub fn requires_authentication(&self) -> bool { - self.config.private - } - - /// Returns `true` is the tracker is in whitelisted mode. - #[must_use] - pub fn is_behind_reverse_proxy(&self) -> bool { - self.config.net.on_reverse_proxy - } - - #[must_use] - pub fn get_announce_policy(&self) -> AnnouncePolicy { - self.config.announce_policy - } - - #[must_use] - pub fn get_maybe_external_ip(&self) -> Option { - self.config.net.external_ip - } } #[cfg(test)] diff --git a/src/servers/http/server.rs b/src/servers/http/server.rs index 3bb49c1ad..3817882df 100644 --- a/src/servers/http/server.rs +++ b/src/servers/http/server.rs @@ -7,6 +7,7 @@ use axum_server::Handle; use derive_more::Constructor; use futures::future::BoxFuture; use tokio::sync::oneshot::{Receiver, Sender}; +use torrust_tracker_configuration::Core; use tracing::instrument; use super::v1::routes::router; @@ -59,6 +60,7 @@ impl Launcher { ))] fn start( &self, + config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -85,6 +87,7 @@ impl Launcher { tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "Starting on: {protocol}://{}", address); let app = router( + config, tracker, announce_handler, scrape_handler, @@ -188,6 +191,7 @@ impl HttpServer { #[allow(clippy::too_many_arguments)] pub async fn start( self, + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -203,6 +207,7 @@ impl HttpServer { let task = tokio::spawn(async move { let server = launcher.start( + core_config, tracker, announce_handler, scrape_handler, @@ -309,6 +314,7 @@ mod tests { let stopped = HttpServer::new(Launcher::new(bind_to, tls)); let started = stopped .start( + Arc::new(cfg.core.clone()), app_container.tracker, app_container.announce_handler, app_container.scrape_handler, diff --git a/src/servers/http/v1/handlers/announce.rs b/src/servers/http/v1/handlers/announce.rs index 39ddd1710..ebdb717c3 100644 --- a/src/servers/http/v1/handlers/announce.rs +++ b/src/servers/http/v1/handlers/announce.rs @@ -18,6 +18,7 @@ use bittorrent_http_protocol::v1::services::peer_ip_resolver; use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; use hyper::StatusCode; use torrust_tracker_clock::clock::Time; +use torrust_tracker_configuration::Core; use torrust_tracker_primitives::core::AnnounceData; use torrust_tracker_primitives::peer; @@ -39,6 +40,7 @@ use crate::CurrentClock; #[allow(clippy::type_complexity)] pub async fn handle_without_key( State(state): State<( + Arc, Arc, Arc, Arc, @@ -56,6 +58,7 @@ pub async fn handle_without_key( &state.2, &state.3, &state.4, + &state.5, &announce_request, &client_ip_sources, None, @@ -69,6 +72,7 @@ pub async fn handle_without_key( #[allow(clippy::type_complexity)] pub async fn handle_with_key( State(state): State<( + Arc, Arc, Arc, Arc, @@ -87,6 +91,7 @@ pub async fn handle_with_key( &state.2, &state.3, &state.4, + &state.5, &announce_request, &client_ip_sources, Some(key), @@ -100,6 +105,7 @@ pub async fn handle_with_key( /// `unauthenticated` modes. #[allow(clippy::too_many_arguments)] async fn handle( + config: &Arc, tracker: &Arc, announce_handler: &Arc, authentication_service: &Arc, @@ -110,6 +116,7 @@ async fn handle( maybe_key: Option, ) -> Response { let announce_data = match handle_announce( + config, tracker, announce_handler, authentication_service, @@ -135,6 +142,7 @@ async fn handle( #[allow(clippy::too_many_arguments)] async fn handle_announce( + core_config: &Arc, tracker: &Arc, announce_handler: &Arc, authentication_service: &Arc, @@ -145,7 +153,7 @@ async fn handle_announce( maybe_key: Option, ) -> Result { // Authentication - if tracker.requires_authentication() { + if core_config.private { match maybe_key { Some(key) => match authentication_service.authenticate(&key).await { Ok(()) => (), @@ -165,7 +173,7 @@ async fn handle_announce( Err(error) => return Err(responses::error::Error::from(error)), } - let peer_ip = match peer_ip_resolver::invoke(tracker.is_behind_reverse_proxy(), client_ip_sources) { + let peer_ip = match peer_ip_resolver::invoke(core_config.net.on_reverse_proxy, client_ip_sources) { Ok(peer_ip) => peer_ip, Err(error) => return Err(responses::error::Error::from(error)), }; @@ -251,7 +259,7 @@ mod tests { use bittorrent_http_protocol::v1::responses; use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; use bittorrent_primitives::info_hash::InfoHash; - use torrust_tracker_configuration::Configuration; + use torrust_tracker_configuration::{Configuration, Core}; use torrust_tracker_test_helpers::configuration; use crate::app_test::initialize_tracker_dependencies; @@ -262,6 +270,7 @@ mod tests { use crate::core::{whitelist, Tracker}; type TrackerAndDeps = ( + Arc, Arc, Arc, Arc>>, @@ -270,23 +279,23 @@ mod tests { ); fn private_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(&configuration::ephemeral_private()) + initialize_tracker_and_deps(configuration::ephemeral_private()) } fn whitelisted_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(&configuration::ephemeral_listed()) + initialize_tracker_and_deps(configuration::ephemeral_listed()) } fn tracker_on_reverse_proxy() -> TrackerAndDeps { - initialize_tracker_and_deps(&configuration::ephemeral_with_reverse_proxy()) + initialize_tracker_and_deps(configuration::ephemeral_with_reverse_proxy()) } fn tracker_not_on_reverse_proxy() -> TrackerAndDeps { - initialize_tracker_and_deps(&configuration::ephemeral_without_reverse_proxy()) + initialize_tracker_and_deps(configuration::ephemeral_without_reverse_proxy()) } /// Initialize tracker's dependencies and tracker. - fn initialize_tracker_and_deps(config: &Configuration) -> TrackerAndDeps { + fn initialize_tracker_and_deps(config: Configuration) -> TrackerAndDeps { let ( _database, _in_memory_whitelist, @@ -295,13 +304,13 @@ mod tests { in_memory_torrent_repository, db_torrent_repository, _torrents_manager, - ) = initialize_tracker_dependencies(config); + ) = initialize_tracker_dependencies(&config); let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); let stats_event_sender = Arc::new(stats_event_sender); let tracker = Arc::new(initialize_tracker( - config, + &config, &in_memory_torrent_repository, &db_torrent_repository, )); @@ -312,7 +321,10 @@ mod tests { &db_torrent_repository, )); + let config = Arc::new(config.core); + ( + config, tracker, announce_handler, stats_event_sender, @@ -361,7 +373,7 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_authentication_key_is_missing() { - let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = + let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = private_tracker(); let tracker = Arc::new(tracker); @@ -370,6 +382,7 @@ mod tests { let maybe_key = None; let response = handle_announce( + &config, &tracker, &announce_handler, &authentication_service, @@ -390,7 +403,7 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_authentication_key_is_invalid() { - let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = + let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = private_tracker(); let tracker = Arc::new(tracker); @@ -401,6 +414,7 @@ mod tests { let maybe_key = Some(unregistered_key); let response = handle_announce( + &config, &tracker, &announce_handler, &authentication_service, @@ -427,7 +441,7 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() { - let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = + let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = whitelisted_tracker(); let tracker = Arc::new(tracker); @@ -436,6 +450,7 @@ mod tests { let announce_request = sample_announce_request(); let response = handle_announce( + &config, &tracker, &announce_handler, &authentication_service, @@ -470,7 +485,7 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() { - let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = + let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = tracker_on_reverse_proxy(); let tracker = Arc::new(tracker); @@ -482,6 +497,7 @@ mod tests { }; let response = handle_announce( + &config, &tracker, &announce_handler, &authentication_service, @@ -513,7 +529,7 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() { - let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = + let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = tracker_not_on_reverse_proxy(); let tracker = Arc::new(tracker); @@ -525,6 +541,7 @@ mod tests { }; let response = handle_announce( + &config, &tracker, &announce_handler, &authentication_service, diff --git a/src/servers/http/v1/handlers/scrape.rs b/src/servers/http/v1/handlers/scrape.rs index 3c19fe324..4f47a066f 100644 --- a/src/servers/http/v1/handlers/scrape.rs +++ b/src/servers/http/v1/handlers/scrape.rs @@ -13,6 +13,7 @@ use bittorrent_http_protocol::v1::requests::scrape::Scrape; use bittorrent_http_protocol::v1::responses; use bittorrent_http_protocol::v1::services::peer_ip_resolver::{self, ClientIpSources}; use hyper::StatusCode; +use torrust_tracker_configuration::Core; use torrust_tracker_primitives::core::ScrapeData; use crate::core::authentication::service::AuthenticationService; @@ -31,6 +32,7 @@ use crate::servers::http::v1::services; #[allow(clippy::type_complexity)] pub async fn handle_without_key( State(state): State<( + Arc, Arc, Arc, Arc, @@ -46,6 +48,7 @@ pub async fn handle_without_key( &state.1, &state.2, &state.3, + &state.4, &scrape_request, &client_ip_sources, None, @@ -61,6 +64,7 @@ pub async fn handle_without_key( #[allow(clippy::type_complexity)] pub async fn handle_with_key( State(state): State<( + Arc, Arc, Arc, Arc, @@ -77,6 +81,7 @@ pub async fn handle_with_key( &state.1, &state.2, &state.3, + &state.4, &scrape_request, &client_ip_sources, Some(key), @@ -84,7 +89,9 @@ pub async fn handle_with_key( .await } +#[allow(clippy::too_many_arguments)] async fn handle( + core_config: &Arc, tracker: &Arc, scrape_handler: &Arc, authentication_service: &Arc, @@ -94,6 +101,7 @@ async fn handle( maybe_key: Option, ) -> Response { let scrape_data = match handle_scrape( + core_config, tracker, scrape_handler, authentication_service, @@ -116,8 +124,10 @@ async fn handle( See https://github.com/torrust/torrust-tracker/discussions/240. */ +#[allow(clippy::too_many_arguments)] async fn handle_scrape( - tracker: &Arc, + core_config: &Arc, + _tracker: &Arc, scrape_handler: &Arc, authentication_service: &Arc, opt_stats_event_sender: &Arc>>, @@ -126,7 +136,7 @@ async fn handle_scrape( maybe_key: Option, ) -> Result { // Authentication - let return_real_scrape_data = if tracker.requires_authentication() { + let return_real_scrape_data = if core_config.private { match maybe_key { Some(key) => match authentication_service.authenticate(&key).await { Ok(()) => true, @@ -141,7 +151,7 @@ async fn handle_scrape( // Authorization for scrape requests is handled at the `Tracker` level // for each torrent. - let peer_ip = match peer_ip_resolver::invoke(tracker.is_behind_reverse_proxy(), client_ip_sources) { + let peer_ip = match peer_ip_resolver::invoke(core_config.net.on_reverse_proxy, client_ip_sources) { Ok(peer_ip) => peer_ip, Err(error) => return Err(responses::error::Error::from(error)), }; @@ -169,6 +179,7 @@ mod tests { use bittorrent_http_protocol::v1::responses; use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; use bittorrent_primitives::info_hash::InfoHash; + use torrust_tracker_configuration::Core; use torrust_tracker_test_helpers::configuration; use crate::app_test::initialize_tracker_dependencies; @@ -179,6 +190,7 @@ mod tests { #[allow(clippy::type_complexity)] fn private_tracker() -> ( + Arc, Arc, Arc, Arc>>, @@ -200,6 +212,8 @@ mod tests { let stats_event_sender = Arc::new(stats_event_sender); + let core_config = Arc::new(config.core.clone()); + let tracker = Arc::new(initialize_tracker( &config, &in_memory_torrent_repository, @@ -208,11 +222,18 @@ mod tests { let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - (tracker, scrape_handler, stats_event_sender, authentication_service) + ( + core_config, + tracker, + scrape_handler, + stats_event_sender, + authentication_service, + ) } #[allow(clippy::type_complexity)] fn whitelisted_tracker() -> ( + Arc, Arc, Arc, Arc>>, @@ -234,6 +255,8 @@ mod tests { let stats_event_sender = Arc::new(stats_event_sender); + let core_config = Arc::new(config.core.clone()); + let tracker = Arc::new(initialize_tracker( &config, &in_memory_torrent_repository, @@ -242,11 +265,18 @@ mod tests { let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - (tracker, scrape_handler, stats_event_sender, authentication_service) + ( + core_config, + tracker, + scrape_handler, + stats_event_sender, + authentication_service, + ) } #[allow(clippy::type_complexity)] fn tracker_on_reverse_proxy() -> ( + Arc, Arc, Arc, Arc>>, @@ -268,6 +298,8 @@ mod tests { let stats_event_sender = Arc::new(stats_event_sender); + let core_config = Arc::new(config.core.clone()); + let tracker = Arc::new(initialize_tracker( &config, &in_memory_torrent_repository, @@ -276,11 +308,18 @@ mod tests { let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - (tracker, scrape_handler, stats_event_sender, authentication_service) + ( + core_config, + tracker, + scrape_handler, + stats_event_sender, + authentication_service, + ) } #[allow(clippy::type_complexity)] fn tracker_not_on_reverse_proxy() -> ( + Arc, Arc, Arc, Arc>>, @@ -302,6 +341,8 @@ mod tests { let stats_event_sender = Arc::new(stats_event_sender); + let core_config = Arc::new(config.core.clone()); + let tracker = Arc::new(initialize_tracker( &config, &in_memory_torrent_repository, @@ -310,7 +351,13 @@ mod tests { let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - (tracker, scrape_handler, stats_event_sender, authentication_service) + ( + core_config, + tracker, + scrape_handler, + stats_event_sender, + authentication_service, + ) } fn sample_scrape_request() -> Scrape { @@ -344,12 +391,13 @@ mod tests { #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_authentication_key_is_missing() { - let (tracker, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); + let (core_config, tracker, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); let scrape_request = sample_scrape_request(); let maybe_key = None; let scrape_data = handle_scrape( + &core_config, &tracker, &scrape_handler, &authentication_service, @@ -368,13 +416,14 @@ mod tests { #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_authentication_key_is_invalid() { - let (tracker, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); + let (core_config, tracker, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); let scrape_request = sample_scrape_request(); let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(); let maybe_key = Some(unregistered_key); let scrape_data = handle_scrape( + &core_config, &tracker, &scrape_handler, &authentication_service, @@ -401,11 +450,12 @@ mod tests { #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_torrent_is_not_whitelisted() { - let (tracker, scrape_handler, stats_event_sender, authentication_service) = whitelisted_tracker(); + let (core_config, tracker, scrape_handler, stats_event_sender, authentication_service) = whitelisted_tracker(); let scrape_request = sample_scrape_request(); let scrape_data = handle_scrape( + &core_config, &tracker, &scrape_handler, &authentication_service, @@ -433,7 +483,7 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() { - let (tracker, scrape_handler, stats_event_sender, authentication_service) = tracker_on_reverse_proxy(); + let (core_config, tracker, scrape_handler, stats_event_sender, authentication_service) = tracker_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -441,6 +491,7 @@ mod tests { }; let response = handle_scrape( + &core_config, &tracker, &scrape_handler, &authentication_service, @@ -469,7 +520,8 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() { - let (tracker, scrape_handler, stats_event_sender, authentication_service) = tracker_not_on_reverse_proxy(); + let (core_config, tracker, scrape_handler, stats_event_sender, authentication_service) = + tracker_not_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -477,6 +529,7 @@ mod tests { }; let response = handle_scrape( + &core_config, &tracker, &scrape_handler, &authentication_service, diff --git a/src/servers/http/v1/routes.rs b/src/servers/http/v1/routes.rs index 50e1494be..85564ca8c 100644 --- a/src/servers/http/v1/routes.rs +++ b/src/servers/http/v1/routes.rs @@ -10,7 +10,7 @@ use axum::routing::get; use axum::{BoxError, Router}; use axum_client_ip::SecureClientIpSource; use hyper::{Request, StatusCode}; -use torrust_tracker_configuration::DEFAULT_TIMEOUT; +use torrust_tracker_configuration::{Core, DEFAULT_TIMEOUT}; use tower::timeout::TimeoutLayer; use tower::ServiceBuilder; use tower_http::classify::ServerErrorsFailureClass; @@ -34,6 +34,7 @@ use crate::servers::logging::Latency; /// /// > **NOTICE**: it's added a layer to get the client IP from the connection /// > info. The tracker could use the connection info to get the client IP. +#[allow(clippy::too_many_arguments)] #[allow(clippy::needless_pass_by_value)] #[instrument(skip( tracker, @@ -45,6 +46,7 @@ use crate::servers::logging::Latency; server_socket_addr ))] pub fn router( + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -60,6 +62,7 @@ pub fn router( .route( "/announce", get(announce::handle_without_key).with_state(( + core_config.clone(), tracker.clone(), announce_handler.clone(), authentication_service.clone(), @@ -70,6 +73,7 @@ pub fn router( .route( "/announce/{key}", get(announce::handle_with_key).with_state(( + core_config.clone(), tracker.clone(), announce_handler.clone(), authentication_service.clone(), @@ -81,6 +85,7 @@ pub fn router( .route( "/scrape", get(scrape::handle_without_key).with_state(( + core_config.clone(), tracker.clone(), scrape_handler.clone(), authentication_service.clone(), @@ -90,6 +95,7 @@ pub fn router( .route( "/scrape/{key}", get(scrape::handle_with_key).with_state(( + core_config.clone(), tracker.clone(), scrape_handler.clone(), authentication_service.clone(), diff --git a/src/servers/http/v1/services/announce.rs b/src/servers/http/v1/services/announce.rs index 1923037b3..5e2e8f716 100644 --- a/src/servers/http/v1/services/announce.rs +++ b/src/servers/http/v1/services/announce.rs @@ -63,6 +63,7 @@ mod tests { use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; use bittorrent_primitives::info_hash::InfoHash; + use torrust_tracker_configuration::Core; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; use torrust_tracker_test_helpers::configuration; @@ -73,7 +74,7 @@ mod tests { use crate::core::Tracker; #[allow(clippy::type_complexity)] - fn public_tracker() -> (Arc, Arc, Arc>>) { + fn public_tracker() -> (Arc, Arc, Arc, Arc>>) { let config = configuration::ephemeral_public(); let ( @@ -100,7 +101,9 @@ mod tests { &db_torrent_repository, )); - (tracker, announce_handler, stats_event_sender) + let core_config = Arc::new(config.core.clone()); + + (core_config, tracker, announce_handler, stats_event_sender) } fn sample_info_hash() -> InfoHash { @@ -176,7 +179,7 @@ mod tests { #[tokio::test] async fn it_should_return_the_announce_data() { - let (tracker, announce_handler, stats_event_sender) = public_tracker(); + let (core_config, tracker, announce_handler, stats_event_sender) = public_tracker(); let mut peer = sample_peer(); @@ -197,7 +200,7 @@ mod tests { complete: 1, incomplete: 0, }, - policy: tracker.get_announce_policy(), + policy: core_config.announce_policy, }; assert_eq!(announce_data, expected_announce_data); diff --git a/src/servers/udp/handlers.rs b/src/servers/udp/handlers.rs index 03a0248d4..b3ec1cb06 100644 --- a/src/servers/udp/handlers.rs +++ b/src/servers/udp/handlers.rs @@ -13,6 +13,7 @@ use aquatic_udp_protocol::{ use bittorrent_primitives::info_hash::InfoHash; use tokio::sync::RwLock; use torrust_tracker_clock::clock::Time as _; +use torrust_tracker_configuration::Core; use tracing::{instrument, Level}; use uuid::Uuid; use zerocopy::network_endian::I32; @@ -60,6 +61,7 @@ impl CookieTimeValues { #[instrument(fields(request_id), skip(udp_request, tracker, announce_handler, scrape_handler, whitelist_authorization, opt_stats_event_sender, cookie_time_values, ban_service), ret(level = Level::TRACE))] pub(crate) async fn handle_packet( udp_request: RawRequest, + core_config: &Arc, tracker: &Tracker, announce_handler: &Arc, scrape_handler: &Arc, @@ -81,6 +83,7 @@ pub(crate) async fn handle_packet( Ok(request) => match handle_request( request, udp_request.from, + core_config, tracker, announce_handler, scrape_handler, @@ -154,6 +157,7 @@ pub(crate) async fn handle_packet( pub async fn handle_request( request: Request, remote_addr: SocketAddr, + core_config: &Arc, tracker: &Tracker, announce_handler: &Arc, scrape_handler: &Arc, @@ -175,6 +179,7 @@ pub async fn handle_request( handle_announce( remote_addr, &announce_request, + core_config, tracker, announce_handler, whitelist_authorization, @@ -240,11 +245,13 @@ pub async fn handle_connect( /// # Errors /// /// If a error happens in the `handle_announce` function, it will just return the `ServerError`. -#[instrument(fields(transaction_id, connection_id, info_hash), skip(tracker, announce_handler, whitelist_authorization, opt_stats_event_sender), ret(level = Level::TRACE))] +#[allow(clippy::too_many_arguments)] +#[instrument(fields(transaction_id, connection_id, info_hash), skip(_tracker, announce_handler, whitelist_authorization, opt_stats_event_sender), ret(level = Level::TRACE))] pub async fn handle_announce( remote_addr: SocketAddr, request: &AnnounceRequest, - tracker: &Tracker, + core_config: &Arc, + _tracker: &Tracker, announce_handler: &Arc, whitelist_authorization: &Arc, opt_stats_event_sender: &Arc>>, @@ -297,7 +304,7 @@ pub async fn handle_announce( let announce_response = AnnounceResponse { fixed: AnnounceResponseFixedData { transaction_id: request.transaction_id, - announce_interval: AnnounceInterval(I32::new(i64::from(tracker.get_announce_policy().interval) as i32)), + announce_interval: AnnounceInterval(I32::new(i64::from(core_config.announce_policy.interval) as i32)), leechers: NumberOfPeers(I32::new(i64::from(response.stats.incomplete) as i32)), seeders: NumberOfPeers(I32::new(i64::from(response.stats.complete) as i32)), }, @@ -322,7 +329,7 @@ pub async fn handle_announce( let announce_response = AnnounceResponse { fixed: AnnounceResponseFixedData { transaction_id: request.transaction_id, - announce_interval: AnnounceInterval(I32::new(i64::from(tracker.get_announce_policy().interval) as i32)), + announce_interval: AnnounceInterval(I32::new(i64::from(core_config.announce_policy.interval) as i32)), leechers: NumberOfPeers(I32::new(i64::from(response.stats.incomplete) as i32)), seeders: NumberOfPeers(I32::new(i64::from(response.stats.complete) as i32)), }, @@ -492,7 +499,7 @@ mod tests { use aquatic_udp_protocol::{NumberOfBytes, PeerId}; use torrust_tracker_clock::clock::Time; - use torrust_tracker_configuration::Configuration; + use torrust_tracker_configuration::{Configuration, Core}; use torrust_tracker_primitives::peer; use torrust_tracker_test_helpers::configuration; @@ -509,6 +516,7 @@ mod tests { use crate::CurrentClock; type TrackerAndDeps = ( + Arc, Arc, Arc, Arc, @@ -536,6 +544,8 @@ mod tests { } fn initialize_tracker_and_deps(config: &Configuration) -> TrackerAndDeps { + let core_config = Arc::new(config.core.clone()); + let ( database, in_memory_whitelist, @@ -565,6 +575,7 @@ mod tests { let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); ( + core_config, tracker, announce_handler, scrape_handler, @@ -670,7 +681,9 @@ mod tests { } } + #[allow(clippy::type_complexity)] fn test_tracker_factory() -> ( + Arc, Arc, Arc, Arc, @@ -678,6 +691,8 @@ mod tests { ) { let config = tracker_configuration(); + let core_config = Arc::new(config.core.clone()); + let ( _database, _in_memory_whitelist, @@ -698,7 +713,13 @@ mod tests { let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - (tracker, announce_handler, scrape_handler, whitelist_authorization) + ( + core_config, + tracker, + announce_handler, + scrape_handler, + whitelist_authorization, + ) } mod connect_request { @@ -910,6 +931,7 @@ mod tests { PeerId as AquaticPeerId, Response, ResponsePeer, }; use mockall::predicate::eq; + use torrust_tracker_configuration::Core; use crate::core::announce_handler::AnnounceHandler; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -925,6 +947,7 @@ mod tests { #[tokio::test] async fn an_announced_peer_should_be_added_to_the_tracker() { let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -953,6 +976,7 @@ mod tests { handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -975,6 +999,7 @@ mod tests { #[tokio::test] async fn the_announced_peer_should_not_be_included_in_the_response() { let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -994,6 +1019,7 @@ mod tests { let response = handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1025,6 +1051,7 @@ mod tests { // "Do note that most trackers will only honor the IP address field under limited circumstances." let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -1056,6 +1083,7 @@ mod tests { handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1087,6 +1115,7 @@ mod tests { } async fn announce_a_new_peer_using_ipv4( + core_config: Arc, tracker: Arc, announce_handler: Arc, whitelist_authorization: Arc, @@ -1102,6 +1131,7 @@ mod tests { handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1115,6 +1145,7 @@ mod tests { #[tokio::test] async fn when_the_announce_request_comes_from_a_client_using_ipv4_the_response_should_not_include_peers_using_ipv6() { let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -1127,8 +1158,13 @@ mod tests { add_a_torrent_peer_using_ipv6(&in_memory_torrent_repository); - let response = - announce_a_new_peer_using_ipv4(tracker.clone(), announce_handler.clone(), whitelist_authorization).await; + let response = announce_a_new_peer_using_ipv4( + core_config.clone(), + tracker.clone(), + announce_handler.clone(), + whitelist_authorization, + ) + .await; // The response should not contain the peer using IPV6 let peers: Option>> = match response { @@ -1150,11 +1186,12 @@ mod tests { let stats_event_sender: Arc>> = Arc::new(Some(Box::new(stats_event_sender_mock))); - let (tracker, announce_handler, _scrape_handler, whitelist_authorization) = test_tracker_factory(); + let (core_config, tracker, announce_handler, _scrape_handler, whitelist_authorization) = test_tracker_factory(); handle_announce( sample_ipv4_socket_address(), &AnnounceRequestBuilder::default().into(), + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1181,6 +1218,7 @@ mod tests { #[tokio::test] async fn the_peer_ip_should_be_changed_to_the_external_ip_in_the_tracker_configuration_if_defined() { let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -1209,6 +1247,7 @@ mod tests { handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1220,7 +1259,7 @@ mod tests { let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash.0.into()); - let external_ip_in_tracker_configuration = tracker.get_maybe_external_ip().unwrap(); + let external_ip_in_tracker_configuration = core_config.net.external_ip.unwrap(); let expected_peer = TorrentPeerBuilder::new() .with_peer_id(peer_id) @@ -1243,6 +1282,7 @@ mod tests { PeerId as AquaticPeerId, Response, ResponsePeer, }; use mockall::predicate::eq; + use torrust_tracker_configuration::Core; use crate::core::announce_handler::AnnounceHandler; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -1258,6 +1298,7 @@ mod tests { #[tokio::test] async fn an_announced_peer_should_be_added_to_the_tracker() { let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -1287,6 +1328,7 @@ mod tests { handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1309,6 +1351,7 @@ mod tests { #[tokio::test] async fn the_announced_peer_should_not_be_included_in_the_response() { let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -1331,6 +1374,7 @@ mod tests { let response = handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1362,6 +1406,7 @@ mod tests { // "Do note that most trackers will only honor the IP address field under limited circumstances." let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -1393,6 +1438,7 @@ mod tests { handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1424,6 +1470,7 @@ mod tests { } async fn announce_a_new_peer_using_ipv6( + core_config: Arc, tracker: Arc, announce_handler: Arc, whitelist_authorization: Arc, @@ -1442,6 +1489,7 @@ mod tests { handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1455,6 +1503,7 @@ mod tests { #[tokio::test] async fn when_the_announce_request_comes_from_a_client_using_ipv6_the_response_should_not_include_peers_using_ipv4() { let ( + core_config, tracker, announce_handler, _scrape_handler, @@ -1467,8 +1516,13 @@ mod tests { add_a_torrent_peer_using_ipv4(&in_memory_torrent_repository); - let response = - announce_a_new_peer_using_ipv6(tracker.clone(), announce_handler.clone(), whitelist_authorization).await; + let response = announce_a_new_peer_using_ipv6( + core_config.clone(), + tracker.clone(), + announce_handler.clone(), + whitelist_authorization, + ) + .await; // The response should not contain the peer using IPV4 let peers: Option>> = match response { @@ -1490,7 +1544,7 @@ mod tests { let stats_event_sender: Arc>> = Arc::new(Some(Box::new(stats_event_sender_mock))); - let (tracker, announce_handler, _scrape_handler, whitelist_authorization) = test_tracker_factory(); + let (core_config, tracker, announce_handler, _scrape_handler, whitelist_authorization) = test_tracker_factory(); let remote_addr = sample_ipv6_remote_addr(); @@ -1501,6 +1555,7 @@ mod tests { handle_announce( remote_addr, &announce_request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1582,9 +1637,12 @@ mod tests { .with_port(client_port) .into(); + let core_config = Arc::new(config.core.clone()); + handle_announce( remote_addr, &request, + &core_config, &tracker, &announce_handler, &whitelist_authorization, @@ -1596,7 +1654,7 @@ mod tests { let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash.0.into()); - let external_ip_in_tracker_configuration = tracker.get_maybe_external_ip().unwrap(); + let external_ip_in_tracker_configuration = core_config.net.external_ip.unwrap(); assert!(external_ip_in_tracker_configuration.is_ipv6()); @@ -1641,6 +1699,7 @@ mod tests { #[tokio::test] async fn should_return_no_stats_when_the_tracker_does_not_have_any_torrent() { let ( + _core_config, _tracker, _announce_handler, scrape_handler, @@ -1750,6 +1809,7 @@ mod tests { #[tokio::test] async fn should_return_torrent_statistics_when_the_tracker_has_the_requested_torrent() { let ( + _core_config, _tracker, _announce_handler, scrape_handler, @@ -1786,6 +1846,7 @@ mod tests { #[tokio::test] async fn should_return_the_torrent_statistics_when_the_requested_torrent_is_whitelisted() { let ( + _core_config, _tracker, _announce_handler, scrape_handler, @@ -1830,6 +1891,7 @@ mod tests { #[tokio::test] async fn should_return_zeroed_statistics_when_the_requested_torrent_is_not_whitelisted() { let ( + _core_config, _tracker, _announce_handler, scrape_handler, @@ -1903,7 +1965,8 @@ mod tests { let remote_addr = sample_ipv4_remote_addr(); - let (_tracker, _announce_handler, scrape_handler, _whitelist_authorization) = test_tracker_factory(); + let (_core_config, _tracker, _announce_handler, scrape_handler, _whitelist_authorization) = + test_tracker_factory(); handle_scrape( remote_addr, @@ -1943,7 +2006,8 @@ mod tests { let remote_addr = sample_ipv6_remote_addr(); - let (_tracker, _announce_handler, scrape_handler, _whitelist_authorization) = test_tracker_factory(); + let (_core_config, _tracker, _announce_handler, scrape_handler, _whitelist_authorization) = + test_tracker_factory(); handle_scrape( remote_addr, diff --git a/src/servers/udp/server/launcher.rs b/src/servers/udp/server/launcher.rs index f1d0e4859..d0ae14029 100644 --- a/src/servers/udp/server/launcher.rs +++ b/src/servers/udp/server/launcher.rs @@ -8,6 +8,7 @@ use futures_util::StreamExt; use tokio::select; use tokio::sync::{oneshot, RwLock}; use tokio::time::interval; +use torrust_tracker_configuration::Core; use tracing::instrument; use super::banning::BanService; @@ -55,6 +56,7 @@ impl Launcher { rx_halt ))] pub async fn run_with_graceful_shutdown( + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -68,7 +70,7 @@ impl Launcher { ) { tracing::info!(target: UDP_TRACKER_LOG_TARGET, "Starting on: {bind_to}"); - if tracker.requires_authentication() { + if core_config.private { tracing::error!("udp services cannot be used for private trackers"); panic!("it should not use udp if using authentication"); } @@ -100,6 +102,7 @@ impl Launcher { tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_with_graceful_shutdown::task (listening...)"); let () = Self::run_udp_server_main( receiver, + core_config.clone(), tracker.clone(), announce_handler.clone(), scrape_handler.clone(), @@ -157,6 +160,7 @@ impl Launcher { ))] async fn run_udp_server_main( mut receiver: Receiver, + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -230,6 +234,7 @@ impl Launcher { let processor = Processor::new( receiver.socket.clone(), + core_config.clone(), tracker.clone(), announce_handler.clone(), scrape_handler.clone(), diff --git a/src/servers/udp/server/mod.rs b/src/servers/udp/server/mod.rs index 53ba588d4..f93d84a65 100644 --- a/src/servers/udp/server/mod.rs +++ b/src/servers/udp/server/mod.rs @@ -82,6 +82,7 @@ mod tests { let started = stopped .start( + Arc::new(cfg.core.clone()), app_container.tracker, app_container.announce_handler, app_container.scrape_handler, @@ -117,6 +118,7 @@ mod tests { let started = stopped .start( + Arc::new(cfg.core.clone()), app_container.tracker, app_container.announce_handler, app_container.scrape_handler, diff --git a/src/servers/udp/server/processor.rs b/src/servers/udp/server/processor.rs index 4cecbc36a..0bb7c92c4 100644 --- a/src/servers/udp/server/processor.rs +++ b/src/servers/udp/server/processor.rs @@ -6,6 +6,7 @@ use std::time::Duration; use aquatic_udp_protocol::Response; use tokio::sync::RwLock; use tokio::time::Instant; +use torrust_tracker_configuration::Core; use tracing::{instrument, Level}; use super::banning::BanService; @@ -20,6 +21,7 @@ use crate::servers::udp::{handlers, RawRequest}; pub struct Processor { socket: Arc, + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -29,8 +31,10 @@ pub struct Processor { } impl Processor { + #[allow(clippy::too_many_arguments)] pub fn new( socket: Arc, + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -40,6 +44,7 @@ impl Processor { ) -> Self { Self { socket, + core_config, tracker, announce_handler, scrape_handler, @@ -57,6 +62,7 @@ impl Processor { let response = handlers::handle_packet( request, + &self.core_config, &self.tracker, &self.announce_handler, &self.scrape_handler, diff --git a/src/servers/udp/server/spawner.rs b/src/servers/udp/server/spawner.rs index ea12b1c0b..ced5fbf4a 100644 --- a/src/servers/udp/server/spawner.rs +++ b/src/servers/udp/server/spawner.rs @@ -7,6 +7,7 @@ use derive_more::derive::Display; use derive_more::Constructor; use tokio::sync::{oneshot, RwLock}; use tokio::task::JoinHandle; +use torrust_tracker_configuration::Core; use super::banning::BanService; use super::launcher::Launcher; @@ -32,6 +33,7 @@ impl Spawner { #[allow(clippy::too_many_arguments)] pub fn spawn_launcher( &self, + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -46,6 +48,7 @@ impl Spawner { tokio::spawn(async move { Launcher::run_with_graceful_shutdown( + core_config, tracker, announce_handler, scrape_handler, diff --git a/src/servers/udp/server/states.rs b/src/servers/udp/server/states.rs index bab04fdcc..4d63dc0a8 100644 --- a/src/servers/udp/server/states.rs +++ b/src/servers/udp/server/states.rs @@ -7,6 +7,7 @@ use derive_more::derive::Display; use derive_more::Constructor; use tokio::sync::RwLock; use tokio::task::JoinHandle; +use torrust_tracker_configuration::Core; use tracing::{instrument, Level}; use super::banning::BanService; @@ -70,6 +71,7 @@ impl Server { #[instrument(skip(self, tracker, announce_handler, scrape_handler, whitelist_authorization, opt_stats_event_sender, ban_service, form), err, ret(Display, level = Level::INFO))] pub async fn start( self, + core_config: Arc, tracker: Arc, announce_handler: Arc, scrape_handler: Arc, @@ -86,6 +88,7 @@ impl Server { // May need to wrap in a task to about a tokio bug. let task = self.state.spawner.spawn_launcher( + core_config, tracker, announce_handler, scrape_handler, diff --git a/tests/servers/http/environment.rs b/tests/servers/http/environment.rs index 78051cbbb..203dc880e 100644 --- a/tests/servers/http/environment.rs +++ b/tests/servers/http/environment.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use bittorrent_primitives::info_hash::InfoHash; use futures::executor::block_on; -use torrust_tracker_configuration::{Configuration, HttpTracker}; +use torrust_tracker_configuration::{Configuration, Core, HttpTracker}; use torrust_tracker_lib::bootstrap::app::{initialize_app_container, initialize_global_services}; use torrust_tracker_lib::bootstrap::jobs::make_rust_tls; use torrust_tracker_lib::core::announce_handler::AnnounceHandler; @@ -20,7 +20,8 @@ use torrust_tracker_lib::servers::registar::Registar; use torrust_tracker_primitives::peer; pub struct Environment { - pub config: Arc, + pub core_config: Arc, + pub http_tracker_config: Arc, pub database: Arc>, pub tracker: Arc, pub announce_handler: Arc, @@ -64,7 +65,8 @@ impl Environment { let server = HttpServer::new(Launcher::new(bind_to, tls)); Self { - config, + http_tracker_config: config, + core_config: Arc::new(configuration.core.clone()), database: app_container.database.clone(), tracker: app_container.tracker.clone(), announce_handler: app_container.announce_handler.clone(), @@ -84,7 +86,8 @@ impl Environment { #[allow(dead_code)] pub async fn start(self) -> Environment { Environment { - config: self.config, + http_tracker_config: self.http_tracker_config, + core_config: self.core_config.clone(), database: self.database.clone(), tracker: self.tracker.clone(), announce_handler: self.announce_handler.clone(), @@ -100,6 +103,7 @@ impl Environment { server: self .server .start( + self.core_config, self.tracker, self.announce_handler, self.scrape_handler, @@ -121,7 +125,8 @@ impl Environment { pub async fn stop(self) -> Environment { Environment { - config: self.config, + http_tracker_config: self.http_tracker_config, + core_config: self.core_config, database: self.database, tracker: self.tracker, announce_handler: self.announce_handler, diff --git a/tests/servers/http/v1/contract.rs b/tests/servers/http/v1/contract.rs index 8a65d941a..33faf8578 100644 --- a/tests/servers/http/v1/contract.rs +++ b/tests/servers/http/v1/contract.rs @@ -449,7 +449,7 @@ mod for_all_config_modes { ) .await; - let announce_policy = env.tracker.get_announce_policy(); + let announce_policy = env.core_config.announce_policy; assert_announce_response( response, @@ -490,7 +490,7 @@ mod for_all_config_modes { ) .await; - let announce_policy = env.tracker.get_announce_policy(); + let announce_policy = env.core_config.announce_policy; // It should only contain the previously announced peer assert_announce_response( @@ -543,7 +543,7 @@ mod for_all_config_modes { ) .await; - let announce_policy = env.tracker.get_announce_policy(); + let announce_policy = env.core_config.announce_policy; // The newly announced peer is not included on the response peer list, // but all the previously announced peers should be included regardless the IP version they are using. @@ -872,7 +872,7 @@ mod for_all_config_modes { let peers = env.in_memory_torrent_repository.get_torrent_peers(&info_hash); let peer_addr = peers[0].peer_addr; - assert_eq!(peer_addr.ip(), env.tracker.get_maybe_external_ip().unwrap()); + assert_eq!(peer_addr.ip(), env.core_config.net.external_ip.unwrap()); assert_ne!(peer_addr.ip(), IpAddr::from_str("2.2.2.2").unwrap()); env.stop().await; @@ -914,7 +914,7 @@ mod for_all_config_modes { let peers = env.in_memory_torrent_repository.get_torrent_peers(&info_hash); let peer_addr = peers[0].peer_addr; - assert_eq!(peer_addr.ip(), env.tracker.get_maybe_external_ip().unwrap()); + assert_eq!(peer_addr.ip(), env.core_config.net.external_ip.unwrap()); assert_ne!(peer_addr.ip(), IpAddr::from_str("2.2.2.2").unwrap()); env.stop().await; diff --git a/tests/servers/udp/environment.rs b/tests/servers/udp/environment.rs index fafb7ef7a..11967aeed 100644 --- a/tests/servers/udp/environment.rs +++ b/tests/servers/udp/environment.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use bittorrent_primitives::info_hash::InfoHash; use tokio::sync::RwLock; -use torrust_tracker_configuration::{Configuration, UdpTracker, DEFAULT_TIMEOUT}; +use torrust_tracker_configuration::{Configuration, Core, UdpTracker, DEFAULT_TIMEOUT}; use torrust_tracker_lib::bootstrap::app::{initialize_app_container, initialize_global_services}; use torrust_tracker_lib::core::announce_handler::AnnounceHandler; use torrust_tracker_lib::core::databases::Database; @@ -23,6 +23,7 @@ pub struct Environment where S: std::fmt::Debug + std::fmt::Display, { + pub core_config: Arc, pub config: Arc, pub database: Arc>, pub tracker: Arc, @@ -64,6 +65,7 @@ impl Environment { let server = Server::new(Spawner::new(bind_to)); Self { + core_config: Arc::new(configuration.core.clone()), config, database: app_container.database.clone(), tracker: app_container.tracker.clone(), @@ -83,6 +85,7 @@ impl Environment { pub async fn start(self) -> Environment { let cookie_lifetime = self.config.cookie_lifetime; Environment { + core_config: self.core_config.clone(), config: self.config, database: self.database.clone(), tracker: self.tracker.clone(), @@ -97,6 +100,7 @@ impl Environment { server: self .server .start( + self.core_config, self.tracker, self.announce_handler, self.scrape_handler, @@ -126,6 +130,7 @@ impl Environment { .expect("it should stop the environment within the timeout"); Environment { + core_config: self.core_config, config: self.config, database: self.database, tracker: self.tracker,