use std::sync::Arc;
#[cfg(not(test))]
use std::sync::OnceLock;
use rustls::{ClientConfig, RootCertStore};
#[cfg(test)]
pub(crate) static TEST_ROOTS: std::sync::Mutex<Vec<rustls::pki_types::CertificateDer<'static>>> =
std::sync::Mutex::new(Vec::new());
fn build() -> Arc<ClientConfig> {
let mut roots = RootCertStore::empty();
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
#[cfg(test)]
for c in TEST_ROOTS.lock().unwrap().iter() {
roots.add(c.clone()).expect("test root");
}
let config =
ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
.with_safe_default_protocol_versions()
.expect("ring supports the default protocol versions")
.with_root_certificates(roots)
.with_no_client_auth();
Arc::new(config)
}
pub(crate) fn client_config() -> Arc<ClientConfig> {
#[cfg(not(test))]
{
static CONFIG: OnceLock<Arc<ClientConfig>> = OnceLock::new();
CONFIG.get_or_init(build).clone()
}
#[cfg(test)]
build()
}