@@ -13,6 +13,7 @@ use std::sync::Arc;
13
13
14
14
use bittorrent_http_tracker_protocol:: v1:: requests:: announce:: { peer_from_request, Announce } ;
15
15
use bittorrent_http_tracker_protocol:: v1:: services:: peer_ip_resolver:: { self , ClientIpSources , PeerIpResolutionError } ;
16
+ use bittorrent_primitives:: info_hash:: InfoHash ;
16
17
use bittorrent_tracker_core:: announce_handler:: { AnnounceHandler , PeersWanted } ;
17
18
use bittorrent_tracker_core:: authentication:: service:: AuthenticationService ;
18
19
use bittorrent_tracker_core:: authentication:: { self , Key } ;
@@ -73,50 +74,61 @@ impl AnnounceService {
73
74
client_ip_sources : & ClientIpSources ,
74
75
maybe_key : Option < Key > ,
75
76
) -> Result < AnnounceData , HttpAnnounceError > {
76
- // Authentication
77
- if self . core_config . private {
78
- match maybe_key {
79
- Some ( key) => match self . authentication_service . authenticate ( & key) . await {
80
- Ok ( ( ) ) => ( ) ,
81
- Err ( error) => return Err ( error. into ( ) ) ,
82
- } ,
83
- None => {
84
- return Err ( authentication:: key:: Error :: MissingAuthKey {
85
- location : Location :: caller ( ) ,
86
- }
87
- . into ( ) )
88
- }
89
- }
90
- }
77
+ self . authenticate ( maybe_key) . await ?;
91
78
92
- // Authorization
93
- match self . whitelist_authorization . authorize ( & announce_request. info_hash ) . await {
94
- Ok ( ( ) ) => ( ) ,
95
- Err ( error) => return Err ( error. into ( ) ) ,
96
- }
79
+ self . authorize ( announce_request. info_hash ) . await ?;
97
80
98
- let peer_ip = match peer_ip_resolver:: invoke ( self . core_config . net . on_reverse_proxy , client_ip_sources) {
99
- Ok ( peer_ip) => peer_ip,
100
- Err ( error) => return Err ( error. into ( ) ) ,
101
- } ;
81
+ let remote_client_ip = self . resolve_remote_client_ip ( client_ip_sources) ?;
102
82
103
- let mut peer = peer_from_request ( announce_request, & peer_ip ) ;
83
+ let mut peer = peer_from_request ( announce_request, & remote_client_ip ) ;
104
84
105
- let peers_wanted = match announce_request. numwant {
106
- Some ( numwant) => PeersWanted :: only ( numwant) ,
107
- None => PeersWanted :: AsManyAsPossible ,
108
- } ;
109
-
110
- let original_peer_ip = peer. peer_addr . ip ( ) ;
85
+ let peers_wanted = Self :: peers_wanted ( announce_request) ;
111
86
112
- // The tracker could change the original peer ip
113
87
let announce_data = self
114
88
. announce_handler
115
- . announce ( & announce_request. info_hash , & mut peer, & original_peer_ip , & peers_wanted)
89
+ . announce ( & announce_request. info_hash , & mut peer, & remote_client_ip , & peers_wanted)
116
90
. await ?;
117
91
92
+ self . send_stats_event ( remote_client_ip) . await ;
93
+
94
+ Ok ( announce_data)
95
+ }
96
+
97
+ async fn authenticate ( & self , maybe_key : Option < Key > ) -> Result < ( ) , authentication:: key:: Error > {
98
+ if self . core_config . private {
99
+ let key = maybe_key. ok_or ( authentication:: key:: Error :: MissingAuthKey {
100
+ location : Location :: caller ( ) ,
101
+ } ) ?;
102
+
103
+ self . authentication_service . authenticate ( & key) . await ?;
104
+ }
105
+
106
+ Ok ( ( ) )
107
+ }
108
+
109
+ async fn authorize ( & self , info_hash : InfoHash ) -> Result < ( ) , WhitelistError > {
110
+ self . whitelist_authorization . authorize ( & info_hash) . await
111
+ }
112
+
113
+ /// Resolves the client's real IP address considering proxy headers
114
+ fn resolve_remote_client_ip ( & self , client_ip_sources : & ClientIpSources ) -> Result < IpAddr , PeerIpResolutionError > {
115
+ match peer_ip_resolver:: invoke ( self . core_config . net . on_reverse_proxy , client_ip_sources) {
116
+ Ok ( peer_ip) => Ok ( peer_ip) ,
117
+ Err ( error) => Err ( error) ,
118
+ }
119
+ }
120
+
121
+ /// Determines how many peers the client wants in the response
122
+ fn peers_wanted ( announce_request : & Announce ) -> PeersWanted {
123
+ match announce_request. numwant {
124
+ Some ( numwant) => PeersWanted :: only ( numwant) ,
125
+ None => PeersWanted :: AsManyAsPossible ,
126
+ }
127
+ }
128
+
129
+ async fn send_stats_event ( & self , peer_ip : IpAddr ) {
118
130
if let Some ( http_stats_event_sender) = self . opt_http_stats_event_sender . as_deref ( ) {
119
- match original_peer_ip {
131
+ match peer_ip {
120
132
IpAddr :: V4 ( _) => {
121
133
http_stats_event_sender
122
134
. send_event ( statistics:: event:: Event :: Tcp4Announce )
@@ -129,8 +141,6 @@ impl AnnounceService {
129
141
}
130
142
}
131
143
}
132
-
133
- Ok ( announce_data)
134
144
}
135
145
}
136
146
0 commit comments