@@ -6,16 +6,12 @@ use std::sync::Arc;
6
6
7
7
use axum:: extract:: State ;
8
8
use axum:: response:: { IntoResponse , Response } ;
9
- use bittorrent_http_tracker_core:: services:: announce:: HttpAnnounceError ;
9
+ use bittorrent_http_tracker_core:: services:: announce:: { AnnounceService , HttpAnnounceError } ;
10
10
use bittorrent_http_tracker_protocol:: v1:: requests:: announce:: { Announce , Compact } ;
11
11
use bittorrent_http_tracker_protocol:: v1:: responses:: { self } ;
12
12
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 ;
15
13
use bittorrent_tracker_core:: authentication:: Key ;
16
- use bittorrent_tracker_core:: whitelist;
17
14
use hyper:: StatusCode ;
18
- use torrust_tracker_configuration:: Core ;
19
15
use torrust_tracker_primitives:: core:: AnnounceData ;
20
16
21
17
use crate :: v1:: extractors:: announce_request:: ExtractRequest ;
@@ -25,91 +21,41 @@ use crate::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
25
21
/// It handles the `announce` request when the HTTP tracker does not require
26
22
/// authentication (no PATH `key` parameter required).
27
23
#[ allow( clippy:: unused_async) ]
28
- #[ allow( clippy:: type_complexity) ]
29
24
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 > > ,
37
26
ExtractRequest ( announce_request) : ExtractRequest ,
38
27
ExtractClientIpSources ( client_ip_sources) : ExtractClientIpSources ,
39
28
) -> Response {
40
29
tracing:: debug!( "http announce request: {:#?}" , announce_request) ;
41
30
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
53
32
}
54
33
55
34
/// It handles the `announce` request when the HTTP tracker requires
56
35
/// authentication (PATH `key` parameter required).
57
36
#[ allow( clippy:: unused_async) ]
58
- #[ allow( clippy:: type_complexity) ]
59
37
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 > > ,
67
39
ExtractRequest ( announce_request) : ExtractRequest ,
68
40
ExtractClientIpSources ( client_ip_sources) : ExtractClientIpSources ,
69
41
ExtractKey ( key) : ExtractKey ,
70
42
) -> Response {
71
43
tracing:: debug!( "http announce request: {:#?}" , announce_request) ;
72
44
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
84
46
}
85
47
86
48
/// It handles the `announce` request.
87
49
///
88
50
/// Internal implementation that handles both the `authenticated` and
89
51
/// `unauthenticated` modes.
90
- #[ allow( clippy:: too_many_arguments) ]
91
52
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 > ,
97
54
announce_request : & Announce ,
98
55
client_ip_sources : & ClientIpSources ,
99
56
maybe_key : Option < Key > ,
100
57
) -> 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 {
113
59
Ok ( announce_data) => announce_data,
114
60
Err ( error) => {
115
61
let error_response = responses:: error:: Error {
@@ -121,28 +67,15 @@ async fn handle(
121
67
build_response ( announce_request, announce_data)
122
68
}
123
69
124
- #[ allow( clippy:: too_many_arguments) ]
125
70
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 > ,
131
72
announce_request : & Announce ,
132
73
client_ip_sources : & ClientIpSources ,
133
74
maybe_key : Option < Key > ,
134
75
) -> 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
146
79
}
147
80
148
81
fn build_response ( announce_request : & Announce , announce_data : AnnounceData ) -> Response {
@@ -163,6 +96,7 @@ mod tests {
163
96
use std:: sync:: Arc ;
164
97
165
98
use aquatic_udp_protocol:: PeerId ;
99
+ use bittorrent_http_tracker_core:: services:: announce:: AnnounceService ;
166
100
use bittorrent_http_tracker_protocol:: v1:: requests:: announce:: Announce ;
167
101
use bittorrent_http_tracker_protocol:: v1:: responses;
168
102
use bittorrent_http_tracker_protocol:: v1:: services:: peer_ip_resolver:: ClientIpSources ;
@@ -174,39 +108,32 @@ mod tests {
174
108
use bittorrent_tracker_core:: torrent:: repository:: persisted:: DatabasePersistentTorrentRepository ;
175
109
use bittorrent_tracker_core:: whitelist:: authorization:: WhitelistAuthorization ;
176
110
use bittorrent_tracker_core:: whitelist:: repository:: in_memory:: InMemoryWhitelist ;
177
- use torrust_tracker_configuration:: { Configuration , Core } ;
111
+ use torrust_tracker_configuration:: Configuration ;
178
112
use torrust_tracker_test_helpers:: configuration;
179
113
180
114
use crate :: tests:: helpers:: sample_info_hash;
181
115
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
-
189
116
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 > ,
191
118
}
192
119
193
- fn initialize_private_tracker ( ) -> ( CoreTrackerServices , CoreHttpTrackerServices ) {
120
+ fn initialize_private_tracker ( ) -> CoreHttpTrackerServices {
194
121
initialize_core_tracker_services ( & configuration:: ephemeral_private ( ) )
195
122
}
196
123
197
- fn initialize_listed_tracker ( ) -> ( CoreTrackerServices , CoreHttpTrackerServices ) {
124
+ fn initialize_listed_tracker ( ) -> CoreHttpTrackerServices {
198
125
initialize_core_tracker_services ( & configuration:: ephemeral_listed ( ) )
199
126
}
200
127
201
- fn initialize_tracker_on_reverse_proxy ( ) -> ( CoreTrackerServices , CoreHttpTrackerServices ) {
128
+ fn initialize_tracker_on_reverse_proxy ( ) -> CoreHttpTrackerServices {
202
129
initialize_core_tracker_services ( & configuration:: ephemeral_with_reverse_proxy ( ) )
203
130
}
204
131
205
- fn initialize_tracker_not_on_reverse_proxy ( ) -> ( CoreTrackerServices , CoreHttpTrackerServices ) {
132
+ fn initialize_tracker_not_on_reverse_proxy ( ) -> CoreHttpTrackerServices {
206
133
initialize_core_tracker_services ( & configuration:: ephemeral_without_reverse_proxy ( ) )
207
134
}
208
135
209
- fn initialize_core_tracker_services ( config : & Configuration ) -> ( CoreTrackerServices , CoreHttpTrackerServices ) {
136
+ fn initialize_core_tracker_services ( config : & Configuration ) -> CoreHttpTrackerServices {
210
137
let core_config = Arc :: new ( config. core . clone ( ) ) ;
211
138
let database = initialize_database ( & config. core ) ;
212
139
let in_memory_whitelist = Arc :: new ( InMemoryWhitelist :: default ( ) ) ;
@@ -228,15 +155,15 @@ mod tests {
228
155
let http_stats_event_sender = Arc :: new ( http_stats_event_sender) ;
229
156
let _http_stats_repository = Arc :: new ( http_stats_repository) ;
230
157
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 }
240
167
}
241
168
242
169
fn sample_announce_request ( ) -> Announce {
@@ -280,16 +207,12 @@ mod tests {
280
207
281
208
#[ tokio:: test]
282
209
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 ( ) ;
284
211
285
212
let maybe_key = None ;
286
213
287
214
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 ,
293
216
& sample_announce_request ( ) ,
294
217
& sample_client_ip_sources ( ) ,
295
218
maybe_key,
@@ -309,18 +232,14 @@ mod tests {
309
232
310
233
#[ tokio:: test]
311
234
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 ( ) ;
313
236
314
237
let unregistered_key = authentication:: Key :: from_str ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) ;
315
238
316
239
let maybe_key = Some ( unregistered_key) ;
317
240
318
241
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 ,
324
243
& sample_announce_request ( ) ,
325
244
& sample_client_ip_sources ( ) ,
326
245
maybe_key,
@@ -349,16 +268,12 @@ mod tests {
349
268
350
269
#[ tokio:: test]
351
270
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 ( ) ;
353
272
354
273
let announce_request = sample_announce_request ( ) ;
355
274
356
275
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 ,
362
277
& announce_request,
363
278
& sample_client_ip_sources ( ) ,
364
279
None ,
@@ -391,19 +306,15 @@ mod tests {
391
306
392
307
#[ tokio:: test]
393
308
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 ( ) ;
395
310
396
311
let client_ip_sources = ClientIpSources {
397
312
right_most_x_forwarded_for : None ,
398
313
connection_info_ip : None ,
399
314
} ;
400
315
401
316
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 ,
407
318
& sample_announce_request ( ) ,
408
319
& client_ip_sources,
409
320
None ,
@@ -433,19 +344,15 @@ mod tests {
433
344
434
345
#[ tokio:: test]
435
346
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 ( ) ;
437
348
438
349
let client_ip_sources = ClientIpSources {
439
350
right_most_x_forwarded_for : None ,
440
351
connection_info_ip : None ,
441
352
} ;
442
353
443
354
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 ,
449
356
& sample_announce_request ( ) ,
450
357
& client_ip_sources,
451
358
None ,
0 commit comments