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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use errors::Result;
use failure::Fail;
use std::time::Duration;
use std::net::IpAddr;
use std::fmt;
use futures::Future;
use futures::Poll;
use tokio::runtime::Runtime;
use trust_dns_resolver as tdr;
use trust_dns_resolver::error::ResolveErrorKind;
use trust_dns_resolver::lookup::Lookup;
use trust_dns_resolver::lookup_ip::LookupIp;
use trust_dns_resolver::system_conf;
use trust_dns_proto::rr::rdata;
use trust_dns_proto::rr::record_data;
pub use trust_dns_proto::rr::record_type::RecordType;
use trust_dns_resolver::config::{ResolverConfig,
ResolverOpts,
NameServerConfig,
Protocol};
use std::io;
use std::net::{SocketAddr, Ipv4Addr, Ipv6Addr};
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct DnsConfig {
pub ns: Vec<SocketAddr>,
}
impl DnsConfig {
pub fn from_system() -> Result<DnsConfig> {
let (conf, _opts) = system_conf::read_system_conf()?;
let ns = conf.name_servers().into_iter()
.map(|x| x.socket_addr)
.collect();
Ok(DnsConfig {
ns,
})
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum DnsReply {
#[serde(rename = "success")]
Success(Vec<RData>),
#[serde(rename = "error")]
Error(DnsError),
}
impl From<Lookup> for DnsReply {
fn from(lookup: Lookup) -> DnsReply {
let mut records = Vec::new();
for data in lookup.iter() {
records.push(data.into());
}
DnsReply::Success(records)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum DnsError {
#[serde(rename = "NX")]
NXDomain,
}
impl Into<DnsReply> for DnsError {
#[inline]
fn into(self) -> DnsReply {
DnsReply::Error(self)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum RData {
A(Ipv4Addr),
AAAA(Ipv6Addr),
CNAME(String),
MX((u16, String)),
NS(String),
PTR(String),
SOA(SOA),
SRV((String, u16)),
TXT(Vec<u8>),
Other(String),
}
impl<'a> From<&'a record_data::RData> for RData {
fn from(rdata: &'a record_data::RData) -> RData {
use trust_dns_proto::rr::record_data::RData::*;
match rdata {
A(ip) => RData::A(ip.clone()),
AAAA(ip) => RData::AAAA(ip.clone()),
CNAME(name) => RData::CNAME(name.to_string()),
MX(mx) => RData::MX((mx.preference(), mx.exchange().to_string())),
NS(ns) => RData::NS(ns.to_string()),
PTR(ptr) => RData::PTR(ptr.to_string()),
SOA(soa) => RData::SOA(soa.into()),
SRV(srv) => RData::SRV((srv.target().to_string(), srv.port())),
TXT(txt) => RData::TXT(txt.iter()
.fold(Vec::new(), |mut a, b| {
a.extend(b.iter());
a
})),
_ => RData::Other("unknown".to_string()),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SOA {
mname: String,
rname: String,
serial: u32,
refresh: i32,
retry: i32,
expire: i32,
minimum: u32
}
impl<'a> From<&'a rdata::soa::SOA> for SOA {
fn from(soa: &'a rdata::soa::SOA) -> SOA {
SOA {
mname: soa.mname().to_string(),
rname: soa.rname().to_string(),
serial: soa.serial(),
refresh: soa.refresh(),
retry: soa.retry(),
expire: soa.expire(),
minimum: soa.minimum(),
}
}
}
pub struct Resolver {
resolver: tdr::Resolver,
}
impl Resolver {
pub fn cloudflare() -> Resolver {
Resolver::with_udp_addr(&["1.1.1.1:53".parse().unwrap(),
"1.0.0.1:53".parse().unwrap()]).unwrap()
}
pub fn from_system() -> Result<Resolver> {
let resolver = tdr::Resolver::from_system_conf()?;
Ok(Resolver {
resolver,
})
}
pub fn from_config(config: DnsConfig) -> Result<Resolver> {
let mut ns = ResolverConfig::default();
for socket_addr in config.ns {
ns.add_name_server(NameServerConfig {
socket_addr,
protocol: Protocol::Udp,
tls_dns_name: None,
});
}
let opts = ResolverOpts::default();
let resolver = tdr::Resolver::new(ns, opts)?;
Ok(Resolver {
resolver,
})
}
pub fn with_udp_addr(recursors: &[SocketAddr]) -> Result<Resolver> {
let mut config = ResolverConfig::new();
for recursor in recursors {
config.add_name_server(NameServerConfig {
socket_addr: recursor.to_owned(),
protocol: Protocol::Udp,
tls_dns_name: None,
});
}
let mut opts = ResolverOpts::default();
opts.use_hosts_file = false;
opts.timeout = Duration::from_secs(1);
let resolver = tdr::Resolver::new(config, opts)?;
Ok(Resolver {
resolver,
})
}
pub fn with_udp(recursors: &[IpAddr]) -> Result<Resolver> {
let recursors = recursors.into_iter()
.map(|x| SocketAddr::new(x.to_owned(), 53))
.collect::<Vec<_>>();
Resolver::with_udp_addr(&recursors)
}
#[inline]
fn transform(lookup: LookupIp) -> Vec<IpAddr> {
lookup.iter().collect()
}
}
impl fmt::Debug for Resolver {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Resolver {{ ... }}")
}
}
pub trait DnsResolver {
fn resolve(&self, name: &str) -> Result<Vec<IpAddr>>;
fn resolve_adv(&self, name: &str, record: RecordType) -> Result<DnsReply>;
}
impl DnsResolver for Resolver {
fn resolve(&self, name: &str) -> Result<Vec<IpAddr>> {
self.resolver.lookup_ip(name)
.map(Resolver::transform)
.map_err(|err| format_err!("resolve error: {}", err))
}
fn resolve_adv(&self, name: &str, record: RecordType) -> Result<DnsReply> {
match self.resolver.lookup(name, record) {
Ok(reply) => Ok(reply.into()),
Err(err) => match err.kind() {
ResolveErrorKind::NoRecordsFound {
query: _,
valid_until: _,
} => Ok(DnsError::NXDomain.into()),
_ => Err(err.context("Failed to resolve").into()),
},
}
}
}
pub struct AsyncResolver {
resolver: tdr::AsyncResolver,
}
impl AsyncResolver {
pub fn cloudflare() -> AsyncResolver {
AsyncResolver::with_udp_addr(&[String::from("1.1.1.1:53"),
String::from("1.0.0.1:53")]).unwrap()
}
pub fn with_udp_addr(recursors: &[String]) -> Result<AsyncResolver> {
let mut config = ResolverConfig::new();
for recursor in recursors {
config.add_name_server(NameServerConfig {
socket_addr: recursor.parse()?,
protocol: Protocol::Udp,
tls_dns_name: None,
});
}
let mut opts = ResolverOpts::default();
opts.timeout = Duration::from_secs(1);
let mut rt = Runtime::new()?;
let (resolver, worker) = tdr::AsyncResolver::new(config, opts);
let worker = rt.block_on(worker);
let _worker = match worker {
Ok(worker) => worker,
Err(_) => bail!("resolver init error"),
};
Ok(AsyncResolver {
resolver,
})
}
pub fn with_udp(recursors: &[String]) -> Result<AsyncResolver> {
let recursors = recursors.iter()
.map(|x| format!("{}:53", x))
.collect::<Vec<_>>();
AsyncResolver::with_udp_addr(&recursors)
}
pub fn resolve(&self, name: &str) -> Resolving {
let fut = self.resolver.lookup_ip(name)
.map(|lookup| {
Resolver::transform(lookup)
})
.map_err(|err| {
io::Error::new(io::ErrorKind::Other, format!("{:?}", err))
});
Resolving(Box::new(fut))
}
}
pub struct Resolving(
Box<Future<Item = Vec<IpAddr>, Error = io::Error> + Send>,
);
impl Future for Resolving {
type Item = Vec<IpAddr>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
#[cfg(test)]
mod tests {
extern crate serde_json;
use super::*;
#[test]
fn verify_dns_config() {
let config = DnsConfig::from_system().expect("DnsConfig::from_system");
let json = serde_json::to_string(&config).expect("to json");
println!("{:?}", json);
let config = serde_json::from_str::<DnsConfig>(&json).expect("to json");
let resolver = Resolver::from_config(config).expect("Resolver::from_config");
resolver.resolve("example.com").expect("resolve failed");
}
#[test]
fn verify_dns_config_from_json() {
let json = r#"{"ns":["1.1.1.1:53","1.1.1.1:53","1.0.0.1:53","1.0.0.1:53"]}"#;
let _config = serde_json::from_str::<DnsConfig>(&json).expect("to json");
}
}