2018-09-11 04:16:46 +02:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::marker::PhantomData;
|
2018-11-12 06:12:30 +01:00
|
|
|
use std::net::IpAddr;
|
2018-09-11 04:16:46 +02:00
|
|
|
|
2018-12-09 19:15:49 +01:00
|
|
|
use actix_service::Service;
|
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};
|
2018-10-30 04:29:47 +01:00
|
|
|
pub use trust_dns_resolver::error::ResolveError;
|
2018-09-11 04:16:46 +02:00
|
|
|
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
|
|
|
use trust_dns_resolver::system_conf::read_system_conf;
|
|
|
|
use trust_dns_resolver::{AsyncResolver, Background};
|
|
|
|
|
2018-11-12 06:12:30 +01:00
|
|
|
/// Host name of the request
|
|
|
|
pub trait RequestHost {
|
2018-09-11 04:16:46 +02:00
|
|
|
fn host(&self) -> &str;
|
|
|
|
}
|
|
|
|
|
2018-11-12 06:12:30 +01:00
|
|
|
impl RequestHost for String {
|
2018-09-11 04:16:46 +02:00
|
|
|
fn host(&self) -> &str {
|
|
|
|
self.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Resolver<T = String> {
|
|
|
|
resolver: AsyncResolver,
|
|
|
|
req: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
2018-11-12 06:12:30 +01:00
|
|
|
impl<T: RequestHost> Default for Resolver<T> {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 06:12:30 +01:00
|
|
|
impl<T: RequestHost> Resolver<T> {
|
|
|
|
/// 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);
|
2018-09-11 04:16:46 +02:00
|
|
|
Resolver {
|
|
|
|
resolver,
|
|
|
|
req: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 04:39:55 +02:00
|
|
|
|
2018-11-12 06:12:30 +01:00
|
|
|
/// Change type of resolver request.
|
|
|
|
pub fn into_request<T2: RequestHost>(&self) -> Resolver<T2> {
|
2018-09-11 04:39:55 +02:00
|
|
|
Resolver {
|
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
req: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for Resolver<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Resolver {
|
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
req: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<T: RequestHost> Service<T> for Resolver<T> {
|
2018-11-12 06:12:30 +01:00
|
|
|
type Response = (T, VecDeque<IpAddr>);
|
2018-10-30 04:29:47 +01:00
|
|
|
type Error = ResolveError;
|
2018-09-11 04:16:46 +02:00
|
|
|
type Future = ResolverFuture<T>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, req: T) -> Self::Future {
|
2018-12-11 03:08:07 +01:00
|
|
|
if let Ok(ip) = req.host().parse() {
|
|
|
|
let mut addrs = VecDeque::new();
|
|
|
|
addrs.push_back(ip);
|
|
|
|
ResolverFuture::new(req, &self.resolver, Some(addrs))
|
|
|
|
} else {
|
|
|
|
ResolverFuture::new(req, &self.resolver, None)
|
|
|
|
}
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
/// Resolver future
|
|
|
|
pub struct ResolverFuture<T> {
|
2018-09-11 04:39:55 +02:00
|
|
|
req: Option<T>,
|
2018-09-11 04:16:46 +02:00
|
|
|
lookup: Option<Background<LookupIpFuture>>,
|
2018-11-12 06:12:30 +01:00
|
|
|
addrs: Option<VecDeque<IpAddr>>,
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 06:12:30 +01:00
|
|
|
impl<T: RequestHost> ResolverFuture<T> {
|
2018-12-11 03:08:07 +01:00
|
|
|
pub fn new(addr: T, resolver: &AsyncResolver, addrs: Option<VecDeque<IpAddr>>) -> Self {
|
2018-09-11 04:16:46 +02:00
|
|
|
// we need to do dns resolution
|
2018-10-30 04:29:47 +01:00
|
|
|
let lookup = Some(resolver.lookup_ip(addr.host()));
|
|
|
|
ResolverFuture {
|
|
|
|
lookup,
|
2018-12-11 03:08:07 +01:00
|
|
|
addrs,
|
2018-10-30 04:29:47 +01:00
|
|
|
req: Some(addr),
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 06:12:30 +01:00
|
|
|
impl<T: RequestHost> Future for ResolverFuture<T> {
|
|
|
|
type Item = (T, VecDeque<IpAddr>);
|
2018-10-30 04:29:47 +01:00
|
|
|
type Error = ResolveError;
|
2018-09-11 04:16:46 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-10-30 04:29:47 +01:00
|
|
|
if let Some(addrs) = self.addrs.take() {
|
|
|
|
Ok(Async::Ready((self.req.take().unwrap(), addrs)))
|
2018-09-11 04:16:46 +02:00
|
|
|
} else {
|
|
|
|
match self.lookup.as_mut().unwrap().poll() {
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
2018-11-12 06:12:30 +01:00
|
|
|
Ok(Async::Ready(ips)) => Ok(Async::Ready((
|
|
|
|
self.req.take().unwrap(),
|
|
|
|
ips.iter().collect(),
|
|
|
|
))),
|
2018-10-30 04:29:47 +01:00
|
|
|
Err(err) => Err(err),
|
2018-09-11 04:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|