@@ -2,39 +2,63 @@ use crate::logging::Logger;
22use crate :: retry;
33use std:: net:: TcpStream ;
44use std:: time:: { Duration , Instant } ;
5- pub fn run ( log : & Logger , targets : & [ String ] , cfg : & retry:: Config , timeout : Duration ,
6- http_status : u16 , insecure_tls : bool ) -> Result < ( ) , String > {
5+ pub fn run (
6+ log : & Logger ,
7+ targets : & [ String ] ,
8+ cfg : & retry:: Config ,
9+ timeout : Duration ,
10+ http_status : u16 ,
11+ insecure_tls : bool ,
12+ ) -> Result < ( ) , String > {
713 if targets. is_empty ( ) {
814 return Err ( "at least one --target is required" . into ( ) ) ;
915 }
1016 let deadline = Instant :: now ( ) + timeout;
1117 for target in targets {
1218 log. info ( "waiting for target" , & [ ( "target" , target) ] ) ;
1319 let result = retry:: do_retry ( cfg, Some ( deadline) , |attempt| {
14- log. debug ( "attempt" , & [ ( "target" , target) , ( "attempt" , & format ! ( "{}" , attempt + 1 ) ) ] ) ;
20+ log. debug (
21+ "attempt" ,
22+ & [ ( "target" , target) , ( "attempt" , & format ! ( "{}" , attempt + 1 ) ) ] ,
23+ ) ;
1524 check_target ( target, http_status, insecure_tls, timeout)
1625 } ) ;
1726 if let Some ( e) = result. err {
1827 log. error ( "target not reachable" , & [ ( "target" , target) , ( "error" , & e) ] ) ;
1928 return Err ( format ! ( "target {} not reachable: {}" , target, e) ) ;
2029 }
21- log. info ( "target is reachable" , & [ ( "target" , target) , ( "attempts" , & format ! ( "{}" , result. attempt + 1 ) ) ] ) ;
30+ log. info (
31+ "target is reachable" ,
32+ & [
33+ ( "target" , target) ,
34+ ( "attempts" , & format ! ( "{}" , result. attempt + 1 ) ) ,
35+ ] ,
36+ ) ;
2237 }
2338 log. info ( "all targets reachable" , & [ ] ) ;
2439 Ok ( ( ) )
2540}
26- fn check_target ( target : & str , expected_status : u16 , insecure_tls : bool , timeout : Duration ) -> Result < ( ) , String > {
41+ fn check_target (
42+ target : & str ,
43+ expected_status : u16 ,
44+ insecure_tls : bool ,
45+ timeout : Duration ,
46+ ) -> Result < ( ) , String > {
2747 if let Some ( addr) = target. strip_prefix ( "tcp://" ) {
2848 check_tcp ( addr, timeout)
2949 } else if target. starts_with ( "http://" ) || target. starts_with ( "https://" ) {
3050 check_http ( target, expected_status, insecure_tls, timeout)
3151 } else {
32- Err ( format ! ( "unsupported target scheme in {:?}; use tcp://, http://, or https://" , target) )
52+ Err ( format ! (
53+ "unsupported target scheme in {:?}; use tcp://, http://, or https://" ,
54+ target
55+ ) )
3356 }
3457}
3558fn check_tcp ( addr : & str , timeout : Duration ) -> Result < ( ) , String > {
3659 let per_req = timeout. min ( Duration :: from_secs ( 5 ) ) ;
37- let addrs: Vec < std:: net:: SocketAddr > = addr. to_socket_addrs_safe ( )
60+ let addrs: Vec < std:: net:: SocketAddr > = addr
61+ . to_socket_addrs_safe ( )
3862 . map_err ( |e| format ! ( "resolving {}: {}" , addr, e) ) ?;
3963 if addrs. is_empty ( ) {
4064 return Err ( format ! ( "could not resolve {}" , addr) ) ;
@@ -43,13 +67,19 @@ fn check_tcp(addr: &str, timeout: Duration) -> Result<(), String> {
4367 . map_err ( |e| format ! ( "tcp dial {}: {}" , addr, e) ) ?;
4468 Ok ( ( ) )
4569}
46- fn check_http ( url : & str , expected_status : u16 , insecure_tls : bool , timeout : Duration ) -> Result < ( ) , String > {
70+ fn check_http (
71+ url : & str ,
72+ expected_status : u16 ,
73+ insecure_tls : bool ,
74+ timeout : Duration ,
75+ ) -> Result < ( ) , String > {
4776 let per_req = timeout. min ( Duration :: from_secs ( 5 ) ) ;
4877 let agent = if insecure_tls {
4978 use std:: sync:: Arc ;
5079 let crypto_provider = rustls:: crypto:: ring:: default_provider ( ) ;
5180 let tls_config = rustls:: ClientConfig :: builder_with_provider ( Arc :: new ( crypto_provider) )
52- . with_safe_default_protocol_versions ( ) . unwrap ( )
81+ . with_safe_default_protocol_versions ( )
82+ . unwrap ( )
5383 . dangerous ( )
5484 . with_custom_certificate_verifier ( Arc :: new ( NoVerifier ) )
5585 . with_no_client_auth ( ) ;
@@ -60,11 +90,16 @@ fn check_http(url: &str, expected_status: u16, insecure_tls: bool, timeout: Dura
6090 } else {
6191 ureq:: AgentBuilder :: new ( ) . timeout ( per_req) . build ( )
6292 } ;
63- let resp = agent. get ( url) . call ( )
93+ let resp = agent
94+ . get ( url)
95+ . call ( )
6496 . map_err ( |e| format ! ( "http request to {}: {}" , url, e) ) ?;
6597 let status = resp. status ( ) ;
6698 if status != expected_status {
67- return Err ( format ! ( "http {} returned status {}, expected {}" , url, status, expected_status) ) ;
99+ return Err ( format ! (
100+ "http {} returned status {}, expected {}" ,
101+ url, status, expected_status
102+ ) ) ;
68103 }
69104 Ok ( ( ) )
70105}
@@ -81,25 +116,34 @@ impl ToSocketAddrs for str {
81116pub struct NoVerifier ;
82117impl rustls:: client:: danger:: ServerCertVerifier for NoVerifier {
83118 fn verify_server_cert (
84- & self , _: & rustls:: pki_types:: CertificateDer < ' _ > , _: & [ rustls:: pki_types:: CertificateDer < ' _ > ] ,
85- _: & rustls:: pki_types:: ServerName < ' _ > , _: & [ u8 ] , _: rustls:: pki_types:: UnixTime ,
119+ & self ,
120+ _: & rustls:: pki_types:: CertificateDer < ' _ > ,
121+ _: & [ rustls:: pki_types:: CertificateDer < ' _ > ] ,
122+ _: & rustls:: pki_types:: ServerName < ' _ > ,
123+ _: & [ u8 ] ,
124+ _: rustls:: pki_types:: UnixTime ,
86125 ) -> Result < rustls:: client:: danger:: ServerCertVerified , rustls:: Error > {
87126 Ok ( rustls:: client:: danger:: ServerCertVerified :: assertion ( ) )
88127 }
89128 fn verify_tls12_signature (
90- & self , _: & [ u8 ] , _: & rustls:: pki_types:: CertificateDer < ' _ > ,
129+ & self ,
130+ _: & [ u8 ] ,
131+ _: & rustls:: pki_types:: CertificateDer < ' _ > ,
91132 _: & rustls:: DigitallySignedStruct ,
92133 ) -> Result < rustls:: client:: danger:: HandshakeSignatureValid , rustls:: Error > {
93134 Ok ( rustls:: client:: danger:: HandshakeSignatureValid :: assertion ( ) )
94135 }
95136 fn verify_tls13_signature (
96- & self , _: & [ u8 ] , _: & rustls:: pki_types:: CertificateDer < ' _ > ,
137+ & self ,
138+ _: & [ u8 ] ,
139+ _: & rustls:: pki_types:: CertificateDer < ' _ > ,
97140 _: & rustls:: DigitallySignedStruct ,
98141 ) -> Result < rustls:: client:: danger:: HandshakeSignatureValid , rustls:: Error > {
99142 Ok ( rustls:: client:: danger:: HandshakeSignatureValid :: assertion ( ) )
100143 }
101144 fn supported_verify_schemes ( & self ) -> Vec < rustls:: SignatureScheme > {
102145 rustls:: crypto:: ring:: default_provider ( )
103- . signature_verification_algorithms . supported_schemes ( )
146+ . signature_verification_algorithms
147+ . supported_schemes ( )
104148 }
105149}
0 commit comments