diff --git a/packages/axum-http-tracker-server/src/server.rs b/packages/axum-http-tracker-server/src/server.rs index ea8003a4f..f14a33602 100644 --- a/packages/axum-http-tracker-server/src/server.rs +++ b/packages/axum-http-tracker-server/src/server.rs @@ -241,15 +241,7 @@ mod tests { use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer; use bittorrent_http_tracker_core::services::announce::AnnounceService; use bittorrent_http_tracker_core::services::scrape::ScrapeService; - use bittorrent_tracker_core::announce_handler::AnnounceHandler; - use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository; - use bittorrent_tracker_core::authentication::service; - use bittorrent_tracker_core::databases::setup::initialize_database; - use bittorrent_tracker_core::scrape_handler::ScrapeHandler; - use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; - use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; - use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; - use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; + use bittorrent_tracker_core::container::TrackerCoreContainer; use torrust_axum_server::tsl::make_rust_tls; use torrust_server_lib::registar::Registar; use torrust_tracker_configuration::{logging, Configuration}; @@ -275,48 +267,25 @@ mod tests { let http_stats_event_sender = Arc::new(http_stats_event_sender); let http_stats_repository = Arc::new(http_stats_repository); - let database = initialize_database(&configuration.core); - let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); - let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&configuration.core, &in_memory_whitelist.clone())); - let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); - let authentication_service = Arc::new(service::AuthenticationService::new( - &configuration.core, - &in_memory_key_repository, - )); - let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); - - let announce_handler = Arc::new(AnnounceHandler::new( - &configuration.core, - &whitelist_authorization, - &in_memory_torrent_repository, - &db_torrent_repository, - )); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); + let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(&core_config)); let announce_service = Arc::new(AnnounceService::new( - core_config.clone(), - announce_handler.clone(), - authentication_service.clone(), - whitelist_authorization.clone(), + tracker_core_container.core_config.clone(), + tracker_core_container.announce_handler.clone(), + tracker_core_container.authentication_service.clone(), + tracker_core_container.whitelist_authorization.clone(), http_stats_event_sender.clone(), )); let scrape_service = Arc::new(ScrapeService::new( - core_config.clone(), - scrape_handler.clone(), - authentication_service.clone(), + tracker_core_container.core_config.clone(), + tracker_core_container.scrape_handler.clone(), + tracker_core_container.authentication_service.clone(), http_stats_event_sender.clone(), )); HttpTrackerCoreContainer { - core_config, - announce_handler, - scrape_handler, - whitelist_authorization, - authentication_service, - + tracker_core_container, http_tracker_config, http_stats_event_sender, http_stats_repository, diff --git a/packages/axum-rest-tracker-api-server/src/v1/context/stats/routes.rs b/packages/axum-rest-tracker-api-server/src/v1/context/stats/routes.rs index 49ba9e829..e92b5b34d 100644 --- a/packages/axum-rest-tracker-api-server/src/v1/context/stats/routes.rs +++ b/packages/axum-rest-tracker-api-server/src/v1/context/stats/routes.rs @@ -16,7 +16,7 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc) -> Router { let v1_prefix = format!("{prefix}/v1"); - let router = auth_key::routes::add(&v1_prefix, router, &http_api_container.keys_handler.clone()); + let router = auth_key::routes::add( + &v1_prefix, + router, + &http_api_container.tracker_core_container.keys_handler.clone(), + ); let router = stats::routes::add(&v1_prefix, router, http_api_container); - let router = whitelist::routes::add(&v1_prefix, router, &http_api_container.whitelist_manager); + let router = whitelist::routes::add( + &v1_prefix, + router, + &http_api_container.tracker_core_container.whitelist_manager, + ); - torrent::routes::add(&v1_prefix, router, &http_api_container.in_memory_torrent_repository.clone()) + torrent::routes::add( + &v1_prefix, + router, + &http_api_container.tracker_core_container.in_memory_torrent_repository.clone(), + ) } diff --git a/packages/http-tracker-core/src/container.rs b/packages/http-tracker-core/src/container.rs index bb9b5014c..ce577f1d8 100644 --- a/packages/http-tracker-core/src/container.rs +++ b/packages/http-tracker-core/src/container.rs @@ -1,10 +1,6 @@ use std::sync::Arc; -use bittorrent_tracker_core::announce_handler::AnnounceHandler; -use bittorrent_tracker_core::authentication::service::AuthenticationService; use bittorrent_tracker_core::container::TrackerCoreContainer; -use bittorrent_tracker_core::scrape_handler::ScrapeHandler; -use bittorrent_tracker_core::whitelist; use torrust_tracker_configuration::{Core, HttpTracker}; use crate::services::announce::AnnounceService; @@ -12,13 +8,7 @@ use crate::services::scrape::ScrapeService; use crate::{event, statistics}; pub struct HttpTrackerCoreContainer { - // todo: replace with TrackerCoreContainer - pub core_config: Arc, - pub announce_handler: Arc, - pub scrape_handler: Arc, - pub whitelist_authorization: Arc, - pub authentication_service: Arc, - + pub tracker_core_container: Arc, pub http_tracker_config: Arc, pub http_stats_event_sender: Arc>>, pub http_stats_repository: Arc, @@ -59,12 +49,7 @@ impl HttpTrackerCoreContainer { )); Arc::new(Self { - core_config: tracker_core_container.core_config.clone(), - announce_handler: tracker_core_container.announce_handler.clone(), - scrape_handler: tracker_core_container.scrape_handler.clone(), - whitelist_authorization: tracker_core_container.whitelist_authorization.clone(), - authentication_service: tracker_core_container.authentication_service.clone(), - + tracker_core_container: tracker_core_container.clone(), http_tracker_config: http_tracker_config.clone(), http_stats_event_sender: http_stats_event_sender.clone(), http_stats_repository: http_stats_repository.clone(), diff --git a/packages/rest-tracker-api-core/src/container.rs b/packages/rest-tracker-api-core/src/container.rs index eb770c1c5..c6a46a195 100644 --- a/packages/rest-tracker-api-core/src/container.rs +++ b/packages/rest-tracker-api-core/src/container.rs @@ -1,10 +1,7 @@ use std::sync::Arc; use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer; -use bittorrent_tracker_core::authentication::handler::KeysHandler; use bittorrent_tracker_core::container::TrackerCoreContainer; -use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; -use bittorrent_tracker_core::whitelist::manager::WhitelistManager; use bittorrent_udp_tracker_core::container::UdpTrackerCoreContainer; use bittorrent_udp_tracker_core::services::banning::BanService; use bittorrent_udp_tracker_core::{self}; @@ -13,22 +10,11 @@ use torrust_tracker_configuration::{Core, HttpApi, HttpTracker, UdpTracker}; use torrust_udp_tracker_server::container::UdpTrackerServerContainer; pub struct TrackerHttpApiCoreContainer { - // todo: replace with TrackerCoreContainer - pub core_config: Arc, - pub in_memory_torrent_repository: Arc, - pub keys_handler: Arc, - pub whitelist_manager: Arc, - - // todo: replace with HttpTrackerCoreContainer + pub tracker_core_container: Arc, pub http_stats_repository: Arc, - - // todo: replace with UdpTrackerCoreContainer pub ban_service: Arc>, pub udp_core_stats_repository: Arc, - - // todo: replace with UdpTrackerServerContainer pub udp_server_stats_repository: Arc, - pub http_api_config: Arc, } @@ -63,10 +49,7 @@ impl TrackerHttpApiCoreContainer { http_api_config: &Arc, ) -> Arc { Arc::new(TrackerHttpApiCoreContainer { - core_config: tracker_core_container.core_config.clone(), - in_memory_torrent_repository: tracker_core_container.in_memory_torrent_repository.clone(), - keys_handler: tracker_core_container.keys_handler.clone(), - whitelist_manager: tracker_core_container.whitelist_manager.clone(), + tracker_core_container: tracker_core_container.clone(), http_stats_repository: http_tracker_core_container.http_stats_repository.clone(), diff --git a/packages/udp-tracker-core/src/container.rs b/packages/udp-tracker-core/src/container.rs index aaa07f150..2ab578151 100644 --- a/packages/udp-tracker-core/src/container.rs +++ b/packages/udp-tracker-core/src/container.rs @@ -1,9 +1,6 @@ use std::sync::Arc; -use bittorrent_tracker_core::announce_handler::AnnounceHandler; use bittorrent_tracker_core::container::TrackerCoreContainer; -use bittorrent_tracker_core::scrape_handler::ScrapeHandler; -use bittorrent_tracker_core::whitelist; use tokio::sync::RwLock; use torrust_tracker_configuration::{Core, UdpTracker}; @@ -14,12 +11,7 @@ use crate::services::scrape::ScrapeService; use crate::{event, statistics, MAX_CONNECTION_ID_ERRORS_PER_IP}; pub struct UdpTrackerCoreContainer { - // todo: replace with TrackerCoreContainer - pub core_config: Arc, - pub announce_handler: Arc, - pub scrape_handler: Arc, - pub whitelist_authorization: Arc, - + pub tracker_core_container: Arc, pub udp_tracker_config: Arc, pub udp_core_stats_event_sender: Arc>>, pub udp_core_stats_repository: Arc, @@ -58,11 +50,7 @@ impl UdpTrackerCoreContainer { )); Arc::new(UdpTrackerCoreContainer { - core_config: tracker_core_container.core_config.clone(), - announce_handler: tracker_core_container.announce_handler.clone(), - scrape_handler: tracker_core_container.scrape_handler.clone(), - whitelist_authorization: tracker_core_container.whitelist_authorization.clone(), - + tracker_core_container: tracker_core_container.clone(), udp_tracker_config: udp_tracker_config.clone(), udp_core_stats_event_sender: udp_core_stats_event_sender.clone(), udp_core_stats_repository: udp_core_stats_repository.clone(), diff --git a/packages/udp-tracker-server/src/handlers/mod.rs b/packages/udp-tracker-server/src/handlers/mod.rs index 61f7bb187..34ac374fa 100644 --- a/packages/udp-tracker-server/src/handlers/mod.rs +++ b/packages/udp-tracker-server/src/handlers/mod.rs @@ -172,7 +172,7 @@ pub async fn handle_request( client_socket_addr, server_socket_addr, &announce_request, - &udp_tracker_core_container.core_config, + &udp_tracker_core_container.tracker_core_container.core_config, &udp_tracker_server_container.udp_server_stats_event_sender, cookie_time_values.valid_range, ) diff --git a/packages/udp-tracker-server/src/server/launcher.rs b/packages/udp-tracker-server/src/server/launcher.rs index c98db0500..b21ac11ba 100644 --- a/packages/udp-tracker-server/src/server/launcher.rs +++ b/packages/udp-tracker-server/src/server/launcher.rs @@ -47,7 +47,7 @@ impl Launcher { ) { tracing::info!(target: UDP_TRACKER_LOG_TARGET, "Starting on: {bind_to}"); - if udp_tracker_core_container.core_config.private { + if udp_tracker_core_container.tracker_core_container.core_config.private { tracing::error!("udp services cannot be used for private trackers"); panic!("it should not use udp if using authentication"); } diff --git a/share/default/config/tracker.development.sqlite3.toml b/share/default/config/tracker.development.sqlite3.toml index 96addaf87..333c6d66c 100644 --- a/share/default/config/tracker.development.sqlite3.toml +++ b/share/default/config/tracker.development.sqlite3.toml @@ -10,11 +10,21 @@ threshold = "info" listed = false private = false +[[udp_trackers]] +bind_address = "0.0.0.0:6868" +tracker_usage_statistics = true + [[udp_trackers]] bind_address = "0.0.0.0:6969" +tracker_usage_statistics = true [[http_trackers]] bind_address = "0.0.0.0:7070" +tracker_usage_statistics = true + +[[http_trackers]] +bind_address = "0.0.0.0:7171" +tracker_usage_statistics = true [http_api] bind_address = "0.0.0.0:1212" diff --git a/src/app.rs b/src/app.rs index 60e907a88..fb8a459ea 100644 --- a/src/app.rs +++ b/src/app.rs @@ -53,6 +53,7 @@ pub async fn start(config: &Configuration, app_container: &Arc) -> // Load peer keys if config.core.private { app_container + .tracker_core_container .keys_handler .load_peer_keys_from_database() .await @@ -62,6 +63,7 @@ pub async fn start(config: &Configuration, app_container: &Arc) -> // Load whitelisted torrents if config.core.listed { app_container + .tracker_core_container .whitelist_manager .load_whitelist_from_database() .await @@ -130,7 +132,10 @@ pub async fn start(config: &Configuration, app_container: &Arc) -> // Start runners to remove torrents without peers, every interval if config.core.inactive_peer_cleanup_interval > 0 { - jobs.push(torrent_cleanup::start_job(&config.core, &app_container.torrents_manager)); + jobs.push(torrent_cleanup::start_job( + &config.core, + &app_container.tracker_core_container.torrents_manager, + )); } // Start Health Check API diff --git a/src/container.rs b/src/container.rs index 3fcda55f0..d3253b5d9 100644 --- a/src/container.rs +++ b/src/container.rs @@ -3,53 +3,18 @@ use std::sync::Arc; use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer; use bittorrent_http_tracker_core::services::announce::AnnounceService; use bittorrent_http_tracker_core::services::scrape::ScrapeService; -use bittorrent_tracker_core::announce_handler::AnnounceHandler; -use bittorrent_tracker_core::authentication::handler::KeysHandler; -use bittorrent_tracker_core::authentication::service::AuthenticationService; use bittorrent_tracker_core::container::TrackerCoreContainer; -use bittorrent_tracker_core::databases::Database; -use bittorrent_tracker_core::scrape_handler::ScrapeHandler; -use bittorrent_tracker_core::torrent::manager::TorrentsManager; -use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; -use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; -use bittorrent_tracker_core::whitelist; -use bittorrent_tracker_core::whitelist::manager::WhitelistManager; -use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; use bittorrent_udp_tracker_core::container::UdpTrackerCoreContainer; use bittorrent_udp_tracker_core::services::banning::BanService; use bittorrent_udp_tracker_core::{self, MAX_CONNECTION_ID_ERRORS_PER_IP}; use tokio::sync::RwLock; use torrust_rest_tracker_api_core::container::TrackerHttpApiCoreContainer; -use torrust_tracker_configuration::{Configuration, Core, HttpApi, HttpTracker, UdpTracker}; +use torrust_tracker_configuration::{Configuration, HttpApi, HttpTracker, UdpTracker}; use torrust_udp_tracker_server::container::UdpTrackerServerContainer; use tracing::instrument; -/* todo: remove duplicate code. - - Use containers from packages as AppContainer fields: - - - bittorrent_tracker_core::container::TrackerCoreContainer - - bittorrent_udp_tracker_core::container::UdpTrackerCoreContainer - - bittorrent_http_tracker_core::container::HttpTrackerCoreContainer - - torrust_udp_tracker_server::container::UdpTrackerServerContainer - - Container initialization is duplicated. -*/ - pub struct AppContainer { - // Tracker Core Services - pub core_config: Arc, - pub database: Arc>, - pub announce_handler: Arc, - pub scrape_handler: Arc, - pub keys_handler: Arc, - pub authentication_service: Arc, - pub in_memory_whitelist: Arc, - pub whitelist_authorization: Arc, - pub whitelist_manager: Arc, - pub in_memory_torrent_repository: Arc, - pub db_torrent_repository: Arc, - pub torrents_manager: Arc, + pub tracker_core_container: Arc, // UDP Tracker Core Services pub udp_core_stats_event_sender: Arc>>, @@ -75,7 +40,7 @@ impl AppContainer { pub fn initialize(configuration: &Configuration) -> AppContainer { let core_config = Arc::new(configuration.core.clone()); - let tracker_core_container = TrackerCoreContainer::initialize(&core_config); + let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(&core_config)); // HTTP Tracker Core Services let (http_stats_event_sender, http_stats_repository) = @@ -122,19 +87,7 @@ impl AppContainer { let udp_server_stats_repository = Arc::new(udp_server_stats_repository); AppContainer { - // Tracker Core Services - core_config, - database: tracker_core_container.database, - announce_handler: tracker_core_container.announce_handler, - scrape_handler: tracker_core_container.scrape_handler, - keys_handler: tracker_core_container.keys_handler, - authentication_service: tracker_core_container.authentication_service, - in_memory_whitelist: tracker_core_container.in_memory_whitelist, - whitelist_authorization: tracker_core_container.whitelist_authorization, - whitelist_manager: tracker_core_container.whitelist_manager, - in_memory_torrent_repository: tracker_core_container.in_memory_torrent_repository, - db_torrent_repository: tracker_core_container.db_torrent_repository, - torrents_manager: tracker_core_container.torrents_manager, + tracker_core_container, // UDP Tracker Core Services udp_core_stats_event_sender, @@ -159,12 +112,7 @@ impl AppContainer { #[must_use] pub fn http_tracker_container(&self, http_tracker_config: &Arc) -> HttpTrackerCoreContainer { HttpTrackerCoreContainer { - core_config: self.core_config.clone(), - announce_handler: self.announce_handler.clone(), - scrape_handler: self.scrape_handler.clone(), - whitelist_authorization: self.whitelist_authorization.clone(), - authentication_service: self.authentication_service.clone(), - + tracker_core_container: self.tracker_core_container.clone(), http_tracker_config: http_tracker_config.clone(), http_stats_event_sender: self.http_stats_event_sender.clone(), http_stats_repository: self.http_stats_repository.clone(), @@ -176,11 +124,7 @@ impl AppContainer { #[must_use] pub fn udp_tracker_container(&self, udp_tracker_config: &Arc) -> UdpTrackerCoreContainer { UdpTrackerCoreContainer { - core_config: self.core_config.clone(), - announce_handler: self.announce_handler.clone(), - scrape_handler: self.scrape_handler.clone(), - whitelist_authorization: self.whitelist_authorization.clone(), - + tracker_core_container: self.tracker_core_container.clone(), udp_tracker_config: udp_tracker_config.clone(), udp_core_stats_event_sender: self.udp_core_stats_event_sender.clone(), udp_core_stats_repository: self.udp_core_stats_repository.clone(), @@ -194,11 +138,8 @@ impl AppContainer { #[must_use] pub fn tracker_http_api_container(&self, http_api_config: &Arc) -> TrackerHttpApiCoreContainer { TrackerHttpApiCoreContainer { + tracker_core_container: self.tracker_core_container.clone(), http_api_config: http_api_config.clone(), - core_config: self.core_config.clone(), - in_memory_torrent_repository: self.in_memory_torrent_repository.clone(), - keys_handler: self.keys_handler.clone(), - whitelist_manager: self.whitelist_manager.clone(), ban_service: self.udp_ban_service.clone(), http_stats_repository: self.http_stats_repository.clone(), udp_core_stats_repository: self.udp_core_stats_repository.clone(),