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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::fmt;
use std::net::IpAddr;
use tokio::net;
use async_smtp::{ClientSecurity, Envelope, SendableEmail, SmtpClient};
pub type Result<T> = std::result::Result<T, EnrichmentError>;
#[derive(Copy, Clone, Debug)]
#[deprecated(
since = "0.1.0",
note = "Prone to be removed in the future, does not currently provide any context."
)]
pub struct EnrichmentError;
impl fmt::Display for EnrichmentError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "")
}
}
#[derive(Debug, Clone)]
pub struct DomainMetadata {
pub fqdn: String,
pub ips: Option<Vec<IpAddr>>,
pub smtp: Option<SmtpMetadata>,
}
#[derive(Debug, Clone)]
pub struct SmtpMetadata {
is_positive: bool,
message: String,
}
impl DomainMetadata {
pub fn new(fqdn: String) -> DomainMetadata {
DomainMetadata {
fqdn: fqdn,
ips: None,
smtp: None,
}
}
pub async fn dns_resolvable(&self) -> Result<DomainMetadata> {
match net::lookup_host(&format!("{}:80", self.fqdn)[..]).await {
Ok(addrs) => Ok(DomainMetadata {
fqdn: self.fqdn.clone(),
ips: Some(addrs.map(|addr| addr.ip()).collect()),
smtp: None,
}),
Err(_) => Err(EnrichmentError),
}
}
pub async fn mx_check(&self) -> Result<DomainMetadata> {
let email = SendableEmail::new(
Envelope::new(
Some("twistrs@example.com".parse().unwrap()),
vec!["twistrs@example.com".parse().unwrap()],
)
.unwrap(),
"Twistrs",
"And that's how the cookie crumbles\n",
);
let smtp_domain = format!("{}:25", &self.fqdn);
match SmtpClient::with_security(smtp_domain.clone(), ClientSecurity::None).await {
Ok(smtp) => {
match smtp.into_transport().connect_and_send(email).await {
Ok(response) => Ok(DomainMetadata {
fqdn: self.fqdn.clone(),
ips: None,
smtp: Some(SmtpMetadata {
is_positive: response.is_positive(),
message: response
.message
.into_iter()
.map(|s| s.to_string())
.collect::<String>(),
}),
}),
Err(_) => Ok(DomainMetadata {
fqdn: self.fqdn.clone(),
ips: None,
smtp: None,
}),
}
}
Err(_) => Ok(DomainMetadata {
fqdn: self.fqdn.clone(),
ips: None,
smtp: None,
}),
}
}
pub async fn all(&self) -> Result<Vec<DomainMetadata>> {
let result = futures::join!(self.dns_resolvable(), self.mx_check());
Ok(vec![result.0.unwrap(), result.1.unwrap()])
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures::executor::block_on;
#[tokio::test]
async fn test_mx_check() {
let domain_metadata = DomainMetadata::new(String::from("example.com"));
assert!(block_on(domain_metadata.mx_check()).is_ok());
}
#[tokio::test]
async fn test_all_modes() {
let domain_metadata = DomainMetadata::new(String::from("example.com"));
assert!(block_on(domain_metadata.all()).is_ok());
}
#[tokio::test]
async fn test_dns_lookup() {
let domain_metadata = DomainMetadata::new(String::from("example.com"));
assert!(block_on(domain_metadata.dns_resolvable()).is_ok());
}
}