@@ -12,6 +12,7 @@ use std::ops::Range;
12
12
use std:: sync:: Arc ;
13
13
14
14
use aquatic_udp_protocol:: AnnounceRequest ;
15
+ use bittorrent_primitives:: info_hash:: InfoHash ;
15
16
use bittorrent_tracker_core:: announce_handler:: { AnnounceHandler , PeersWanted } ;
16
17
use bittorrent_tracker_core:: error:: { AnnounceError , WhitelistError } ;
17
18
use bittorrent_tracker_core:: whitelist;
@@ -60,47 +61,54 @@ impl AnnounceService {
60
61
request : & AnnounceRequest ,
61
62
cookie_valid_range : Range < f64 > ,
62
63
) -> Result < AnnounceData , UdpAnnounceError > {
63
- // Authentication
64
- check (
65
- & request. connection_id ,
66
- gen_remote_fingerprint ( & remote_addr) ,
67
- cookie_valid_range,
68
- ) ?;
64
+ Self :: authenticate ( remote_addr, request, cookie_valid_range) ?;
69
65
70
66
let info_hash = request. info_hash . into ( ) ;
71
- let remote_client_ip = remote_addr. ip ( ) ;
72
67
73
- // Authorization
74
- self . whitelist_authorization . authorize ( & info_hash) . await ?;
68
+ self . authorize ( & info_hash) . await ?;
69
+
70
+ let remote_client_ip = remote_addr. ip ( ) ;
75
71
76
72
let mut peer = peer_builder:: from_request ( request, & remote_client_ip) ;
77
- let peers_wanted: PeersWanted = i32:: from ( request. peers_wanted . 0 ) . into ( ) ;
78
73
79
- let original_peer_ip = peer . peer_addr . ip ( ) ;
74
+ let peers_wanted : PeersWanted = i32 :: from ( request . peers_wanted . 0 ) . into ( ) ;
80
75
81
- // The tracker could change the original peer ip
82
76
let announce_data = self
83
77
. announce_handler
84
- . announce ( & info_hash, & mut peer, & original_peer_ip , & peers_wanted)
78
+ . announce ( & info_hash, & mut peer, & remote_client_ip , & peers_wanted)
85
79
. await ?;
86
80
87
- if let Some ( udp_stats_event_sender) = self . opt_udp_core_stats_event_sender . as_deref ( ) {
88
- match original_peer_ip {
89
- IpAddr :: V4 ( _) => {
90
- udp_stats_event_sender
91
- . send_event ( statistics:: event:: Event :: Udp4Announce )
92
- . await ;
93
- }
94
- IpAddr :: V6 ( _) => {
95
- udp_stats_event_sender
96
- . send_event ( statistics:: event:: Event :: Udp6Announce )
97
- . await ;
98
- }
99
- }
100
- }
81
+ self . send_stats_event ( remote_client_ip) . await ;
101
82
102
83
Ok ( announce_data)
103
84
}
85
+
86
+ fn authenticate (
87
+ remote_addr : SocketAddr ,
88
+ request : & AnnounceRequest ,
89
+ cookie_valid_range : Range < f64 > ,
90
+ ) -> Result < f64 , ConnectionCookieError > {
91
+ check (
92
+ & request. connection_id ,
93
+ gen_remote_fingerprint ( & remote_addr) ,
94
+ cookie_valid_range,
95
+ )
96
+ }
97
+
98
+ async fn authorize ( & self , info_hash : & InfoHash ) -> Result < ( ) , WhitelistError > {
99
+ self . whitelist_authorization . authorize ( info_hash) . await
100
+ }
101
+
102
+ async fn send_stats_event ( & self , peer_ip : IpAddr ) {
103
+ if let Some ( udp_stats_event_sender) = self . opt_udp_core_stats_event_sender . as_deref ( ) {
104
+ let event = match peer_ip {
105
+ IpAddr :: V4 ( _) => statistics:: event:: Event :: Udp4Announce ,
106
+ IpAddr :: V6 ( _) => statistics:: event:: Event :: Udp6Announce ,
107
+ } ;
108
+
109
+ udp_stats_event_sender. send_event ( event) . await ;
110
+ }
111
+ }
104
112
}
105
113
106
114
/// Errors related to announce requests.
0 commit comments