Skip to content

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

feat: Add possibility to verify emails via proxy (#286)

Amaury Martinya0ab93fde510
diff --git a/Cargo.lock b/Cargo.lock
index b07d9e63..154c9195 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -9,6 +9,30 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "amaurymartiny-async-smtp"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e1f1e257ba329a50a48cb897aa9596d742350d041e7fc8ad201c6bef7716892"
+dependencies = [
+ "async-native-tls",
+ "async-std",
+ "async-trait",
+ "base64",
+ "bufstream",
+ "fast-socks5",
+ "fast_chemail",
+ "hostname 0.1.5",
+ "log",
+ "nom",
+ "pin-project",
+ "pin-utils",
+ "serde",
+ "serde_derive",
+ "serde_json",
+ "thiserror",
+]
+
[[package]]
name = "ansi_term"
version = "0.11.0"
@@ -18,6 +42,12 @@ dependencies = [
"winapi 0.3.8",
]
+[[package]]
+name = "anyhow"
+version = "1.0.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff"
+
[[package]]
name = "arrayvec"
version = "0.4.12"
@@ -45,28 +75,6 @@ dependencies = [
"url",
]
-[[package]]
-name = "async-smtp"
-version = "0.2.0"
-source = "git+https://github.com/amaurymartiny/async-smtp?branch=am-add-command#a33a27428b5fb7674fa367a84a5048684f674e0a"
-dependencies = [
- "async-native-tls",
- "async-std",
- "async-trait",
- "base64",
- "bufstream",
- "fast_chemail",
- "hostname 0.1.5",
- "log",
- "nom",
- "pin-project",
- "pin-utils",
- "serde",
- "serde_derive",
- "serde_json",
- "thiserror",
-]
-
[[package]]
name = "async-std"
version = "1.5.0"
@@ -205,9 +213,10 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "check-if-email-exists"
-version = "0.7.0"
+version = "0.7.1"
dependencies = [
- "async-smtp",
+ "amaurymartiny-async-smtp",
+ "fast-socks5",
"futures",
"log",
"mailchecker",
@@ -219,7 +228,7 @@ dependencies = [
[[package]]
name = "check-if-email-exists-cli"
-version = "0.7.0"
+version = "0.7.1"
dependencies = [
"check-if-email-exists",
"clap",
@@ -232,7 +241,7 @@ dependencies = [
[[package]]
name = "check-if-email-exists-test-suite"
-version = "0.7.0"
+version = "0.7.1"
dependencies = [
"check-if-email-exists",
"serde",
@@ -353,6 +362,19 @@ dependencies = [
"termcolor",
]
+[[package]]
+name = "fast-socks5"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "83424040c717f5937e28cbc5821b503b51afc7eb16e9436b1c5a079d95ab58ed"
+dependencies = [
+ "anyhow",
+ "async-std",
+ "futures",
+ "log",
+ "thiserror",
+]
+
[[package]]
name = "fast_chemail"
version = "0.9.6"
diff --git a/Cargo.toml b/Cargo.toml
index 37a6c499..707e3a17 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists-cli"
-version = "0.7.0"
+version = "0.7.1"
default-run = "check_if_email_exists"
edition = "2018"
license = "GPL-3.0"
diff --git a/README.md b/README.md
index 6d2324d3..7d784a0a 100644
--- a/README.md
+++ b/README.md
@@ -93,7 +93,7 @@ Head to the [releases page](https://github.com/amaurymartiny/check-if-email-exis
```
> $ check_if_email_exists --help
-check_if_email_exists 0.7.0
+check_if_email_exists 0.7.1
Check if an email address exists without sending any email.
USAGE:
@@ -144,7 +144,7 @@ In your own Rust project, you can add `check-if-email-exists` in your `Cargo.tom
```toml
[dependencies]
-check-if-email-exists = "0.6"
+check-if-email-exists = "0.7"
```
And use it in your code as follows (async/await syntax):
diff --git a/async-smtp b/async-smtp
new file mode 160000
index 00000000..b6ed418e
--- /dev/null
+++ b/async-smtp
@@ -0,0 +1 @@
+Subproject commit b6ed418ec5c3b4db4c4a7e81cc402f888d117572
diff --git a/core/Cargo.toml b/core/Cargo.toml
index cb9db281..6edb5fe4 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists"
-version = "0.7.0"
+version = "0.7.1"
authors = ["Amaury Martiny <[email protected]>"]
categories = ["email"]
description = "Check if an email address exists without sending any email"
@@ -17,7 +17,8 @@ appveyor = { repository = "amaurymartiny/check-if-email-exists", service = "gith
travis-ci = { repository = "amaurymartiny/check-if-email-exists", service = "github" }
[dependencies]
-async-smtp = { git = "https://github.com/amaurymartiny/async-smtp", branch = "am-add-command" }
+async-smtp = { package = "amaurymartiny-async-smtp", version = "0.1" }
+fast-socks5 = "0.3"
futures = "0.3"
log = "0.4"
mailchecker = "3.3"
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 5b3c5939..5057e31d 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -14,13 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with check-if-email-exists. If not, see <http://www.gnu.org/licenses/>.
-extern crate async_smtp;
#[macro_use]
extern crate log;
-extern crate mailchecker;
-extern crate rand;
-extern crate serde;
-extern crate trust_dns_resolver;
pub mod misc;
pub mod mx;
@@ -144,6 +139,7 @@ pub async fn email_exists(email_input: &EmailInput) -> SingleEmail {
SMTP_PORT,
my_syntax.domain.as_str(),
email_input.hello_name.as_ref(),
+ &email_input.proxy,
);
// https://rust-lang.github.io/async-book/04_pinning/01_chapter.html
diff --git a/core/src/smtp.rs b/core/src/smtp.rs
index 2b48b61b..dfcfd69e 100644
--- a/core/src/smtp.rs
+++ b/core/src/smtp.rs
@@ -16,14 +16,23 @@
use crate::util::ser_with_display::ser_with_display;
use async_smtp::{
- smtp::{commands::*, error::Error as AsyncSmtpError, extension::ClientId},
+ smtp::{
+ client::net::NetworkStream, commands::*, error::Error as AsyncSmtpError,
+ extension::ClientId,
+ },
ClientSecurity, EmailAddress, SmtpClient, SmtpTransport,
};
+use fast_socks5::{
+ client::{Config, Socks5Stream},
+ Result, SocksError,
+};
use rand::{distributions::Alphanumeric, Rng};
use serde::Serialize;
use std::time::Duration;
use trust_dns_resolver::Name;
+use super::util::email_input::EmailInputProxy;
+
/// Details that we gathered from connecting to this email via SMTP
#[derive(Debug, Serialize)]
pub struct SmtpDetails {
@@ -43,6 +52,9 @@ pub struct SmtpDetails {
pub enum SmtpError {
/// Skipped checking SMTP details
Skipped,
+ /// Error if we're using a SOCKS5 proxy
+ #[serde(serialize_with = "ser_with_display")]
+ SocksError(SocksError),
/// Error when communicating with SMTP server
#[serde(serialize_with = "ser_with_display")]
SmtpError(AsyncSmtpError),
@@ -54,6 +66,12 @@ impl From<AsyncSmtpError> for SmtpError {
}
}
+impl From<SocksError> for SmtpError {
+ fn from(error: SocksError) -> Self {
+ SmtpError::SocksError(error)
+ }
+}
+
/// Try to send an smtp command, close and return Err if fails.
macro_rules! try_smtp (
($res: expr, $client: ident, $host: expr, $port: expr) => ({
@@ -61,7 +79,7 @@ macro_rules! try_smtp (
debug!("Closing {}:{}, because of error '{}'.", $host, $port, err);
$client.close().await?;
- return Err(err);
+ return Err(err.into());
}
})
);
@@ -72,8 +90,8 @@ async fn connect_to_host(
host: &Name,
port: u16,
hello_name: &str,
-) -> Result<SmtpTransport, AsyncSmtpError> {
- debug!("Connecting to {}:{}", host, port);
+ proxy: &Option<EmailInputProxy>,
+) -> Result<SmtpTransport, SmtpError> {
let mut smtp_client =
SmtpClient::with_security((host.to_utf8().as_str(), port), ClientSecurity::None)
.await?
@@ -81,8 +99,28 @@ async fn connect_to_host(
.timeout(Some(Duration::new(30, 0))) // Set timeout to 30s
.into_transport();
- // Connect to the host.
- try_smtp!(smtp_client.connect().await, smtp_client, host, port);
+ // Connect to the host. If the proxy argument is set, use it.
+ debug!("Connecting to {}:{}", host, port);
+ if let Some(proxy) = proxy {
+ let stream = Socks5Stream::connect(
+ (proxy.host.as_ref(), proxy.port),
+ host.to_utf8(),
+ port,
+ Config::default(),
+ )
+ .await?;
+
+ try_smtp!(
+ smtp_client
+ .connect_with_stream(NetworkStream::Socks5Stream(stream))
+ .await,
+ smtp_client,
+ host,
+ port
+ );
+ } else {
+ try_smtp!(smtp_client.connect().await, smtp_client, host, port);
+ }
// "MAIL FROM: [email protected]"
// FIXME Do not clone?
@@ -220,8 +258,9 @@ pub async fn smtp_details(
port: u16,
domain: &str,
hello_name: &str,
+ proxy: &Option<EmailInputProxy>,
) -> Result<SmtpDetails, SmtpError> {
- let mut smtp_client = connect_to_host(from_email, host, port, hello_name).await?;
+ let mut smtp_client = connect_to_host(from_email, host, port, hello_name, proxy).await?;
let is_catch_all = email_has_catch_all(&mut smtp_client, domain)
.await
diff --git a/core/src/util/email_input.rs b/core/src/util/email_input.rs
index dcfa85ad..0501b5ad 100644
--- a/core/src/util/email_input.rs
+++ b/core/src/util/email_input.rs
@@ -14,17 +14,31 @@
// You should have received a copy of the GNU General Public License
// along with check-if-email-exists. If not, see <http://www.gnu.org/licenses/>.
+/// Perform the email verification via a specified proxy. The usage of a proxy
+/// is of course optional.
+pub struct EmailInputProxy {
+ /// 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,
+}
+
/// Builder pattern for the input argument into the main `email_exists`
/// function.
pub struct EmailInput {
/// The email to validate.
pub to_email: String,
/// Email to use in the `MAIL FROM:` SMTP command.
+ ///
/// Defaults to "[email protected]".
pub from_email: String,
/// Name to use in the `EHLO:` SMTP command.
+ ///
/// Defaults to "localhost" (note: "localhost" is not a FQDN).
pub hello_name: String,
+ /// Perform the email verification via a specified proxy. The usage of a
+ /// proxy is of course optional.
+ pub proxy: Option<EmailInputProxy>,
}
impl EmailInput {
@@ -34,6 +48,7 @@ impl EmailInput {
to_email: email,
from_email: "[email protected]".into(),
hello_name: "localhost".into(),
+ proxy: None,
}
}
@@ -48,4 +63,13 @@ impl EmailInput {
self.hello_name = name;
self
}
+
+ /// Use the specified proxy to perform email verification.
+ pub fn proxy(&mut self, proxy_host: String, proxy_port: u16) -> &mut EmailInput {
+ self.proxy = Some(EmailInputProxy {
+ host: proxy_host,
+ port: proxy_port,
+ });
+ self
+ }
}
diff --git a/src/cli.yml b/src/cli.yml
index b8371e94..0a1e6ba2 100644
--- a/src/cli.yml
+++ b/src/cli.yml
@@ -4,28 +4,37 @@ about: Check if an email address exists without sending any email.
args:
- FROM_EMAIL:
default_value: [email protected]
- long: from-email
help: The email to use in the `MAIL FROM:` SMTP command.
+ long: from-email
takes_value: true
- HELLO_NAME:
default_value: localhost
- long: hello-name
help: The name to use in the `EHLO:` SMTP command.
+ long: hello-name
takes_value: true
- HTTP:
- long: http
help: Runs a HTTP server.
+ long: http
- HTTP_HOST:
default_value: "127.0.0.1"
- long: http-host
help: Sets the host IP address on which the HTTP server should bind. Only used when `--http` flag is on.
+ long: http-host
takes_value: true
value_name: HOST
- HTTP_PORT:
- long: http-port
help: Sets the port on which the HTTP server should bind. Only used when `--http` flag is on. If not set, then it will use $PORT, or default to 3000.
+ long: http-port
takes_value: true
value_name: PORT
+ - PROXY_HOST:
+ help: Use the specified SOCKS5 proxy host to perform email verification.
+ long: proxy-host
+ takes_value: true
+ - PROXY_PORT:
+ default_value: '1080'
+ help: Use the specified SOCKS5 proxy port to perform email verification. Only used when `--proxy-host` flag is set.
+ long: proxy-port
+ takes_value: true
- TO_EMAIL:
help: The email to check.
index: 1
diff --git a/src/http.rs b/src/http.rs
index eac0a4d0..57f09d0d 100644
--- a/src/http.rs
+++ b/src/http.rs
@@ -26,6 +26,7 @@ pub struct PostReqBody {
from_email: Option<String>,
hello_name: Option<String>,
to_email: String,
+ // TODO Add fields for proxy
}
/// Error Response from POST /
diff --git a/src/main.rs b/src/main.rs
index 90eaf6d0..3227ba5e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -23,7 +23,7 @@ extern crate tokio;
mod http;
use check_if_email_exists::{email_exists, EmailInput};
-use clap::{crate_version, App};
+use clap::{crate_version, value_t, App};
use serde_json;
use std::env;
@@ -43,12 +43,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.value_of("HELLO_NAME")
.expect("HELLO_NAME has a default value. qed.");
- let result = email_exists(
- EmailInput::new(to_email.into())
- .from_email(from_email.into())
- .hello_name(hello_name.into()),
- )
- .await;
+ let mut input = EmailInput::new(to_email.into());
+ input
+ .from_email(from_email.into())
+ .hello_name(hello_name.into());
+
+ if let Some(proxy_host) = matches.value_of("PROXY_HOST") {
+ let proxy_port = value_t!(matches.value_of("PROXY_PORT"), u16)
+ .expect("PROXY_PORT has a default value of type u16. qed.");
+ input.proxy(proxy_host.into(), proxy_port);
+ }
+
+ let result = email_exists(&input).await;
match serde_json::to_string_pretty(&result) {
Ok(output) => {
diff --git a/test_suite/Cargo.toml b/test_suite/Cargo.toml
index 91c58389..9ed289d9 100644
--- a/test_suite/Cargo.toml
+++ b/test_suite/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "check-if-email-exists-test-suite"
-version = "0.7.0"
+version = "0.7.1"
edition = "2018"
publish = false