Skip to content

Commit 3b735a7

Browse files
committed
refactor: [torrust#639] Tracker Checker: prepare outout for UDP checks
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 ```
1 parent c291ef1 commit 3b735a7

File tree

1 file changed

+58
-21
lines changed

1 file changed

+58
-21
lines changed

src/console/clients/checker/service.rs

+58-21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44
use std::time::Duration;
55

66
use colored::Colorize;
7+
use log::debug;
78
use reqwest::{Client as HttpClient, Url};
89

910
use super::config::Configuration;
@@ -38,7 +39,7 @@ impl Service {
3839

3940
let mut check_results = vec![];
4041

41-
self.check_udp_trackers();
42+
self.check_udp_trackers(&mut check_results).await;
4243

4344
self.check_http_trackers(&mut check_results).await;
4445

@@ -47,11 +48,44 @@ impl Service {
4748
check_results
4849
}
4950

50-
fn check_udp_trackers(&self) {
51+
async fn check_udp_trackers(&self, check_results: &mut Vec<CheckResult>) {
5152
self.console.println("UDP trackers ...");
5253

5354
for udp_tracker in &self.config.udp_trackers {
54-
self.check_udp_tracker(udp_tracker);
55+
let colored_tracker_url = udp_tracker.to_string().yellow();
56+
57+
/* todo:
58+
- Initialize the UDP client
59+
- Pass the connected client the the check function
60+
- Connect to the tracker
61+
- Make the request (announce or scrape)
62+
*/
63+
64+
match self.check_udp_announce(udp_tracker).await {
65+
Ok(()) => {
66+
check_results.push(Ok(()));
67+
self.console
68+
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
69+
}
70+
Err(err) => {
71+
check_results.push(Err(err));
72+
self.console
73+
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
74+
}
75+
}
76+
77+
match self.check_udp_scrape(udp_tracker).await {
78+
Ok(()) => {
79+
check_results.push(Ok(()));
80+
self.console
81+
.println(&format!("{} - Scrape at {} is OK", "✓".green(), colored_tracker_url));
82+
}
83+
Err(err) => {
84+
check_results.push(Err(err));
85+
self.console
86+
.println(&format!("{} - Scrape at {} is failing", "✗".red(), colored_tracker_url));
87+
}
88+
}
5589
}
5690
}
5791

@@ -65,7 +99,7 @@ impl Service {
6599
Ok(()) => {
66100
check_results.push(Ok(()));
67101
self.console
68-
.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url));
102+
.println(&format!("{} - Announce at {} is OK (TODO)", "✓".green(), colored_tracker_url));
69103
}
70104
Err(err) => {
71105
check_results.push(Err(err));
@@ -78,7 +112,7 @@ impl Service {
78112
Ok(()) => {
79113
check_results.push(Ok(()));
80114
self.console
81-
.println(&format!("{} - Scrape at {} is OK", "✓".green(), colored_tracker_url));
115+
.println(&format!("{} - Scrape at {} is OK (TODO)", "✓".green(), colored_tracker_url));
82116
}
83117
Err(err) => {
84118
check_results.push(Err(err));
@@ -100,37 +134,39 @@ impl Service {
100134
}
101135
}
102136

103-
fn check_udp_tracker(&self, address: &SocketAddr) {
104-
// todo:
105-
// - Make announce request
106-
// - Make scrape request
107-
108-
let colored_address = address.to_string().yellow();
137+
#[allow(clippy::unused_async)]
138+
async fn check_udp_announce(&self, tracker_socket_addr: &SocketAddr) -> Result<(), CheckError> {
139+
debug!("{tracker_socket_addr}");
140+
Ok(())
141+
}
109142

110-
self.console.println(&format!(
111-
"{} - UDP tracker at udp://{} is OK ({})",
112-
"✓".green(),
113-
colored_address,
114-
"TODO".red(),
115-
));
143+
#[allow(clippy::unused_async)]
144+
async fn check_udp_scrape(&self, tracker_socket_addr: &SocketAddr) -> Result<(), CheckError> {
145+
debug!("{tracker_socket_addr}");
146+
Ok(())
116147
}
117148

118-
async fn check_http_announce(&self, url: &Url) -> Result<(), CheckError> {
149+
async fn check_http_announce(&self, tracker_url: &Url) -> Result<(), CheckError> {
119150
let info_hash_str = "9c38422213e30bff212b30c360d26f9a02136422".to_string(); // # DevSkim: ignore DS173237
120151
let info_hash = InfoHash::from_str(&info_hash_str).expect("a valid info-hash is required");
121152

122-
let response = Client::new(url.clone())
153+
let response = Client::new(tracker_url.clone())
123154
.announce(&QueryBuilder::with_default_values().with_info_hash(&info_hash).query())
124155
.await;
125156

126157
if let Ok(body) = response.bytes().await {
127158
if let Ok(_announce_response) = serde_bencode::from_bytes::<Announce>(&body) {
128159
Ok(())
129160
} else {
130-
Err(CheckError::HttpError { url: url.clone() })
161+
debug!("announce body {:#?}", body);
162+
Err(CheckError::HttpError {
163+
url: tracker_url.clone(),
164+
})
131165
}
132166
} else {
133-
Err(CheckError::HttpError { url: url.clone() })
167+
Err(CheckError::HttpError {
168+
url: tracker_url.clone(),
169+
})
134170
}
135171
}
136172

@@ -144,6 +180,7 @@ impl Service {
144180
if let Ok(_scrape_response) = scrape::Response::try_from_bencoded(&body) {
145181
Ok(())
146182
} else {
183+
debug!("scrape body {:#?}", body);
147184
Err(CheckError::HttpError { url: url.clone() })
148185
}
149186
} else {

0 commit comments

Comments
 (0)