Skip to content

Commit

Permalink
Add option to configure TLS server name indication (SNI) (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvenix committed Nov 9, 2022
1 parent f11e958 commit e302f75
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/async_impl/client.rs
Expand Up @@ -83,6 +83,8 @@ struct Config {
hostname_verification: bool,
#[cfg(feature = "__tls")]
certs_verification: bool,
#[cfg(feature = "__tls")]
tls_sni: bool,
connect_timeout: Option<Duration>,
connection_verbose: bool,
pool_idle_timeout: Option<Duration>,
Expand Down Expand Up @@ -150,6 +152,8 @@ impl ClientBuilder {
hostname_verification: true,
#[cfg(feature = "__tls")]
certs_verification: true,
#[cfg(feature = "__tls")]
tls_sni: true,
connect_timeout: None,
connection_verbose: false,
pool_idle_timeout: Some(Duration::from_secs(90)),
Expand Down Expand Up @@ -268,6 +272,8 @@ impl ClientBuilder {

tls.danger_accept_invalid_certs(!config.certs_verification);

tls.use_sni(config.tls_sni);

tls.disable_built_in_roots(!config.tls_built_in_root_certs);

for cert in config.root_certs {
Expand Down Expand Up @@ -429,6 +435,8 @@ impl ClientBuilder {
.set_certificate_verifier(Arc::new(NoVerifier));
}

tls.enable_sni = config.tls_sni;

// ALPN protocol
match config.http_version_pref {
HttpVersionPref::Http1 => {
Expand Down Expand Up @@ -1140,6 +1148,28 @@ impl ClientBuilder {
self
}

/// Controls the use of TLS server name indication.
///
/// Defaults to `true`.
///
/// # Optional
///
/// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
/// feature to be enabled.
#[cfg(feature = "__tls")]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "default-tls",
feature = "native-tls",
feature = "rustls-tls"
)))
)]
pub fn tls_sni(mut self, tls_sni: bool) -> ClientBuilder {
self.config.tls_sni = tls_sni;
self
}

/// Set the minimum required TLS version for connections.
///
/// By default the TLS backend's own default is used.
Expand Down Expand Up @@ -1706,6 +1736,8 @@ impl Config {
if let Some(ref max_tls_version) = self.max_tls_version {
f.field("max_tls_version", max_tls_version);
}

f.field("tls_sni", &self.tls_sni);
}

#[cfg(all(feature = "native-tls-crate", feature = "__rustls"))]
Expand Down
16 changes: 16 additions & 0 deletions src/blocking/client.rs
Expand Up @@ -620,6 +620,22 @@ impl ClientBuilder {
self.with_inner(|inner| inner.danger_accept_invalid_certs(accept_invalid_certs))
}

/// Controls the use of TLS server name indication.
///
/// Defaults to `true`.
#[cfg(feature = "__tls")]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "default-tls",
feature = "native-tls",
feature = "rustls-tls"
)))
)]
pub fn tls_sni(self, tls_sni: bool) -> ClientBuilder {
self.with_inner(|inner| inner.tls_sni(tls_sni))
}

/// Set the minimum required TLS version for connections.
///
/// By default the TLS backend's own default is used.
Expand Down

0 comments on commit e302f75

Please sign in to comment.