@@ -12,7 +12,8 @@ use crate::console::clients::checker::printer::Printer;
12
12
use crate :: shared:: bit_torrent:: info_hash:: InfoHash ;
13
13
use crate :: shared:: bit_torrent:: tracker:: http:: client:: requests:: announce:: QueryBuilder ;
14
14
use crate :: shared:: bit_torrent:: tracker:: http:: client:: responses:: announce:: Announce ;
15
- use crate :: shared:: bit_torrent:: tracker:: http:: client:: Client ;
15
+ use crate :: shared:: bit_torrent:: tracker:: http:: client:: responses:: scrape;
16
+ use crate :: shared:: bit_torrent:: tracker:: http:: client:: { requests, Client } ;
16
17
17
18
pub struct Service {
18
19
pub ( crate ) config : Arc < Configuration > ,
@@ -58,9 +59,32 @@ impl Service {
58
59
self . console . println ( "HTTP trackers ..." ) ;
59
60
60
61
for http_tracker in & self . config . http_trackers {
61
- match self . check_http_tracker ( http_tracker) . await {
62
- Ok ( ( ) ) => check_results. push ( Ok ( ( ) ) ) ,
63
- Err ( err) => check_results. push ( Err ( err) ) ,
62
+ let colored_tracker_url = http_tracker. to_string ( ) . yellow ( ) ;
63
+
64
+ match self . check_http_announce ( http_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_http_scrape ( http_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
+ }
64
88
}
65
89
}
66
90
}
@@ -80,57 +104,81 @@ impl Service {
80
104
// todo:
81
105
// - Make announce request
82
106
// - Make scrape request
83
- self . console
84
- . println ( & format ! ( "{} - UDP tracker at udp://{:?} is OK (TODO)" , "✓" . green( ) , address) ) ;
107
+
108
+ let colored_address = address. to_string ( ) . yellow ( ) ;
109
+
110
+ self . console . println ( & format ! (
111
+ "{} - UDP tracker at udp://{} is OK ({})" ,
112
+ "✓" . green( ) ,
113
+ colored_address,
114
+ "TODO" . red( ) ,
115
+ ) ) ;
85
116
}
86
117
87
- async fn check_http_tracker ( & self , url : & Url ) -> Result < ( ) , CheckError > {
118
+ async fn check_http_announce ( & self , url : & Url ) -> Result < ( ) , CheckError > {
88
119
let info_hash_str = "9c38422213e30bff212b30c360d26f9a02136422" . to_string ( ) ; // # DevSkim: ignore DS173237
89
120
let info_hash = InfoHash :: from_str ( & info_hash_str) . expect ( "a valid info-hash is required" ) ;
90
121
91
- // Announce request
92
-
93
122
let response = Client :: new ( url. clone ( ) )
94
123
. announce ( & QueryBuilder :: with_default_values ( ) . with_info_hash ( & info_hash) . query ( ) )
95
124
. await ;
96
125
97
126
if let Ok ( body) = response. bytes ( ) . await {
98
127
if let Ok ( _announce_response) = serde_bencode:: from_bytes :: < Announce > ( & body) {
99
- self . console . println ( & format ! ( "{} - Announce at {} is OK" , "✓" . green( ) , url) ) ;
100
-
101
128
Ok ( ( ) )
102
129
} else {
103
- self . console . println ( & format ! ( "{} - Announce at {} failing" , "✗" . red( ) , url) ) ;
104
130
Err ( CheckError :: HttpError { url : url. clone ( ) } )
105
131
}
106
132
} else {
107
- self . console . println ( & format ! ( "{} - Announce at {} failing" , "✗" . red( ) , url) ) ;
108
133
Err ( CheckError :: HttpError { url : url. clone ( ) } )
109
134
}
135
+ }
136
+
137
+ async fn check_http_scrape ( & self , url : & Url ) -> Result < ( ) , CheckError > {
138
+ let info_hashes: Vec < String > = vec ! [ "9c38422213e30bff212b30c360d26f9a02136422" . to_string( ) ] ; // # DevSkim: ignore DS173237
139
+ let query = requests:: scrape:: Query :: try_from ( info_hashes) . expect ( "a valid array of info-hashes is required" ) ;
110
140
111
- // Scrape request
141
+ let response = Client :: new ( url . clone ( ) ) . scrape ( & query ) . await ;
112
142
113
- // todo
143
+ if let Ok ( body) = response. bytes ( ) . await {
144
+ if let Ok ( _scrape_response) = scrape:: Response :: try_from_bencoded ( & body) {
145
+ Ok ( ( ) )
146
+ } else {
147
+ Err ( CheckError :: HttpError { url : url. clone ( ) } )
148
+ }
149
+ } else {
150
+ Err ( CheckError :: HttpError { url : url. clone ( ) } )
151
+ }
114
152
}
115
153
116
154
async fn run_health_check ( & self , url : Url ) -> Result < ( ) , CheckError > {
117
155
let client = HttpClient :: builder ( ) . timeout ( Duration :: from_secs ( 5 ) ) . build ( ) . unwrap ( ) ;
118
156
157
+ let colored_url = url. to_string ( ) . yellow ( ) ;
158
+
119
159
match client. get ( url. clone ( ) ) . send ( ) . await {
120
160
Ok ( response) => {
121
161
if response. status ( ) . is_success ( ) {
122
162
self . console
123
- . println ( & format ! ( "{} - Health API at {} is OK" , "✓" . green( ) , url ) ) ;
163
+ . println ( & format ! ( "{} - Health API at {} is OK" , "✓" . green( ) , colored_url ) ) ;
124
164
Ok ( ( ) )
125
165
} else {
126
- self . console
127
- . eprintln ( & format ! ( "{} - Health API at {} failing: {:?}" , "✗" . red( ) , url, response) ) ;
166
+ self . console . eprintln ( & format ! (
167
+ "{} - Health API at {} is failing: {:?}" ,
168
+ "✗" . red( ) ,
169
+ colored_url,
170
+ response
171
+ ) ) ;
128
172
Err ( CheckError :: HealthCheckError { url } )
129
173
}
130
174
}
131
175
Err ( err) => {
132
- self . console
133
- . eprintln ( & format ! ( "{} - Health API at {} failing: {:?}" , "✗" . red( ) , url, err) ) ;
176
+ self . console . eprintln ( & format ! (
177
+ "{} - Health API at {} is failing: {:?}" ,
178
+ "✗" . red( ) ,
179
+ colored_url,
180
+ err
181
+ ) ) ;
134
182
Err ( CheckError :: HealthCheckError { url } )
135
183
}
136
184
}
0 commit comments