1- use std:: { fmt, sync:: Arc } ;
1+ use std:: { fmt, fmt :: Write as _ , sync:: Arc } ;
22
33use color_eyre:: eyre:: { OptionExt as _, WrapErr as _, eyre} ;
44
@@ -21,7 +21,7 @@ impl TryFrom<&str> for ProxyType {
2121 type Error = color_eyre:: Report ;
2222
2323 fn try_from ( string : & str ) -> color_eyre:: Result < Self > {
24- match string {
24+ match string. to_ascii_lowercase ( ) . as_str ( ) {
2525 "http" | "https" => Ok ( Self :: Http ) ,
2626 "socks4" => Ok ( Self :: Socks4 ) ,
2727 "socks5" => Ok ( Self :: Socks5 ) ,
@@ -60,24 +60,34 @@ pub struct Proxy {
6060 pub exit_ip : Option < String > ,
6161}
6262
63- impl Proxy {
64- pub async fn check (
65- & mut self ,
66- config : Arc < Config > ,
67- ) -> color_eyre:: Result < ( ) > {
68- let mut proxy = reqwest:: Proxy :: all ( format ! (
63+ impl TryFrom < & mut Proxy > for reqwest:: Proxy {
64+ type Error = color_eyre:: Report ;
65+
66+ fn try_from ( value : & mut Proxy ) -> Result < Self , Self :: Error > {
67+ let proxy = Self :: all ( format ! (
6968 "{}://{}:{}" ,
70- self . protocol, self . host, self . port
69+ value . protocol, value . host, value . port
7170 ) )
7271 . wrap_err ( "failed to create reqwest::Proxy" ) ?;
72+
7373 if let ( Some ( username) , Some ( password) ) =
74- ( self . username . as_ref ( ) , self . password . as_ref ( ) )
74+ ( value . username . as_ref ( ) , value . password . as_ref ( ) )
7575 {
76- proxy = proxy. basic_auth ( username, password) ;
76+ Ok ( proxy. basic_auth ( username, password) )
77+ } else {
78+ Ok ( proxy)
7779 }
80+ }
81+ }
82+
83+ impl Proxy {
84+ pub async fn check (
85+ & mut self ,
86+ config : Arc < Config > ,
87+ ) -> color_eyre:: Result < ( ) > {
7888 let client = reqwest:: Client :: builder ( )
7989 . user_agent ( USER_AGENT )
80- . proxy ( proxy )
90+ . proxy ( self . try_into ( ) ? )
8191 . timeout ( config. timeout )
8292 . pool_max_idle_per_host ( 0 )
8393 . tcp_keepalive ( None )
@@ -134,20 +144,14 @@ impl Proxy {
134144 pub fn as_str ( & self , include_protocol : bool ) -> String {
135145 let mut s = String :: new ( ) ;
136146 if include_protocol {
137- s. push_str ( & self . protocol . to_string ( ) ) ;
138- s. push_str ( "://" ) ;
147+ write ! ( & mut s, "{}://" , self . protocol) . unwrap ( ) ;
139148 }
140149 if let ( Some ( username) , Some ( password) ) =
141150 ( self . username . as_ref ( ) , self . password . as_ref ( ) )
142151 {
143- s. push_str ( username) ;
144- s. push ( ':' ) ;
145- s. push_str ( password) ;
146- s. push ( '@' ) ;
152+ write ! ( & mut s, "{username}:{password}@" ) . unwrap ( ) ;
147153 }
148- s. push_str ( & self . host ) ;
149- s. push ( ':' ) ;
150- s. push_str ( & self . port . to_string ( ) ) ;
154+ write ! ( & mut s, "{}:{}" , self . host, self . port) . unwrap ( ) ;
151155 s
152156 }
153157}
0 commit comments