Skip to content

Commit b402bb9

Browse files
committed
refactor: [#1209] inline core Tracker methods
1 parent 5f65e16 commit b402bb9

File tree

7 files changed

+68
-28
lines changed

7 files changed

+68
-28
lines changed

src/app.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
//! - UDP trackers: the user can enable multiple UDP tracker on several ports.
2222
//! - HTTP trackers: the user can enable multiple HTTP tracker on several ports.
2323
//! - Tracker REST API: the tracker API can be enabled/disabled.
24+
use std::sync::Arc;
25+
2426
use tokio::task::JoinHandle;
2527
use torrust_tracker_configuration::Configuration;
2628
use tracing::instrument;
@@ -100,6 +102,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
100102
for http_tracker_config in http_trackers {
101103
if let Some(job) = http_tracker::start_job(
102104
http_tracker_config,
105+
Arc::new(config.core.clone()),
103106
app_container.tracker.clone(),
104107
app_container.announce_handler.clone(),
105108
app_container.scrape_handler.clone(),

src/bootstrap/jobs/http_tracker.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::sync::Arc;
1515

1616
use axum_server::tls_rustls::RustlsConfig;
1717
use tokio::task::JoinHandle;
18-
use torrust_tracker_configuration::HttpTracker;
18+
use torrust_tracker_configuration::{Core, HttpTracker};
1919
use tracing::instrument;
2020

2121
use super::make_rust_tls;
@@ -49,6 +49,7 @@ use crate::servers::registar::ServiceRegistrationForm;
4949
))]
5050
pub async fn start_job(
5151
config: &HttpTracker,
52+
core_config: Arc<Core>,
5253
tracker: Arc<core::Tracker>,
5354
announce_handler: Arc<AnnounceHandler>,
5455
scrape_handler: Arc<ScrapeHandler>,
@@ -69,6 +70,7 @@ pub async fn start_job(
6970
start_v1(
7071
socket,
7172
tls,
73+
core_config.clone(),
7274
tracker.clone(),
7375
announce_handler.clone(),
7476
scrape_handler.clone(),
@@ -97,6 +99,7 @@ pub async fn start_job(
9799
async fn start_v1(
98100
socket: SocketAddr,
99101
tls: Option<RustlsConfig>,
102+
config: Arc<Core>,
100103
tracker: Arc<core::Tracker>,
101104
announce_handler: Arc<AnnounceHandler>,
102105
scrape_handler: Arc<ScrapeHandler>,
@@ -107,6 +110,7 @@ async fn start_v1(
107110
) -> JoinHandle<()> {
108111
let server = HttpServer::new(Launcher::new(socket, tls))
109112
.start(
113+
config,
110114
tracker,
111115
announce_handler,
112116
scrape_handler,
@@ -156,6 +160,7 @@ mod tests {
156160

157161
start_job(
158162
config,
163+
Arc::new(cfg.core.clone()),
159164
app_container.tracker,
160165
app_container.announce_handler,
161166
app_container.scrape_handler,

src/core/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ use torrust_tracker_configuration::{AnnouncePolicy, Core};
469469
/// > the network layer.
470470
pub struct Tracker {
471471
/// The tracker configuration.
472-
config: Core,
472+
core_config: Core,
473473

474474
/// The in-memory torrents repository.
475475
_in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
@@ -485,12 +485,12 @@ impl Tracker {
485485
///
486486
/// Will return a `databases::error::Error` if unable to connect to database. The `Tracker` is responsible for the persistence.
487487
pub fn new(
488-
config: &Core,
488+
core_config: &Core,
489489
in_memory_torrent_repository: &Arc<InMemoryTorrentRepository>,
490490
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
491491
) -> Result<Tracker, databases::error::Error> {
492492
Ok(Tracker {
493-
config: config.clone(),
493+
core_config: core_config.clone(),
494494
_in_memory_torrent_repository: in_memory_torrent_repository.clone(),
495495
_db_torrent_repository: db_torrent_repository.clone(),
496496
})
@@ -499,23 +499,23 @@ impl Tracker {
499499
/// Returns `true` if the tracker requires authentication.
500500
#[must_use]
501501
pub fn requires_authentication(&self) -> bool {
502-
self.config.private
502+
self.core_config.private
503503
}
504504

505505
/// Returns `true` is the tracker is in whitelisted mode.
506506
#[must_use]
507507
pub fn is_behind_reverse_proxy(&self) -> bool {
508-
self.config.net.on_reverse_proxy
508+
self.core_config.net.on_reverse_proxy
509509
}
510510

511511
#[must_use]
512512
pub fn get_announce_policy(&self) -> AnnouncePolicy {
513-
self.config.announce_policy
513+
self.core_config.announce_policy
514514
}
515515

516516
#[must_use]
517517
pub fn get_maybe_external_ip(&self) -> Option<IpAddr> {
518-
self.config.net.external_ip
518+
self.core_config.net.external_ip
519519
}
520520
}
521521

src/servers/http/server.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use axum_server::Handle;
77
use derive_more::Constructor;
88
use futures::future::BoxFuture;
99
use tokio::sync::oneshot::{Receiver, Sender};
10+
use torrust_tracker_configuration::Core;
1011
use tracing::instrument;
1112

1213
use super::v1::routes::router;
@@ -59,6 +60,7 @@ impl Launcher {
5960
))]
6061
fn start(
6162
&self,
63+
config: Arc<Core>,
6264
tracker: Arc<Tracker>,
6365
announce_handler: Arc<AnnounceHandler>,
6466
scrape_handler: Arc<ScrapeHandler>,
@@ -85,6 +87,7 @@ impl Launcher {
8587
tracing::info!(target: HTTP_TRACKER_LOG_TARGET, "Starting on: {protocol}://{}", address);
8688

8789
let app = router(
90+
config,
8891
tracker,
8992
announce_handler,
9093
scrape_handler,
@@ -188,6 +191,7 @@ impl HttpServer<Stopped> {
188191
#[allow(clippy::too_many_arguments)]
189192
pub async fn start(
190193
self,
194+
core_config: Arc<Core>,
191195
tracker: Arc<Tracker>,
192196
announce_handler: Arc<AnnounceHandler>,
193197
scrape_handler: Arc<ScrapeHandler>,
@@ -203,6 +207,7 @@ impl HttpServer<Stopped> {
203207

204208
let task = tokio::spawn(async move {
205209
let server = launcher.start(
210+
core_config,
206211
tracker,
207212
announce_handler,
208213
scrape_handler,
@@ -309,6 +314,7 @@ mod tests {
309314
let stopped = HttpServer::new(Launcher::new(bind_to, tls));
310315
let started = stopped
311316
.start(
317+
Arc::new(cfg.core.clone()),
312318
app_container.tracker,
313319
app_container.announce_handler,
314320
app_container.scrape_handler,

src/servers/http/v1/handlers/announce.rs

+31-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use bittorrent_http_protocol::v1::services::peer_ip_resolver;
1818
use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;
1919
use hyper::StatusCode;
2020
use torrust_tracker_clock::clock::Time;
21+
use torrust_tracker_configuration::Core;
2122
use torrust_tracker_primitives::core::AnnounceData;
2223
use torrust_tracker_primitives::peer;
2324

@@ -39,6 +40,7 @@ use crate::CurrentClock;
3940
#[allow(clippy::type_complexity)]
4041
pub async fn handle_without_key(
4142
State(state): State<(
43+
Arc<Core>,
4244
Arc<Tracker>,
4345
Arc<AnnounceHandler>,
4446
Arc<AuthenticationService>,
@@ -56,6 +58,7 @@ pub async fn handle_without_key(
5658
&state.2,
5759
&state.3,
5860
&state.4,
61+
&state.5,
5962
&announce_request,
6063
&client_ip_sources,
6164
None,
@@ -69,6 +72,7 @@ pub async fn handle_without_key(
6972
#[allow(clippy::type_complexity)]
7073
pub async fn handle_with_key(
7174
State(state): State<(
75+
Arc<Core>,
7276
Arc<Tracker>,
7377
Arc<AnnounceHandler>,
7478
Arc<AuthenticationService>,
@@ -87,6 +91,7 @@ pub async fn handle_with_key(
8791
&state.2,
8892
&state.3,
8993
&state.4,
94+
&state.5,
9095
&announce_request,
9196
&client_ip_sources,
9297
Some(key),
@@ -100,6 +105,7 @@ pub async fn handle_with_key(
100105
/// `unauthenticated` modes.
101106
#[allow(clippy::too_many_arguments)]
102107
async fn handle(
108+
config: &Arc<Core>,
103109
tracker: &Arc<Tracker>,
104110
announce_handler: &Arc<AnnounceHandler>,
105111
authentication_service: &Arc<AuthenticationService>,
@@ -110,6 +116,7 @@ async fn handle(
110116
maybe_key: Option<Key>,
111117
) -> Response {
112118
let announce_data = match handle_announce(
119+
config,
113120
tracker,
114121
announce_handler,
115122
authentication_service,
@@ -135,6 +142,7 @@ async fn handle(
135142

136143
#[allow(clippy::too_many_arguments)]
137144
async fn handle_announce(
145+
config: &Arc<Core>,
138146
tracker: &Arc<Tracker>,
139147
announce_handler: &Arc<AnnounceHandler>,
140148
authentication_service: &Arc<AuthenticationService>,
@@ -145,7 +153,7 @@ async fn handle_announce(
145153
maybe_key: Option<Key>,
146154
) -> Result<AnnounceData, responses::error::Error> {
147155
// Authentication
148-
if tracker.requires_authentication() {
156+
if config.private {
149157
match maybe_key {
150158
Some(key) => match authentication_service.authenticate(&key).await {
151159
Ok(()) => (),
@@ -251,7 +259,7 @@ mod tests {
251259
use bittorrent_http_protocol::v1::responses;
252260
use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;
253261
use bittorrent_primitives::info_hash::InfoHash;
254-
use torrust_tracker_configuration::Configuration;
262+
use torrust_tracker_configuration::{Configuration, Core};
255263
use torrust_tracker_test_helpers::configuration;
256264

257265
use crate::app_test::initialize_tracker_dependencies;
@@ -262,6 +270,7 @@ mod tests {
262270
use crate::core::{whitelist, Tracker};
263271

264272
type TrackerAndDeps = (
273+
Arc<Core>,
265274
Arc<Tracker>,
266275
Arc<AnnounceHandler>,
267276
Arc<Option<Box<dyn Sender>>>,
@@ -270,23 +279,23 @@ mod tests {
270279
);
271280

272281
fn private_tracker() -> TrackerAndDeps {
273-
initialize_tracker_and_deps(&configuration::ephemeral_private())
282+
initialize_tracker_and_deps(configuration::ephemeral_private())
274283
}
275284

276285
fn whitelisted_tracker() -> TrackerAndDeps {
277-
initialize_tracker_and_deps(&configuration::ephemeral_listed())
286+
initialize_tracker_and_deps(configuration::ephemeral_listed())
278287
}
279288

280289
fn tracker_on_reverse_proxy() -> TrackerAndDeps {
281-
initialize_tracker_and_deps(&configuration::ephemeral_with_reverse_proxy())
290+
initialize_tracker_and_deps(configuration::ephemeral_with_reverse_proxy())
282291
}
283292

284293
fn tracker_not_on_reverse_proxy() -> TrackerAndDeps {
285-
initialize_tracker_and_deps(&configuration::ephemeral_without_reverse_proxy())
294+
initialize_tracker_and_deps(configuration::ephemeral_without_reverse_proxy())
286295
}
287296

288297
/// Initialize tracker's dependencies and tracker.
289-
fn initialize_tracker_and_deps(config: &Configuration) -> TrackerAndDeps {
298+
fn initialize_tracker_and_deps(config: Configuration) -> TrackerAndDeps {
290299
let (
291300
_database,
292301
_in_memory_whitelist,
@@ -295,13 +304,13 @@ mod tests {
295304
in_memory_torrent_repository,
296305
db_torrent_repository,
297306
_torrents_manager,
298-
) = initialize_tracker_dependencies(config);
307+
) = initialize_tracker_dependencies(&config);
299308

300309
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
301310
let stats_event_sender = Arc::new(stats_event_sender);
302311

303312
let tracker = Arc::new(initialize_tracker(
304-
config,
313+
&config,
305314
&in_memory_torrent_repository,
306315
&db_torrent_repository,
307316
));
@@ -312,7 +321,10 @@ mod tests {
312321
&db_torrent_repository,
313322
));
314323

324+
let config = Arc::new(config.core);
325+
315326
(
327+
config,
316328
tracker,
317329
announce_handler,
318330
stats_event_sender,
@@ -361,7 +373,7 @@ mod tests {
361373

362374
#[tokio::test]
363375
async fn it_should_fail_when_the_authentication_key_is_missing() {
364-
let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
376+
let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
365377
private_tracker();
366378

367379
let tracker = Arc::new(tracker);
@@ -370,6 +382,7 @@ mod tests {
370382
let maybe_key = None;
371383

372384
let response = handle_announce(
385+
&config,
373386
&tracker,
374387
&announce_handler,
375388
&authentication_service,
@@ -390,7 +403,7 @@ mod tests {
390403

391404
#[tokio::test]
392405
async fn it_should_fail_when_the_authentication_key_is_invalid() {
393-
let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
406+
let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
394407
private_tracker();
395408

396409
let tracker = Arc::new(tracker);
@@ -401,6 +414,7 @@ mod tests {
401414
let maybe_key = Some(unregistered_key);
402415

403416
let response = handle_announce(
417+
&config,
404418
&tracker,
405419
&announce_handler,
406420
&authentication_service,
@@ -427,7 +441,7 @@ mod tests {
427441

428442
#[tokio::test]
429443
async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() {
430-
let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
444+
let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
431445
whitelisted_tracker();
432446

433447
let tracker = Arc::new(tracker);
@@ -436,6 +450,7 @@ mod tests {
436450
let announce_request = sample_announce_request();
437451

438452
let response = handle_announce(
453+
&config,
439454
&tracker,
440455
&announce_handler,
441456
&authentication_service,
@@ -470,7 +485,7 @@ mod tests {
470485

471486
#[tokio::test]
472487
async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() {
473-
let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
488+
let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
474489
tracker_on_reverse_proxy();
475490

476491
let tracker = Arc::new(tracker);
@@ -482,6 +497,7 @@ mod tests {
482497
};
483498

484499
let response = handle_announce(
500+
&config,
485501
&tracker,
486502
&announce_handler,
487503
&authentication_service,
@@ -513,7 +529,7 @@ mod tests {
513529

514530
#[tokio::test]
515531
async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() {
516-
let (tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
532+
let (config, tracker, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) =
517533
tracker_not_on_reverse_proxy();
518534

519535
let tracker = Arc::new(tracker);
@@ -525,6 +541,7 @@ mod tests {
525541
};
526542

527543
let response = handle_announce(
544+
&config,
528545
&tracker,
529546
&announce_handler,
530547
&authentication_service,

0 commit comments

Comments
 (0)