1717//! let mut proxy = Proxy::new(Intercept::All, proxy_uri);
1818//! proxy.set_authorization(Authorization::basic("John Doe", "Agent1234"));
1919//! let connector = HttpConnector::new();
20- //! # #[cfg(not(any(feature = "tls", feature = "rustls-base")))]
20+ //! # #[cfg(not(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls" )))]
2121//! # let proxy_connector = ProxyConnector::from_proxy_unsecured(connector, proxy);
22- //! # #[cfg(any(feature = "tls", feature = "rustls-base"))]
22+ //! # #[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl" ))]
2323//! let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap();
2424//! proxy_connector
2525//! };
5252//! }
5353//! ```
5454
55- #![ deny ( missing_docs) ]
55+ #![ allow ( missing_docs) ]
5656
5757mod stream;
5858mod tunnel;
@@ -67,7 +67,8 @@ use std::{
6767 pin:: Pin ,
6868 task:: { Context , Poll } ,
6969} ;
70- use stream:: ProxyStream ;
70+
71+ pub use stream:: ProxyStream ;
7172use tokio:: io:: { AsyncRead , AsyncWrite } ;
7273
7374#[ cfg( feature = "tls" ) ]
@@ -77,7 +78,12 @@ use native_tls::TlsConnector as NativeTlsConnector;
7778use tokio_native_tls:: TlsConnector ;
7879#[ cfg( feature = "rustls-base" ) ]
7980use tokio_rustls:: TlsConnector ;
80- use headers:: { Authorization , authorization:: Credentials , HeaderMapExt , ProxyAuthorization } ;
81+
82+ use headers:: { authorization:: Credentials , Authorization , HeaderMapExt , ProxyAuthorization } ;
83+ #[ cfg( feature = "openssl-tls" ) ]
84+ use openssl:: ssl:: { SslConnector as OpenSslConnector , SslMethod } ;
85+ #[ cfg( feature = "openssl-tls" ) ]
86+ use tokio_openssl:: SslStream ;
8187#[ cfg( feature = "rustls-base" ) ]
8288use webpki:: DNSNameRef ;
8389
@@ -187,7 +193,7 @@ impl Proxy {
187193 }
188194
189195 /// Set `Proxy` authorization
190- pub fn set_authorization < C : Credentials + Clone > ( & mut self , credentials : Authorization :: < C > ) {
196+ pub fn set_authorization < C : Credentials + Clone > ( & mut self , credentials : Authorization < C > ) {
191197 match self . intercept {
192198 Intercept :: Http => {
193199 self . headers . typed_insert ( Authorization ( credentials. 0 ) ) ;
@@ -241,7 +247,10 @@ pub struct ProxyConnector<C> {
241247 #[ cfg( feature = "rustls-base" ) ]
242248 tls : Option < TlsConnector > ,
243249
244- #[ cfg( not( any( feature = "tls" , feature = "rustls-base" ) ) ) ]
250+ #[ cfg( feature = "openssl-tls" ) ]
251+ tls : Option < OpenSslConnector > ,
252+
253+ #[ cfg( not( any( feature = "tls" , feature = "rustls-base" , feature = "openssl-tls" ) ) ) ]
245254 tls : Option < ( ) > ,
246255}
247256
@@ -304,6 +313,20 @@ impl<C> ProxyConnector<C> {
304313 } )
305314 }
306315
316+ #[ allow( missing_docs) ]
317+ #[ cfg( feature = "openssl-tls" ) ]
318+ pub fn new ( connector : C ) -> Result < Self , io:: Error > {
319+ let builder = OpenSslConnector :: builder ( SslMethod :: tls ( ) )
320+ . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: Other , e) ) ?;
321+ let tls = builder. build ( ) ;
322+
323+ Ok ( ProxyConnector {
324+ proxies : Vec :: new ( ) ,
325+ connector : connector,
326+ tls : Some ( tls) ,
327+ } )
328+ }
329+
307330 /// Create a new unsecured Proxy
308331 pub fn unsecured ( connector : C ) -> Self {
309332 ProxyConnector {
@@ -314,7 +337,7 @@ impl<C> ProxyConnector<C> {
314337 }
315338
316339 /// Create a proxy connector and attach a particular proxy
317- #[ cfg( any( feature = "tls" , feature = "rustls-base" ) ) ]
340+ #[ cfg( any( feature = "tls" , feature = "rustls-base" , feature = "openssl-tls" ) ) ]
318341 pub fn from_proxy ( connector : C , proxy : Proxy ) -> Result < Self , io:: Error > {
319342 let mut c = ProxyConnector :: new ( connector) ?;
320343 c. proxies . push ( proxy) ;
@@ -349,6 +372,12 @@ impl<C> ProxyConnector<C> {
349372 self . tls = tls;
350373 }
351374
375+ /// Set or unset tls when tunneling
376+ #[ cfg( any( feature = "openssl-tls" ) ) ]
377+ pub fn set_tls ( & mut self , tls : Option < OpenSslConnector > ) {
378+ self . tls = tls;
379+ }
380+
352381 /// Get the current proxies
353382 pub fn proxies ( & self ) -> & [ Proxy ] {
354383 & self . proxies
@@ -450,7 +479,22 @@ where
450479 Ok ( ProxyStream :: Secured ( secure_stream) )
451480 }
452481
453- #[ cfg( not( any( feature = "tls" , feature = "rustls-base" ) ) ) ]
482+ #[ cfg( feature = "openssl-tls" ) ]
483+ Some ( tls) => {
484+ let config = tls. configure ( ) . map_err ( io_err) ?;
485+ let ssl = config. into_ssl ( & host) . map_err ( io_err) ?;
486+
487+ let mut stream = mtry ! ( SslStream :: new( ssl, tunnel_stream) ) ;
488+ mtry ! ( Pin :: new( & mut stream) . connect( ) . await . map_err( io_err) ) ;
489+
490+ Ok ( ProxyStream :: Secured ( stream) )
491+ }
492+
493+ #[ cfg( not( any(
494+ feature = "tls" ,
495+ feature = "rustls-base" ,
496+ feature = "openssl-tls"
497+ ) ) ) ]
454498 Some ( _) => panic ! ( "hyper-proxy was not built with TLS support" ) ,
455499
456500 None => Ok ( ProxyStream :: Regular ( tunnel_stream) ) ,
0 commit comments