Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor containers #1406

Merged
merged 6 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 10 additions & 41 deletions packages/axum-http-tracker-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
router.route(
&format!("{prefix}/stats"),
get(get_stats_handler).with_state((
http_api_container.in_memory_torrent_repository.clone(),
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
http_api_container.ban_service.clone(),
http_api_container.http_stats_repository.clone(),
http_api_container.udp_server_stats_repository.clone(),
Expand Down
18 changes: 15 additions & 3 deletions packages/axum-rest-tracker-api-server/src/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@ use super::context::{auth_key, stats, torrent, whitelist};
pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApiCoreContainer>) -> 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(),
)
}
19 changes: 2 additions & 17 deletions packages/http-tracker-core/src/container.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
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;
use crate::services::scrape::ScrapeService;
use crate::{event, statistics};

pub struct HttpTrackerCoreContainer {
// todo: replace with TrackerCoreContainer
pub core_config: Arc<Core>,
pub announce_handler: Arc<AnnounceHandler>,
pub scrape_handler: Arc<ScrapeHandler>,
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
pub authentication_service: Arc<AuthenticationService>,

pub tracker_core_container: Arc<TrackerCoreContainer>,
pub http_tracker_config: Arc<HttpTracker>,
pub http_stats_event_sender: Arc<Option<Box<dyn event::sender::Sender>>>,
pub http_stats_repository: Arc<statistics::repository::Repository>,
Expand Down Expand Up @@ -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(),
Expand Down
21 changes: 2 additions & 19 deletions packages/rest-tracker-api-core/src/container.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<Core>,
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
pub keys_handler: Arc<KeysHandler>,
pub whitelist_manager: Arc<WhitelistManager>,

// todo: replace with HttpTrackerCoreContainer
pub tracker_core_container: Arc<TrackerCoreContainer>,
pub http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,

// todo: replace with UdpTrackerCoreContainer
pub ban_service: Arc<RwLock<BanService>>,
pub udp_core_stats_repository: Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,

// todo: replace with UdpTrackerServerContainer
pub udp_server_stats_repository: Arc<torrust_udp_tracker_server::statistics::repository::Repository>,

pub http_api_config: Arc<HttpApi>,
}

Expand Down Expand Up @@ -63,10 +49,7 @@ impl TrackerHttpApiCoreContainer {
http_api_config: &Arc<HttpApi>,
) -> Arc<TrackerHttpApiCoreContainer> {
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(),

Expand Down
16 changes: 2 additions & 14 deletions packages/udp-tracker-core/src/container.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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<Core>,
pub announce_handler: Arc<AnnounceHandler>,
pub scrape_handler: Arc<ScrapeHandler>,
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,

pub tracker_core_container: Arc<TrackerCoreContainer>,
pub udp_tracker_config: Arc<UdpTracker>,
pub udp_core_stats_event_sender: Arc<Option<Box<dyn event::sender::Sender>>>,
pub udp_core_stats_repository: Arc<statistics::repository::Repository>,
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion packages/udp-tracker-server/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion packages/udp-tracker-server/src/server/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
10 changes: 10 additions & 0 deletions share/default/config/tracker.development.sqlite3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) ->
// Load peer keys
if config.core.private {
app_container
.tracker_core_container
.keys_handler
.load_peer_keys_from_database()
.await
Expand All @@ -62,6 +63,7 @@ pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) ->
// Load whitelisted torrents
if config.core.listed {
app_container
.tracker_core_container
.whitelist_manager
.load_whitelist_from_database()
.await
Expand Down Expand Up @@ -130,7 +132,10 @@ pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) ->

// 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
Expand Down
Loading