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;

// pub async fn resolve(host: &str) -> io::Result<IpAddr> {
//     let host = format!("{}:0", host);
//     let ip = tokio::task::spawn_blocking(move || match host[..].to_socket_addrs() {
//         Ok(it) => {
//             let mut it = it.filter(|x| match x.ip() {
//                 IpAddr::V4(_) => true,
//                 IpAddr::V6(_) => false,
//             });
//             if let Some(addr) = it.next() {
//                 Ok(addr.ip())
//             } else {
//                 Err(other("no ip return"))
//             }
//         }
//         Err(e) => Err(e),
//     })
//     .await?;
//     ip
// }

lazy_static! {
    // setup the global Resolver
    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))),
    }
}