Compare commits

...

1 Commits

Author SHA1 Message Date
Amaury M
1a1d6d7c9b feat: Allow user to define SMTP client security for TLS 2022-01-25 13:47:49 +01:00
3 changed files with 47 additions and 8 deletions

View File

@ -224,6 +224,7 @@ pub async fn check_email(inputs: &CheckEmailInput) -> Vec<CheckEmailOutput> {
proxy: inputs.proxy.clone(),
retries: inputs.retries,
smtp_port: inputs.smtp_port,
smtp_security: inputs.smtp_security,
smtp_timeout: inputs.smtp_timeout,
yahoo_use_api: inputs.yahoo_use_api,
}

View File

@ -25,7 +25,7 @@ use async_smtp::{
client::net::NetworkStream, commands::*, error::Error as AsyncSmtpError,
extension::ClientId,
},
ClientSecurity, ClientTlsParameters, EmailAddress, SmtpClient, SmtpTransport,
ClientTlsParameters, EmailAddress, SmtpClient, SmtpTransport,
};
use async_std::future;
use fast_socks5::{
@ -118,11 +118,11 @@ async fn connect_to_host(
) -> Result<SmtpTransport, SmtpError> {
let security = {
let host_string = host.to_utf8();
let host_str = host_string.trim_end_matches('.'); // hostname verification fails if it ends with '.'
ClientSecurity::Opportunistic(ClientTlsParameters::new(
host_str.to_string(),
TlsConnector::new().use_sni(true),
))
let host_string = host_string.trim_end_matches('.'); // hostname verification fails if it ends with '.'
let tls_params =
ClientTlsParameters::new(host_string.to_string(), TlsConnector::new().use_sni(true));
input.smtp_security.to_client_security(tls_params)
};
let mut smtp_client = SmtpClient::with_security((host.to_utf8().as_ref(), port), security)
.await?

View File

@ -18,6 +18,7 @@ use crate::misc::{MiscDetails, MiscError};
use crate::mx::{MxDetails, MxError};
use crate::smtp::{SmtpDetails, SmtpError};
use crate::syntax::SyntaxDetails;
use async_smtp::{ClientSecurity, ClientTlsParameters};
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
use std::time::Duration;
@ -31,6 +32,31 @@ pub struct CheckEmailInputProxy {
pub port: u16,
}
/// Define how to apply TLS to a SMTP client connection. Will be converted into
/// async_smtp::ClientSecurity.
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub enum SmtpSecurity {
/// Insecure connection only (for testing purposes).
None,
/// Start with insecure connection and use `STARTTLS` when available.
Opportunistic,
/// Start with insecure connection and require `STARTTLS`.
Required,
/// Use TLS wrapped connection.
Wrapper,
}
impl SmtpSecurity {
pub fn to_client_security(self, tls_params: ClientTlsParameters) -> ClientSecurity {
match self {
Self::None => ClientSecurity::None,
Self::Opportunistic => ClientSecurity::Opportunistic(tls_params),
Self::Required => ClientSecurity::Required(tls_params),
Self::Wrapper => ClientSecurity::Wrapper(tls_params),
}
}
}
/// Builder pattern for the input argument into the main `email_exists`
/// function.
#[derive(Debug, Clone, Deserialize, Serialize)]
@ -60,9 +86,14 @@ pub struct CheckEmailInput {
///
/// Defaults to true.
pub yahoo_use_api: bool,
/// Number of retries of SMTP connections to do. Defaults to 2 to avoid
/// greylisting.
/// Number of retries of SMTP connections to do.
///
/// Defaults to 2 to avoid greylisting.
pub retries: usize,
/// How to apply TLS to a SMTP client connection.
///
/// Defaults to Opportunistic.
pub smtp_security: SmtpSecurity,
}
impl Default for CheckEmailInput {
@ -73,6 +104,7 @@ impl Default for CheckEmailInput {
hello_name: "localhost".into(),
proxy: None,
smtp_port: 25,
smtp_security: SmtpSecurity::Opportunistic,
smtp_timeout: None,
yahoo_use_api: true,
retries: 2,
@ -154,6 +186,12 @@ impl CheckEmailInput {
self
}
/// Set the SMTP client security to use for TLS.
pub fn set_smtp_security(&mut self, smtp_security: SmtpSecurity) -> &mut CheckEmailInput {
self.smtp_security = smtp_security;
self
}
/// Add optional timeout for the SMTP verification step.
pub fn set_smtp_timeout(&mut self, duration: Duration) -> &mut CheckEmailInput {
self.smtp_timeout = Some(duration);