diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 6af56353b..eea5f1f07 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -311,8 +311,10 @@ impl Tls { /// /// # Returns /// - /// The TLS chain of trust for the host - pub fn probe(&self, host: &str) -> Result>, Error> { + /// * A Vec containing the TLS chain of trust offered by the host + /// * Error if the communication with the host fails + /// + pub fn probe(&self, host: &str) -> Result, Error> { log::info!("starting TLS probe"); match host.to_owned().try_into() { Ok(server_name) => { @@ -337,9 +339,9 @@ impl Tls { Some(certificates) => { let cert_owned: Vec> = certificates.iter().map(|cert| cert.clone().into_owned()).collect(); - Ok(Some(cert_owned)) + Ok(cert_owned) } - None => Ok(None), + None => Ok(Vec::>::new()), } } Err(e) => { @@ -369,10 +371,13 @@ impl Tls { /// the number of trusted Certificates offered by the host pub fn inspect(&self, host: &str) -> Result { match self.probe(host) { - Ok(certs) => match certs { - Some(certs) => Ok(self.trust_modal(certs.to_vec())), - None => Ok(0), - }, + Ok(certs) => { + if certs.len() > 0 { + Ok(self.trust_modal(certs.to_vec())) + } else { + Ok(0) + } + } Err(e) => { log::warn!("failed to probe {host}: {e}"); Ok(0) @@ -394,14 +399,12 @@ impl Tls { /// # Returns /// /// true if the user trusts at least one of the Certificates offered by the host. + /// pub fn accessible(&self, host: &str, inspect: bool) -> bool { match self.probe(host) { - Ok(certs) => match certs { - Some(certs) => match certs.iter().find(|&cert| self.is_trusted_cert(cert.clone())) { - Some(_) => true, - None => inspect && (self.trust_modal(certs.to_vec()) > 0), - }, - None => false, + Ok(certs) => match certs.iter().find(|&cert| self.is_trusted_cert(cert.clone())) { + Some(_) => true, + None => inspect && (self.trust_modal(certs.to_vec()) > 0), }, Err(e) => { log::warn!("failed to probe {host}: {e}"); diff --git a/libs/tls/src/xtls.rs b/libs/tls/src/xtls.rs index 7c55fc80c..81e0c02aa 100644 --- a/libs/tls/src/xtls.rs +++ b/libs/tls/src/xtls.rs @@ -37,11 +37,9 @@ impl ureq::TlsConnector for TlsConnector { if let Some(rustls_error) = inner.downcast_ref::() { if let rustls::Error::InvalidCertificate(_) = rustls_error { if let Ok(certs) = tls.probe(dns_name) { - if let Some(certs) = certs { - if certs.len() > 0 { - log::info!("try again with new trusted certs"); - continue; - } + if certs.len() > 0 { + log::info!("try again with new trusted certs"); + continue; } } } @@ -56,11 +54,9 @@ impl ureq::TlsConnector for TlsConnector { // errors generated early in the tls handshake Err(rustls::Error::InvalidCertificate(_)) => { if let Ok(certs) = tls.probe(dns_name) { - if let Some(certs) = certs { - if certs.len() > 0 { - log::info!("try again with new trusted certs"); - continue; - } + if certs.len() > 0 { + log::info!("try again with new trusted certs"); + continue; } } }