1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-01 04:00:08 +01:00
actix-net/actix-connect/src/resolver.rs

187 lines
5.2 KiB
Rust
Raw Normal View History

2018-09-11 04:16:46 +02:00
use std::collections::VecDeque;
2019-03-13 23:37:12 +01:00
use std::marker::PhantomData;
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 23:37:12 +01:00
use crate::connect::{Address, Connect};
2019-03-13 20:40:11 +01:00
use crate::error::ConnectError;
/// DNS Resolver Service factory
2019-03-13 23:37:12 +01:00
pub struct ResolverFactory<T> {
2019-03-13 20:40:11 +01:00
resolver: AsyncResolver,
2019-03-13 23:37:12 +01:00
_t: PhantomData<T>,
2018-09-11 04:16:46 +02:00
}
2019-03-13 23:37:12 +01:00
impl<T> Default for ResolverFactory<T> {
2019-03-13 20:40:11 +01:00
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 23:37:12 +01:00
impl<T> ResolverFactory<T> {
2019-03-13 20:40:11 +01:00
/// 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);
2019-03-13 23:37:12 +01:00
ResolverFactory {
resolver,
_t: PhantomData,
}
2019-03-13 20:40:11 +01:00
}
}
2019-03-13 23:37:12 +01:00
impl<T> Clone for ResolverFactory<T> {
2019-03-13 20:40:11 +01:00
fn clone(&self) -> Self {
ResolverFactory {
resolver: self.resolver.clone(),
2019-03-13 23:37:12 +01:00
_t: PhantomData,
2019-03-13 20:40:11 +01:00
}
}
}
2019-03-13 23:37:12 +01:00
impl<T: Address> NewService for ResolverFactory<T> {
type Request = Connect<T>;
type Response = Connect<T>;
2019-03-13 20:40:11 +01:00
type Error = ConnectError;
2019-03-13 23:37:12 +01:00
type Service = Resolver<T>;
2019-03-13 20:40:11 +01:00
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &()) -> Self::Future {
ok(Resolver {
resolver: self.resolver.clone(),
2019-03-13 23:37:12 +01:00
_t: PhantomData,
2019-03-13 20:40:11 +01:00
})
}
}
/// DNS Resolver Service
2019-03-13 23:37:12 +01:00
pub struct Resolver<T> {
2018-09-11 04:16:46 +02:00
resolver: AsyncResolver,
2019-03-13 23:37:12 +01:00
_t: PhantomData<T>,
2018-09-11 04:16:46 +02:00
}
2019-03-13 23:37:12 +01:00
impl<T> 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)
}
}
2019-03-13 23:37:12 +01:00
impl<T> 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);
2019-03-13 23:37:12 +01:00
Resolver {
resolver,
_t: PhantomData,
}
2018-09-11 04:39:55 +02:00
}
2018-09-11 04:16:46 +02:00
}
2019-03-13 23:37:12 +01:00
impl<T> Clone for Resolver<T> {
2018-09-11 04:16:46 +02:00
fn clone(&self) -> Self {
Resolver {
resolver: self.resolver.clone(),
2019-03-13 23:37:12 +01:00
_t: PhantomData,
2018-09-11 04:16:46 +02:00
}
}
}
2019-03-13 23:37:12 +01:00
impl<T: Address> Service for Resolver<T> {
type Request = Connect<T>;
type Response = Connect<T>;
2019-03-13 20:40:11 +01:00
type Error = ConnectError;
2019-03-13 23:37:12 +01:00
type Future = Either<ResolverFuture<T>, FutureResult<Connect<T>, Self::Error>>;
2018-09-11 04:16:46 +02:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
2019-03-13 23:37:12 +01:00
fn call(&mut self, mut req: Connect<T>) -> Self::Future {
if req.addr.is_some() {
Either::B(ok(req))
} else {
if let Ok(ip) = req.host().parse() {
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
Either::B(ok(req))
} else {
trace!("DNS resolver: resolving host {:?}", req.host());
Either::A(ResolverFuture::new(req, &self.resolver))
2019-03-13 20:40:11 +01:00
}
2018-12-11 03:08:07 +01:00
}
2018-09-11 04:16:46 +02:00
}
}
#[doc(hidden)]
/// Resolver future
2019-03-13 23:37:12 +01:00
pub struct ResolverFuture<T: Address> {
req: Option<Connect<T>>,
2018-09-11 04:16:46 +02:00
lookup: Option<Background<LookupIpFuture>>,
}
2019-03-13 23:37:12 +01:00
impl<T: Address> ResolverFuture<T> {
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
2018-10-30 04:29:47 +01:00
ResolverFuture {
2019-03-13 23:37:12 +01:00
lookup: Some(resolver.lookup_ip(req.host())),
req: Some(req),
2018-09-11 04:16:46 +02:00
}
}
}
2019-03-13 23:37:12 +01:00
impl<T: Address> Future for ResolverFuture<T> {
type Item = Connect<T>;
2019-03-13 20:40:11 +01:00
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: {}",
2019-03-13 23:37:12 +01:00
self.req.as_ref().unwrap().host(),
2019-03-13 20:40:11 +01:00
e
);
e
})? {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(ips) => {
2019-03-13 23:37:12 +01:00
let mut req = self.req.take().unwrap();
2019-03-13 20:40:11 +01:00
let mut addrs: VecDeque<_> = ips
.iter()
2019-03-13 23:37:12 +01:00
.map(|ip| SocketAddr::new(ip, req.port()))
2019-03-13 20:40:11 +01:00
.collect();
2019-03-13 23:37:12 +01:00
trace!(
"DNS resolver: host {:?} resolved to {:?}",
req.host(),
addrs
);
2019-03-13 20:40:11 +01:00
if addrs.len() == 1 {
2019-03-13 23:37:12 +01:00
req.addr = Some(either::Either::Left(addrs.pop_front().unwrap()));
Ok(Async::Ready(req))
2019-03-13 20:40:11 +01:00
} else {
2019-03-13 23:37:12 +01:00
req.addr = Some(either::Either::Right(addrs));
Ok(Async::Ready(req))
2019-03-13 20:40:11 +01:00
}
2018-09-11 04:16:46 +02:00
}
}
}
}