Skip to content

Commit

Permalink
fix: upstream proxy for https connect
Browse files Browse the repository at this point in the history
Signed-off-by: zu1k <i@zu1k.com>
  • Loading branch information
zu1k committed Sep 13, 2022
1 parent 9eccc0b commit 14f594f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bytes = { version = "1", features = ["serde"] }
cfg-if = "1"
http = "0.2"
hyper = { version = "0.14", features = ["client", "http1", "server", "stream", "tcp"] }
hyper-proxy = { version = "0.9", default-features = false }
hyper-proxy = { version = "0.9" }
hyper-rustls = { version = "0.23" }
hyper-tls = { version = "0.5", optional = true }
log = "0.4"
Expand Down
7 changes: 6 additions & 1 deletion crates/core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use rcgen::RcgenError;
use std::io;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("invalid CA")]
Tls(#[from] RcgenError),
#[error("network error")]
Network(#[from] hyper::Error),
HyperError(#[from] hyper::Error),
#[error("TlsConnector error")]
TlsConnectorError(#[from] hyper_tls::native_tls::Error),
#[error("IO error")]
IO(#[from] io::Error),
#[error("unable to decode response body")]
Decode,
#[error("unknown error")]
Expand Down
16 changes: 8 additions & 8 deletions crates/core/src/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::Error;
use hyper::{client::HttpConnector, Client};
use hyper_proxy::{Proxy as UpstreamProxy, ProxyConnector};
use rustls::client::{ServerCertVerified, ServerCertVerifier};
Expand All @@ -19,16 +20,15 @@ pub enum HttpClient {
Https(Client<HttpsConnector<HttpConnector>>),
}

pub fn gen_client(upstream_proxy: Option<UpstreamProxy>) -> HttpClient {
pub fn gen_client(upstream_proxy: Option<UpstreamProxy>) -> Result<HttpClient, Error> {
cfg_if::cfg_if! {
if #[cfg(feature = "request-native-tls")] {
let https = {
let tls = TlsConnector::builder()
.danger_accept_invalid_certs(true)
.danger_accept_invalid_hostnames(true)
.disable_built_in_roots(true)
.build()
.unwrap();
.build()?;
let mut http = HttpConnector::new();
http.enforce_http(false);
HttpsConnector::from((http, tls.into()))
Expand All @@ -54,20 +54,20 @@ pub fn gen_client(upstream_proxy: Option<UpstreamProxy>) -> HttpClient {
}

if let Some(proxy) = upstream_proxy {
let connector = ProxyConnector::from_proxy_unsecured(https, proxy);
return HttpClient::Proxy(
let connector = ProxyConnector::from_proxy(https, proxy)?;
return Ok(HttpClient::Proxy(
Client::builder()
.http1_title_case_headers(true)
.http1_preserve_header_case(true)
.build(connector),
);
));
} else {
HttpClient::Https(
Ok(HttpClient::Https(
Client::builder()
.http1_title_case_headers(true)
.http1_preserve_header_case(true)
.build(https),
)
))
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
D: CustomContextData,
{
pub async fn start_proxy(self) -> Result<(), Error> {
let client = gen_client(self.upstream_proxy);
let client = gen_client(self.upstream_proxy)?;
let ca = Arc::new(self.ca);

let http_handler = Arc::new(self.handler);
Expand Down Expand Up @@ -85,6 +85,6 @@ where
.serve(make_service)
.with_graceful_shutdown(self.shutdown_signal)
.await
.map_err(|err| err.into())
.map_err(Error::from)
}
}

1 comment on commit 14f594f

@vercel
Copy link

@vercel vercel bot commented on 14f594f Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.