Skip to content

Commit

Permalink
feat: [#640] Tracker Chekcer: announce check
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 30, 2024
1 parent 005a8cf commit cb5bb68
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions src/console/clients/checker/service.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use colored::Colorize;
use reqwest::{Client, Url};
use reqwest::{Client as HttpClient, Url};

use super::config::Configuration;
use super::console::Console;
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;

pub struct Service {
pub(crate) config: Arc<Configuration>,
Expand All @@ -19,7 +24,7 @@ pub type CheckResult = Result<(), CheckError>;
#[derive(Debug)]
pub enum CheckError {
UdpError,
HttpError,
HttpError { url: Url },

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L27 was not covered by tests
HealthCheckError { url: Url },
}

Expand All @@ -30,10 +35,15 @@ impl Service {
pub async fn run_checks(&self) -> Vec<CheckResult> {
self.console.println("Running checks for trackers ...");

let mut check_results = vec![];

self.check_udp_trackers();
self.check_http_trackers();

self.run_health_checks().await
self.check_http_trackers(&mut check_results).await;

self.run_health_checks(&mut check_results).await;

check_results
}

fn check_udp_trackers(&self) {
Expand All @@ -44,47 +54,67 @@ impl Service {
}
}

fn check_http_trackers(&self) {
async fn check_http_trackers(&self, check_results: &mut Vec<CheckResult>) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L57 was not covered by tests
self.console.println("HTTP trackers ...");

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

async fn run_health_checks(&self) -> Vec<CheckResult> {
async fn run_health_checks(&self, check_results: &mut Vec<CheckResult>) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L68 was not covered by tests
self.console.println("Health checks ...");

let mut check_results = vec![];

for health_check_url in &self.config.health_checks {
match self.run_health_check(health_check_url.clone()).await {
Ok(()) => check_results.push(Ok(())),
Err(err) => check_results.push(Err(err)),
}
}

check_results
}

fn check_udp_tracker(&self, address: &SocketAddr) {
// todo:
// - Make announce request
// - Make scrape request
self.console
.println(&format!("{} - UDP tracker at {:?} is OK (TODO)", "✓".green(), address));
.println(&format!("{} - UDP tracker at udp://{:?} is OK (TODO)", "✓".green(), address));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L84 was not covered by tests
}

fn check_http_tracker(&self, url: &Url) {
// todo:
// - Make announce request
// - Make scrape request
self.console
.println(&format!("{} - HTTP tracker at {} is OK (TODO)", "✓".green(), url));
async fn check_http_tracker(&self, url: &Url) -> Result<(), CheckError> {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L87 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() })
}

// Scrape request

// todo
}

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

match client.get(url.clone()).send().await {
Ok(response) => {
Expand Down

0 comments on commit cb5bb68

Please sign in to comment.