@@ -6,16 +6,12 @@ use std::sync::Arc;
66
77use axum:: extract:: State ;
88use axum:: response:: { IntoResponse , Response } ;
9- use bittorrent_http_tracker_core:: services:: announce:: HttpAnnounceError ;
9+ use bittorrent_http_tracker_core:: services:: announce:: { AnnounceService , HttpAnnounceError } ;
1010use bittorrent_http_tracker_protocol:: v1:: requests:: announce:: { Announce , Compact } ;
1111use bittorrent_http_tracker_protocol:: v1:: responses:: { self } ;
1212use 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 ;
1513use bittorrent_tracker_core:: authentication:: Key ;
16- use bittorrent_tracker_core:: whitelist;
1714use hyper:: StatusCode ;
18- use torrust_tracker_configuration:: Core ;
1915use torrust_tracker_primitives:: core:: AnnounceData ;
2016
2117use 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) ]
2924pub 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) ]
5937pub 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) ]
9152async 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) ]
12570async 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
14881fn 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 ,
0 commit comments