Skip to content

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

Handle all cases

Amaury Martinycfba2c7d1003
diff --git a/src/main.rs b/src/main.rs
index 273aafe6..6acc1ab8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,6 +5,7 @@ extern crate env_logger;
extern crate log;
use clap::App;
+use std::process;
mod mx_hosts;
mod telnet;
@@ -32,8 +33,19 @@ fn main() {
debug!("Domain is: {}", domain);
let hosts = mx_hosts::get_mx_lookup(domain);
- for host in hosts.iter() {
- println!("{}", host.exchange());
- telnet::connect("[email protected]", email, host.exchange(), 25);
+ let ports = vec![25, 465, 587];
+ for port in ports.into_iter() {
+ for host in hosts.iter() {
+ if telnet::connect(
+ email,
+ host.exchange(),
+ port,
+ ) {
+ println!("true");
+ process::exit(0x0100);
+ };
+ }
}
+ println!("false");
}
diff --git a/src/telnet.rs b/src/telnet.rs
index eb3b1bc8..1739e062 100644
--- a/src/telnet.rs
+++ b/src/telnet.rs
@@ -15,69 +15,94 @@ enum Step {
NotFound,
}
-pub fn connect(from: &str, to: &str, domain: Name, port: u16) {
- debug!("Connecting to: {}...", domain);
+pub fn connect(from: &str, to: &str, domain: &Name, port: u16) -> bool {
+ debug!("Telnet connection to {}...", domain);
- let mut connection =
- Telnet::connect((domain, port), 256).expect("Couldn't connect to the server...");
+ let mut connection = match Telnet::connect((domain.to_utf8().as_str(), port), 256) {
+ Ok(i) => i,
+ Err(e) => {
+ debug!("Failed to connect, {}", e);
+ return false;
+ }
+ };
let mut step = Step::Welcome;
+
loop {
- let event = connection.read().expect("Read Error");
- match event {
- TelnetEvent::Data(read_buffer) => {
- // `answer` is what we get from the server
- let answer = from_utf8(&read_buffer).unwrap();
+ match step {
+ Step::Welcome | Step::SentHelo | Step::SentMailFrom | Step::SentRcptTo => {
+ // If we are in one of the above steps, then we read from the
+ // telnet connection.
+ let event = match connection.read() {
+ Ok(i) => i,
+ Err(_) => break,
+ };
- debug!("Received: {}", answer);
+ match event {
+ TelnetEvent::Data(read_buffer) => {
+ // `answer` is what we get from the server
+ let answer = match from_utf8(&read_buffer) {
+ Ok(i) => i,
+ _ => break,
+ };
- // `question` is what we ask the server
- let question = match step {
- Step::Welcome => {
- if answer.contains("220") {
- step = Step::SentHelo;
- String::from("HELO Hi\n")
- } else {
- panic!("Got an unexpected answer at Welcome step.");
- }
- }
- Step::SentHelo => {
- if answer.contains("250") {
- step = Step::SentMailFrom;
- format!("{}{}{}", "MAIL FROM: <", from, ">\n")
- } else {
- panic!("Got an unexpected answer at SentHelo step.");
- }
- }
- Step::SentMailFrom => {
- if answer.contains("250") {
- step = Step::SentRcptTo;
- format!("{}{}{}", "RCPT TO: <", to, ">\n")
- } else {
- panic!("Got an unexpected answer at SentMailFrom step.");
- }
- }
- Step::SentRcptTo => {
- // 2.1.5 means address exists
- if answer.contains("2.1.5") {
- step = Step::Found;
- } else {
- step = Step::NotFound;
- }
- String::from("QUIT\n")
- }
- _ => panic!("Got an unexpected answer at Found/NotFound step."),
- };
+ debug!("Received: {}", answer);
+
+ // `question` is what we ask the server
+ let question = match step {
+ Step::Welcome => {
+ if answer.contains("220") {
+ step = Step::SentHelo;
+ String::from("HELO Hi\n")
+ } else {
+ break;
+ }
+ }
+ Step::SentHelo => {
+ if answer.contains("250") {
+ step = Step::SentMailFrom;
+ format!("{}{}{}", "MAIL FROM: <", from, ">\n")
+ } else {
+ break;
+ }
+ }
+ Step::SentMailFrom => {
+ if answer.contains("2.1.0") {
+ step = Step::SentRcptTo;
+ format!("{}{}{}", "RCPT TO: <", to, ">\n")
+ } else {
+ break;
+ }
+ }
+ Step::SentRcptTo => {
+ // 2.1.5 means address exists
+ if answer.contains("2.1.5") {
+ step = Step::Found;
+ } else {
+ step = Step::NotFound;
+ }
+ String::from("QUIT\n")
+ }
+ _ => panic!("Step is Found/NotFound where it shouldn't be."),
+ };
- debug!("Sent: {}", question);
+ debug!("Sent: {}", question);
- // Buffer to write to telnet
- let write_buffer = question.as_bytes(); // TODO Define buffer depending on read_buffer
- connection
- .write(&write_buffer)
- .expect("Error while writing");
+ // Buffer to write to telnet
+ let write_buffer = question.as_bytes(); // TODO Define buffer depending on read_buffer
+ if let Err(e) = connection.write(&write_buffer) {
+ debug!("Error while writing, {}", e);
+ }
+ }
+ _ => break,
+ }
}
- _ => {}
+ _ => break,
}
}
+
+ match step {
+ Step::Found => true,
+ _ => false,
+ }
}