2
2
//!
3
3
//! The handlers perform the authentication and authorization of the request,
4
4
//! and resolve the client IP address.
5
+ use std:: net:: SocketAddr ;
5
6
use std:: sync:: Arc ;
6
7
7
8
use axum:: extract:: State ;
@@ -22,27 +23,27 @@ use crate::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
22
23
/// authentication (no PATH `key` parameter required).
23
24
#[ allow( clippy:: unused_async) ]
24
25
pub async fn handle_without_key (
25
- State ( state) : State < Arc < AnnounceService > > ,
26
+ State ( state) : State < ( Arc < AnnounceService > , SocketAddr ) > ,
26
27
ExtractRequest ( announce_request) : ExtractRequest ,
27
28
ExtractClientIpSources ( client_ip_sources) : ExtractClientIpSources ,
28
29
) -> Response {
29
30
tracing:: debug!( "http announce request: {:#?}" , announce_request) ;
30
31
31
- handle ( & state, & announce_request, & client_ip_sources, None ) . await
32
+ handle ( & state. 0 , & announce_request, & client_ip_sources, & state . 1 , None ) . await
32
33
}
33
34
34
35
/// It handles the `announce` request when the HTTP tracker requires
35
36
/// authentication (PATH `key` parameter required).
36
37
#[ allow( clippy:: unused_async) ]
37
38
pub async fn handle_with_key (
38
- State ( state) : State < Arc < AnnounceService > > ,
39
+ State ( state) : State < ( Arc < AnnounceService > , SocketAddr ) > ,
39
40
ExtractRequest ( announce_request) : ExtractRequest ,
40
41
ExtractClientIpSources ( client_ip_sources) : ExtractClientIpSources ,
41
42
ExtractKey ( key) : ExtractKey ,
42
43
) -> Response {
43
44
tracing:: debug!( "http announce request: {:#?}" , announce_request) ;
44
45
45
- handle ( & state, & announce_request, & client_ip_sources, Some ( key) ) . await
46
+ handle ( & state. 0 , & announce_request, & client_ip_sources, & state . 1 , Some ( key) ) . await
46
47
}
47
48
48
49
/// It handles the `announce` request.
@@ -53,9 +54,18 @@ async fn handle(
53
54
announce_service : & Arc < AnnounceService > ,
54
55
announce_request : & Announce ,
55
56
client_ip_sources : & ClientIpSources ,
57
+ server_socket_addr : & SocketAddr ,
56
58
maybe_key : Option < Key > ,
57
59
) -> Response {
58
- let announce_data = match handle_announce ( announce_service, announce_request, client_ip_sources, maybe_key) . await {
60
+ let announce_data = match handle_announce (
61
+ announce_service,
62
+ announce_request,
63
+ client_ip_sources,
64
+ server_socket_addr,
65
+ maybe_key,
66
+ )
67
+ . await
68
+ {
59
69
Ok ( announce_data) => announce_data,
60
70
Err ( error) => {
61
71
let error_response = responses:: error:: Error {
@@ -71,10 +81,11 @@ async fn handle_announce(
71
81
announce_service : & Arc < AnnounceService > ,
72
82
announce_request : & Announce ,
73
83
client_ip_sources : & ClientIpSources ,
84
+ server_socket_addr : & SocketAddr ,
74
85
maybe_key : Option < Key > ,
75
86
) -> Result < AnnounceData , HttpAnnounceError > {
76
87
announce_service
77
- . handle_announce ( announce_request, client_ip_sources, maybe_key)
88
+ . handle_announce ( announce_request, client_ip_sources, server_socket_addr , maybe_key)
78
89
. await
79
90
}
80
91
@@ -183,7 +194,7 @@ mod tests {
183
194
fn sample_client_ip_sources ( ) -> ClientIpSources {
184
195
ClientIpSources {
185
196
right_most_x_forwarded_for : None ,
186
- connection_info_ip : None ,
197
+ connection_info_socket_address : None ,
187
198
}
188
199
}
189
200
@@ -196,6 +207,7 @@ mod tests {
196
207
197
208
mod with_tracker_in_private_mode {
198
209
210
+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
199
211
use std:: str:: FromStr ;
200
212
201
213
use bittorrent_http_tracker_protocol:: v1:: responses;
@@ -209,12 +221,15 @@ mod tests {
209
221
async fn it_should_fail_when_the_authentication_key_is_missing ( ) {
210
222
let http_core_tracker_services = initialize_private_tracker ( ) ;
211
223
224
+ let server_socket_addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ;
225
+
212
226
let maybe_key = None ;
213
227
214
228
let response = handle_announce (
215
229
& http_core_tracker_services. announce_service ,
216
230
& sample_announce_request ( ) ,
217
231
& sample_client_ip_sources ( ) ,
232
+ & server_socket_addr,
218
233
maybe_key,
219
234
)
220
235
. await
@@ -236,12 +251,15 @@ mod tests {
236
251
237
252
let unregistered_key = authentication:: Key :: from_str ( "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ" ) . unwrap ( ) ;
238
253
254
+ let server_socket_addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ;
255
+
239
256
let maybe_key = Some ( unregistered_key) ;
240
257
241
258
let response = handle_announce (
242
259
& http_core_tracker_services. announce_service ,
243
260
& sample_announce_request ( ) ,
244
261
& sample_client_ip_sources ( ) ,
262
+ & server_socket_addr,
245
263
maybe_key,
246
264
)
247
265
. await
@@ -260,6 +278,8 @@ mod tests {
260
278
261
279
mod with_tracker_in_listed_mode {
262
280
281
+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
282
+
263
283
use bittorrent_http_tracker_protocol:: v1:: responses;
264
284
265
285
use super :: { initialize_listed_tracker, sample_announce_request, sample_client_ip_sources} ;
@@ -272,10 +292,13 @@ mod tests {
272
292
273
293
let announce_request = sample_announce_request ( ) ;
274
294
295
+ let server_socket_addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ;
296
+
275
297
let response = handle_announce (
276
298
& http_core_tracker_services. announce_service ,
277
299
& announce_request,
278
300
& sample_client_ip_sources ( ) ,
301
+ & server_socket_addr,
279
302
None ,
280
303
)
281
304
. await
@@ -297,6 +320,8 @@ mod tests {
297
320
298
321
mod with_tracker_on_reverse_proxy {
299
322
323
+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
324
+
300
325
use bittorrent_http_tracker_protocol:: v1:: responses;
301
326
use bittorrent_http_tracker_protocol:: v1:: services:: peer_ip_resolver:: ClientIpSources ;
302
327
@@ -310,13 +335,16 @@ mod tests {
310
335
311
336
let client_ip_sources = ClientIpSources {
312
337
right_most_x_forwarded_for : None ,
313
- connection_info_ip : None ,
338
+ connection_info_socket_address : None ,
314
339
} ;
315
340
341
+ let server_socket_addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ;
342
+
316
343
let response = handle_announce (
317
344
& http_core_tracker_services. announce_service ,
318
345
& sample_announce_request ( ) ,
319
346
& client_ip_sources,
347
+ & server_socket_addr,
320
348
None ,
321
349
)
322
350
. await
@@ -335,6 +363,8 @@ mod tests {
335
363
336
364
mod with_tracker_not_on_reverse_proxy {
337
365
366
+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
367
+
338
368
use bittorrent_http_tracker_protocol:: v1:: responses;
339
369
use bittorrent_http_tracker_protocol:: v1:: services:: peer_ip_resolver:: ClientIpSources ;
340
370
@@ -348,13 +378,16 @@ mod tests {
348
378
349
379
let client_ip_sources = ClientIpSources {
350
380
right_most_x_forwarded_for : None ,
351
- connection_info_ip : None ,
381
+ connection_info_socket_address : None ,
352
382
} ;
353
383
384
+ let server_socket_addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ;
385
+
354
386
let response = handle_announce (
355
387
& http_core_tracker_services. announce_service ,
356
388
& sample_announce_request ( ) ,
357
389
& client_ip_sources,
390
+ & server_socket_addr,
358
391
None ,
359
392
)
360
393
. await
0 commit comments