|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use colored::Colorize; |
| 4 | +use log::debug; |
| 5 | +use reqwest::Url as ServiceUrl; |
| 6 | +use url::Url; |
| 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::shared::bit_torrent::info_hash::InfoHash; |
| 12 | +use crate::shared::bit_torrent::tracker::http::client::requests::announce::QueryBuilder; |
| 13 | +use crate::shared::bit_torrent::tracker::http::client::responses::announce::Announce; |
| 14 | +use crate::shared::bit_torrent::tracker::http::client::responses::scrape; |
| 15 | +use crate::shared::bit_torrent::tracker::http::client::{requests, Client}; |
| 16 | + |
| 17 | +pub async fn run(http_trackers: &Vec<ServiceUrl>, console: &Console, check_results: &mut Vec<CheckResult>) { |
| 18 | + console.println("HTTP trackers ..."); |
| 19 | + |
| 20 | + for http_tracker in http_trackers { |
| 21 | + let colored_tracker_url = http_tracker.to_string().yellow(); |
| 22 | + |
| 23 | + match check_http_announce(http_tracker).await { |
| 24 | + Ok(()) => { |
| 25 | + check_results.push(Ok(())); |
| 26 | + console.println(&format!("{} - Announce at {} is OK", "✓".green(), colored_tracker_url)); |
| 27 | + } |
| 28 | + Err(err) => { |
| 29 | + check_results.push(Err(err)); |
| 30 | + console.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url)); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + match check_http_scrape(http_tracker).await { |
| 35 | + Ok(()) => { |
| 36 | + check_results.push(Ok(())); |
| 37 | + console.println(&format!("{} - Scrape at {} is OK", "✓".green(), colored_tracker_url)); |
| 38 | + } |
| 39 | + Err(err) => { |
| 40 | + check_results.push(Err(err)); |
| 41 | + console.println(&format!("{} - Scrape at {} is failing", "✗".red(), colored_tracker_url)); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async fn check_http_announce(tracker_url: &Url) -> Result<(), CheckError> { |
| 48 | + let info_hash_str = "9c38422213e30bff212b30c360d26f9a02136422".to_string(); // # DevSkim: ignore DS173237 |
| 49 | + let info_hash = InfoHash::from_str(&info_hash_str).expect("a valid info-hash is required"); |
| 50 | + |
| 51 | + // todo: HTTP request could panic.For example, if the server is not accessible. |
| 52 | + // We should change the client to catch that error and return a `CheckError`. |
| 53 | + // Otherwise the checking process will stop. The idea is to process all checks |
| 54 | + // and return a final report. |
| 55 | + let response = Client::new(tracker_url.clone()) |
| 56 | + .announce(&QueryBuilder::with_default_values().with_info_hash(&info_hash).query()) |
| 57 | + .await; |
| 58 | + |
| 59 | + if let Ok(body) = response.bytes().await { |
| 60 | + if let Ok(_announce_response) = serde_bencode::from_bytes::<Announce>(&body) { |
| 61 | + Ok(()) |
| 62 | + } else { |
| 63 | + debug!("announce body {:#?}", body); |
| 64 | + Err(CheckError::HttpError { |
| 65 | + url: tracker_url.clone(), |
| 66 | + }) |
| 67 | + } |
| 68 | + } else { |
| 69 | + Err(CheckError::HttpError { |
| 70 | + url: tracker_url.clone(), |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +async fn check_http_scrape(url: &Url) -> Result<(), CheckError> { |
| 76 | + let info_hashes: Vec<String> = vec!["9c38422213e30bff212b30c360d26f9a02136422".to_string()]; // # DevSkim: ignore DS173237 |
| 77 | + let query = requests::scrape::Query::try_from(info_hashes).expect("a valid array of info-hashes is required"); |
| 78 | + |
| 79 | + // todo: HTTP request could panic.For example, if the server is not accessible. |
| 80 | + // We should change the client to catch that error and return a `CheckError`. |
| 81 | + // Otherwise the checking process will stop. The idea is to process all checks |
| 82 | + // and return a final report. |
| 83 | + let response = Client::new(url.clone()).scrape(&query).await; |
| 84 | + |
| 85 | + if let Ok(body) = response.bytes().await { |
| 86 | + if let Ok(_scrape_response) = scrape::Response::try_from_bencoded(&body) { |
| 87 | + Ok(()) |
| 88 | + } else { |
| 89 | + debug!("scrape body {:#?}", body); |
| 90 | + Err(CheckError::HttpError { url: url.clone() }) |
| 91 | + } |
| 92 | + } else { |
| 93 | + Err(CheckError::HttpError { url: url.clone() }) |
| 94 | + } |
| 95 | +} |
0 commit comments