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
use std::io;
use std::net::IpAddr;
use crate::util::other;
use lazy_static::lazy_static;
use tokio;
use trust_dns_resolver::AsyncResolver;
lazy_static! {
static ref GLOBAL_DNS_RESOLVER: AsyncResolver = {
let (resolver, bg) = AsyncResolver::from_system_conf().expect("Failed to create AsyncResolver");
tokio::spawn(bg);
resolver
};
}
pub async fn resolve(host: &str) -> io::Result<IpAddr> {
match GLOBAL_DNS_RESOLVER.lookup_ip(host).await {
Ok(r) => {
if let Some(addr) = r.iter().next() {
Ok(addr)
} else {
Err(other("no ip return"))
}
}
Err(e) => Err(other(&format!("resolve fail: {:?}", e))),
}
}