Skip to content

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

Re-add parallelization

Amaury Martiny7647fdc4e8e9
diff --git a/src/main.rs b/src/main.rs
index 7ef52fd0..c36a950c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,8 +11,6 @@ use clap::App;
use lettre::smtp::{SMTP_PORT, SUBMISSION_PORT};
use rayon::prelude::*;
use std::process;
-use std::str::FromStr;
-use trust_dns_resolver::Name;
mod mx_hosts;
mod with_lettre;
@@ -39,33 +37,21 @@ fn main() {
};
debug!("Domain name is '{}'", domain);
- // debug!("Getting MX lookup...");
- // let hosts = mx_hosts::get_mx_lookup(domain);
- // info!("Found the following MX hosts {:?}", hosts);
- // let ports = vec![SMTP_PORT, SUBMISSION_PORT, 465];
- // let mut combinations = Vec::new(); // `(host, port)` combination
- // for port in ports.into_iter() {
- // for host in hosts.iter() {
- // combinations.push((host.exchange(), port))
- // }
- // }
-
- with_lettre::email_exists(
- from_email,
- to_email,
- &Name::from_str("smtp.gmail.com").unwrap(),
- 465,
- );
+ debug!("Getting MX lookup...");
+ let hosts = mx_hosts::get_mx_lookup(domain);
+ info!("Found the following MX hosts {:?}", hosts);
+ let ports = vec![SMTP_PORT, SUBMISSION_PORT, 465];
+ let mut combinations = Vec::new(); // `(host, port)` combination
+ for port in ports.into_iter() {
+ for host in hosts.iter() {
+ combinations.push((host.exchange(), port))
+ }
+ }
- // let found = combinations
- // .par_iter() // Parallelize the find_any
- // .find_any(|(host, port)| with_lettre::email_exists(
- // from_email,
- // to_email,
- // host,
- // *port,
- // ))
- // .is_some();
+ let found = combinations
+ .par_iter() // Parallelize the find_any
+ .find_any(|(host, port)| with_lettre::email_exists(from_email, to_email, host, *port))
+ .is_some();
- // println!("{}", found);
+ println!("{}", found);
}
diff --git a/src/with_lettre.rs b/src/with_lettre.rs
index 19a91f6f..388c2889 100644
--- a/src/with_lettre.rs
+++ b/src/with_lettre.rs
@@ -6,11 +6,9 @@ use self::lettre::smtp::client::net::NetworkStream;
use self::lettre::smtp::client::Client;
use self::lettre::smtp::commands::*;
use self::lettre::smtp::extension::{ClientId, Extension, ServerInfo};
-use self::lettre::smtp::{ClientSecurity, SMTP_PORT, SUBMISSION_PORT};
use self::lettre::{ClientTlsParameters, EmailAddress};
-use self::native_tls::{Protocol, TlsConnector};
+use self::native_tls::TlsConnector;
use self::trust_dns_resolver::Name;
-use std::net::{SocketAddr, ToSocketAddrs};
use std::time::Duration;
macro_rules! try_smtp (
@@ -19,7 +17,7 @@ macro_rules! try_smtp (
Ok(res) => {
if !res.is_positive() {
if let Some(message) = res.first_line() {
- error!("Got error {}", message);
+ error!("{}", message);
}
$client.close();
return false;
@@ -38,21 +36,25 @@ macro_rules! try_smtp (
pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> bool {
info!("Connecting to {}:{}...", host, port);
let mut email_client: Client<NetworkStream> = Client::new();
- if let Err(err) = email_client.set_timeout(Some(Duration::new(3, 0))) {
+ if let Err(err) = email_client.set_timeout(Some(Duration::new(1, 0))) {
error!("{}", err);
email_client.close();
return false;
}
+ let tls_builder = TlsConnector::builder();
+ let tls_parameters =
+ ClientTlsParameters::new(host.to_string(), tls_builder.unwrap().build().unwrap());
+
// Connect to the host
try_smtp!(
- email_client.connect(&(host.to_utf8().as_str(), port), None),
+ email_client.connect(&(host.to_utf8().as_str(), port), Some(&tls_parameters)),
email_client
);
// Send ehlo and get server info
let ehlo_response = try_smtp!(
- email_client.command(EhloCommand::new(ClientId::new("from".to_string()))),
+ email_client.command(EhloCommand::new(ClientId::new("localhost".to_string()))),
email_client
);
let server_info = ServerInfo::from_response(&ehlo_response);
@@ -66,16 +68,6 @@ pub fn email_exists(from: &str, to: &str, host: &Name, port: u16) -> bool {
{
try_smtp!(email_client.command(StarttlsCommand), email_client);
- let tls_builder = TlsConnector::builder();
- let tls_parameters =
- ClientTlsParameters::new(host.to_string(), tls_builder.unwrap().build().unwrap());
-
- if let Err(err) = email_client.upgrade_tls_stream(&tls_parameters) {
- error!("{}", err);
- email_client.close();
- return false;
- }
-
debug!("Connection is encrypted.");
}