Skip to content

Commit

Permalink
refactor: [torrust#639] Tracker Checker: prepare outout for UDP checks
Browse files Browse the repository at this point in the history
The output for the UDP tracker checks are now the same as the HTTP
tracker checks. But not implemented yet (TODO).

```output
Running checks for trackers ...
UDP trackers ...
✓ - Announce at 127.0.0.1:6969 is OK
✓ - Scrape at 127.0.0.1:6969 is OK
HTTP trackers ...
✓ - Announce at http://127.0.0.1:7070/ is OK (TODO)
✓ - Scrape at http://127.0.0.1:7070/ is OK (TODO)
Health checks ...
✓ - Health API at http://127.0.0.1:1313/health_check is OK
```
  • Loading branch information
josecelano committed Jan 31, 2024
1 parent 95c5aa4 commit 8d07a34
Showing 1 changed file with 58 additions and 21 deletions.
79 changes: 58 additions & 21 deletions src/console/clients/checker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;
use std::time::Duration;

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

use super::config::Configuration;
Expand Down Expand Up @@ -38,7 +39,7 @@ impl Service {

let mut check_results = vec![];

self.check_udp_trackers();
self.check_udp_trackers(&mut check_results).await;

self.check_http_trackers(&mut check_results).await;

Expand All @@ -47,11 +48,44 @@ impl Service {
check_results
}

fn check_udp_trackers(&self) {
async fn check_udp_trackers(&self, check_results: &mut Vec<CheckResult>) {
self.console.println("UDP trackers ...");

for udp_tracker in &self.config.udp_trackers {
self.check_udp_tracker(udp_tracker);
let colored_tracker_url = udp_tracker.to_string().yellow();

/* todo:
- Initialize the UDP client
- Pass the connected client the the check function
- Connect to the tracker
- Make the request (announce or scrape)
*/

match self.check_udp_announce(udp_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_udp_scrape(udp_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 @@ -65,7 +99,7 @@ impl Service {
Ok(()) => {
check_results.push(Ok(()));
self.console
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
.println(&format!("{} - Announce at {} is OK (TODO)", "✓".green(), colored_tracker_url));
}
Err(err) => {
check_results.push(Err(err));
Expand All @@ -78,7 +112,7 @@ impl Service {
Ok(()) => {
check_results.push(Ok(()));
self.console
.println(&format!("{} - Scrape at {} is OK", "✓".green(), colored_tracker_url));
.println(&format!("{} - Scrape at {} is OK (TODO)", "✓".green(), colored_tracker_url));
}
Err(err) => {
check_results.push(Err(err));
Expand All @@ -100,37 +134,39 @@ impl Service {
}
}

fn check_udp_tracker(&self, address: &SocketAddr) {
// todo:
// - Make announce request
// - Make scrape request

let colored_address = address.to_string().yellow();
#[allow(clippy::unused_async)]
async fn check_udp_announce(&self, tracker_socket_addr: &SocketAddr) -> Result<(), CheckError> {
debug!("{tracker_socket_addr}");
Ok(())
}

self.console.println(&format!(
"{} - UDP tracker at udp://{} is OK ({})",
"✓".green(),
colored_address,
"TODO".red(),
));
#[allow(clippy::unused_async)]
async fn check_udp_scrape(&self, tracker_socket_addr: &SocketAddr) -> Result<(), CheckError> {
debug!("{tracker_socket_addr}");
Ok(())
}

async fn check_http_announce(&self, url: &Url) -> Result<(), CheckError> {
async fn check_http_announce(&self, tracker_url: &Url) -> Result<(), CheckError> {
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");

let response = Client::new(url.clone())
let response = Client::new(tracker_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) {
Ok(())
} else {
Err(CheckError::HttpError { url: url.clone() })
debug!("announce body {:#?}", body);
Err(CheckError::HttpError {
url: tracker_url.clone(),
})
}
} else {
Err(CheckError::HttpError { url: url.clone() })
Err(CheckError::HttpError {
url: tracker_url.clone(),
})
}
}

Expand All @@ -144,6 +180,7 @@ impl Service {
if let Ok(_scrape_response) = scrape::Response::try_from_bencoded(&body) {
Ok(())
} else {
debug!("scrape body {:#?}", body);
Err(CheckError::HttpError { url: url.clone() })
}
} else {
Expand Down

0 comments on commit 8d07a34

Please sign in to comment.