Skip to content

Commit 884ef87

Browse files
authored
Merge pull request #292 from blackbeam/tls-domain-override
Allow overriding domain used for TLS hostname verification
2 parents 11d621e + 7390b73 commit 884ef87

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/conn/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,10 @@ impl Conn {
550550
self.write_struct(&ssl_request).await?;
551551
let conn = self;
552552
let ssl_opts = conn.opts().ssl_opts().cloned().expect("unreachable");
553-
let domain = conn.opts().ip_or_hostname().into();
553+
let domain = ssl_opts
554+
.tls_hostname_override()
555+
.unwrap_or_else(|| conn.opts().ip_or_hostname())
556+
.into();
554557
conn.stream_mut()?.make_secure(domain, ssl_opts).await?;
555558
Ok(())
556559
} else {

src/opts/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ pub struct SslOpts {
195195
root_certs: Vec<PathOrBuf<'static>>,
196196
skip_domain_validation: bool,
197197
accept_invalid_certs: bool,
198+
tls_hostname_override: Option<Cow<'static, str>>,
198199
}
199200

200201
impl SslOpts {
@@ -228,6 +229,18 @@ impl SslOpts {
228229
self
229230
}
230231

232+
/// If set, will override the hostname used to verify the server's certificate.
233+
///
234+
/// This is useful when connecting to a server via a tunnel, where the server hostname is
235+
/// different from the hostname used to connect to the tunnel.
236+
pub fn with_danger_tls_hostname_override<T: Into<Cow<'static, str>>>(
237+
mut self,
238+
domain: Option<T>,
239+
) -> Self {
240+
self.tls_hostname_override = domain.map(Into::into);
241+
self
242+
}
243+
231244
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
232245
pub fn client_identity(&self) -> Option<&ClientIdentity> {
233246
self.client_identity.as_ref()
@@ -244,6 +257,10 @@ impl SslOpts {
244257
pub fn accept_invalid_certs(&self) -> bool {
245258
self.accept_invalid_certs
246259
}
260+
261+
pub fn tls_hostname_override(&self) -> Option<&str> {
262+
self.tls_hostname_override.as_deref()
263+
}
247264
}
248265

249266
/// Connection pool options.

0 commit comments

Comments
 (0)