diff --git a/Cargo.toml b/Cargo.toml index d1384e0..e87d1c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,11 @@ include = [ hyper = { version = "0.14", features = ["full"] } lazy_static = "1.4" unicase = "2.6" +hyper-tls = { version = "0.5", optional = true } [dev-dependencies] -tokio = { version = "1", features = ["full"] } \ No newline at end of file +tokio = { version = "1", features = ["full"] } + +[features] +https = ["hyper-tls"] +default = ["https"] diff --git a/src/lib.rs b/src/lib.rs index 2b6afdb..d3ff3b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,12 @@ //! tokio = { version = "1", features = ["full"] } //! ``` //! +//! To enable support for connecting to downstream HTTPS servers, enable the `https` feature: +//! +//! ```toml +//! hyper-reverse-proxy = { version = "0.4", features = ["https"] } +//! ``` +//! //! The following example will set up a reverse proxy listening on `127.0.0.1:13900`, //! and will proxy these calls: //! @@ -90,6 +96,7 @@ //! ``` //! +use hyper::client::{connect::dns::GaiResolver, HttpConnector}; use hyper::header::{HeaderMap, HeaderValue}; use hyper::http::header::{InvalidHeaderValue, ToStrError}; use hyper::http::uri::InvalidUri; @@ -203,6 +210,17 @@ fn create_proxied_request( Ok(request) } +#[cfg(feature = "https")] +fn build_client() -> Client>, hyper::Body> { + let https = hyper_tls::HttpsConnector::new(); + Client::builder().build::<_, hyper::Body>(https) +} + +#[cfg(not(feature = "https"))] +fn build_client() -> Client, hyper::Body> { + Client::new() +} + pub async fn call( client_ip: IpAddr, forward_uri: &str, @@ -210,7 +228,7 @@ pub async fn call( ) -> Result, ProxyError> { let proxied_request = create_proxied_request(client_ip, &forward_uri, request)?; - let client = Client::new(); + let client = build_client(); let response = client.request(proxied_request).await?; let proxied_response = create_proxied_response(response); Ok(proxied_response)