Skip to content

Commit 6a50c67

Browse files
committed
Now we can override domain certificate for TLS endpoint
1 parent 3bf5c2a commit 6a50c67

13 files changed

+62
-34
lines changed

src/app_configuration/proxy_pass_location_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct ProxyPassLocationConfig {
1717
pub modify_headers: Option<ModifyHttpHeadersSettings>,
1818
pub whitelisted_ip: WhiteListedIpList,
1919
pub remote_type: HttpType,
20+
pub domain_name: Option<String>,
2021
proxy_pass_to: ProxyPassTo,
2122
}
2223

@@ -27,6 +28,7 @@ impl ProxyPassLocationConfig {
2728
modify_headers: Option<ModifyHttpHeadersSettings>,
2829
whitelisted_ip: WhiteListedIpList,
2930
proxy_pass_to: ProxyPassTo,
31+
domain_name: Option<String>,
3032
remote_type: HttpType,
3133
) -> Self {
3234
Self {
@@ -36,6 +38,7 @@ impl ProxyPassLocationConfig {
3638
whitelisted_ip,
3739
proxy_pass_to,
3840
remote_type,
41+
domain_name,
3942
}
4043
}
4144
pub fn get_proxy_pass_to_as_string(&self) -> String {

src/http_client/connect_to_tls_endpoint.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,36 @@ use super::HttpClientError;
1313

1414
pub async fn connect_to_tls_endpoint(
1515
remote_host: &RemoteHost,
16+
domain_name: &Option<String>,
1617
) -> Result<SendRequest<Full<Bytes>>, HttpClientError> {
1718
use tokio_rustls::rustls::pki_types::ServerName;
1819

1920
let connect_result = TcpStream::connect(remote_host.get_host_port()).await;
2021

22+
println!(
23+
"Connecting to TLS remote host: {}",
24+
remote_host.get_host_port(),
25+
);
2126
match connect_result {
2227
Ok(tcp_stream) => {
2328
let config = tokio_rustls::rustls::ClientConfig::builder()
2429
.with_root_certificates(ROOT_CERT_STORE.clone())
2530
.with_no_client_auth();
2631

2732
let connector = tokio_rustls::TlsConnector::from(Arc::new(config));
33+
let domain = if let Some(domain_name) = domain_name {
34+
ServerName::try_from(domain_name.to_string()).unwrap()
35+
} else {
36+
ServerName::try_from(remote_host.get_host().to_string()).unwrap()
37+
};
2838

29-
let domain = ServerName::try_from(remote_host.get_host().to_string()).unwrap();
39+
println!("TLS Domain Name: {:?}", domain);
3040

31-
let tls_stream = connector.connect(domain, tcp_stream).await?;
41+
let tls_stream = connector
42+
.connect_with(domain, tcp_stream, |itm| {
43+
println!("Debugging: {:?}", itm.alpn_protocol());
44+
})
45+
.await?;
3246

3347
let io = TokioIo::new(tls_stream);
3448

@@ -38,6 +52,7 @@ pub async fn connect_to_tls_endpoint(
3852
Ok((mut sender, conn)) => {
3953
let host_port = remote_host.to_string();
4054
tokio::task::spawn(async move {
55+
println!("Connected to TLS remote host: {}", host_port,);
4156
if let Err(err) = conn.await {
4257
println!(
4358
"Https Connection to https://{} is failed: {:?}",
@@ -51,6 +66,11 @@ pub async fn connect_to_tls_endpoint(
5166
return Ok(sender);
5267
}
5368
Err(err) => {
69+
println!(
70+
"Can not connect to TLS remote host: {}. Err: {}",
71+
remote_host.get_host_port(),
72+
err
73+
);
5474
return Err(HttpClientError::InvalidHttp1HandShake(format!("{}", err)));
5575
}
5676
}

src/http_client/http1_client.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ pub struct Http1Client {
1515
}
1616

1717
impl Http1Client {
18-
pub async fn connect(remote_host: &RemoteHost) -> Result<Self, HttpClientError> {
19-
let send_request = Self::connect_to_http(remote_host).await?;
18+
pub async fn connect(
19+
remote_host: &RemoteHost,
20+
domain_name: &Option<String>,
21+
) -> Result<Self, HttpClientError> {
22+
let send_request = Self::connect_to_http(remote_host, domain_name).await?;
2023

2124
let result = Self {
2225
send_request,
@@ -44,9 +47,10 @@ impl Http1Client {
4447

4548
async fn connect_to_http(
4649
remote_host: &RemoteHost,
50+
domain_name: &Option<String>,
4751
) -> Result<SendRequest<Full<Bytes>>, HttpClientError> {
4852
if remote_host.is_https() {
49-
let future = super::connect_to_tls_endpoint(remote_host);
53+
let future = super::connect_to_tls_endpoint(remote_host, domain_name);
5054

5155
let result = tokio::time::timeout(HTTP_CLIENT_TIMEOUT, future).await;
5256

src/http_client/http2_client_connection.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/http_client/http_client.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,24 @@ impl HttpClient {
6060
pub async fn connect_to_http1(
6161
&mut self,
6262
remote_host: &RemoteHost,
63+
domain_name: &Option<String>,
6364
) -> Result<(), HttpClientError> {
64-
let client = Http1Client::connect(remote_host).await?;
65-
*self = Self::Http(client);
66-
Ok(())
65+
let connect_result = Http1Client::connect(remote_host, domain_name).await;
66+
67+
match connect_result {
68+
Ok(client) => {
69+
*self = Self::Http(client);
70+
Ok(())
71+
}
72+
Err(err) => {
73+
println!(
74+
"Can not connect to remote port: {}. Err:{:?}",
75+
remote_host.get_host_port(),
76+
err
77+
);
78+
Err(err)
79+
}
80+
}
6781
}
6882

6983
pub async fn connect_to_http1_over_ssh(

src/http_client/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ mod error;
66
pub use error::*;
77
mod connect_to_tls_endpoint;
88
pub use connect_to_tls_endpoint::*;
9-
//mod http1_client_connection;
109

11-
//pub use http1_client_connection::*;
1210
mod connect_to_http_over_ssh;
1311
pub use connect_to_http_over_ssh::*;
1412
mod connect_to_http2_endpoint;
1513
pub use connect_to_http2_endpoint::*;
16-
//mod http2_client_connection;
17-
//pub use http2_client_connection::*;
1814
mod http2_client;
1915
pub use http2_client::*;
2016
mod http_client;

src/http_content_source/remote_http_content_src.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl RemoteHttpContentSource {
4040
pub async fn connect_if_require(
4141
&mut self,
4242
app: &AppContext,
43+
domain_name: &Option<String>,
4344
debug: bool,
4445
) -> Result<(), ProxyPassError> {
4546
if self.http_client.has_connection() {
@@ -51,7 +52,7 @@ impl RemoteHttpContentSource {
5152
if debug {
5253
println!("Connecting to Http remote endpoint: {:?}", uri);
5354
}
54-
self.http_client.connect_to_http1(uri).await?;
55+
self.http_client.connect_to_http1(uri, domain_name).await?;
5556
}
5657

5758
HttpProxyPassRemoteEndpoint::Http2(uri) => {

src/http_proxy_pass/http_proxy_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl HttpProxyPass {
351351
if dispose_connection {
352352
remote_http_content_source.dispose();
353353
remote_http_content_source
354-
.connect_if_require(app, debug)
354+
.connect_if_require(app, &location.config.domain_name, debug)
355355
.await?;
356356
}
357357
}

src/http_proxy_pass/http_proxy_pass_content_source.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ impl HttpProxyPassContentSource {
2929
pub async fn connect_if_require(
3030
&mut self,
3131
app: &AppContext,
32+
domain_name: &Option<String>,
3233
debug: bool,
3334
) -> Result<(), ProxyPassError> {
3435
match self {
3536
Self::Http(remote_http_location) => {
36-
return remote_http_location.connect_if_require(app, debug).await;
37+
return remote_http_location
38+
.connect_if_require(app, domain_name, debug)
39+
.await;
3740
}
3841

3942
Self::LocalPath(_) => return Ok(()),

src/http_proxy_pass/proxy_pass_location.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ impl ProxyPassLocation {
4848
app: &AppContext,
4949
debug: bool,
5050
) -> Result<(), ProxyPassError> {
51-
self.content_source.connect_if_require(app, debug).await
51+
self.content_source
52+
.connect_if_require(app, &self.config.domain_name, debug)
53+
.await
5254
}
5355
}

0 commit comments

Comments
 (0)