@@ -61,6 +61,8 @@ use http::header::{HeaderMap, HeaderName, HeaderValue};
6161use hyper:: { service:: Service , Uri } ;
6262
6363use futures_util:: future:: TryFutureExt ;
64+ #[ cfg( feature = "rustls-base" ) ]
65+ use std:: convert:: TryFrom ;
6466use std:: { fmt, io, sync:: Arc } ;
6567use std:: {
6668 future:: Future ,
@@ -77,15 +79,13 @@ use native_tls::TlsConnector as NativeTlsConnector;
7779#[ cfg( feature = "tls" ) ]
7880use tokio_native_tls:: TlsConnector ;
7981#[ cfg( feature = "rustls-base" ) ]
80- use tokio_rustls:: TlsConnector ;
82+ use tokio_rustls:: { rustls :: ServerName , TlsConnector } ;
8183
8284use headers:: { authorization:: Credentials , Authorization , HeaderMapExt , ProxyAuthorization } ;
8385#[ cfg( feature = "openssl-tls" ) ]
8486use openssl:: ssl:: { SslConnector as OpenSslConnector , SslMethod } ;
8587#[ cfg( feature = "openssl-tls" ) ]
8688use tokio_openssl:: SslStream ;
87- #[ cfg( feature = "rustls-base" ) ]
88- use webpki:: DNSNameRef ;
8989
9090type BoxError = Box < dyn std:: error:: Error + Send + Sync > ;
9191
@@ -288,20 +288,27 @@ impl<C> ProxyConnector<C> {
288288 /// Create a new secured Proxies
289289 #[ cfg( feature = "rustls-base" ) ]
290290 pub fn new ( connector : C ) -> Result < Self , io:: Error > {
291- let mut config = tokio_rustls:: rustls:: ClientConfig :: new ( ) ;
292-
291+ let mut roots = tokio_rustls:: rustls:: RootCertStore :: empty ( ) ;
293292 #[ cfg( feature = "rustls" ) ]
294- {
295- config. root_store =
296- rustls_native_certs:: load_native_certs ( ) . map_err ( |( _store, io) | io) ?;
293+ for cert in rustls_native_certs:: load_native_certs ( ) ? {
294+ roots
295+ . add ( & tokio_rustls:: rustls:: Certificate ( cert. 0 ) )
296+ . map_err ( io_err) ?;
297297 }
298298
299299 #[ cfg( feature = "rustls-webpki" ) ]
300- {
301- config
302- . root_store
303- . add_server_trust_anchors ( & webpki_roots:: TLS_SERVER_ROOTS ) ;
304- }
300+ roots. add_server_trust_anchors ( webpki_roots:: TLS_SERVER_ROOTS . 0 . iter ( ) . map ( |ta| {
301+ tokio_rustls:: rustls:: OwnedTrustAnchor :: from_subject_spki_name_constraints (
302+ ta. subject ,
303+ ta. spki ,
304+ ta. name_constraints ,
305+ )
306+ } ) ) ;
307+
308+ let config = tokio_rustls:: rustls:: ClientConfig :: builder ( )
309+ . with_safe_defaults ( )
310+ . with_root_certificates ( roots)
311+ . with_no_client_auth ( ) ;
305312
306313 let cfg = Arc :: new ( config) ;
307314 let tls = TlsConnector :: from ( cfg) ;
@@ -442,7 +449,13 @@ where
442449 if let ( Some ( p) , Some ( host) ) = ( self . match_proxy ( & uri) , uri. host ( ) ) {
443450 if uri. scheme ( ) == Some ( & http:: uri:: Scheme :: HTTPS ) || p. force_connect {
444451 let host = host. to_owned ( ) ;
445- let port = uri. port_u16 ( ) . unwrap_or ( if uri. scheme ( ) == Some ( & http:: uri:: Scheme :: HTTP ) { 80 } else { 443 } ) ;
452+ let port =
453+ uri. port_u16 ( )
454+ . unwrap_or ( if uri. scheme ( ) == Some ( & http:: uri:: Scheme :: HTTP ) {
455+ 80
456+ } else {
457+ 443
458+ } ) ;
446459 let tunnel = tunnel:: new ( & host, port, & p. headers ) ;
447460 let connection =
448461 proxy_dst ( & uri, & p. uri ) . map ( |proxy_url| self . connector . call ( proxy_url) ) ;
@@ -470,11 +483,13 @@ where
470483
471484 #[ cfg( feature = "rustls-base" ) ]
472485 Some ( tls) => {
473- let dnsref =
474- mtry ! ( DNSNameRef :: try_from_ascii_str ( & host) . map_err( io_err) ) ;
486+ let server_name =
487+ mtry ! ( ServerName :: try_from ( host. as_str ( ) ) . map_err( io_err) ) ;
475488 let tls = TlsConnector :: from ( tls) ;
476- let secure_stream =
477- mtry ! ( tls. connect( dnsref, tunnel_stream) . await . map_err( io_err) ) ;
489+ let secure_stream = mtry ! ( tls
490+ . connect( server_name, tunnel_stream)
491+ . await
492+ . map_err( io_err) ) ;
478493
479494 Ok ( ProxyStream :: Secured ( secure_stream) )
480495 }
0 commit comments