@@ -21,13 +21,16 @@ use crate::connection_cookie::{check, gen_remote_fingerprint, ConnectionCookieEr
21
21
use crate :: statistics;
22
22
23
23
/// The `ScrapeService` is responsible for handling the `scrape` requests.
24
+ ///
25
+ /// The service sends an statistics event that increments:
26
+ ///
27
+ /// - The number of UDP `scrape` requests handled by the UDP tracker.
24
28
pub struct ScrapeService {
25
29
scrape_handler : Arc < ScrapeHandler > ,
26
30
opt_udp_stats_event_sender : Arc < Option < Box < dyn statistics:: event:: sender:: Sender > > > ,
27
31
}
28
32
29
33
impl ScrapeService {
30
- /// Creates a new `ScrapeService`.
31
34
#[ must_use]
32
35
pub fn new (
33
36
scrape_handler : Arc < ScrapeHandler > ,
@@ -46,33 +49,46 @@ impl ScrapeService {
46
49
/// It will return an error if the tracker core scrape handler returns an error.
47
50
pub async fn handle_scrape (
48
51
& self ,
49
- remote_addr : SocketAddr ,
52
+ remote_client_addr : SocketAddr ,
50
53
request : & ScrapeRequest ,
51
54
cookie_valid_range : Range < f64 > ,
52
55
) -> Result < ScrapeData , UdpScrapeError > {
56
+ Self :: authenticate ( remote_client_addr, request, cookie_valid_range) ?;
57
+
58
+ let scrape_data = self
59
+ . scrape_handler
60
+ . scrape ( & Self :: convert_from_aquatic ( & request. info_hashes ) )
61
+ . await ?;
62
+
63
+ self . send_stats_event ( remote_client_addr) . await ;
64
+
65
+ Ok ( scrape_data)
66
+ }
67
+
68
+ fn authenticate (
69
+ remote_addr : SocketAddr ,
70
+ request : & ScrapeRequest ,
71
+ cookie_valid_range : Range < f64 > ,
72
+ ) -> Result < f64 , ConnectionCookieError > {
53
73
check (
54
74
& request. connection_id ,
55
75
gen_remote_fingerprint ( & remote_addr) ,
56
76
cookie_valid_range,
57
- ) ?;
58
-
59
- // Convert from aquatic infohashes
60
- let info_hashes: Vec < InfoHash > = request. info_hashes . iter ( ) . map ( |& x| x. into ( ) ) . collect ( ) ;
77
+ )
78
+ }
61
79
62
- let scrape_data = self . scrape_handler . scrape ( & info_hashes) . await ?;
80
+ fn convert_from_aquatic ( aquatic_infohashes : & [ aquatic_udp_protocol:: common:: InfoHash ] ) -> Vec < InfoHash > {
81
+ aquatic_infohashes. iter ( ) . map ( |& x| x. into ( ) ) . collect ( )
82
+ }
63
83
84
+ async fn send_stats_event ( & self , remote_addr : SocketAddr ) {
64
85
if let Some ( udp_stats_event_sender) = self . opt_udp_stats_event_sender . as_deref ( ) {
65
- match remote_addr {
66
- SocketAddr :: V4 ( _) => {
67
- udp_stats_event_sender. send_event ( statistics:: event:: Event :: Udp4Scrape ) . await ;
68
- }
69
- SocketAddr :: V6 ( _) => {
70
- udp_stats_event_sender. send_event ( statistics:: event:: Event :: Udp6Scrape ) . await ;
71
- }
72
- }
86
+ let event = match remote_addr {
87
+ SocketAddr :: V4 ( _) => statistics:: event:: Event :: Udp4Scrape ,
88
+ SocketAddr :: V6 ( _) => statistics:: event:: Event :: Udp6Scrape ,
89
+ } ;
90
+ udp_stats_event_sender. send_event ( event) . await ;
73
91
}
74
-
75
- Ok ( scrape_data)
76
92
}
77
93
}
78
94
0 commit comments