Add support for connecting to HTTPS servers (#6)

* Add the `https` feature which condionally compiles the `hyper-tls` crate
 * Abstract HTTP(S) client building to a helper function with two versions: One which uses the `hyper-tls` `HttpsConnector` connector, and one which uses the default built-in `HttpConnector`
 * Update documentation on how to enable HTTPS support
This commit is contained in:
Casey Primozic 2022-03-17 19:05:07 -07:00 committed by GitHub
parent 4dee11dc35
commit 8e4efa3c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -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"] }
tokio = { version = "1", features = ["full"] }
[features]
https = ["hyper-tls"]
default = ["https"]

View File

@ -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<B>(
Ok(request)
}
#[cfg(feature = "https")]
fn build_client() -> Client<hyper_tls::HttpsConnector<HttpConnector<GaiResolver>>, hyper::Body> {
let https = hyper_tls::HttpsConnector::new();
Client::builder().build::<_, hyper::Body>(https)
}
#[cfg(not(feature = "https"))]
fn build_client() -> Client<HttpConnector<GaiResolver>, hyper::Body> {
Client::new()
}
pub async fn call(
client_ip: IpAddr,
forward_uri: &str,
@ -210,7 +228,7 @@ pub async fn call(
) -> Result<Response<Body>, 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)