@@ -14,8 +14,6 @@ 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:: error:: ScrapeError ;
19
17
use bittorrent_tracker_core:: scrape_handler:: ScrapeHandler ;
20
18
use torrust_tracker_configuration:: Core ;
21
19
use torrust_tracker_primitives:: core:: ScrapeData ;
@@ -42,47 +40,28 @@ use crate::packages::http_tracker_core;
42
40
pub async fn handle_scrape (
43
41
core_config : & Arc < Core > ,
44
42
scrape_handler : & Arc < ScrapeHandler > ,
45
- _authentication_service : & Arc < AuthenticationService > ,
46
43
opt_http_stats_event_sender : & Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > ,
47
44
scrape_request : & Scrape ,
48
45
client_ip_sources : & ClientIpSources ,
49
- return_real_scrape_data : bool ,
46
+ return_fake_scrape_data : bool ,
50
47
) -> Result < ScrapeData , responses:: error:: Error > {
51
- // Authorization for scrape requests is handled at the `http_tracker_core `
48
+ // Authorization for scrape requests is handled at the `bittorrent-_racker_core `
52
49
// level for each torrent.
53
50
54
51
let peer_ip = match peer_ip_resolver:: invoke ( core_config. net . on_reverse_proxy , client_ip_sources) {
55
52
Ok ( peer_ip) => peer_ip,
56
53
Err ( error) => return Err ( responses:: error:: Error :: from ( error) ) ,
57
54
} ;
58
55
59
- if return_real_scrape_data {
60
- let scrape_data = invoke (
61
- scrape_handler,
62
- opt_http_stats_event_sender,
63
- & scrape_request. info_hashes ,
64
- & peer_ip,
65
- )
66
- . await ?;
67
-
68
- Ok ( scrape_data)
69
- } else {
70
- Ok ( http_tracker_core:: services:: scrape:: fake ( opt_http_stats_event_sender, & scrape_request. info_hashes , & peer_ip) . await )
56
+ if return_fake_scrape_data {
57
+ return Ok (
58
+ http_tracker_core:: services:: scrape:: fake ( opt_http_stats_event_sender, & scrape_request. info_hashes , & peer_ip) . await ,
59
+ ) ;
71
60
}
72
- }
73
61
74
- /// # Errors
75
- ///
76
- /// This function will return an error if the tracker core scrape handler fails.
77
- pub async fn invoke (
78
- scrape_handler : & Arc < ScrapeHandler > ,
79
- opt_http_stats_event_sender : & Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > ,
80
- info_hashes : & Vec < InfoHash > ,
81
- original_peer_ip : & IpAddr ,
82
- ) -> Result < ScrapeData , ScrapeError > {
83
- let scrape_data = scrape_handler. scrape ( info_hashes) . await ?;
62
+ let scrape_data = scrape_handler. scrape ( & scrape_request. info_hashes ) . await ?;
84
63
85
- send_scrape_event ( original_peer_ip , opt_http_stats_event_sender) . await ;
64
+ send_scrape_event ( & peer_ip , opt_http_stats_event_sender) . await ;
86
65
87
66
Ok ( scrape_data)
88
67
}
@@ -141,15 +120,20 @@ mod tests {
141
120
use futures:: future:: BoxFuture ;
142
121
use mockall:: mock;
143
122
use tokio:: sync:: mpsc:: error:: SendError ;
123
+ use torrust_tracker_configuration:: Configuration ;
144
124
use torrust_tracker_primitives:: { peer, DurationSinceUnixEpoch } ;
145
125
use torrust_tracker_test_helpers:: configuration;
146
126
147
127
use crate :: packages:: http_tracker_core;
148
128
use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
149
129
150
130
fn initialize_announce_and_scrape_handlers_for_public_tracker ( ) -> ( Arc < AnnounceHandler > , Arc < ScrapeHandler > ) {
151
- let config = configuration:: ephemeral_public ( ) ;
131
+ initialize_announce_and_scrape_handlers_with_configuration ( & configuration:: ephemeral_public ( ) )
132
+ }
152
133
134
+ fn initialize_announce_and_scrape_handlers_with_configuration (
135
+ config : & Configuration ,
136
+ ) -> ( Arc < AnnounceHandler > , Arc < ScrapeHandler > ) {
153
137
let database = initialize_database ( & config. core ) ;
154
138
let in_memory_whitelist = Arc :: new ( InMemoryWhitelist :: default ( ) ) ;
155
139
let whitelist_authorization = Arc :: new ( WhitelistAuthorization :: new ( & config. core , & in_memory_whitelist. clone ( ) ) ) ;
@@ -182,9 +166,7 @@ mod tests {
182
166
}
183
167
}
184
168
185
- fn initialize_scrape_handler ( ) -> Arc < ScrapeHandler > {
186
- let config = configuration:: ephemeral ( ) ;
187
-
169
+ fn initialize_scrape_handler_with_config ( config : & Configuration ) -> Arc < ScrapeHandler > {
188
170
let in_memory_whitelist = Arc :: new ( InMemoryWhitelist :: default ( ) ) ;
189
171
let whitelist_authorization = Arc :: new ( WhitelistAuthorization :: new ( & config. core , & in_memory_whitelist. clone ( ) ) ) ;
190
172
let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
@@ -205,41 +187,63 @@ mod tests {
205
187
use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
206
188
use std:: sync:: Arc ;
207
189
190
+ use bittorrent_http_protocol:: v1:: requests:: scrape:: Scrape ;
191
+ use bittorrent_http_protocol:: v1:: services:: peer_ip_resolver:: ClientIpSources ;
208
192
use bittorrent_tracker_core:: announce_handler:: PeersWanted ;
209
193
use mockall:: predicate:: eq;
210
194
use torrust_tracker_primitives:: core:: ScrapeData ;
211
195
use torrust_tracker_primitives:: swarm_metadata:: SwarmMetadata ;
196
+ use torrust_tracker_test_helpers:: configuration;
212
197
213
- use crate :: packages:: http_tracker_core:: services:: scrape:: invoke ;
198
+ use crate :: packages:: http_tracker_core:: services:: scrape:: handle_scrape ;
214
199
use crate :: packages:: http_tracker_core:: services:: scrape:: tests:: {
215
- initialize_announce_and_scrape_handlers_for_public_tracker , initialize_scrape_handler , sample_info_hashes ,
216
- sample_peer, MockHttpStatsEventSender ,
200
+ initialize_announce_and_scrape_handlers_with_configuration , initialize_scrape_handler_with_config ,
201
+ sample_info_hashes , sample_peer, MockHttpStatsEventSender ,
217
202
} ;
218
203
use crate :: packages:: { self , http_tracker_core} ;
219
204
use crate :: servers:: http:: test_helpers:: tests:: sample_info_hash;
220
205
221
206
#[ tokio:: test]
222
207
async fn it_should_return_the_scrape_data_for_a_torrent ( ) {
208
+ let configuration = configuration:: ephemeral_public ( ) ;
209
+ let core_config = Arc :: new ( configuration. core . clone ( ) ) ;
210
+
223
211
let ( http_stats_event_sender, _http_stats_repository) =
224
212
packages:: http_tracker_core:: statistics:: setup:: factory ( false ) ;
225
213
let http_stats_event_sender = Arc :: new ( http_stats_event_sender) ;
226
214
227
- let ( announce_handler, scrape_handler) = initialize_announce_and_scrape_handlers_for_public_tracker ( ) ;
215
+ let ( announce_handler, scrape_handler) = initialize_announce_and_scrape_handlers_with_configuration ( & configuration ) ;
228
216
229
217
let info_hash = sample_info_hash ( ) ;
230
218
let info_hashes = vec ! [ info_hash] ;
231
219
232
- // Announce a new peer to force scrape data to contain not zeroed data
220
+ // Announce a new peer to force scrape data to contain non zeroed data
233
221
let mut peer = sample_peer ( ) ;
234
222
let original_peer_ip = peer. ip ( ) ;
235
223
announce_handler
236
224
. announce ( & info_hash, & mut peer, & original_peer_ip, & PeersWanted :: AsManyAsPossible )
237
225
. await
238
226
. unwrap ( ) ;
239
227
240
- let scrape_data = invoke ( & scrape_handler, & http_stats_event_sender, & info_hashes, & original_peer_ip)
241
- . await
242
- . unwrap ( ) ;
228
+ let scrape_request = Scrape {
229
+ info_hashes : info_hashes. clone ( ) ,
230
+ } ;
231
+
232
+ let client_ip_sources = ClientIpSources {
233
+ right_most_x_forwarded_for : None ,
234
+ connection_info_ip : Some ( original_peer_ip) ,
235
+ } ;
236
+
237
+ let scrape_data = handle_scrape (
238
+ & core_config,
239
+ & scrape_handler,
240
+ & http_stats_event_sender,
241
+ & scrape_request,
242
+ & client_ip_sources,
243
+ false ,
244
+ )
245
+ . await
246
+ . unwrap ( ) ;
243
247
244
248
let mut expected_scrape_data = ScrapeData :: empty ( ) ;
245
249
expected_scrape_data. add_file (
@@ -256,6 +260,8 @@ mod tests {
256
260
257
261
#[ tokio:: test]
258
262
async fn it_should_send_the_tcp_4_scrape_event_when_the_peer_uses_ipv4 ( ) {
263
+ let config = configuration:: ephemeral ( ) ;
264
+
259
265
let mut http_stats_event_sender_mock = MockHttpStatsEventSender :: new ( ) ;
260
266
http_stats_event_sender_mock
261
267
. expect_send_event ( )
@@ -265,17 +271,35 @@ mod tests {
265
271
let http_stats_event_sender: Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > =
266
272
Arc :: new ( Some ( Box :: new ( http_stats_event_sender_mock) ) ) ;
267
273
268
- let scrape_handler = initialize_scrape_handler ( ) ;
274
+ let scrape_handler = initialize_scrape_handler_with_config ( & config ) ;
269
275
270
276
let peer_ip = IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) ;
271
277
272
- invoke ( & scrape_handler, & http_stats_event_sender, & sample_info_hashes ( ) , & peer_ip)
273
- . await
274
- . unwrap ( ) ;
278
+ let scrape_request = Scrape {
279
+ info_hashes : sample_info_hashes ( ) ,
280
+ } ;
281
+
282
+ let client_ip_sources = ClientIpSources {
283
+ right_most_x_forwarded_for : None ,
284
+ connection_info_ip : Some ( peer_ip) ,
285
+ } ;
286
+
287
+ handle_scrape (
288
+ & Arc :: new ( config. core ) ,
289
+ & scrape_handler,
290
+ & http_stats_event_sender,
291
+ & scrape_request,
292
+ & client_ip_sources,
293
+ false ,
294
+ )
295
+ . await
296
+ . unwrap ( ) ;
275
297
}
276
298
277
299
#[ tokio:: test]
278
300
async fn it_should_send_the_tcp_6_scrape_event_when_the_peer_uses_ipv6 ( ) {
301
+ let config = configuration:: ephemeral ( ) ;
302
+
279
303
let mut http_stats_event_sender_mock = MockHttpStatsEventSender :: new ( ) ;
280
304
http_stats_event_sender_mock
281
305
. expect_send_event ( )
@@ -285,13 +309,29 @@ mod tests {
285
309
let http_stats_event_sender: Arc < Option < Box < dyn http_tracker_core:: statistics:: event:: sender:: Sender > > > =
286
310
Arc :: new ( Some ( Box :: new ( http_stats_event_sender_mock) ) ) ;
287
311
288
- let scrape_handler = initialize_scrape_handler ( ) ;
312
+ let scrape_handler = initialize_scrape_handler_with_config ( & config ) ;
289
313
290
314
let peer_ip = IpAddr :: V6 ( Ipv6Addr :: new ( 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 , 0x6969 ) ) ;
291
315
292
- invoke ( & scrape_handler, & http_stats_event_sender, & sample_info_hashes ( ) , & peer_ip)
293
- . await
294
- . unwrap ( ) ;
316
+ let scrape_request = Scrape {
317
+ info_hashes : sample_info_hashes ( ) ,
318
+ } ;
319
+
320
+ let client_ip_sources = ClientIpSources {
321
+ right_most_x_forwarded_for : None ,
322
+ connection_info_ip : Some ( peer_ip) ,
323
+ } ;
324
+
325
+ handle_scrape (
326
+ & Arc :: new ( config. core ) ,
327
+ & scrape_handler,
328
+ & http_stats_event_sender,
329
+ & scrape_request,
330
+ & client_ip_sources,
331
+ false ,
332
+ )
333
+ . await
334
+ . unwrap ( ) ;
295
335
}
296
336
}
297
337
0 commit comments