2018-09-11 04:16:46 +02:00
|
|
|
use std::collections::VecDeque;
|
2019-03-13 20:40:11 +01:00
|
|
|
use std::net::SocketAddr;
|
2018-09-11 04:16:46 +02:00
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
use actix_service::{NewService, Service};
|
|
|
|
use futures::future::{ok, Either, FutureResult};
|
2018-12-11 03:08:07 +01:00
|
|
|
use futures::{Async, Future, Poll};
|
2018-09-11 04:16:46 +02:00
|
|
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
|
|
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
|
|
|
use trust_dns_resolver::system_conf::read_system_conf;
|
|
|
|
use trust_dns_resolver::{AsyncResolver, Background};
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
use crate::connect::Connect;
|
|
|
|
use crate::error::ConnectError;
|
|
|
|
|
|
|
|
/// DNS Resolver Service factory
|
|
|
|
pub struct ResolverFactory {
|
|
|
|
resolver: AsyncResolver,
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl Default for ResolverFactory {
|
|
|
|
fn default() -> Self {
|
|
|
|
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
|
|
|
|
(cfg, opts)
|
|
|
|
} else {
|
|
|
|
(ResolverConfig::default(), ResolverOpts::default())
|
|
|
|
};
|
|
|
|
|
|
|
|
ResolverFactory::new(cfg, opts)
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl ResolverFactory {
|
|
|
|
/// Create new resolver instance with custom configuration and options.
|
|
|
|
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
|
|
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
|
|
|
tokio_current_thread::spawn(bg);
|
|
|
|
ResolverFactory { resolver }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for ResolverFactory {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ResolverFactory {
|
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NewService for ResolverFactory {
|
|
|
|
type Request = Connect;
|
|
|
|
type Response = Connect;
|
|
|
|
type Error = ConnectError;
|
|
|
|
type Service = Resolver;
|
|
|
|
type InitError = ();
|
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
ok(Resolver {
|
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DNS Resolver Service
|
|
|
|
pub struct Resolver {
|
2018-09-11 04:16:46 +02:00
|
|
|
resolver: AsyncResolver,
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl Default for Resolver {
|
2018-09-11 04:16:46 +02:00
|
|
|
fn default() -> Self {
|
|
|
|
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
|
|
|
|
(cfg, opts)
|
|
|
|
} else {
|
|
|
|
(ResolverConfig::default(), ResolverOpts::default())
|
|
|
|
};
|
|
|
|
|
|
|
|
Resolver::new(cfg, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl Resolver {
|
2018-11-12 06:12:30 +01:00
|
|
|
/// Create new resolver instance with custom configuration and options.
|
2018-09-11 04:16:46 +02:00
|
|
|
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
|
|
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
2019-01-26 22:15:17 +01:00
|
|
|
tokio_current_thread::spawn(bg);
|
2019-03-13 20:40:11 +01:00
|
|
|
Resolver { resolver }
|
2018-09-11 04:39:55 +02:00
|
|
|
}
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl Clone for Resolver {
|
2018-09-11 04:16:46 +02:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Resolver {
|
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl Service for Resolver {
|
|
|
|
type Request = Connect;
|
|
|
|
type Response = Connect;
|
|
|
|
type Error = ConnectError;
|
|
|
|
type Future = Either<ResolverFuture, FutureResult<Connect, Self::Error>>;
|
2018-09-11 04:16:46 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
fn call(&mut self, req: Connect) -> Self::Future {
|
|
|
|
match req {
|
|
|
|
Connect::Host { host, port } => {
|
|
|
|
if let Ok(ip) = host.parse() {
|
|
|
|
Either::B(ok(Connect::Addr {
|
|
|
|
host: host,
|
|
|
|
addr: either::Either::Left(SocketAddr::new(ip, port)),
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
trace!("DNS resolver: resolving host {:?}", host);
|
|
|
|
Either::A(ResolverFuture::new(host, port, &self.resolver))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
other => Either::B(ok(other)),
|
2018-12-11 03:08:07 +01:00
|
|
|
}
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
/// Resolver future
|
2019-03-13 20:40:11 +01:00
|
|
|
pub struct ResolverFuture {
|
|
|
|
host: Option<String>,
|
|
|
|
port: u16,
|
2018-09-11 04:16:46 +02:00
|
|
|
lookup: Option<Background<LookupIpFuture>>,
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl ResolverFuture {
|
|
|
|
pub fn new(host: String, port: u16, resolver: &AsyncResolver) -> Self {
|
2018-10-30 04:29:47 +01:00
|
|
|
ResolverFuture {
|
2019-03-13 20:40:11 +01:00
|
|
|
lookup: Some(resolver.lookup_ip(host.as_str())),
|
|
|
|
host: Some(host),
|
|
|
|
port,
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
impl Future for ResolverFuture {
|
|
|
|
type Item = Connect;
|
|
|
|
type Error = ConnectError;
|
2018-09-11 04:16:46 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-03-13 20:40:11 +01:00
|
|
|
match self.lookup.as_mut().unwrap().poll().map_err(|e| {
|
|
|
|
trace!(
|
|
|
|
"DNS resolver: failed to resolve host {:?} err: {}",
|
|
|
|
self.host.as_ref().unwrap(),
|
|
|
|
e
|
|
|
|
);
|
|
|
|
e
|
|
|
|
})? {
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(ips) => {
|
|
|
|
let host = self.host.take().unwrap();
|
|
|
|
let mut addrs: VecDeque<_> = ips
|
|
|
|
.iter()
|
|
|
|
.map(|ip| SocketAddr::new(ip, self.port))
|
|
|
|
.collect();
|
|
|
|
trace!("DNS resolver: host {:?} resolved to {:?}", host, addrs);
|
|
|
|
if addrs.len() == 1 {
|
|
|
|
Ok(Async::Ready(Connect::Addr {
|
|
|
|
addr: either::Either::Left(addrs.pop_front().unwrap()),
|
|
|
|
host,
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Ok(Async::Ready(Connect::Addr {
|
|
|
|
addr: either::Either::Right(addrs),
|
|
|
|
host,
|
|
|
|
}))
|
|
|
|
}
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|