Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to configure TLS server name indication (SNI) #1669

Merged
merged 1 commit into from Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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