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

169 lines
4.6 KiB
Rust
Raw Permalink 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::lookup_ip::LookupIpFuture;
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> ResolverFactory<T> {
2019-03-13 20:40:11 +01:00
/// Create new resolver instance with custom configuration and options.
2019-03-14 06:51:31 +01:00
pub fn new(resolver: AsyncResolver) -> Self {
2019-03-13 23:37:12 +01:00
ResolverFactory {
resolver,
_t: PhantomData,
}
2019-03-13 20:40:11 +01:00
}
2019-03-14 06:51:31 +01:00
pub fn resolver(&self) -> &AsyncResolver {
&self.resolver
}
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> Resolver<T> {
/// Create new resolver instance with custom configuration and options.
2019-03-14 06:51:31 +01:00
pub fn new(resolver: AsyncResolver) -> Self {
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>>,
2019-03-14 06:51:31 +01:00
lookup: Background<LookupIpFuture>,
2018-09-11 04:16:46 +02:00
}
2019-03-13 23:37:12 +01:00
impl<T: Address> ResolverFuture<T> {
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
2019-03-14 06:51:31 +01:00
let lookup = if let Some(host) = req.host().splitn(2, ':').next() {
resolver.lookup_ip(host)
} else {
resolver.lookup_ip(req.host())
};
2018-10-30 04:29:47 +01:00
ResolverFuture {
2019-03-14 06:51:31 +01:00
lookup,
2019-03-13 23:37:12 +01:00
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-14 06:51:31 +01:00
match self.lookup.poll().map_err(|e| {
2019-03-13 20:40:11 +01:00
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-15 19:37:51 +01:00
if addrs.is_empty() {
Err(ConnectError::NoRecords)
} else 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
}
}
}
}