Skip to content

Check if an email address exists without sending any email, written in Rust. Comes with a ⚙️ HTTP backend.

feat: Add proxy username/password (#1057)

Amauryd9583c6ae0d3
diff --git a/Cargo.lock b/Cargo.lock
index 7f23a6f5..c479a653 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -318,7 +318,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "check-if-email-exists"
-version = "0.8.27"
+version = "0.8.28"
dependencies = [
"async-native-tls",
"async-recursion",
@@ -340,7 +340,7 @@ dependencies = [
[[package]]
name = "check-if-email-exists-cli"
-version = "0.8.27"
+version = "0.8.28"
dependencies = [
"check-if-email-exists",
"clap",
diff --git a/Cargo.toml b/Cargo.toml
index 3f182728..89ef9875 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists-cli"
-version = "0.8.27"
+version = "0.8.28"
default-run = "check_if_email_exists"
edition = "2018"
description = "Check if an email address exists without sending any email."
diff --git a/Dockerfile b/Dockerfile
index 1c4c561f..89d20d5a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -8,7 +8,7 @@ FROM alpine
# `ciee` stands for check-if-email-exists
WORKDIR /ciee
# Fetch latest version
-ENV CIEE_VERSION 0.8.27
+ENV CIEE_VERSION 0.8.28
# Install needed libraries
RUN apk update && \
diff --git a/README.md b/README.md
index 3c75399f..3b27d128 100644
--- a/README.md
+++ b/README.md
@@ -88,7 +88,7 @@ Head to the [releases page](https://github.com/reacherhq/check-if-email-exists/r
```
> $ check_if_email_exists --help
-check_if_email_exists 0.8.27
+check_if_email_exists 0.8.28
Check if an email address exists without sending any email.
USAGE:
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 8e90a22a..c6cc721b 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists"
-version = "0.8.27"
+version = "0.8.28"
authors = ["Amaury <[email protected]>"]
categories = ["email"]
description = "Check if an email address exists without sending any email"
diff --git a/core/src/smtp/mod.rs b/core/src/smtp/mod.rs
index 9f784966..3f7dff61 100644
--- a/core/src/smtp/mod.rs
+++ b/core/src/smtp/mod.rs
@@ -133,15 +133,18 @@ async fn connect_to_host(
port: u16,
input: &CheckEmailInput,
) -> Result<SmtpTransport, SmtpError> {
+ // hostname verification fails if it ends with '.', for example, using
+ // SOCKS5 proxies we can `io: incomplete` error.
+ let host = host.to_string();
+ let host = host.trim_end_matches('.').to_string();
+
let security = {
- let host_string = host.to_utf8();
- 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));
+ let tls_params = ClientTlsParameters::new(host.clone(), 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)
+
+ let mut smtp_transport = SmtpClient::with_security((host.as_ref(), port), security)
.await
.map_err(SmtpError::HeloError)?
// FIXME Do not clone?
@@ -157,20 +160,36 @@ async fn connect_to_host(
host,
port
);
+
if let Some(proxy) = &input.proxy {
- let stream = Socks5Stream::connect(
- (proxy.host.as_ref(), proxy.port),
- host.to_utf8(),
- port,
- Config::default(),
- )
- .await?;
+ let stream = match (&proxy.username, &proxy.password) {
+ (Some(username), Some(password)) => {
+ Socks5Stream::connect_with_password(
+ (proxy.host.as_ref(), proxy.port),
+ host.clone(),
+ port,
+ username.to_string(),
+ password.to_string(),
+ Config::default(),
+ )
+ .await?
+ }
+ _ => {
+ Socks5Stream::connect(
+ (proxy.host.as_ref(), proxy.port),
+ host.clone(),
+ port,
+ Config::default(),
+ )
+ .await?
+ }
+ };
try_smtp!(
- smtp_client
+ smtp_transport
.connect_with_stream(NetworkStream::Socks5Stream(stream))
.await,
- smtp_client,
+ smtp_transport,
input.to_emails[0],
host,
port,
@@ -178,8 +197,8 @@ async fn connect_to_host(
);
} else {
try_smtp!(
- smtp_client.connect().await,
- smtp_client,
+ smtp_transport.connect().await,
+ smtp_transport,
input.to_emails[0],
host,
port,
@@ -196,18 +215,18 @@ async fn connect_to_host(
EmailAddress::from_str("[email protected]").expect("This is a valid email. qed.")
});
try_smtp!(
- smtp_client
+ smtp_transport
// FIXME Do not clone?
.command(MailCommand::new(Some(from_email.clone()), vec![],))
.await,
- smtp_client,
+ smtp_transport,
input.to_emails[0],
host,
port,
SmtpError::MailFromError
);
- Ok(smtp_client)
+ Ok(smtp_transport)
}
/// Description of the deliverability information we can gather from
@@ -224,12 +243,12 @@ struct Deliverability {
/// Check if `to_email` exists on host SMTP server. This is the core logic of
/// this tool.
async fn email_deliverable(
- smtp_client: &mut SmtpTransport,
+ smtp_transport: &mut SmtpTransport,
to_email: &EmailAddress,
) -> Result<Deliverability, SmtpError> {
// "RCPT TO: [email protected]"
// FIXME Do not clone?
- match smtp_client
+ match smtp_transport
.command(RcptCommand::new(to_email.clone(), vec![]))
.await
{
@@ -362,7 +381,7 @@ async fn email_deliverable(
/// Verify the existence of a catch-all on the domain.
async fn smtp_is_catch_all(
- smtp_client: &mut SmtpTransport,
+ smtp_transport: &mut SmtpTransport,
domain: &str,
) -> Result<bool, SmtpError> {
// Create a random 15-char alphanumerical string.
@@ -375,7 +394,7 @@ async fn smtp_is_catch_all(
let random_email = EmailAddress::new(format!("{}@{}", random_email, domain));
email_deliverable(
- smtp_client,
+ smtp_transport,
&random_email.expect("Email is correctly constructed. qed."),
)
.await
@@ -391,9 +410,9 @@ async fn create_smtp_future(
) -> Result<(bool, Deliverability), SmtpError> {
// FIXME If the SMTP is not connectable, we should actually return an
// Ok(SmtpDetails { can_connect_smtp: false, ... }).
- let mut smtp_client = connect_to_host(host, port, input).await?;
+ let mut smtp_transport = connect_to_host(host, port, input).await?;
- let is_catch_all = smtp_is_catch_all(&mut smtp_client, domain)
+ let is_catch_all = smtp_is_catch_all(&mut smtp_transport, domain)
.await
.unwrap_or(false);
let deliverability = if is_catch_all {
@@ -403,12 +422,12 @@ async fn create_smtp_future(
is_disabled: false,
}
} else {
- let mut result = email_deliverable(&mut smtp_client, to_email).await;
+ let mut result = email_deliverable(&mut smtp_transport, to_email).await;
// Some SMTP servers automatically close the connection after an error,
// so we should reconnect to perform a next command.
//
- // Unfortunately `smtp_client.is_connected()` doesn't report about this,
+ // Unfortunately `smtp_transport.is_connected()` doesn't report about this,
// so we can only check for "io: incomplete" SMTP error being returned.
// https://github.com/async-email/async-smtp/issues/37
if is_io_incomplete_smtp_error(&result) {
@@ -417,15 +436,18 @@ async fn create_smtp_future(
"Got `io: incomplete` error, reconnecting."
);
- let _ = smtp_client.close().await;
- smtp_client = connect_to_host(host, port, input).await?;
- result = email_deliverable(&mut smtp_client, to_email).await;
+ let _ = smtp_transport.close().await;
+ smtp_transport = connect_to_host(host, port, input).await?;
+ result = email_deliverable(&mut smtp_transport, to_email).await;
}
result?
};
- smtp_client.close().await.map_err(SmtpError::CloseError)?;
+ smtp_transport
+ .close()
+ .await
+ .map_err(SmtpError::CloseError)?;
Ok((is_catch_all, deliverability))
}
diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rs
index 92ba7e73..ee43bdbe 100644
--- a/core/src/util/input_output.rs
+++ b/core/src/util/input_output.rs
@@ -24,12 +24,16 @@ use std::time::Duration;
/// Perform the email verification via a specified proxy. The usage of a proxy
/// is optional.
-#[derive(Debug, Clone, Deserialize, Serialize)]
+#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct CheckEmailInputProxy {
/// Use the specified SOCKS5 proxy host to perform email verification.
pub host: String,
/// Use the specified SOCKS5 proxy port to perform email verification.
pub port: u16,
+ /// Username to pass to proxy authentication.
+ pub username: Option<String>,
+ /// Password to pass to proxy authentication.
+ pub password: Option<String>,
}
/// Define how to apply TLS to a SMTP client connection. Will be converted into
@@ -104,7 +108,7 @@ impl Default for CheckEmailInput {
hello_name: "localhost".into(),
proxy: None,
smtp_port: 25,
- smtp_security: SmtpSecurity::Opportunistic,
+ smtp_security: SmtpSecurity::None,
smtp_timeout: None,
yahoo_use_api: true,
retries: 2,
@@ -157,6 +161,7 @@ impl CheckEmailInput {
self.proxy = Some(CheckEmailInputProxy {
host: proxy_host,
port: proxy_port,
+ ..Default::default()
});
self
}
diff --git a/src/http.rs b/src/http.rs
index ed1b0ada..0a3d6c2e 100644
--- a/src/http.rs
+++ b/src/http.rs
@@ -78,7 +78,8 @@ async fn req_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error>
if let Some(proxy_host) = body.proxy_host.map(Cow::Owned).or_else(|| CONF.proxy_host.as_ref().map(Cow::Borrowed)) {
input.set_proxy(CheckEmailInputProxy {
host:proxy_host.into_owned(),
- port: body.proxy_port.unwrap_or(CONF.proxy_port)
+ port: body.proxy_port.unwrap_or(CONF.proxy_port),
+ ..Default::default()
});
}
diff --git a/src/main.rs b/src/main.rs
index 785e20be..3d82debc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -43,6 +43,16 @@ pub struct Cli {
#[clap(long, env, default_value = "1080")]
pub proxy_port: u16,
+ /// Username passed to the specified SOCKS5 proxy port to perform email verification.
+ /// Only used when `--proxy-host` flag is set.
+ #[clap(long, env)]
+ pub proxy_username: Option<String>,
+
+ /// Username passed to the specified SOCKS5 proxy port to perform email verification.
+ /// Only used when `--proxy-host` flag is set.
+ #[clap(long, env)]
+ pub proxy_password: Option<String>,
+
/// The port to use for the SMTP request.
#[clap(long, env, default_value = "25")]
pub smtp_port: u16,
@@ -104,6 +114,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
input.set_proxy(CheckEmailInputProxy {
host: proxy_host.clone(),
port: CONF.proxy_port,
+ username: CONF.proxy_username.clone(),
+ password: CONF.proxy_password.clone(),
});
}