Skip to content

Commit

Permalink
refactor: [#639] Tracker Checker: extract mod for UDP checks
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 1, 2024
1 parent 592c0dd commit 77c32a1
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 84 deletions.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions src/console/clients/checker/checks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod health;
pub mod http;
pub mod udp;
87 changes: 87 additions & 0 deletions src/console/clients/checker/checks/udp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::net::SocketAddr;

use aquatic_udp_protocol::{Port, TransactionId};
use colored::Colorize;
use hex_literal::hex;
use log::debug;

use crate::console::clients::checker::console::Console;
use crate::console::clients::checker::printer::Printer;
use crate::console::clients::checker::service::{CheckError, CheckResult};
use crate::console::clients::udp::checker;
use crate::shared::bit_torrent::info_hash::InfoHash;

const ASSIGNED_BY_OS: u16 = 0;
const RANDOM_TRANSACTION_ID: i32 = -888_840_697;

pub async fn run(udp_trackers: &Vec<SocketAddr>, console: &Console, check_results: &mut Vec<CheckResult>) {
console.println("UDP trackers ...");

for udp_tracker in udp_trackers {
debug!("UDP tracker: {:?}", udp_tracker);

let colored_tracker_url = udp_tracker.to_string().yellow();

let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

debug!("Bind and connect");

let Ok(bound_to) = client.bind_and_connect(ASSIGNED_BY_OS, udp_tracker).await else {
check_results.push(Err(CheckError::UdpError {
socket_addr: *udp_tracker,
}));
console.println(&format!("{} - Can't connect to socket {}", "✗".red(), colored_tracker_url));
break;
};

debug!("Send connection request");

let Ok(connection_id) = client.send_connection_request(transaction_id).await else {
check_results.push(Err(CheckError::UdpError {
socket_addr: *udp_tracker,
}));
console.println(&format!(
"{} - Can't make tracker connection request to {}",
"✗".red(),
colored_tracker_url
));
break;
};

let info_hash = InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422")); // # DevSkim: ignore DS173237

debug!("Send announce request");

if (client
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
.await)
.is_ok()
{
check_results.push(Ok(()));
console.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
} else {
let err = CheckError::UdpError {
socket_addr: *udp_tracker,
};
check_results.push(Err(err));
console.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
}

debug!("Send scrape request");

let info_hashes = vec![InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422"))]; // # DevSkim: ignore DS173237

if (client.send_scrape_request(connection_id, transaction_id, info_hashes).await).is_ok() {
check_results.push(Ok(()));
console.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
} else {
let err = CheckError::UdpError {
socket_addr: *udp_tracker,
};
check_results.push(Err(err));
console.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
}
}
}
1 change: 1 addition & 0 deletions src/console/clients/checker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod app;
pub mod checks;
pub mod config;
pub mod console;
pub mod logger;
Expand Down
86 changes: 2 additions & 84 deletions src/console/clients/checker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use aquatic_udp_protocol::{Port, TransactionId};
use colored::Colorize;
use hex_literal::hex;
use log::debug;
use reqwest::{Client as HttpClient, Url};

use super::checks;
use super::config::Configuration;
use super::console::Console;
use crate::console::clients::checker::printer::Printer;
use crate::console::clients::udp::checker;
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::responses::scrape;
use crate::shared::bit_torrent::tracker::http::client::{requests, Client};

const ASSIGNED_BY_OS: u16 = 0;
const RANDOM_TRANSACTION_ID: i32 = -888_840_697;

pub struct Service {
pub(crate) config: Arc<Configuration>,
pub(crate) console: Console,
Expand All @@ -45,7 +40,7 @@ impl Service {

let mut check_results = vec![];

self.check_udp_trackers(&mut check_results).await;
checks::udp::run(&self.config.udp_trackers, &self.console, &mut check_results).await;

self.check_http_trackers(&mut check_results).await;

Expand All @@ -54,83 +49,6 @@ impl Service {
check_results
}

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

for udp_tracker in &self.config.udp_trackers {
debug!("UDP tracker: {:?}", udp_tracker);

let colored_tracker_url = udp_tracker.to_string().yellow();

let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

debug!("Bind and connect");

let Ok(bound_to) = client.bind_and_connect(ASSIGNED_BY_OS, udp_tracker).await else {
check_results.push(Err(CheckError::UdpError {
socket_addr: *udp_tracker,
}));
self.console
.println(&format!("{} - Can't connect to socket {}", "✗".red(), colored_tracker_url));
break;
};

debug!("Send connection request");

let Ok(connection_id) = client.send_connection_request(transaction_id).await else {
check_results.push(Err(CheckError::UdpError {
socket_addr: *udp_tracker,
}));
self.console.println(&format!(
"{} - Can't make tracker connection request to {}",
"✗".red(),
colored_tracker_url
));
break;
};

let info_hash = InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422")); // # DevSkim: ignore DS173237

debug!("Send announce request");

if (client
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
.await)
.is_ok()
{
check_results.push(Ok(()));
self.console
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
} else {
let err = CheckError::UdpError {
socket_addr: *udp_tracker,
};
check_results.push(Err(err));
self.console
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
}

debug!("Send scrape request");

let info_hashes = vec![InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422"))]; // # DevSkim: ignore DS173237

if (client.send_scrape_request(connection_id, transaction_id, info_hashes).await).is_ok() {
check_results.push(Ok(()));
self.console
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
} else {
let err = CheckError::UdpError {
socket_addr: *udp_tracker,
};
check_results.push(Err(err));
self.console
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
}
}
}

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

Expand Down

0 comments on commit 77c32a1

Please sign in to comment.