@@ -14,6 +14,8 @@ use bittorrent_http_protocol::v1::requests::scrape::Scrape;
14
14
use bittorrent_http_protocol:: v1:: responses;
15
15
use bittorrent_http_protocol:: v1:: services:: peer_ip_resolver:: { self , ClientIpSources } ;
16
16
use bittorrent_primitives:: info_hash:: InfoHash ;
17
+ use bittorrent_tracker_core:: authentication:: service:: AuthenticationService ;
18
+ use bittorrent_tracker_core:: authentication:: Key ;
17
19
use bittorrent_tracker_core:: scrape_handler:: ScrapeHandler ;
18
20
use torrust_tracker_configuration:: Core ;
19
21
use torrust_tracker_primitives:: core:: ScrapeData ;
@@ -40,12 +42,26 @@ use crate::packages::http_tracker_core;
40
42
pub async fn handle_scrape (
41
43
core_config : & Arc < Core > ,
42
44
scrape_handler : & Arc < ScrapeHandler > ,
45
+ authentication_service : & Arc < AuthenticationService > ,
43
46
opt_http_stats_event_sender : & Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > ,
44
47
scrape_request : & Scrape ,
45
48
client_ip_sources : & ClientIpSources ,
46
- return_fake_scrape_data : bool ,
49
+ maybe_key : Option < Key > ,
47
50
) -> 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`
49
65
// level for each torrent.
50
66
51
67
let peer_ip = match peer_ip_resolver:: invoke ( core_config. net . on_reverse_proxy , client_ip_sources) {
@@ -111,6 +127,8 @@ mod tests {
111
127
use aquatic_udp_protocol:: { AnnounceEvent , NumberOfBytes , PeerId } ;
112
128
use bittorrent_primitives:: info_hash:: InfoHash ;
113
129
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 ;
114
132
use bittorrent_tracker_core:: databases:: setup:: initialize_database;
115
133
use bittorrent_tracker_core:: scrape_handler:: ScrapeHandler ;
116
134
use bittorrent_tracker_core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
@@ -127,27 +145,39 @@ mod tests {
127
145
use crate :: packages:: http_tracker_core;
128
146
use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
129
147
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 > ,
132
152
}
133
153
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 {
137
159
let database = initialize_database ( & config. core ) ;
138
160
let in_memory_whitelist = Arc :: new ( InMemoryWhitelist :: default ( ) ) ;
139
161
let whitelist_authorization = Arc :: new ( WhitelistAuthorization :: new ( & config. core , & in_memory_whitelist. clone ( ) ) ) ;
140
162
let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
141
163
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
+
142
167
let announce_handler = Arc :: new ( AnnounceHandler :: new (
143
168
& config. core ,
144
169
& whitelist_authorization,
145
170
& in_memory_torrent_repository,
146
171
& db_torrent_repository,
147
172
) ) ;
173
+
148
174
let scrape_handler = Arc :: new ( ScrapeHandler :: new ( & whitelist_authorization, & in_memory_torrent_repository) ) ;
149
175
150
- ( announce_handler, scrape_handler)
176
+ Container {
177
+ announce_handler,
178
+ scrape_handler,
179
+ authentication_service,
180
+ }
151
181
}
152
182
153
183
fn sample_info_hashes ( ) -> Vec < InfoHash > {
@@ -166,14 +196,6 @@ mod tests {
166
196
}
167
197
}
168
198
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
-
177
199
mock ! {
178
200
HttpStatsEventSender { }
179
201
impl http_tracker_core:: statistics:: event:: sender:: Sender for HttpStatsEventSender {
@@ -197,8 +219,7 @@ mod tests {
197
219
198
220
use crate :: packages:: http_tracker_core:: services:: scrape:: handle_scrape;
199
221
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 ,
202
223
} ;
203
224
use crate :: packages:: { self , http_tracker_core} ;
204
225
use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
@@ -212,15 +233,16 @@ mod tests {
212
233
packages:: http_tracker_core:: statistics:: setup:: factory ( false ) ;
213
234
let http_stats_event_sender = Arc :: new ( http_stats_event_sender) ;
214
235
215
- let ( announce_handler , scrape_handler ) = initialize_announce_and_scrape_handlers_with_configuration ( & configuration) ;
236
+ let container = initialize_services_with_configuration ( & configuration) ;
216
237
217
238
let info_hash = sample_info_hash ( ) ;
218
239
let info_hashes = vec ! [ info_hash] ;
219
240
220
241
// Announce a new peer to force scrape data to contain non zeroed data
221
242
let mut peer = sample_peer ( ) ;
222
243
let original_peer_ip = peer. ip ( ) ;
223
- announce_handler
244
+ container
245
+ . announce_handler
224
246
. announce ( & info_hash, & mut peer, & original_peer_ip, & PeersWanted :: AsManyAsPossible )
225
247
. await
226
248
. unwrap ( ) ;
@@ -236,11 +258,12 @@ mod tests {
236
258
237
259
let scrape_data = handle_scrape (
238
260
& core_config,
239
- & scrape_handler,
261
+ & container. scrape_handler ,
262
+ & container. authentication_service ,
240
263
& http_stats_event_sender,
241
264
& scrape_request,
242
265
& client_ip_sources,
243
- false ,
266
+ None ,
244
267
)
245
268
. await
246
269
. unwrap ( ) ;
@@ -271,7 +294,7 @@ mod tests {
271
294
let http_stats_event_sender: Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > =
272
295
Arc :: new ( Some ( Box :: new ( http_stats_event_sender_mock) ) ) ;
273
296
274
- let scrape_handler = initialize_scrape_handler_with_config ( & config) ;
297
+ let container = initialize_services_with_configuration ( & config) ;
275
298
276
299
let peer_ip = IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) ;
277
300
@@ -286,11 +309,12 @@ mod tests {
286
309
287
310
handle_scrape (
288
311
& Arc :: new ( config. core ) ,
289
- & scrape_handler,
312
+ & container. scrape_handler ,
313
+ & container. authentication_service ,
290
314
& http_stats_event_sender,
291
315
& scrape_request,
292
316
& client_ip_sources,
293
- false ,
317
+ None ,
294
318
)
295
319
. await
296
320
. unwrap ( ) ;
@@ -309,7 +333,7 @@ mod tests {
309
333
let http_stats_event_sender: Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > =
310
334
Arc :: new ( Some ( Box :: new ( http_stats_event_sender_mock) ) ) ;
311
335
312
- let scrape_handler = initialize_scrape_handler_with_config ( & config) ;
336
+ let container = initialize_services_with_configuration ( & config) ;
313
337
314
338
let peer_ip = IpAddr :: V6 ( Ipv6Addr :: new ( 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 ) ) ;
315
339
@@ -324,11 +348,12 @@ mod tests {
324
348
325
349
handle_scrape (
326
350
& Arc :: new ( config. core ) ,
327
- & scrape_handler,
351
+ & container. scrape_handler ,
352
+ & container. authentication_service ,
328
353
& http_stats_event_sender,
329
354
& scrape_request,
330
355
& client_ip_sources,
331
- false ,
356
+ None ,
332
357
)
333
358
. await
334
359
. unwrap ( ) ;
@@ -347,7 +372,7 @@ mod tests {
347
372
348
373
use crate :: packages:: http_tracker_core:: services:: scrape:: fake;
349
374
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 ,
351
376
} ;
352
377
use crate :: packages:: { self , http_tracker_core} ;
353
378
use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
@@ -358,15 +383,16 @@ mod tests {
358
383
packages:: http_tracker_core:: statistics:: setup:: factory ( false ) ;
359
384
let http_stats_event_sender = Arc :: new ( http_stats_event_sender) ;
360
385
361
- let ( announce_handler , _scrape_handler ) = initialize_announce_and_scrape_handlers_for_public_tracker ( ) ;
386
+ let container = initialize_services_for_public_tracker ( ) ;
362
387
363
388
let info_hash = sample_info_hash ( ) ;
364
389
let info_hashes = vec ! [ info_hash] ;
365
390
366
391
// Announce a new peer to force scrape data to contain not zeroed data
367
392
let mut peer = sample_peer ( ) ;
368
393
let original_peer_ip = peer. ip ( ) ;
369
- announce_handler
394
+ container
395
+ . announce_handler
370
396
. announce ( & info_hash, & mut peer, & original_peer_ip, & PeersWanted :: AsManyAsPossible )
371
397
. await
372
398
. unwrap ( ) ;
0 commit comments