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:
parent
4dee11dc35
commit
8e4efa3c10
@ -22,6 +22,11 @@ include = [
|
|||||||
hyper = { version = "0.14", features = ["full"] }
|
hyper = { version = "0.14", features = ["full"] }
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
unicase = "2.6"
|
unicase = "2.6"
|
||||||
|
hyper-tls = { version = "0.5", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
https = ["hyper-tls"]
|
||||||
|
default = ["https"]
|
||||||
|
20
src/lib.rs
20
src/lib.rs
@ -22,6 +22,12 @@
|
|||||||
//! tokio = { version = "1", features = ["full"] }
|
//! 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`,
|
//! The following example will set up a reverse proxy listening on `127.0.0.1:13900`,
|
||||||
//! and will proxy these calls:
|
//! and will proxy these calls:
|
||||||
//!
|
//!
|
||||||
@ -90,6 +96,7 @@
|
|||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
|
||||||
|
use hyper::client::{connect::dns::GaiResolver, HttpConnector};
|
||||||
use hyper::header::{HeaderMap, HeaderValue};
|
use hyper::header::{HeaderMap, HeaderValue};
|
||||||
use hyper::http::header::{InvalidHeaderValue, ToStrError};
|
use hyper::http::header::{InvalidHeaderValue, ToStrError};
|
||||||
use hyper::http::uri::InvalidUri;
|
use hyper::http::uri::InvalidUri;
|
||||||
@ -203,6 +210,17 @@ fn create_proxied_request<B>(
|
|||||||
Ok(request)
|
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(
|
pub async fn call(
|
||||||
client_ip: IpAddr,
|
client_ip: IpAddr,
|
||||||
forward_uri: &str,
|
forward_uri: &str,
|
||||||
@ -210,7 +228,7 @@ pub async fn call(
|
|||||||
) -> Result<Response<Body>, ProxyError> {
|
) -> Result<Response<Body>, ProxyError> {
|
||||||
let proxied_request = create_proxied_request(client_ip, &forward_uri, request)?;
|
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 response = client.request(proxied_request).await?;
|
||||||
let proxied_response = create_proxied_response(response);
|
let proxied_response = create_proxied_response(response);
|
||||||
Ok(proxied_response)
|
Ok(proxied_response)
|
||||||
|
Loading…
Reference in New Issue
Block a user