Skip to content

Commit 77c32a1

Browse files
committed
refactor: [torrust#639] Tracker Checker: extract mod for UDP checks
1 parent 592c0dd commit 77c32a1

File tree

6 files changed

+93
-84
lines changed

6 files changed

+93
-84
lines changed

src/console/clients/checker/checks/health.rs

Whitespace-only changes.

src/console/clients/checker/checks/http.rs

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod health;
2+
pub mod http;
3+
pub mod udp;
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::net::SocketAddr;
2+
3+
use aquatic_udp_protocol::{Port, TransactionId};
4+
use colored::Colorize;
5+
use hex_literal::hex;
6+
use log::debug;
7+
8+
use crate::console::clients::checker::console::Console;
9+
use crate::console::clients::checker::printer::Printer;
10+
use crate::console::clients::checker::service::{CheckError, CheckResult};
11+
use crate::console::clients::udp::checker;
12+
use crate::shared::bit_torrent::info_hash::InfoHash;
13+
14+
const ASSIGNED_BY_OS: u16 = 0;
15+
const RANDOM_TRANSACTION_ID: i32 = -888_840_697;
16+
17+
pub async fn run(udp_trackers: &Vec<SocketAddr>, console: &Console, check_results: &mut Vec<CheckResult>) {
18+
console.println("UDP trackers ...");
19+
20+
for udp_tracker in udp_trackers {
21+
debug!("UDP tracker: {:?}", udp_tracker);
22+
23+
let colored_tracker_url = udp_tracker.to_string().yellow();
24+
25+
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);
26+
27+
let mut client = checker::Client::default();
28+
29+
debug!("Bind and connect");
30+
31+
let Ok(bound_to) = client.bind_and_connect(ASSIGNED_BY_OS, udp_tracker).await else {
32+
check_results.push(Err(CheckError::UdpError {
33+
socket_addr: *udp_tracker,
34+
}));
35+
console.println(&format!("{} - Can't connect to socket {}", "✗".red(), colored_tracker_url));
36+
break;
37+
};
38+
39+
debug!("Send connection request");
40+
41+
let Ok(connection_id) = client.send_connection_request(transaction_id).await else {
42+
check_results.push(Err(CheckError::UdpError {
43+
socket_addr: *udp_tracker,
44+
}));
45+
console.println(&format!(
46+
"{} - Can't make tracker connection request to {}",
47+
"✗".red(),
48+
colored_tracker_url
49+
));
50+
break;
51+
};
52+
53+
let info_hash = InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422")); // # DevSkim: ignore DS173237
54+
55+
debug!("Send announce request");
56+
57+
if (client
58+
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
59+
.await)
60+
.is_ok()
61+
{
62+
check_results.push(Ok(()));
63+
console.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
64+
} else {
65+
let err = CheckError::UdpError {
66+
socket_addr: *udp_tracker,
67+
};
68+
check_results.push(Err(err));
69+
console.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
70+
}
71+
72+
debug!("Send scrape request");
73+
74+
let info_hashes = vec![InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422"))]; // # DevSkim: ignore DS173237
75+
76+
if (client.send_scrape_request(connection_id, transaction_id, info_hashes).await).is_ok() {
77+
check_results.push(Ok(()));
78+
console.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
79+
} else {
80+
let err = CheckError::UdpError {
81+
socket_addr: *udp_tracker,
82+
};
83+
check_results.push(Err(err));
84+
console.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
85+
}
86+
}
87+
}

src/console/clients/checker/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod app;
2+
pub mod checks;
23
pub mod config;
34
pub mod console;
45
pub mod logger;

src/console/clients/checker/service.rs

+2-84
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@ use std::str::FromStr;
33
use std::sync::Arc;
44
use std::time::Duration;
55

6-
use aquatic_udp_protocol::{Port, TransactionId};
76
use colored::Colorize;
8-
use hex_literal::hex;
97
use log::debug;
108
use reqwest::{Client as HttpClient, Url};
119

10+
use super::checks;
1211
use super::config::Configuration;
1312
use super::console::Console;
1413
use crate::console::clients::checker::printer::Printer;
15-
use crate::console::clients::udp::checker;
1614
use crate::shared::bit_torrent::info_hash::InfoHash;
1715
use crate::shared::bit_torrent::tracker::http::client::requests::announce::QueryBuilder;
1816
use crate::shared::bit_torrent::tracker::http::client::responses::announce::Announce;
1917
use crate::shared::bit_torrent::tracker::http::client::responses::scrape;
2018
use crate::shared::bit_torrent::tracker::http::client::{requests, Client};
2119

22-
const ASSIGNED_BY_OS: u16 = 0;
23-
const RANDOM_TRANSACTION_ID: i32 = -888_840_697;
24-
2520
pub struct Service {
2621
pub(crate) config: Arc<Configuration>,
2722
pub(crate) console: Console,
@@ -45,7 +40,7 @@ impl Service {
4540

4641
let mut check_results = vec![];
4742

48-
self.check_udp_trackers(&mut check_results).await;
43+
checks::udp::run(&self.config.udp_trackers, &self.console, &mut check_results).await;
4944

5045
self.check_http_trackers(&mut check_results).await;
5146

@@ -54,83 +49,6 @@ impl Service {
5449
check_results
5550
}
5651

57-
async fn check_udp_trackers(&self, check_results: &mut Vec<CheckResult>) {
58-
self.console.println("UDP trackers ...");
59-
60-
for udp_tracker in &self.config.udp_trackers {
61-
debug!("UDP tracker: {:?}", udp_tracker);
62-
63-
let colored_tracker_url = udp_tracker.to_string().yellow();
64-
65-
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);
66-
67-
let mut client = checker::Client::default();
68-
69-
debug!("Bind and connect");
70-
71-
let Ok(bound_to) = client.bind_and_connect(ASSIGNED_BY_OS, udp_tracker).await else {
72-
check_results.push(Err(CheckError::UdpError {
73-
socket_addr: *udp_tracker,
74-
}));
75-
self.console
76-
.println(&format!("{} - Can't connect to socket {}", "✗".red(), colored_tracker_url));
77-
break;
78-
};
79-
80-
debug!("Send connection request");
81-
82-
let Ok(connection_id) = client.send_connection_request(transaction_id).await else {
83-
check_results.push(Err(CheckError::UdpError {
84-
socket_addr: *udp_tracker,
85-
}));
86-
self.console.println(&format!(
87-
"{} - Can't make tracker connection request to {}",
88-
"✗".red(),
89-
colored_tracker_url
90-
));
91-
break;
92-
};
93-
94-
let info_hash = InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422")); // # DevSkim: ignore DS173237
95-
96-
debug!("Send announce request");
97-
98-
if (client
99-
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
100-
.await)
101-
.is_ok()
102-
{
103-
check_results.push(Ok(()));
104-
self.console
105-
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
106-
} else {
107-
let err = CheckError::UdpError {
108-
socket_addr: *udp_tracker,
109-
};
110-
check_results.push(Err(err));
111-
self.console
112-
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
113-
}
114-
115-
debug!("Send scrape request");
116-
117-
let info_hashes = vec![InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422"))]; // # DevSkim: ignore DS173237
118-
119-
if (client.send_scrape_request(connection_id, transaction_id, info_hashes).await).is_ok() {
120-
check_results.push(Ok(()));
121-
self.console
122-
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
123-
} else {
124-
let err = CheckError::UdpError {
125-
socket_addr: *udp_tracker,
126-
};
127-
check_results.push(Err(err));
128-
self.console
129-
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
130-
}
131-
}
132-
}
133-
13452
async fn check_http_trackers(&self, check_results: &mut Vec<CheckResult>) {
13553
self.console.println("HTTP trackers ...");
13654

0 commit comments

Comments
 (0)