Skip to content

Commit 9cda8ec

Browse files
committedFeb 28, 2025··
refactor: [torrust#1326] extract bittorrent_http_tracker_core::services::announce::AnnounceService
1 parent 809b85b commit 9cda8ec

File tree

6 files changed

+220
-262
lines changed

6 files changed

+220
-262
lines changed
 

‎packages/axum-http-tracker-server/src/server.rs

+10
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ mod tests {
239239
use std::sync::Arc;
240240

241241
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
242+
use bittorrent_http_tracker_core::services::announce::AnnounceService;
242243
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
243244
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
244245
use bittorrent_tracker_core::authentication::service;
@@ -293,6 +294,14 @@ mod tests {
293294

294295
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));
295296

297+
let announce_service = Arc::new(AnnounceService::new(
298+
core_config.clone(),
299+
announce_handler.clone(),
300+
authentication_service.clone(),
301+
whitelist_authorization.clone(),
302+
http_stats_event_sender.clone(),
303+
));
304+
296305
HttpTrackerCoreContainer {
297306
core_config,
298307
announce_handler,
@@ -303,6 +312,7 @@ mod tests {
303312
http_tracker_config,
304313
http_stats_event_sender,
305314
http_stats_repository,
315+
announce_service,
306316
}
307317
}
308318

‎packages/axum-http-tracker-server/src/v1/handlers/announce.rs

+38-131
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ use std::sync::Arc;
66

77
use axum::extract::State;
88
use axum::response::{IntoResponse, Response};
9-
use bittorrent_http_tracker_core::services::announce::HttpAnnounceError;
9+
use bittorrent_http_tracker_core::services::announce::{AnnounceService, HttpAnnounceError};
1010
use bittorrent_http_tracker_protocol::v1::requests::announce::{Announce, Compact};
1111
use bittorrent_http_tracker_protocol::v1::responses::{self};
1212
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;
13-
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
14-
use bittorrent_tracker_core::authentication::service::AuthenticationService;
1513
use bittorrent_tracker_core::authentication::Key;
16-
use bittorrent_tracker_core::whitelist;
1714
use hyper::StatusCode;
18-
use torrust_tracker_configuration::Core;
1915
use torrust_tracker_primitives::core::AnnounceData;
2016

2117
use crate::v1::extractors::announce_request::ExtractRequest;
@@ -25,91 +21,41 @@ use crate::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
2521
/// It handles the `announce` request when the HTTP tracker does not require
2622
/// authentication (no PATH `key` parameter required).
2723
#[allow(clippy::unused_async)]
28-
#[allow(clippy::type_complexity)]
2924
pub async fn handle_without_key(
30-
State(state): State<(
31-
Arc<Core>,
32-
Arc<AnnounceHandler>,
33-
Arc<AuthenticationService>,
34-
Arc<whitelist::authorization::WhitelistAuthorization>,
35-
Arc<Option<Box<dyn bittorrent_http_tracker_core::statistics::event::sender::Sender>>>,
36-
)>,
25+
State(state): State<Arc<AnnounceService>>,
3726
ExtractRequest(announce_request): ExtractRequest,
3827
ExtractClientIpSources(client_ip_sources): ExtractClientIpSources,
3928
) -> Response {
4029
tracing::debug!("http announce request: {:#?}", announce_request);
4130

42-
handle(
43-
&state.0,
44-
&state.1,
45-
&state.2,
46-
&state.3,
47-
&state.4,
48-
&announce_request,
49-
&client_ip_sources,
50-
None,
51-
)
52-
.await
31+
handle(&state, &announce_request, &client_ip_sources, None).await
5332
}
5433

5534
/// It handles the `announce` request when the HTTP tracker requires
5635
/// authentication (PATH `key` parameter required).
5736
#[allow(clippy::unused_async)]
58-
#[allow(clippy::type_complexity)]
5937
pub async fn handle_with_key(
60-
State(state): State<(
61-
Arc<Core>,
62-
Arc<AnnounceHandler>,
63-
Arc<AuthenticationService>,
64-
Arc<whitelist::authorization::WhitelistAuthorization>,
65-
Arc<Option<Box<dyn bittorrent_http_tracker_core::statistics::event::sender::Sender>>>,
66-
)>,
38+
State(state): State<Arc<AnnounceService>>,
6739
ExtractRequest(announce_request): ExtractRequest,
6840
ExtractClientIpSources(client_ip_sources): ExtractClientIpSources,
6941
ExtractKey(key): ExtractKey,
7042
) -> Response {
7143
tracing::debug!("http announce request: {:#?}", announce_request);
7244

73-
handle(
74-
&state.0,
75-
&state.1,
76-
&state.2,
77-
&state.3,
78-
&state.4,
79-
&announce_request,
80-
&client_ip_sources,
81-
Some(key),
82-
)
83-
.await
45+
handle(&state, &announce_request, &client_ip_sources, Some(key)).await
8446
}
8547

8648
/// It handles the `announce` request.
8749
///
8850
/// Internal implementation that handles both the `authenticated` and
8951
/// `unauthenticated` modes.
90-
#[allow(clippy::too_many_arguments)]
9152
async fn handle(
92-
config: &Arc<Core>,
93-
announce_handler: &Arc<AnnounceHandler>,
94-
authentication_service: &Arc<AuthenticationService>,
95-
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
96-
opt_http_stats_event_sender: &Arc<Option<Box<dyn bittorrent_http_tracker_core::statistics::event::sender::Sender>>>,
53+
announce_service: &Arc<AnnounceService>,
9754
announce_request: &Announce,
9855
client_ip_sources: &ClientIpSources,
9956
maybe_key: Option<Key>,
10057
) -> Response {
101-
let announce_data = match handle_announce(
102-
config,
103-
announce_handler,
104-
authentication_service,
105-
whitelist_authorization,
106-
opt_http_stats_event_sender,
107-
announce_request,
108-
client_ip_sources,
109-
maybe_key,
110-
)
111-
.await
112-
{
58+
let announce_data = match handle_announce(announce_service, announce_request, client_ip_sources, maybe_key).await {
11359
Ok(announce_data) => announce_data,
11460
Err(error) => {
11561
let error_response = responses::error::Error {
@@ -121,28 +67,15 @@ async fn handle(
12167
build_response(announce_request, announce_data)
12268
}
12369

124-
#[allow(clippy::too_many_arguments)]
12570
async fn handle_announce(
126-
core_config: &Arc<Core>,
127-
announce_handler: &Arc<AnnounceHandler>,
128-
authentication_service: &Arc<AuthenticationService>,
129-
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
130-
opt_http_stats_event_sender: &Arc<Option<Box<dyn bittorrent_http_tracker_core::statistics::event::sender::Sender>>>,
71+
announce_service: &Arc<AnnounceService>,
13172
announce_request: &Announce,
13273
client_ip_sources: &ClientIpSources,
13374
maybe_key: Option<Key>,
13475
) -> Result<AnnounceData, HttpAnnounceError> {
135-
bittorrent_http_tracker_core::services::announce::handle_announce(
136-
&core_config.clone(),
137-
&announce_handler.clone(),
138-
&authentication_service.clone(),
139-
&whitelist_authorization.clone(),
140-
&opt_http_stats_event_sender.clone(),
141-
announce_request,
142-
client_ip_sources,
143-
maybe_key,
144-
)
145-
.await
76+
announce_service
77+
.handle_announce(announce_request, client_ip_sources, maybe_key)
78+
.await
14679
}
14780

14881
fn build_response(announce_request: &Announce, announce_data: AnnounceData) -> Response {
@@ -163,6 +96,7 @@ mod tests {
16396
use std::sync::Arc;
16497

16598
use aquatic_udp_protocol::PeerId;
99+
use bittorrent_http_tracker_core::services::announce::AnnounceService;
166100
use bittorrent_http_tracker_protocol::v1::requests::announce::Announce;
167101
use bittorrent_http_tracker_protocol::v1::responses;
168102
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;
@@ -174,39 +108,32 @@ mod tests {
174108
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
175109
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
176110
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
177-
use torrust_tracker_configuration::{Configuration, Core};
111+
use torrust_tracker_configuration::Configuration;
178112
use torrust_tracker_test_helpers::configuration;
179113

180114
use crate::tests::helpers::sample_info_hash;
181115

182-
struct CoreTrackerServices {
183-
pub core_config: Arc<Core>,
184-
pub announce_handler: Arc<AnnounceHandler>,
185-
pub whitelist_authorization: Arc<WhitelistAuthorization>,
186-
pub authentication_service: Arc<AuthenticationService>,
187-
}
188-
189116
struct CoreHttpTrackerServices {
190-
pub http_stats_event_sender: Arc<Option<Box<dyn bittorrent_http_tracker_core::statistics::event::sender::Sender>>>,
117+
pub announce_service: Arc<AnnounceService>,
191118
}
192119

193-
fn initialize_private_tracker() -> (CoreTrackerServices, CoreHttpTrackerServices) {
120+
fn initialize_private_tracker() -> CoreHttpTrackerServices {
194121
initialize_core_tracker_services(&configuration::ephemeral_private())
195122
}
196123

197-
fn initialize_listed_tracker() -> (CoreTrackerServices, CoreHttpTrackerServices) {
124+
fn initialize_listed_tracker() -> CoreHttpTrackerServices {
198125
initialize_core_tracker_services(&configuration::ephemeral_listed())
199126
}
200127

201-
fn initialize_tracker_on_reverse_proxy() -> (CoreTrackerServices, CoreHttpTrackerServices) {
128+
fn initialize_tracker_on_reverse_proxy() -> CoreHttpTrackerServices {
202129
initialize_core_tracker_services(&configuration::ephemeral_with_reverse_proxy())
203130
}
204131

205-
fn initialize_tracker_not_on_reverse_proxy() -> (CoreTrackerServices, CoreHttpTrackerServices) {
132+
fn initialize_tracker_not_on_reverse_proxy() -> CoreHttpTrackerServices {
206133
initialize_core_tracker_services(&configuration::ephemeral_without_reverse_proxy())
207134
}
208135

209-
fn initialize_core_tracker_services(config: &Configuration) -> (CoreTrackerServices, CoreHttpTrackerServices) {
136+
fn initialize_core_tracker_services(config: &Configuration) -> CoreHttpTrackerServices {
210137
let core_config = Arc::new(config.core.clone());
211138
let database = initialize_database(&config.core);
212139
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
@@ -228,15 +155,15 @@ mod tests {
228155
let http_stats_event_sender = Arc::new(http_stats_event_sender);
229156
let _http_stats_repository = Arc::new(http_stats_repository);
230157

231-
(
232-
CoreTrackerServices {
233-
core_config,
234-
announce_handler,
235-
whitelist_authorization,
236-
authentication_service,
237-
},
238-
CoreHttpTrackerServices { http_stats_event_sender },
239-
)
158+
let announce_service = Arc::new(AnnounceService::new(
159+
core_config.clone(),
160+
announce_handler.clone(),
161+
authentication_service.clone(),
162+
whitelist_authorization.clone(),
163+
http_stats_event_sender.clone(),
164+
));
165+
166+
CoreHttpTrackerServices { announce_service }
240167
}
241168

242169
fn sample_announce_request() -> Announce {
@@ -280,16 +207,12 @@ mod tests {
280207

281208
#[tokio::test]
282209
async fn it_should_fail_when_the_authentication_key_is_missing() {
283-
let (core_tracker_services, http_core_tracker_services) = initialize_private_tracker();
210+
let http_core_tracker_services = initialize_private_tracker();
284211

285212
let maybe_key = None;
286213

287214
let response = handle_announce(
288-
&core_tracker_services.core_config,
289-
&core_tracker_services.announce_handler,
290-
&core_tracker_services.authentication_service,
291-
&core_tracker_services.whitelist_authorization,
292-
&http_core_tracker_services.http_stats_event_sender,
215+
&http_core_tracker_services.announce_service,
293216
&sample_announce_request(),
294217
&sample_client_ip_sources(),
295218
maybe_key,
@@ -309,18 +232,14 @@ mod tests {
309232

310233
#[tokio::test]
311234
async fn it_should_fail_when_the_authentication_key_is_invalid() {
312-
let (core_tracker_services, http_core_tracker_services) = initialize_private_tracker();
235+
let http_core_tracker_services = initialize_private_tracker();
313236

314237
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
315238

316239
let maybe_key = Some(unregistered_key);
317240

318241
let response = handle_announce(
319-
&core_tracker_services.core_config,
320-
&core_tracker_services.announce_handler,
321-
&core_tracker_services.authentication_service,
322-
&core_tracker_services.whitelist_authorization,
323-
&http_core_tracker_services.http_stats_event_sender,
242+
&http_core_tracker_services.announce_service,
324243
&sample_announce_request(),
325244
&sample_client_ip_sources(),
326245
maybe_key,
@@ -349,16 +268,12 @@ mod tests {
349268

350269
#[tokio::test]
351270
async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() {
352-
let (core_tracker_services, http_core_tracker_services) = initialize_listed_tracker();
271+
let http_core_tracker_services = initialize_listed_tracker();
353272

354273
let announce_request = sample_announce_request();
355274

356275
let response = handle_announce(
357-
&core_tracker_services.core_config,
358-
&core_tracker_services.announce_handler,
359-
&core_tracker_services.authentication_service,
360-
&core_tracker_services.whitelist_authorization,
361-
&http_core_tracker_services.http_stats_event_sender,
276+
&http_core_tracker_services.announce_service,
362277
&announce_request,
363278
&sample_client_ip_sources(),
364279
None,
@@ -391,19 +306,15 @@ mod tests {
391306

392307
#[tokio::test]
393308
async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() {
394-
let (core_tracker_services, http_core_tracker_services) = initialize_tracker_on_reverse_proxy();
309+
let http_core_tracker_services = initialize_tracker_on_reverse_proxy();
395310

396311
let client_ip_sources = ClientIpSources {
397312
right_most_x_forwarded_for: None,
398313
connection_info_ip: None,
399314
};
400315

401316
let response = handle_announce(
402-
&core_tracker_services.core_config,
403-
&core_tracker_services.announce_handler,
404-
&core_tracker_services.authentication_service,
405-
&core_tracker_services.whitelist_authorization,
406-
&http_core_tracker_services.http_stats_event_sender,
317+
&http_core_tracker_services.announce_service,
407318
&sample_announce_request(),
408319
&client_ip_sources,
409320
None,
@@ -433,19 +344,15 @@ mod tests {
433344

434345
#[tokio::test]
435346
async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() {
436-
let (core_tracker_services, http_core_tracker_services) = initialize_tracker_not_on_reverse_proxy();
347+
let http_core_tracker_services = initialize_tracker_not_on_reverse_proxy();
437348

438349
let client_ip_sources = ClientIpSources {
439350
right_most_x_forwarded_for: None,
440351
connection_info_ip: None,
441352
};
442353

443354
let response = handle_announce(
444-
&core_tracker_services.core_config,
445-
&core_tracker_services.announce_handler,
446-
&core_tracker_services.authentication_service,
447-
&core_tracker_services.whitelist_authorization,
448-
&http_core_tracker_services.http_stats_event_sender,
355+
&http_core_tracker_services.announce_service,
449356
&sample_announce_request(),
450357
&client_ip_sources,
451358
None,

‎packages/axum-http-tracker-server/src/v1/routes.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,11 @@ pub fn router(http_tracker_container: Arc<HttpTrackerCoreContainer>, server_sock
3838
// Announce request
3939
.route(
4040
"/announce",
41-
get(announce::handle_without_key).with_state((
42-
http_tracker_container.core_config.clone(),
43-
http_tracker_container.announce_handler.clone(),
44-
http_tracker_container.authentication_service.clone(),
45-
http_tracker_container.whitelist_authorization.clone(),
46-
http_tracker_container.http_stats_event_sender.clone(),
47-
)),
41+
get(announce::handle_without_key).with_state(http_tracker_container.announce_service.clone()),
4842
)
4943
.route(
5044
"/announce/{key}",
51-
get(announce::handle_with_key).with_state((
52-
http_tracker_container.core_config.clone(),
53-
http_tracker_container.announce_handler.clone(),
54-
http_tracker_container.authentication_service.clone(),
55-
http_tracker_container.whitelist_authorization.clone(),
56-
http_tracker_container.http_stats_event_sender.clone(),
57-
)),
45+
get(announce::handle_with_key).with_state(http_tracker_container.announce_service.clone()),
5846
)
5947
// Scrape request
6048
.route(

‎packages/http-tracker-core/src/container.rs

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
77
use bittorrent_tracker_core::whitelist;
88
use torrust_tracker_configuration::{Core, HttpTracker};
99

10+
use crate::services::announce::AnnounceService;
1011
use crate::statistics;
1112

1213
pub struct HttpTrackerCoreContainer {
@@ -20,6 +21,7 @@ pub struct HttpTrackerCoreContainer {
2021
pub http_tracker_config: Arc<HttpTracker>,
2122
pub http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
2223
pub http_stats_repository: Arc<statistics::repository::Repository>,
24+
pub announce_service: Arc<AnnounceService>,
2325
}
2426

2527
impl HttpTrackerCoreContainer {
@@ -39,6 +41,14 @@ impl HttpTrackerCoreContainer {
3941
let http_stats_event_sender = Arc::new(http_stats_event_sender);
4042
let http_stats_repository = Arc::new(http_stats_repository);
4143

44+
let announce_service = Arc::new(AnnounceService::new(
45+
tracker_core_container.core_config.clone(),
46+
tracker_core_container.announce_handler.clone(),
47+
tracker_core_container.authentication_service.clone(),
48+
tracker_core_container.whitelist_authorization.clone(),
49+
http_stats_event_sender.clone(),
50+
));
51+
4252
Arc::new(Self {
4353
core_config: tracker_core_container.core_config.clone(),
4454
announce_handler: tracker_core_container.announce_handler.clone(),
@@ -49,6 +59,7 @@ impl HttpTrackerCoreContainer {
4959
http_tracker_config: http_tracker_config.clone(),
5060
http_stats_event_sender: http_stats_event_sender.clone(),
5161
http_stats_repository: http_stats_repository.clone(),
62+
announce_service: announce_service.clone(),
5263
})
5364
}
5465
}

‎packages/http-tracker-core/src/services/announce.rs

+135-111
Original file line numberDiff line numberDiff line change
@@ -83,81 +83,105 @@ impl From<authentication::key::Error> for HttpAnnounceError {
8383
/// > **NOTICE**: as the HTTP tracker does not requires a connection request
8484
/// > like the UDP tracker, the number of TCP connections is incremented for
8585
/// > each `announce` request.
86-
///
87-
/// # Errors
88-
///
89-
/// This function will return an error if:
90-
///
91-
/// - The tracker is running in `listed` mode and the torrent is not whitelisted.
92-
/// - There is an error when resolving the client IP address.
93-
#[allow(clippy::too_many_arguments)]
94-
pub async fn handle_announce(
95-
core_config: &Arc<Core>,
96-
announce_handler: &Arc<AnnounceHandler>,
97-
authentication_service: &Arc<AuthenticationService>,
98-
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
99-
opt_http_stats_event_sender: &Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
100-
announce_request: &Announce,
101-
client_ip_sources: &ClientIpSources,
102-
maybe_key: Option<Key>,
103-
) -> Result<AnnounceData, HttpAnnounceError> {
104-
// Authentication
105-
if core_config.private {
106-
match maybe_key {
107-
Some(key) => match authentication_service.authenticate(&key).await {
108-
Ok(()) => (),
109-
Err(error) => return Err(error.into()),
110-
},
111-
None => {
112-
return Err(authentication::key::Error::MissingAuthKey {
113-
location: Location::caller(),
86+
pub struct AnnounceService {
87+
core_config: Arc<Core>,
88+
announce_handler: Arc<AnnounceHandler>,
89+
authentication_service: Arc<AuthenticationService>,
90+
whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
91+
opt_http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
92+
}
93+
94+
impl AnnounceService {
95+
#[must_use]
96+
pub fn new(
97+
core_config: Arc<Core>,
98+
announce_handler: Arc<AnnounceHandler>,
99+
authentication_service: Arc<AuthenticationService>,
100+
whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
101+
opt_http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
102+
) -> Self {
103+
Self {
104+
core_config,
105+
announce_handler,
106+
authentication_service,
107+
whitelist_authorization,
108+
opt_http_stats_event_sender,
109+
}
110+
}
111+
112+
/// Handles an announce request.
113+
///
114+
/// # Errors
115+
///
116+
/// This function will return an error if:
117+
///
118+
/// - The tracker is running in `listed` mode and the torrent is not whitelisted.
119+
/// - There is an error when resolving the client IP address.
120+
pub async fn handle_announce(
121+
&self,
122+
announce_request: &Announce,
123+
client_ip_sources: &ClientIpSources,
124+
maybe_key: Option<Key>,
125+
) -> Result<AnnounceData, HttpAnnounceError> {
126+
// Authentication
127+
if self.core_config.private {
128+
match maybe_key {
129+
Some(key) => match self.authentication_service.authenticate(&key).await {
130+
Ok(()) => (),
131+
Err(error) => return Err(error.into()),
132+
},
133+
None => {
134+
return Err(authentication::key::Error::MissingAuthKey {
135+
location: Location::caller(),
136+
}
137+
.into())
114138
}
115-
.into())
116139
}
117140
}
118-
}
119141

120-
// Authorization
121-
match whitelist_authorization.authorize(&announce_request.info_hash).await {
122-
Ok(()) => (),
123-
Err(error) => return Err(error.into()),
124-
}
142+
// Authorization
143+
match self.whitelist_authorization.authorize(&announce_request.info_hash).await {
144+
Ok(()) => (),
145+
Err(error) => return Err(error.into()),
146+
}
125147

126-
let peer_ip = match peer_ip_resolver::invoke(core_config.net.on_reverse_proxy, client_ip_sources) {
127-
Ok(peer_ip) => peer_ip,
128-
Err(error) => return Err(error.into()),
129-
};
148+
let peer_ip = match peer_ip_resolver::invoke(self.core_config.net.on_reverse_proxy, client_ip_sources) {
149+
Ok(peer_ip) => peer_ip,
150+
Err(error) => return Err(error.into()),
151+
};
130152

131-
let mut peer = peer_from_request(announce_request, &peer_ip);
153+
let mut peer = peer_from_request(announce_request, &peer_ip);
132154

133-
let peers_wanted = match announce_request.numwant {
134-
Some(numwant) => PeersWanted::only(numwant),
135-
None => PeersWanted::AsManyAsPossible,
136-
};
155+
let peers_wanted = match announce_request.numwant {
156+
Some(numwant) => PeersWanted::only(numwant),
157+
None => PeersWanted::AsManyAsPossible,
158+
};
137159

138-
let original_peer_ip = peer.peer_addr.ip();
160+
let original_peer_ip = peer.peer_addr.ip();
139161

140-
// The tracker could change the original peer ip
141-
let announce_data = announce_handler
142-
.announce(&announce_request.info_hash, &mut peer, &original_peer_ip, &peers_wanted)
143-
.await?;
162+
// The tracker could change the original peer ip
163+
let announce_data = self
164+
.announce_handler
165+
.announce(&announce_request.info_hash, &mut peer, &original_peer_ip, &peers_wanted)
166+
.await?;
144167

145-
if let Some(http_stats_event_sender) = opt_http_stats_event_sender.as_deref() {
146-
match original_peer_ip {
147-
IpAddr::V4(_) => {
148-
http_stats_event_sender
149-
.send_event(statistics::event::Event::Tcp4Announce)
150-
.await;
151-
}
152-
IpAddr::V6(_) => {
153-
http_stats_event_sender
154-
.send_event(statistics::event::Event::Tcp6Announce)
155-
.await;
168+
if let Some(http_stats_event_sender) = self.opt_http_stats_event_sender.as_deref() {
169+
match original_peer_ip {
170+
IpAddr::V4(_) => {
171+
http_stats_event_sender
172+
.send_event(statistics::event::Event::Tcp4Announce)
173+
.await;
174+
}
175+
IpAddr::V6(_) => {
176+
http_stats_event_sender
177+
.send_event(statistics::event::Event::Tcp6Announce)
178+
.await;
179+
}
156180
}
157181
}
158-
}
159182

160-
Ok(announce_data)
183+
Ok(announce_data)
184+
}
161185
}
162186

163187
#[cfg(test)]
@@ -302,11 +326,11 @@ mod tests {
302326
use torrust_tracker_test_helpers::configuration;
303327

304328
use super::{sample_peer_using_ipv4, sample_peer_using_ipv6};
305-
use crate::services::announce::handle_announce;
306329
use crate::services::announce::tests::{
307330
initialize_core_tracker_services, initialize_core_tracker_services_with_config, sample_announce_request_for_peer,
308331
sample_peer, MockHttpStatsEventSender,
309332
};
333+
use crate::services::announce::AnnounceService;
310334
use crate::statistics;
311335

312336
#[tokio::test]
@@ -317,18 +341,18 @@ mod tests {
317341

318342
let (announce_request, client_ip_sources) = sample_announce_request_for_peer(peer);
319343

320-
let announce_data = handle_announce(
321-
&core_tracker_services.core_config,
322-
&core_tracker_services.announce_handler,
323-
&core_tracker_services.authentication_service,
324-
&core_tracker_services.whitelist_authorization,
325-
&core_http_tracker_services.http_stats_event_sender,
326-
&announce_request,
327-
&client_ip_sources,
328-
None,
329-
)
330-
.await
331-
.unwrap();
344+
let announce_service = AnnounceService::new(
345+
core_tracker_services.core_config.clone(),
346+
core_tracker_services.announce_handler.clone(),
347+
core_tracker_services.authentication_service.clone(),
348+
core_tracker_services.whitelist_authorization.clone(),
349+
core_http_tracker_services.http_stats_event_sender.clone(),
350+
);
351+
352+
let announce_data = announce_service
353+
.handle_announce(&announce_request, &client_ip_sources, None)
354+
.await
355+
.unwrap();
332356

333357
let expected_announce_data = AnnounceData {
334358
peers: vec![],
@@ -361,18 +385,18 @@ mod tests {
361385

362386
let (announce_request, client_ip_sources) = sample_announce_request_for_peer(peer);
363387

364-
let _announce_data = handle_announce(
365-
&core_tracker_services.core_config,
366-
&core_tracker_services.announce_handler,
367-
&core_tracker_services.authentication_service,
368-
&core_tracker_services.whitelist_authorization,
369-
&core_http_tracker_services.http_stats_event_sender,
370-
&announce_request,
371-
&client_ip_sources,
372-
None,
373-
)
374-
.await
375-
.unwrap();
388+
let announce_service = AnnounceService::new(
389+
core_tracker_services.core_config.clone(),
390+
core_tracker_services.announce_handler.clone(),
391+
core_tracker_services.authentication_service.clone(),
392+
core_tracker_services.whitelist_authorization.clone(),
393+
core_http_tracker_services.http_stats_event_sender.clone(),
394+
);
395+
396+
let _announce_data = announce_service
397+
.handle_announce(&announce_request, &client_ip_sources, None)
398+
.await
399+
.unwrap();
376400
}
377401

378402
fn tracker_with_an_ipv6_external_ip() -> Configuration {
@@ -413,18 +437,18 @@ mod tests {
413437

414438
let (announce_request, client_ip_sources) = sample_announce_request_for_peer(peer);
415439

416-
let _announce_data = handle_announce(
417-
&core_tracker_services.core_config,
418-
&core_tracker_services.announce_handler,
419-
&core_tracker_services.authentication_service,
420-
&core_tracker_services.whitelist_authorization,
421-
&core_http_tracker_services.http_stats_event_sender,
422-
&announce_request,
423-
&client_ip_sources,
424-
None,
425-
)
426-
.await
427-
.unwrap();
440+
let announce_service = AnnounceService::new(
441+
core_tracker_services.core_config.clone(),
442+
core_tracker_services.announce_handler.clone(),
443+
core_tracker_services.authentication_service.clone(),
444+
core_tracker_services.whitelist_authorization.clone(),
445+
core_http_tracker_services.http_stats_event_sender.clone(),
446+
);
447+
448+
let _announce_data = announce_service
449+
.handle_announce(&announce_request, &client_ip_sources, None)
450+
.await
451+
.unwrap();
428452
}
429453

430454
#[tokio::test]
@@ -446,18 +470,18 @@ mod tests {
446470

447471
let (announce_request, client_ip_sources) = sample_announce_request_for_peer(peer);
448472

449-
let _announce_data = handle_announce(
450-
&core_tracker_services.core_config,
451-
&core_tracker_services.announce_handler,
452-
&core_tracker_services.authentication_service,
453-
&core_tracker_services.whitelist_authorization,
454-
&core_http_tracker_services.http_stats_event_sender,
455-
&announce_request,
456-
&client_ip_sources,
457-
None,
458-
)
459-
.await
460-
.unwrap();
473+
let announce_service = AnnounceService::new(
474+
core_tracker_services.core_config.clone(),
475+
core_tracker_services.announce_handler.clone(),
476+
core_tracker_services.authentication_service.clone(),
477+
core_tracker_services.whitelist_authorization.clone(),
478+
core_http_tracker_services.http_stats_event_sender.clone(),
479+
);
480+
481+
let _announce_data = announce_service
482+
.handle_announce(&announce_request, &client_ip_sources, None)
483+
.await
484+
.unwrap();
461485
}
462486
}
463487
}

‎src/container.rs

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

33
use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
4+
use bittorrent_http_tracker_core::services::announce::AnnounceService;
45
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
56
use bittorrent_tracker_core::authentication::handler::KeysHandler;
67
use bittorrent_tracker_core::authentication::service::AuthenticationService;
@@ -45,6 +46,7 @@ pub struct AppContainer {
4546
// HTTP Tracker Core Services
4647
pub http_stats_event_sender: Arc<Option<Box<dyn bittorrent_http_tracker_core::statistics::event::sender::Sender>>>,
4748
pub http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
49+
pub http_announce_service: Arc<bittorrent_http_tracker_core::services::announce::AnnounceService>,
4850

4951
// UDP Tracker Server Services
5052
pub udp_server_stats_event_sender: Arc<Option<Box<dyn torrust_udp_tracker_server::statistics::event::sender::Sender>>>,
@@ -58,27 +60,35 @@ impl AppContainer {
5860

5961
let tracker_core_container = TrackerCoreContainer::initialize(&core_config);
6062

61-
// HTTP core stats
63+
// HTTP Tracker Core Services
6264
let (http_stats_event_sender, http_stats_repository) =
6365
bittorrent_http_tracker_core::statistics::setup::factory(configuration.core.tracker_usage_statistics);
6466
let http_stats_event_sender = Arc::new(http_stats_event_sender);
6567
let http_stats_repository = Arc::new(http_stats_repository);
66-
67-
// UDP core stats
68+
let http_announce_service = Arc::new(AnnounceService::new(
69+
tracker_core_container.core_config.clone(),
70+
tracker_core_container.announce_handler.clone(),
71+
tracker_core_container.authentication_service.clone(),
72+
tracker_core_container.whitelist_authorization.clone(),
73+
http_stats_event_sender.clone(),
74+
));
75+
76+
// UDP Tracker Core Services
6877
let (udp_core_stats_event_sender, udp_core_stats_repository) =
6978
bittorrent_udp_tracker_core::statistics::setup::factory(configuration.core.tracker_usage_statistics);
7079
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);
7180
let udp_core_stats_repository = Arc::new(udp_core_stats_repository);
7281

7382
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
7483

75-
// UDP server stats
84+
// UDP Tracker Server Services
7685
let (udp_server_stats_event_sender, udp_server_stats_repository) =
7786
torrust_udp_tracker_server::statistics::setup::factory(configuration.core.tracker_usage_statistics);
7887
let udp_server_stats_event_sender = Arc::new(udp_server_stats_event_sender);
7988
let udp_server_stats_repository = Arc::new(udp_server_stats_repository);
8089

8190
AppContainer {
91+
// Tracker Core Services
8292
core_config,
8393
database: tracker_core_container.database,
8494
announce_handler: tracker_core_container.announce_handler,
@@ -91,11 +101,18 @@ impl AppContainer {
91101
in_memory_torrent_repository: tracker_core_container.in_memory_torrent_repository,
92102
db_torrent_repository: tracker_core_container.db_torrent_repository,
93103
torrents_manager: tracker_core_container.torrents_manager,
104+
105+
// UDP Tracker Core Services
94106
ban_service,
95-
http_stats_event_sender,
96107
udp_core_stats_event_sender,
97-
http_stats_repository,
98108
udp_core_stats_repository,
109+
110+
// HTTP Tracker Core Services
111+
http_stats_event_sender,
112+
http_stats_repository,
113+
http_announce_service,
114+
115+
// UDP Tracker Server Services
99116
udp_server_stats_event_sender,
100117
udp_server_stats_repository,
101118
}
@@ -113,6 +130,7 @@ impl AppContainer {
113130
http_tracker_config: http_tracker_config.clone(),
114131
http_stats_event_sender: self.http_stats_event_sender.clone(),
115132
http_stats_repository: self.http_stats_repository.clone(),
133+
announce_service: self.http_announce_service.clone(),
116134
}
117135
}
118136

0 commit comments

Comments
 (0)
Please sign in to comment.