@@ -14,6 +14,8 @@ use bittorrent_http_protocol::v1::requests::scrape::Scrape;
1414use bittorrent_http_protocol:: v1:: responses;
1515use bittorrent_http_protocol:: v1:: services:: peer_ip_resolver:: { self , ClientIpSources } ;
1616use bittorrent_primitives:: info_hash:: InfoHash ;
17+ use bittorrent_tracker_core:: authentication:: service:: AuthenticationService ;
18+ use bittorrent_tracker_core:: authentication:: Key ;
1719use bittorrent_tracker_core:: scrape_handler:: ScrapeHandler ;
1820use torrust_tracker_configuration:: Core ;
1921use torrust_tracker_primitives:: core:: ScrapeData ;
@@ -40,12 +42,26 @@ use crate::packages::http_tracker_core;
4042pub async fn handle_scrape (
4143 core_config : & Arc < Core > ,
4244 scrape_handler : & Arc < ScrapeHandler > ,
45+ authentication_service : & Arc < AuthenticationService > ,
4346 opt_http_stats_event_sender : & Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > ,
4447 scrape_request : & Scrape ,
4548 client_ip_sources : & ClientIpSources ,
46- return_fake_scrape_data : bool ,
49+ maybe_key : Option < Key > ,
4750) -> Result < ScrapeData , responses:: error:: Error > {
48- // Authorization for scrape requests is handled at the `bittorrent-_racker_core`
51+ // Authentication
52+ let return_fake_scrape_data = if core_config. private {
53+ match maybe_key {
54+ Some ( key) => match authentication_service. authenticate ( & key) . await {
55+ Ok ( ( ) ) => false ,
56+ Err ( _error) => true ,
57+ } ,
58+ None => true ,
59+ }
60+ } else {
61+ false
62+ } ;
63+
64+ // Authorization for scrape requests is handled at the `bittorrent_tracker_core`
4965 // level for each torrent.
5066
5167 let peer_ip = match peer_ip_resolver:: invoke ( core_config. net . on_reverse_proxy , client_ip_sources) {
@@ -111,6 +127,8 @@ mod tests {
111127 use aquatic_udp_protocol:: { AnnounceEvent , NumberOfBytes , PeerId } ;
112128 use bittorrent_primitives:: info_hash:: InfoHash ;
113129 use bittorrent_tracker_core:: announce_handler:: AnnounceHandler ;
130+ use bittorrent_tracker_core:: authentication:: key:: repository:: in_memory:: InMemoryKeyRepository ;
131+ use bittorrent_tracker_core:: authentication:: service:: AuthenticationService ;
114132 use bittorrent_tracker_core:: databases:: setup:: initialize_database;
115133 use bittorrent_tracker_core:: scrape_handler:: ScrapeHandler ;
116134 use bittorrent_tracker_core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
@@ -127,27 +145,39 @@ mod tests {
127145 use crate :: packages:: http_tracker_core;
128146 use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
129147
130- fn initialize_announce_and_scrape_handlers_for_public_tracker ( ) -> ( Arc < AnnounceHandler > , Arc < ScrapeHandler > ) {
131- initialize_announce_and_scrape_handlers_with_configuration ( & configuration:: ephemeral_public ( ) )
148+ struct Container {
149+ announce_handler : Arc < AnnounceHandler > ,
150+ scrape_handler : Arc < ScrapeHandler > ,
151+ authentication_service : Arc < AuthenticationService > ,
132152 }
133153
134- fn initialize_announce_and_scrape_handlers_with_configuration (
135- config : & Configuration ,
136- ) -> ( Arc < AnnounceHandler > , Arc < ScrapeHandler > ) {
154+ fn initialize_services_for_public_tracker ( ) -> Container {
155+ initialize_services_with_configuration ( & configuration:: ephemeral_public ( ) )
156+ }
157+
158+ fn initialize_services_with_configuration ( config : & Configuration ) -> Container {
137159 let database = initialize_database ( & config. core ) ;
138160 let in_memory_whitelist = Arc :: new ( InMemoryWhitelist :: default ( ) ) ;
139161 let whitelist_authorization = Arc :: new ( WhitelistAuthorization :: new ( & config. core , & in_memory_whitelist. clone ( ) ) ) ;
140162 let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
141163 let db_torrent_repository = Arc :: new ( DatabasePersistentTorrentRepository :: new ( & database) ) ;
164+ let in_memory_key_repository = Arc :: new ( InMemoryKeyRepository :: default ( ) ) ;
165+ let authentication_service = Arc :: new ( AuthenticationService :: new ( & config. core , & in_memory_key_repository) ) ;
166+
142167 let announce_handler = Arc :: new ( AnnounceHandler :: new (
143168 & config. core ,
144169 & whitelist_authorization,
145170 & in_memory_torrent_repository,
146171 & db_torrent_repository,
147172 ) ) ;
173+
148174 let scrape_handler = Arc :: new ( ScrapeHandler :: new ( & whitelist_authorization, & in_memory_torrent_repository) ) ;
149175
150- ( announce_handler, scrape_handler)
176+ Container {
177+ announce_handler,
178+ scrape_handler,
179+ authentication_service,
180+ }
151181 }
152182
153183 fn sample_info_hashes ( ) -> Vec < InfoHash > {
@@ -166,14 +196,6 @@ mod tests {
166196 }
167197 }
168198
169- fn initialize_scrape_handler_with_config ( config : & Configuration ) -> Arc < ScrapeHandler > {
170- let in_memory_whitelist = Arc :: new ( InMemoryWhitelist :: default ( ) ) ;
171- let whitelist_authorization = Arc :: new ( WhitelistAuthorization :: new ( & config. core , & in_memory_whitelist. clone ( ) ) ) ;
172- let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
173-
174- Arc :: new ( ScrapeHandler :: new ( & whitelist_authorization, & in_memory_torrent_repository) )
175- }
176-
177199 mock ! {
178200 HttpStatsEventSender { }
179201 impl http_tracker_core:: statistics:: event:: sender:: Sender for HttpStatsEventSender {
@@ -197,8 +219,7 @@ mod tests {
197219
198220 use crate :: packages:: http_tracker_core:: services:: scrape:: handle_scrape;
199221 use crate :: packages:: http_tracker_core:: services:: scrape:: tests:: {
200- initialize_announce_and_scrape_handlers_with_configuration, initialize_scrape_handler_with_config,
201- sample_info_hashes, sample_peer, MockHttpStatsEventSender ,
222+ initialize_services_with_configuration, sample_info_hashes, sample_peer, MockHttpStatsEventSender ,
202223 } ;
203224 use crate :: packages:: { self , http_tracker_core} ;
204225 use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
@@ -212,15 +233,16 @@ mod tests {
212233 packages:: http_tracker_core:: statistics:: setup:: factory ( false ) ;
213234 let http_stats_event_sender = Arc :: new ( http_stats_event_sender) ;
214235
215- let ( announce_handler , scrape_handler ) = initialize_announce_and_scrape_handlers_with_configuration ( & configuration) ;
236+ let container = initialize_services_with_configuration ( & configuration) ;
216237
217238 let info_hash = sample_info_hash ( ) ;
218239 let info_hashes = vec ! [ info_hash] ;
219240
220241 // Announce a new peer to force scrape data to contain non zeroed data
221242 let mut peer = sample_peer ( ) ;
222243 let original_peer_ip = peer. ip ( ) ;
223- announce_handler
244+ container
245+ . announce_handler
224246 . announce ( & info_hash, & mut peer, & original_peer_ip, & PeersWanted :: AsManyAsPossible )
225247 . await
226248 . unwrap ( ) ;
@@ -236,11 +258,12 @@ mod tests {
236258
237259 let scrape_data = handle_scrape (
238260 & core_config,
239- & scrape_handler,
261+ & container. scrape_handler ,
262+ & container. authentication_service ,
240263 & http_stats_event_sender,
241264 & scrape_request,
242265 & client_ip_sources,
243- false ,
266+ None ,
244267 )
245268 . await
246269 . unwrap ( ) ;
@@ -271,7 +294,7 @@ mod tests {
271294 let http_stats_event_sender: Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > =
272295 Arc :: new ( Some ( Box :: new ( http_stats_event_sender_mock) ) ) ;
273296
274- let scrape_handler = initialize_scrape_handler_with_config ( & config) ;
297+ let container = initialize_services_with_configuration ( & config) ;
275298
276299 let peer_ip = IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) ;
277300
@@ -286,11 +309,12 @@ mod tests {
286309
287310 handle_scrape (
288311 & Arc :: new ( config. core ) ,
289- & scrape_handler,
312+ & container. scrape_handler ,
313+ & container. authentication_service ,
290314 & http_stats_event_sender,
291315 & scrape_request,
292316 & client_ip_sources,
293- false ,
317+ None ,
294318 )
295319 . await
296320 . unwrap ( ) ;
@@ -309,7 +333,7 @@ mod tests {
309333 let http_stats_event_sender: Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > =
310334 Arc :: new ( Some ( Box :: new ( http_stats_event_sender_mock) ) ) ;
311335
312- let scrape_handler = initialize_scrape_handler_with_config ( & config) ;
336+ let container = initialize_services_with_configuration ( & config) ;
313337
314338 let peer_ip = IpAddr :: V6 ( Ipv6Addr :: new ( 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 ) ) ;
315339
@@ -324,11 +348,12 @@ mod tests {
324348
325349 handle_scrape (
326350 & Arc :: new ( config. core ) ,
327- & scrape_handler,
351+ & container. scrape_handler ,
352+ & container. authentication_service ,
328353 & http_stats_event_sender,
329354 & scrape_request,
330355 & client_ip_sources,
331- false ,
356+ None ,
332357 )
333358 . await
334359 . unwrap ( ) ;
@@ -347,7 +372,7 @@ mod tests {
347372
348373 use crate :: packages:: http_tracker_core:: services:: scrape:: fake;
349374 use crate :: packages:: http_tracker_core:: services:: scrape:: tests:: {
350- initialize_announce_and_scrape_handlers_for_public_tracker , sample_info_hashes, sample_peer, MockHttpStatsEventSender ,
375+ initialize_services_for_public_tracker , sample_info_hashes, sample_peer, MockHttpStatsEventSender ,
351376 } ;
352377 use crate :: packages:: { self , http_tracker_core} ;
353378 use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
@@ -358,15 +383,16 @@ mod tests {
358383 packages:: http_tracker_core:: statistics:: setup:: factory ( false ) ;
359384 let http_stats_event_sender = Arc :: new ( http_stats_event_sender) ;
360385
361- let ( announce_handler , _scrape_handler ) = initialize_announce_and_scrape_handlers_for_public_tracker ( ) ;
386+ let container = initialize_services_for_public_tracker ( ) ;
362387
363388 let info_hash = sample_info_hash ( ) ;
364389 let info_hashes = vec ! [ info_hash] ;
365390
366391 // Announce a new peer to force scrape data to contain not zeroed data
367392 let mut peer = sample_peer ( ) ;
368393 let original_peer_ip = peer. ip ( ) ;
369- announce_handler
394+ container
395+ . announce_handler
370396 . announce ( & info_hash, & mut peer, & original_peer_ip, & PeersWanted :: AsManyAsPossible )
371397 . await
372398 . unwrap ( ) ;
0 commit comments