11use std:: net:: SocketAddr ;
2- use std:: str:: FromStr ;
32use std:: sync:: Arc ;
43use std:: time:: Duration ;
54
65use colored:: Colorize ;
7- use log:: debug;
86use reqwest:: { Client as HttpClient , Url } ;
97
108use super :: checks;
119use super :: config:: Configuration ;
1210use super :: console:: Console ;
1311use crate :: console:: clients:: checker:: printer:: Printer ;
14- use crate :: shared:: bit_torrent:: info_hash:: InfoHash ;
15- use crate :: shared:: bit_torrent:: tracker:: http:: client:: requests:: announce:: QueryBuilder ;
16- use crate :: shared:: bit_torrent:: tracker:: http:: client:: responses:: announce:: Announce ;
17- use crate :: shared:: bit_torrent:: tracker:: http:: client:: responses:: scrape;
18- use crate :: shared:: bit_torrent:: tracker:: http:: client:: { requests, Client } ;
1912
2013pub struct Service {
2114 pub ( crate ) config : Arc < Configuration > ,
@@ -42,47 +35,13 @@ impl Service {
4235
4336 checks:: udp:: run ( & self . config . udp_trackers , & self . console , & mut check_results) . await ;
4437
45- self . check_http_trackers ( & mut check_results) . await ;
38+ checks :: http :: run ( & self . config . http_trackers , & self . console , & mut check_results) . await ;
4639
4740 self . run_health_checks ( & mut check_results) . await ;
4841
4942 check_results
5043 }
5144
52- async fn check_http_trackers ( & self , check_results : & mut Vec < CheckResult > ) {
53- self . console . println ( "HTTP trackers ..." ) ;
54-
55- for http_tracker in & self . config . http_trackers {
56- let colored_tracker_url = http_tracker. to_string ( ) . yellow ( ) ;
57-
58- match self . check_http_announce ( http_tracker) . await {
59- Ok ( ( ) ) => {
60- check_results. push ( Ok ( ( ) ) ) ;
61- self . console
62- . println ( & format ! ( "{} - Announce at {} is OK" , "✓" . green( ) , colored_tracker_url) ) ;
63- }
64- Err ( err) => {
65- check_results. push ( Err ( err) ) ;
66- self . console
67- . println ( & format ! ( "{} - Announce at {} is failing" , "✗" . red( ) , colored_tracker_url) ) ;
68- }
69- }
70-
71- match self . check_http_scrape ( http_tracker) . await {
72- Ok ( ( ) ) => {
73- check_results. push ( Ok ( ( ) ) ) ;
74- self . console
75- . println ( & format ! ( "{} - Scrape at {} is OK" , "✓" . green( ) , colored_tracker_url) ) ;
76- }
77- Err ( err) => {
78- check_results. push ( Err ( err) ) ;
79- self . console
80- . println ( & format ! ( "{} - Scrape at {} is failing" , "✗" . red( ) , colored_tracker_url) ) ;
81- }
82- }
83- }
84- }
85-
8645 async fn run_health_checks ( & self , check_results : & mut Vec < CheckResult > ) {
8746 self . console . println ( "Health checks ..." ) ;
8847
@@ -94,56 +53,6 @@ impl Service {
9453 }
9554 }
9655
97- async fn check_http_announce ( & self , tracker_url : & Url ) -> Result < ( ) , CheckError > {
98- let info_hash_str = "9c38422213e30bff212b30c360d26f9a02136422" . to_string ( ) ; // # DevSkim: ignore DS173237
99- let info_hash = InfoHash :: from_str ( & info_hash_str) . expect ( "a valid info-hash is required" ) ;
100-
101- // todo: HTTP request could panic.For example, if the server is not accessible.
102- // We should change the client to catch that error and return a `CheckError`.
103- // Otherwise the checking process will stop. The idea is to process all checks
104- // and return a final report.
105- let response = Client :: new ( tracker_url. clone ( ) )
106- . announce ( & QueryBuilder :: with_default_values ( ) . with_info_hash ( & info_hash) . query ( ) )
107- . await ;
108-
109- if let Ok ( body) = response. bytes ( ) . await {
110- if let Ok ( _announce_response) = serde_bencode:: from_bytes :: < Announce > ( & body) {
111- Ok ( ( ) )
112- } else {
113- debug ! ( "announce body {:#?}" , body) ;
114- Err ( CheckError :: HttpError {
115- url : tracker_url. clone ( ) ,
116- } )
117- }
118- } else {
119- Err ( CheckError :: HttpError {
120- url : tracker_url. clone ( ) ,
121- } )
122- }
123- }
124-
125- async fn check_http_scrape ( & self , url : & Url ) -> Result < ( ) , CheckError > {
126- let info_hashes: Vec < String > = vec ! [ "9c38422213e30bff212b30c360d26f9a02136422" . to_string( ) ] ; // # DevSkim: ignore DS173237
127- let query = requests:: scrape:: Query :: try_from ( info_hashes) . expect ( "a valid array of info-hashes is required" ) ;
128-
129- // todo: HTTP request could panic.For example, if the server is not accessible.
130- // We should change the client to catch that error and return a `CheckError`.
131- // Otherwise the checking process will stop. The idea is to process all checks
132- // and return a final report.
133- let response = Client :: new ( url. clone ( ) ) . scrape ( & query) . await ;
134-
135- if let Ok ( body) = response. bytes ( ) . await {
136- if let Ok ( _scrape_response) = scrape:: Response :: try_from_bencoded ( & body) {
137- Ok ( ( ) )
138- } else {
139- debug ! ( "scrape body {:#?}" , body) ;
140- Err ( CheckError :: HttpError { url : url. clone ( ) } )
141- }
142- } else {
143- Err ( CheckError :: HttpError { url : url. clone ( ) } )
144- }
145- }
146-
14756 async fn run_health_check ( & self , url : Url ) -> Result < ( ) , CheckError > {
14857 let client = HttpClient :: builder ( ) . timeout ( Duration :: from_secs ( 5 ) ) . build ( ) . unwrap ( ) ;
14958
0 commit comments