@@ -80,9 +80,10 @@ impl ScrapeService {
80
80
self . scrape_handler . scrape ( & scrape_request. info_hashes ) . await ?
81
81
} ;
82
82
83
- let remote_client_ip = self . resolve_remote_client_ip ( client_ip_sources) ?;
83
+ let ( remote_client_ip, opt_client_port ) = self . resolve_remote_client_ip ( client_ip_sources) ?;
84
84
85
- self . send_stats_event ( remote_client_ip, * server_socket_addr) . await ;
85
+ self . send_stats_event ( remote_client_ip, opt_client_port, * server_socket_addr)
86
+ . await ;
86
87
87
88
Ok ( scrape_data)
88
89
}
@@ -100,18 +101,33 @@ impl ScrapeService {
100
101
}
101
102
102
103
/// Resolves the client's real IP address considering proxy headers.
103
- fn resolve_remote_client_ip ( & self , client_ip_sources : & ClientIpSources ) -> Result < IpAddr , PeerIpResolutionError > {
104
- peer_ip_resolver:: invoke ( self . core_config . net . on_reverse_proxy , client_ip_sources)
104
+ fn resolve_remote_client_ip (
105
+ & self ,
106
+ client_ip_sources : & ClientIpSources ,
107
+ ) -> Result < ( IpAddr , Option < u16 > ) , PeerIpResolutionError > {
108
+ let ip = peer_ip_resolver:: invoke ( self . core_config . net . on_reverse_proxy , client_ip_sources) ?;
109
+
110
+ let port = if client_ip_sources. connection_info_socket_address . is_some ( ) {
111
+ client_ip_sources
112
+ . connection_info_socket_address
113
+ . map ( |socket_addr| socket_addr. port ( ) )
114
+ } else {
115
+ None
116
+ } ;
117
+
118
+ Ok ( ( ip, port) )
105
119
}
106
120
107
- async fn send_stats_event ( & self , original_peer_ip : IpAddr , server_socket_addr : SocketAddr ) {
121
+ async fn send_stats_event (
122
+ & self ,
123
+ original_peer_ip : IpAddr ,
124
+ opt_original_peer_port : Option < u16 > ,
125
+ server_socket_addr : SocketAddr ,
126
+ ) {
108
127
if let Some ( http_stats_event_sender) = self . opt_http_stats_event_sender . as_deref ( ) {
109
128
http_stats_event_sender
110
129
. send_event ( statistics:: event:: Event :: TcpScrape {
111
- connection : ConnectionContext {
112
- client_ip_addr : original_peer_ip,
113
- server_socket_addr,
114
- } ,
130
+ connection : ConnectionContext :: new ( original_peer_ip, opt_original_peer_port, server_socket_addr) ,
115
131
} )
116
132
. await ;
117
133
}
@@ -336,10 +352,11 @@ mod tests {
336
352
http_stats_event_sender_mock
337
353
. expect_send_event ( )
338
354
. with ( eq ( statistics:: event:: Event :: TcpScrape {
339
- connection : ConnectionContext {
340
- client_ip_addr : IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) ,
341
- server_socket_addr : SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ,
342
- } ,
355
+ connection : ConnectionContext :: new (
356
+ IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) ,
357
+ Some ( 8080 ) ,
358
+ SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ,
359
+ ) ,
343
360
} ) )
344
361
. times ( 1 )
345
362
. returning ( |_| Box :: pin ( future:: ready ( Some ( Ok ( ( ) ) ) ) ) ) ;
@@ -384,10 +401,11 @@ mod tests {
384
401
http_stats_event_sender_mock
385
402
. expect_send_event ( )
386
403
. with ( eq ( statistics:: event:: Event :: TcpScrape {
387
- connection : ConnectionContext {
388
- client_ip_addr : IpAddr :: V6 ( Ipv6Addr :: new ( 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 ) ) ,
404
+ connection : ConnectionContext :: new (
405
+ IpAddr :: V6 ( Ipv6Addr :: new ( 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 ) ) ,
406
+ Some ( 8080 ) ,
389
407
server_socket_addr,
390
- } ,
408
+ ) ,
391
409
} ) )
392
410
. times ( 1 )
393
411
. returning ( |_| Box :: pin ( future:: ready ( Some ( Ok ( ( ) ) ) ) ) ) ;
@@ -504,10 +522,11 @@ mod tests {
504
522
http_stats_event_sender_mock
505
523
. expect_send_event ( )
506
524
. with ( eq ( statistics:: event:: Event :: TcpScrape {
507
- connection : ConnectionContext {
508
- client_ip_addr : IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) ,
509
- server_socket_addr : SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ,
510
- } ,
525
+ connection : ConnectionContext :: new (
526
+ IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) ,
527
+ Some ( 8080 ) ,
528
+ SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ,
529
+ ) ,
511
530
} ) )
512
531
. times ( 1 )
513
532
. returning ( |_| Box :: pin ( future:: ready ( Some ( Ok ( ( ) ) ) ) ) ) ;
@@ -552,10 +571,11 @@ mod tests {
552
571
http_stats_event_sender_mock
553
572
. expect_send_event ( )
554
573
. with ( eq ( statistics:: event:: Event :: TcpScrape {
555
- connection : ConnectionContext {
556
- client_ip_addr : IpAddr :: V6 ( Ipv6Addr :: new ( 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 ) ) ,
574
+ connection : ConnectionContext :: new (
575
+ IpAddr :: V6 ( Ipv6Addr :: new ( 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 ) ) ,
576
+ Some ( 8080 ) ,
557
577
server_socket_addr,
558
- } ,
578
+ ) ,
559
579
} ) )
560
580
. times ( 1 )
561
581
. returning ( |_| Box :: pin ( future:: ready ( Some ( Ok ( ( ) ) ) ) ) ) ;
0 commit comments