Skip to content

Commit 6651343

Browse files
committed
refactor: [torrust#1270] inline http tracker scrape invoke fn
1 parent 096d503 commit 6651343

File tree

2 files changed

+95
-56
lines changed

2 files changed

+95
-56
lines changed

src/packages/http_tracker_core/services/scrape.rs

+89-49
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use bittorrent_http_protocol::v1::requests::scrape::Scrape;
1414
use bittorrent_http_protocol::v1::responses;
1515
use bittorrent_http_protocol::v1::services::peer_ip_resolver::{self, ClientIpSources};
1616
use bittorrent_primitives::info_hash::InfoHash;
17-
use bittorrent_tracker_core::authentication::service::AuthenticationService;
18-
use bittorrent_tracker_core::error::ScrapeError;
1917
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
2018
use torrust_tracker_configuration::Core;
2119
use torrust_tracker_primitives::core::ScrapeData;
@@ -42,47 +40,28 @@ use crate::packages::http_tracker_core;
4240
pub async fn handle_scrape(
4341
core_config: &Arc<Core>,
4442
scrape_handler: &Arc<ScrapeHandler>,
45-
_authentication_service: &Arc<AuthenticationService>,
4643
opt_http_stats_event_sender: &Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
4744
scrape_request: &Scrape,
4845
client_ip_sources: &ClientIpSources,
49-
return_real_scrape_data: bool,
46+
return_fake_scrape_data: bool,
5047
) -> 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`
5249
// level for each torrent.
5350

5451
let peer_ip = match peer_ip_resolver::invoke(core_config.net.on_reverse_proxy, client_ip_sources) {
5552
Ok(peer_ip) => peer_ip,
5653
Err(error) => return Err(responses::error::Error::from(error)),
5754
};
5855

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+
);
7160
}
72-
}
7361

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?;
8463

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;
8665

8766
Ok(scrape_data)
8867
}
@@ -141,15 +120,20 @@ mod tests {
141120
use futures::future::BoxFuture;
142121
use mockall::mock;
143122
use tokio::sync::mpsc::error::SendError;
123+
use torrust_tracker_configuration::Configuration;
144124
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
145125
use torrust_tracker_test_helpers::configuration;
146126

147127
use crate::packages::http_tracker_core;
148128
use crate::servers::http::test_helpers::tests::sample_info_hash;
149129

150130
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+
}
152133

134+
fn initialize_announce_and_scrape_handlers_with_configuration(
135+
config: &Configuration,
136+
) -> (Arc<AnnounceHandler>, Arc<ScrapeHandler>) {
153137
let database = initialize_database(&config.core);
154138
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
155139
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
@@ -182,9 +166,7 @@ mod tests {
182166
}
183167
}
184168

185-
fn initialize_scrape_handler() -> Arc<ScrapeHandler> {
186-
let config = configuration::ephemeral();
187-
169+
fn initialize_scrape_handler_with_config(config: &Configuration) -> Arc<ScrapeHandler> {
188170
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
189171
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
190172
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
@@ -205,41 +187,63 @@ mod tests {
205187
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
206188
use std::sync::Arc;
207189

190+
use bittorrent_http_protocol::v1::requests::scrape::Scrape;
191+
use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;
208192
use bittorrent_tracker_core::announce_handler::PeersWanted;
209193
use mockall::predicate::eq;
210194
use torrust_tracker_primitives::core::ScrapeData;
211195
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
196+
use torrust_tracker_test_helpers::configuration;
212197

213-
use crate::packages::http_tracker_core::services::scrape::invoke;
198+
use crate::packages::http_tracker_core::services::scrape::handle_scrape;
214199
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,
217202
};
218203
use crate::packages::{self, http_tracker_core};
219204
use crate::servers::http::test_helpers::tests::sample_info_hash;
220205

221206
#[tokio::test]
222207
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+
223211
let (http_stats_event_sender, _http_stats_repository) =
224212
packages::http_tracker_core::statistics::setup::factory(false);
225213
let http_stats_event_sender = Arc::new(http_stats_event_sender);
226214

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);
228216

229217
let info_hash = sample_info_hash();
230218
let info_hashes = vec![info_hash];
231219

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
233221
let mut peer = sample_peer();
234222
let original_peer_ip = peer.ip();
235223
announce_handler
236224
.announce(&info_hash, &mut peer, &original_peer_ip, &PeersWanted::AsManyAsPossible)
237225
.await
238226
.unwrap();
239227

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();
243247

244248
let mut expected_scrape_data = ScrapeData::empty();
245249
expected_scrape_data.add_file(
@@ -256,6 +260,8 @@ mod tests {
256260

257261
#[tokio::test]
258262
async fn it_should_send_the_tcp_4_scrape_event_when_the_peer_uses_ipv4() {
263+
let config = configuration::ephemeral();
264+
259265
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
260266
http_stats_event_sender_mock
261267
.expect_send_event()
@@ -265,17 +271,35 @@ mod tests {
265271
let http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>> =
266272
Arc::new(Some(Box::new(http_stats_event_sender_mock)));
267273

268-
let scrape_handler = initialize_scrape_handler();
274+
let scrape_handler = initialize_scrape_handler_with_config(&config);
269275

270276
let peer_ip = IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1));
271277

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();
275297
}
276298

277299
#[tokio::test]
278300
async fn it_should_send_the_tcp_6_scrape_event_when_the_peer_uses_ipv6() {
301+
let config = configuration::ephemeral();
302+
279303
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
280304
http_stats_event_sender_mock
281305
.expect_send_event()
@@ -285,13 +309,29 @@ mod tests {
285309
let http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>> =
286310
Arc::new(Some(Box::new(http_stats_event_sender_mock)));
287311

288-
let scrape_handler = initialize_scrape_handler();
312+
let scrape_handler = initialize_scrape_handler_with_config(&config);
289313

290314
let peer_ip = IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969));
291315

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();
295335
}
296336
}
297337

src/servers/http/v1/handlers/scrape.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,25 @@ async fn handle_scrape(
123123
// todo: move authentication inside `http_tracker_core::services::scrape::handle_scrape`
124124

125125
// Authentication
126-
let return_real_scrape_data = if core_config.private {
126+
let return_fake_scrape_data = if core_config.private {
127127
match maybe_key {
128128
Some(key) => match authentication_service.authenticate(&key).await {
129-
Ok(()) => true,
130-
Err(_error) => false,
129+
Ok(()) => false,
130+
Err(_error) => true,
131131
},
132-
None => false,
132+
None => true,
133133
}
134134
} else {
135-
true
135+
false
136136
};
137137

138138
http_tracker_core::services::scrape::handle_scrape(
139139
core_config,
140140
scrape_handler,
141-
authentication_service,
142141
opt_http_stats_event_sender,
143142
scrape_request,
144143
client_ip_sources,
145-
return_real_scrape_data,
144+
return_fake_scrape_data,
146145
)
147146
.await
148147
}

0 commit comments

Comments
 (0)