Skip to content

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

feat: Consider CLI config parameters in HTTP request checks (#827)

Kai Ren88b751a17f43
diff --git a/Cargo.lock b/Cargo.lock
index 77d1d81c..b9eaceb7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -24,15 +24,6 @@ dependencies = [
"memchr",
]
-[[package]]
-name = "ansi_term"
-version = "0.11.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
-dependencies = [
- "winapi 0.3.9",
-]
-
[[package]]
name = "anyhow"
version = "1.0.37"
@@ -383,6 +374,7 @@ dependencies = [
"clap",
"env_logger",
"hyper",
+ "once_cell",
"serde",
"serde_json",
"tokio",
@@ -400,18 +392,34 @@ dependencies = [
[[package]]
name = "clap"
-version = "2.33.3"
+version = "3.0.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
+checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142"
dependencies = [
- "ansi_term",
"atty",
"bitflags",
+ "clap_derive",
+ "indexmap",
+ "lazy_static",
+ "os_str_bytes",
"strsim",
+ "termcolor",
"textwrap",
"unicode-width",
"vec_map",
- "yaml-rust",
+]
+
+[[package]]
+name = "clap_derive"
+version = "3.0.0-beta.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1"
+dependencies = [
+ "heck",
+ "proc-macro-error",
+ "proc-macro2",
+ "quote",
+ "syn",
]
[[package]]
@@ -1184,6 +1192,12 @@ dependencies = [
"vcpkg",
]
+[[package]]
+name = "os_str_bytes"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
+
[[package]]
name = "parking"
version = "2.0.0"
@@ -1279,6 +1293,30 @@ version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
+[[package]]
+name = "proc-macro-error"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
+dependencies = [
+ "proc-macro-error-attr",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro-error-attr"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "version_check",
+]
+
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
@@ -1574,9 +1612,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strsim"
-version = "0.8.0"
+version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
@@ -1614,9 +1652,9 @@ dependencies = [
[[package]]
name = "textwrap"
-version = "0.11.0"
+version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789"
dependencies = [
"unicode-width",
]
@@ -2088,9 +2126,3 @@ dependencies = [
"winapi 0.2.8",
"winapi-build",
]
-
-[[package]]
-name = "yaml-rust"
-version = "0.3.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e66366e18dc58b46801afbf2ca7661a9f59cc8c5962c29892b6039b4f86fa992"
diff --git a/Cargo.toml b/Cargo.toml
index 70b1ce25..eff656ec 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,6 +3,8 @@ name = "check-if-email-exists-cli"
version = "0.8.18"
default-run = "check_if_email_exists"
edition = "2018"
+description = "Check if an email address exists without sending any email."
+authors = ["Amaury Martiny <[email protected]>"]
license = "AGPL-3.0"
publish = false
@@ -12,9 +14,10 @@ path = "src/main.rs"
[dependencies]
check-if-email-exists = { path = "./core" }
-clap = { version = "2.33", features = ["yaml"] }
+clap = "3.0.0-beta.2"
env_logger = "0.8"
-hyper = { version = "0.13" }
+hyper = "0.13"
+once_cell = "1.5"
serde = "1.0"
serde_json = "1.0"
tokio = { version = "0.2", features = ["macros", "tcp", "dns", "io-util"] }
diff --git a/src/cli.yml b/src/cli.yml
deleted file mode 100644
index 96caa4e0..00000000
--- a/src/cli.yml
+++ /dev/null
@@ -1,44 +0,0 @@
-name: check_if_email_exists
-author: Amaury Martiny <[email protected]>
-about: Check if an email address exists without sending any email.
-args:
- - FROM_EMAIL:
- default_value: [email protected]
- help: The email to use in the `MAIL FROM:` SMTP command.
- long: from-email
- takes_value: true
- - HELLO_NAME:
- default_value: localhost
- help: The name to use in the `EHLO:` SMTP command.
- long: hello-name
- takes_value: true
- - HTTP:
- help: Runs a HTTP server.
- long: http
- - HTTP_HOST:
- default_value: "127.0.0.1"
- 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:
- 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
- - YAHOO_USE_API:
- default_value: "true"
- help: For Yahoo email addresses, use Yahoo's API instead of connecting directly to their SMTP servers.
- long: yahoo-use-api
diff --git a/src/http.rs b/src/http.rs
index f12da09b..69a88a01 100644
--- a/src/http.rs
+++ b/src/http.rs
@@ -14,11 +14,14 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+use std::{borrow::Cow, net::SocketAddr};
+
use check_if_email_exists::{check_email, CheckEmailInput};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use serde::{Deserialize, Serialize};
-use std::net::SocketAddr;
+
+use crate::CONF;
/// JSON Request from POST /
#[derive(Debug, Deserialize, Serialize)]
@@ -59,11 +62,15 @@ async fn req_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error>
// Create EmailInput from body
let mut input = CheckEmailInput::new(body.to_emails);
- input.from_email(body.from_email.unwrap_or_else(|| "[email protected]".into())).hello_name(body.hello_name.unwrap_or_else(|| "localhost".into()));
-
- // Add proxy if both fields `proxy_host` and `proxy_port` are set.
- if let (Some(proxy_host), Some(proxy_port)) = (body.proxy_host, body.proxy_port) {
- input.proxy(proxy_host, proxy_port);
+ input
+ .from_email(body.from_email.unwrap_or_else(|| CONF.from_email.clone()))
+ .hello_name(body.hello_name.unwrap_or_else(|| CONF.hello_name.clone()))
+ .yahoo_use_api(CONF.yahoo_use_api);
+ if let Some(proxy_host) = body.proxy_host.map(Cow::Owned).or_else(|| CONF.proxy_host.as_ref().map(Cow::Borrowed)) {
+ input.proxy(
+ proxy_host.into_owned(),
+ body.proxy_port.unwrap_or(CONF.proxy_port),
+ );
}
let body = check_email(&input).await;
@@ -92,10 +99,12 @@ async fn req_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error>
}
}
-pub async fn run(host: &str, port: u16) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
- // This is our socket address
- let addr = SocketAddr::new(host.parse()?, port);
+pub async fn run<A: Into<SocketAddr>>(
+ addr: A,
+) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(req_handler)) });
+
+ let addr = addr.into();
let server = Server::bind(&addr).serve(service);
println!("Listening on http://{}", addr);
diff --git a/src/main.rs b/src/main.rs
index b264e832..6f56941f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,47 +14,74 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-extern crate clap;
-extern crate env_logger;
-extern crate hyper;
-extern crate serde;
-extern crate tokio;
-
mod http;
+use std::net::IpAddr;
+
use check_if_email_exists::{check_email, CheckEmailInput};
-use clap::{crate_version, value_t, App};
-use std::env;
+use clap::Clap;
+use once_cell::sync::Lazy;
+
+/// CLI options of this binary.
+#[derive(Clap, Debug)]
+#[clap(author, version, about)]
+pub struct Cli {
+ /// The email to use in the `MAIL FROM:` SMTP command.
+ #[clap(long, env, default_value = "[email protected]")]
+ pub from_email: String,
+
+ /// The name to use in the `EHLO:` SMTP command.
+ #[clap(long, env, default_value = "localhost")]
+ pub hello_name: String,
+
+ /// Use the specified SOCKS5 proxy host to perform email verification.
+ #[clap(long, env)]
+ pub proxy_host: Option<String>,
+
+ /// Use the specified SOCKS5 proxy port to perform email verification.
+ /// Only used when `--proxy-host` flag is set.
+ #[clap(long, env, default_value = "1080")]
+ pub proxy_port: u16,
+
+ /// For Yahoo email addresses, use Yahoo's API instead of connecting
+ /// directly to their SMTP servers.
+ #[clap(long, env, default_value = "true", parse(try_from_str))]
+ pub yahoo_use_api: bool,
+
+ /// The email to check.
+ pub to_email: Option<String>,
+
+ /// Runs a HTTP server.
+ #[clap(long)]
+ pub http: bool,
+
+ /// Sets the host IP address on which the HTTP server should bind.
+ /// Only used when `--http` flag is on.
+ #[clap(long, env = "HOST", default_value = "127.0.0.1")]
+ pub http_host: IpAddr,
+
+ /// 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.
+ #[clap(long, env = "PORT", default_value = "3000")]
+ pub http_port: u16,
+}
+
+/// Global config of this application.
+pub(crate) static CONF: Lazy<Cli> = Lazy::new(Cli::parse);
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
env_logger::init();
- // The YAML file is found relative to the current file, similar to how modules are found
- let yaml = clap::load_yaml!("cli.yml");
- let matches = App::from_yaml(yaml).version(crate_version!()).get_matches();
-
- if let Some(to_email) = matches.value_of("TO_EMAIL") {
- let from_email = matches
- .value_of("FROM_EMAIL")
- .expect("FROM_EMAIL has a default value. qed.");
- let hello_name = matches
- .value_of("HELLO_NAME")
- .expect("HELLO_NAME has a default value. qed.");
-
- let mut input = CheckEmailInput::new(vec![to_email.into()]);
+ if let Some(to_email) = &CONF.to_email {
+ let mut input = CheckEmailInput::new(vec![to_email.clone()]);
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);
- }
-
- if let Ok(yahoo_use_api) = value_t!(matches.value_of("YAHOO_USE_API"), bool) {
- input.yahoo_use_api(yahoo_use_api);
+ .from_email(CONF.from_email.clone())
+ .hello_name(CONF.hello_name.clone())
+ .yahoo_use_api(CONF.yahoo_use_api);
+ if let Some(proxy_host) = &CONF.proxy_host {
+ input.proxy(proxy_host.clone(), CONF.proxy_port);
}
let result = check_email(&input).await;
@@ -70,18 +97,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
// Run the web server if flag is on
- if matches.is_present("HTTP") {
- let http_host = matches
- .value_of("HTTP_HOST")
- .expect("HTTP_HOST has a default value. qed.");
- // http_port is, in this order:
- // - the value of `--http-port` flag
- // - if not set, then the $PORT env varialbe
- // - if not set, then 3000
- let env_port = env::var("PORT").unwrap_or_else(|_| "3000".into());
- let http_port = matches.value_of("HTTP_PORT").unwrap_or(&env_port);
-
- http::run(http_host, http_port.parse::<u16>().unwrap()).await?
+ if CONF.http {
+ http::run((CONF.http_host, CONF.http_port)).await?;
}
Ok(())