1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
extern crate regex;
#[macro_use]
extern crate lazy_static;
extern crate failure;
extern crate futures;
extern crate trust_dns;
use failure::Error;
use futures::{future, prelude::*};
use regex::Regex;
use trust_dns::client::ClientHandle;
use trust_dns::client::{ClientFuture};
use trust_dns::rr::{DNSClass, Name, RecordType};
use trust_dns::udp::UdpClientStream;
lazy_static! {
static ref EMAIL_RE: Regex = Regex::new(r".+@.+\..+").unwrap();
}
pub fn validate(email: &str) -> Box<Future<Item = bool, Error = Error>> {
if !validate_re(email) {
return Box::new(future::ok(false));
}
Box::new(validate_mx(email))
}
pub fn validate_re(email: &str) -> bool {
EMAIL_RE.is_match(email)
}
pub fn validate_mx(email: &str) -> impl Future<Item = bool, Error = Error> {
let domain = format!("{}.", email.split('@').nth(1).unwrap());
let address = "1.1.1.1:53".parse().unwrap();
let (stream, sender) = UdpClientStream::new(address);
ClientFuture::new(stream, sender, None)
.from_err()
.and_then(move |mut client| {
let name: Name = domain.parse().unwrap();
client
.query(name, DNSClass::IN, RecordType::MX)
.from_err()
.map(|response| {
response.answers().len() > 0
})
})
}
#[cfg(test)]
mod tests {
extern crate tokio;
use self::tokio::runtime::Runtime;
use super::{validate_re, validate_mx};
#[test]
fn test_validate_re() {
assert!(validate_re("user@example.com"));
assert!(!validate_re("user"));
assert!(!validate_re("user@example"));
}
#[test]
fn test_validate_mx() {
let mut rt = Runtime::new().unwrap();
assert!(!rt.block_on(validate_mx("user@example.com")).unwrap());
assert!(rt.block_on(validate_mx("user@gmail.com")).unwrap());
assert!(rt.block_on(validate_mx("user@github.com")).unwrap());
assert!(rt.block_on(validate_mx("user@nwytg.net")).unwrap());
}
}