diff --git a/rpxy-bin/Cargo.toml b/rpxy-bin/Cargo.toml index 50fb549..1512b7d 100644 --- a/rpxy-bin/Cargo.toml +++ b/rpxy-bin/Cargo.toml @@ -21,6 +21,7 @@ native-roots = ["rpxy-lib/native-roots"] [dependencies] rpxy-lib = { path = "../rpxy-lib/", default-features = false, features = [ "sticky-cookie", + "native-tls-backend", ] } anyhow = "1.0.75" diff --git a/rpxy-lib/Cargo.toml b/rpxy-lib/Cargo.toml index 67f217a..7c6cf24 100644 --- a/rpxy-lib/Cargo.toml +++ b/rpxy-lib/Cargo.toml @@ -12,7 +12,7 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["http3-quinn", "sticky-cookie", "cache"] +default = ["http3-quinn", "sticky-cookie", "cache", "native-tls-backend"] http3-quinn = ["socket2", "quinn", "h3", "h3-quinn"] http3-s2n = [ "h3", @@ -22,6 +22,8 @@ http3-s2n = [ "s2n-quic-h3", ] sticky-cookie = ["base64", "sha2", "chrono"] +native-tls-backend = ["hyper-tls"] +rustls-backend = [] cache = [] #"http-cache-semantics", "lru"] native-roots = [] #"hyper-rustls/native-tokio"] @@ -53,8 +55,8 @@ hyper-util = { version = "0.1.1", features = ["full"] } futures-util = { version = "0.3.29", default-features = false } futures-channel = { version = "0.3.29", default-features = false } -# http client -hyper-tls = { version = "0.6.0", features = ["alpn"] } +# http client for upstream +hyper-tls = { version = "0.6.0", features = ["alpn"], optional = true } # hyper-rustls = { version = "0.24.2", default-features = false, features = [ # "tokio-runtime", # "webpki-tokio", diff --git a/rpxy-lib/src/forwarder/client.rs b/rpxy-lib/src/forwarder/client.rs index c587be9..3d0f995 100644 --- a/rpxy-lib/src/forwarder/client.rs +++ b/rpxy-lib/src/forwarder/client.rs @@ -5,11 +5,11 @@ use crate::{ body::{wrap_incoming_body_response, IncomingOr}, rt::LocalExecutor, }, + log::*, }; use async_trait::async_trait; use http::{Request, Response, Version}; use hyper::body::Body; -use hyper_tls::HttpsConnector; use hyper_util::client::legacy::{ connect::{Connect, HttpConnector}, Client, @@ -66,8 +66,38 @@ where } } +#[cfg(not(any(feature = "native-tls-backend", feature = "rustls-backend")))] +impl Forwarder +where + B: Body + Send + Unpin + 'static, + ::Data: Send, + ::Error: Into>, +{ + /// Build inner client with http + pub fn try_new(_globals: &Arc) -> RpxyResult { + warn!( + " +-------------------------------------------------------------------------------------------------- +Request forwarder is working without TLS support!!! +We recommend to use this just for testing. +Please enable native-tls-backend or rustls-backend feature to enable TLS support. +--------------------------------------------------------------------------------------------------" + ); + let executor = LocalExecutor::new(_globals.runtime_handle.clone()); + let mut http = HttpConnector::new(); + http.set_reuse_address(true); + let inner = Client::builder(executor).build::<_, B>(http); + + Ok(Self { + inner, + inner_h2: inner.clone(), + }) + } +} + +#[cfg(feature = "native-tls-backend")] /// Build forwarder with hyper-tls (native-tls) -impl Forwarder, B1> +impl Forwarder, B1> where B1: Body + Send + Unpin + 'static, ::Data: Send, @@ -76,6 +106,7 @@ where /// Build forwarder pub async fn try_new(_globals: &Arc) -> RpxyResult { // build hyper client with hyper-tls + info!("Native TLS support is enabled for the connection to backend applications"); let executor = LocalExecutor::new(_globals.runtime_handle.clone()); let try_build_connector = |alpns: &[&str]| { @@ -87,7 +118,7 @@ where let mut http = HttpConnector::new(); http.enforce_http(false); http.set_reuse_address(true); - HttpsConnector::from((http, tls.into())) + hyper_tls::HttpsConnector::from((http, tls.into())) }) }; @@ -99,6 +130,27 @@ where .http2_only(true) .build::<_, B1>(connector_h2); + // #[cfg(feature = "cache")] + // { + // let cache = RpxyCache::new(_globals).await; + // Self { inner, inner_h2, cache } + // } + // #[cfg(not(feature = "cache"))] + Ok(Self { inner, inner_h2 }) + } +} + +#[cfg(feature = "rustls-backend")] +/// Build forwarder with hyper-rustls (rustls) +impl Forwarder, B1> +where + B1: Body + Send + Unpin + 'static, + ::Data: Send, + ::Error: Into>, +{ + /// Build forwarder + pub async fn try_new(_globals: &Arc) -> RpxyResult { + todo!("Not implemented yet. Please use native-tls-backend feature for now."); // #[cfg(feature = "native-roots")] // let builder = hyper_rustls::HttpsConnectorBuilder::new().with_native_roots(); // #[cfg(feature = "native-roots")] @@ -118,13 +170,5 @@ where // let inner = Client::builder().build::<_, Body>(connector); // let inner_h2 = Client::builder().http2_only(true).build::<_, Body>(connector_h2); - - // #[cfg(feature = "cache")] - // { - // let cache = RpxyCache::new(_globals).await; - // Self { inner, inner_h2, cache } - // } - // #[cfg(not(feature = "cache"))] - Ok(Self { inner, inner_h2 }) } }