@@ -61,6 +61,8 @@ use http::header::{HeaderMap, HeaderName, HeaderValue};
61
61
use hyper:: { service:: Service , Uri } ;
62
62
63
63
use futures_util:: future:: TryFutureExt ;
64
+ #[ cfg( feature = "rustls-base" ) ]
65
+ use std:: convert:: TryFrom ;
64
66
use std:: { fmt, io, sync:: Arc } ;
65
67
use std:: {
66
68
future:: Future ,
@@ -77,15 +79,13 @@ use native_tls::TlsConnector as NativeTlsConnector;
77
79
#[ cfg( feature = "tls" ) ]
78
80
use tokio_native_tls:: TlsConnector ;
79
81
#[ cfg( feature = "rustls-base" ) ]
80
- use tokio_rustls:: TlsConnector ;
82
+ use tokio_rustls:: { rustls :: ServerName , TlsConnector } ;
81
83
82
84
use headers:: { authorization:: Credentials , Authorization , HeaderMapExt , ProxyAuthorization } ;
83
85
#[ cfg( feature = "openssl-tls" ) ]
84
86
use openssl:: ssl:: { SslConnector as OpenSslConnector , SslMethod } ;
85
87
#[ cfg( feature = "openssl-tls" ) ]
86
88
use tokio_openssl:: SslStream ;
87
- #[ cfg( feature = "rustls-base" ) ]
88
- use webpki:: DNSNameRef ;
89
89
90
90
type BoxError = Box < dyn std:: error:: Error + Send + Sync > ;
91
91
@@ -288,20 +288,27 @@ impl<C> ProxyConnector<C> {
288
288
/// Create a new secured Proxies
289
289
#[ cfg( feature = "rustls-base" ) ]
290
290
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 ( ) ;
293
292
#[ 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) ?;
297
297
}
298
298
299
299
#[ 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 ( ) ;
305
312
306
313
let cfg = Arc :: new ( config) ;
307
314
let tls = TlsConnector :: from ( cfg) ;
@@ -442,7 +449,13 @@ where
442
449
if let ( Some ( p) , Some ( host) ) = ( self . match_proxy ( & uri) , uri. host ( ) ) {
443
450
if uri. scheme ( ) == Some ( & http:: uri:: Scheme :: HTTPS ) || p. force_connect {
444
451
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
+ } ) ;
446
459
let tunnel = tunnel:: new ( & host, port, & p. headers ) ;
447
460
let connection =
448
461
proxy_dst ( & uri, & p. uri ) . map ( |proxy_url| self . connector . call ( proxy_url) ) ;
@@ -470,11 +483,13 @@ where
470
483
471
484
#[ cfg( feature = "rustls-base" ) ]
472
485
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) ) ;
475
488
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) ) ;
478
493
479
494
Ok ( ProxyStream :: Secured ( secure_stream) )
480
495
}
0 commit comments