@@ -21,13 +21,16 @@ use crate::connection_cookie::{check, gen_remote_fingerprint, ConnectionCookieEr
2121use crate :: statistics;
2222
2323/// 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.
2428pub struct ScrapeService {
2529 scrape_handler : Arc < ScrapeHandler > ,
2630 opt_udp_stats_event_sender : Arc < Option < Box < dyn statistics:: event:: sender:: Sender > > > ,
2731}
2832
2933impl ScrapeService {
30- /// Creates a new `ScrapeService`.
3134 #[ must_use]
3235 pub fn new (
3336 scrape_handler : Arc < ScrapeHandler > ,
@@ -46,33 +49,46 @@ impl ScrapeService {
4649 /// It will return an error if the tracker core scrape handler returns an error.
4750 pub async fn handle_scrape (
4851 & self ,
49- remote_addr : SocketAddr ,
52+ remote_client_addr : SocketAddr ,
5053 request : & ScrapeRequest ,
5154 cookie_valid_range : Range < f64 > ,
5255 ) -> 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 > {
5373 check (
5474 & request. connection_id ,
5575 gen_remote_fingerprint ( & remote_addr) ,
5676 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+ }
6179
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+ }
6383
84+ async fn send_stats_event ( & self , remote_addr : SocketAddr ) {
6485 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 ;
7391 }
74-
75- Ok ( scrape_data)
7692 }
7793}
7894
0 commit comments