shadowsocks/
resolver.rs

1use std::io;
2use std::net::IpAddr;
3use std::str::FromStr;
4
5use dns_resolver::Resolver;
6use once_cell::sync::Lazy;
7
8use crate::util::other;
9
10static GLOBAL_RESOLVER: Lazy<Resolver> = Lazy::new(Resolver::new);
11
12pub async fn resolve(host: &str) -> io::Result<IpAddr> {
13    if let Ok(addr) = IpAddr::from_str(host) {
14        return Ok(addr);
15    }
16
17    let results = GLOBAL_RESOLVER
18        .lookup_host(host)
19        .await
20        .map_err(|e| other(&e.to_string()))?;
21
22    if !results.is_empty() {
23        return Ok(results[0]);
24    }
25
26    Err(other("resolve fail"))
27}