Skip to content

Commit

Permalink
feat: [#640] Tracker Chekcer: scrape check
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 30, 2024
1 parent cb5bb68 commit 4456203
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 20 deletions.
88 changes: 68 additions & 20 deletions src/console/clients/checker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::console::clients::checker::printer::Printer;
use crate::shared::bit_torrent::info_hash::InfoHash;
use crate::shared::bit_torrent::tracker::http::client::requests::announce::QueryBuilder;
use crate::shared::bit_torrent::tracker::http::client::responses::announce::Announce;
use crate::shared::bit_torrent::tracker::http::client::Client;
use crate::shared::bit_torrent::tracker::http::client::responses::scrape;
use crate::shared::bit_torrent::tracker::http::client::{requests, Client};

pub struct Service {
pub(crate) config: Arc<Configuration>,
Expand Down Expand Up @@ -58,9 +59,32 @@ impl Service {
self.console.println("HTTP trackers ...");

for http_tracker in &self.config.http_trackers {
match self.check_http_tracker(http_tracker).await {
Ok(()) => check_results.push(Ok(())),
Err(err) => check_results.push(Err(err)),
let colored_tracker_url = http_tracker.to_string().yellow();

match self.check_http_announce(http_tracker).await {
Ok(()) => {
check_results.push(Ok(()));
self.console
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
}
Err(err) => {
check_results.push(Err(err));
self.console
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
}
}

match self.check_http_scrape(http_tracker).await {
Ok(()) => {
check_results.push(Ok(()));
self.console
.println(&format!("{} - Scrape at {} is OK", "✓".green(), colored_tracker_url));
}
Err(err) => {
check_results.push(Err(err));
self.console
.println(&format!("{} - Scrape at {} is failing", "✗".red(), colored_tracker_url));
}
}
}
}
Expand All @@ -80,57 +104,81 @@ impl Service {
// todo:
// - Make announce request
// - Make scrape request
self.console
.println(&format!("{} - UDP tracker at udp://{:?} is OK (TODO)", "✓".green(), address));

let colored_address = address.to_string().yellow();

Check warning on line 108 in src/console/clients/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/service.rs#L108

Added line #L108 was not covered by tests

self.console.println(&format!(

Check warning on line 110 in src/console/clients/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/service.rs#L110

Added line #L110 was not covered by tests
"{} - UDP tracker at udp://{} is OK ({})",
"✓".green(),

Check warning on line 112 in src/console/clients/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/service.rs#L112

Added line #L112 was not covered by tests
colored_address,
"TODO".red(),
));

Check warning on line 115 in src/console/clients/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/service.rs#L114-L115

Added lines #L114 - L115 were not covered by tests
}

async fn check_http_tracker(&self, url: &Url) -> Result<(), CheckError> {
async fn check_http_announce(&self, url: &Url) -> Result<(), CheckError> {

Check warning on line 118 in src/console/clients/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/service.rs#L118

Added line #L118 was not covered by tests
let info_hash_str = "9c38422213e30bff212b30c360d26f9a02136422".to_string(); // # DevSkim: ignore DS173237
let info_hash = InfoHash::from_str(&info_hash_str).expect("a valid info-hash is required");

// Announce request

let response = Client::new(url.clone())
.announce(&QueryBuilder::with_default_values().with_info_hash(&info_hash).query())
.await;

if let Ok(body) = response.bytes().await {
if let Ok(_announce_response) = serde_bencode::from_bytes::<Announce>(&body) {
self.console.println(&format!("{} - Announce at {} is OK", "✓".green(), url));

Ok(())
} else {
self.console.println(&format!("{} - Announce at {} failing", "✗".red(), url));
Err(CheckError::HttpError { url: url.clone() })
}
} else {
self.console.println(&format!("{} - Announce at {} failing", "✗".red(), url));
Err(CheckError::HttpError { url: url.clone() })
}
}

Check warning on line 135 in src/console/clients/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/service.rs#L135

Added line #L135 was not covered by tests

async fn check_http_scrape(&self, url: &Url) -> Result<(), CheckError> {

Check warning on line 137 in src/console/clients/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/service.rs#L137

Added line #L137 was not covered by tests
let info_hashes: Vec<String> = vec!["9c38422213e30bff212b30c360d26f9a02136422".to_string()]; // # DevSkim: ignore DS173237
let query = requests::scrape::Query::try_from(info_hashes).expect("a valid array of info-hashes is required");

// Scrape request
let response = Client::new(url.clone()).scrape(&query).await;

// todo
if let Ok(body) = response.bytes().await {
if let Ok(_scrape_response) = scrape::Response::try_from_bencoded(&body) {
Ok(())
} else {
Err(CheckError::HttpError { url: url.clone() })
}
} else {
Err(CheckError::HttpError { url: url.clone() })
}
}

async fn run_health_check(&self, url: Url) -> Result<(), CheckError> {
let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();

let colored_url = url.to_string().yellow();

match client.get(url.clone()).send().await {
Ok(response) => {
if response.status().is_success() {
self.console
.println(&format!("{} - Health API at {} is OK", "✓".green(), url));
.println(&format!("{} - Health API at {} is OK", "✓".green(), colored_url));
Ok(())
} else {
self.console
.eprintln(&format!("{} - Health API at {} failing: {:?}", "✗".red(), url, response));
self.console.eprintln(&format!(
"{} - Health API at {} is failing: {:?}",
"✗".red(),
colored_url,
response
));
Err(CheckError::HealthCheckError { url })
}
}
Err(err) => {
self.console
.eprintln(&format!("{} - Health API at {} failing: {:?}", "✗".red(), url, err));
self.console.eprintln(&format!(
"{} - Health API at {} is failing: {:?}",
"✗".red(),
colored_url,
err
));
Err(CheckError::HealthCheckError { url })
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/shared/bit_torrent/tracker/http/client/requests/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ impl TryFrom<&[String]> for Query {
}
}

impl TryFrom<Vec<String>> for Query {
type Error = ConversionError;

fn try_from(info_hashes: Vec<String>) -> Result<Self, Self::Error> {
let mut validated_info_hashes: Vec<ByteArray20> = Vec::new();

Check warning on line 52 in src/shared/bit_torrent/tracker/http/client/requests/scrape.rs

View check run for this annotation

Codecov / codecov/patch

src/shared/bit_torrent/tracker/http/client/requests/scrape.rs#L51-L52

Added lines #L51 - L52 were not covered by tests

for info_hash in info_hashes {
let validated_info_hash = InfoHash::from_str(&info_hash).map_err(|_| ConversionError(info_hash.clone()))?;
validated_info_hashes.push(validated_info_hash.0);
}

Check warning on line 57 in src/shared/bit_torrent/tracker/http/client/requests/scrape.rs

View check run for this annotation

Codecov / codecov/patch

src/shared/bit_torrent/tracker/http/client/requests/scrape.rs#L54-L57

Added lines #L54 - L57 were not covered by tests

Ok(Self {
info_hash: validated_info_hashes,
})
}

Check warning on line 62 in src/shared/bit_torrent/tracker/http/client/requests/scrape.rs

View check run for this annotation

Codecov / codecov/patch

src/shared/bit_torrent/tracker/http/client/requests/scrape.rs#L59-L62

Added lines #L59 - L62 were not covered by tests
}

/// HTTP Tracker Scrape Request:
///
/// <https://www.bittorrent.org/beps/bep_0048.html>
Expand Down

0 comments on commit 4456203

Please sign in to comment.