2019-11-14 18:38:24 +06:00
|
|
|
use std::future::Future;
|
2019-03-13 15:37:12 -07:00
|
|
|
use std::marker::PhantomData;
|
2019-03-13 12:40:11 -07:00
|
|
|
use std::net::SocketAddr;
|
2019-11-14 18:38:24 +06:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-09-10 19:16:46 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use actix_service::{Service, ServiceFactory};
|
|
|
|
use futures::future::{ok, Either, Ready};
|
2018-09-10 19:16:46 -07:00
|
|
|
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
|
|
|
use trust_dns_resolver::{AsyncResolver, Background};
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
use crate::connect::{Address, Connect};
|
2019-03-13 12:40:11 -07:00
|
|
|
use crate::error::ConnectError;
|
2019-04-11 09:57:21 -07:00
|
|
|
use crate::get_default_resolver;
|
2019-03-13 12:40:11 -07:00
|
|
|
|
|
|
|
/// DNS Resolver Service factory
|
2019-03-13 15:37:12 -07:00
|
|
|
pub struct ResolverFactory<T> {
|
2019-04-11 09:57:21 -07:00
|
|
|
resolver: Option<AsyncResolver>,
|
2019-03-13 15:37:12 -07:00
|
|
|
_t: PhantomData<T>,
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T> ResolverFactory<T> {
|
2019-03-13 12:40:11 -07:00
|
|
|
/// Create new resolver instance with custom configuration and options.
|
2019-03-13 22:51:31 -07:00
|
|
|
pub fn new(resolver: AsyncResolver) -> Self {
|
2019-03-13 15:37:12 -07:00
|
|
|
ResolverFactory {
|
2019-04-11 09:57:21 -07:00
|
|
|
resolver: Some(resolver),
|
2019-03-13 15:37:12 -07:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
2019-08-05 09:52:50 -07:00
|
|
|
|
|
|
|
pub fn service(&self) -> Resolver<T> {
|
|
|
|
Resolver {
|
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 09:57:21 -07:00
|
|
|
}
|
2019-03-13 22:51:31 -07:00
|
|
|
|
2019-04-11 09:57:21 -07:00
|
|
|
impl<T> Default for ResolverFactory<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
ResolverFactory {
|
|
|
|
resolver: None,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2019-03-13 22:51:31 -07:00
|
|
|
}
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T> Clone for ResolverFactory<T> {
|
2019-03-13 12:40:11 -07:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ResolverFactory {
|
|
|
|
resolver: self.resolver.clone(),
|
2019-03-13 15:37:12 -07:00
|
|
|
_t: PhantomData,
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<T: Address> ServiceFactory for ResolverFactory<T> {
|
2019-03-13 15:37:12 -07:00
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = Connect<T>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config = ();
|
2019-03-13 15:37:12 -07:00
|
|
|
type Service = Resolver<T>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type InitError = ();
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
2019-03-13 12:40:11 -07:00
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2019-08-05 09:52:50 -07:00
|
|
|
ok(self.service())
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DNS Resolver Service
|
2019-03-13 15:37:12 -07:00
|
|
|
pub struct Resolver<T> {
|
2019-04-11 09:57:21 -07:00
|
|
|
resolver: Option<AsyncResolver>,
|
2019-03-13 15:37:12 -07:00
|
|
|
_t: PhantomData<T>,
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T> Resolver<T> {
|
2018-11-11 21:12:30 -08:00
|
|
|
/// Create new resolver instance with custom configuration and options.
|
2019-03-13 22:51:31 -07:00
|
|
|
pub fn new(resolver: AsyncResolver) -> Self {
|
2019-03-13 15:37:12 -07:00
|
|
|
Resolver {
|
2019-04-11 09:57:21 -07:00
|
|
|
resolver: Some(resolver),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for Resolver<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Resolver {
|
|
|
|
resolver: None,
|
2019-03-13 15:37:12 -07:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2018-09-10 19:39:55 -07:00
|
|
|
}
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T> Clone for Resolver<T> {
|
2018-09-10 19:16:46 -07:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Resolver {
|
|
|
|
resolver: self.resolver.clone(),
|
2019-03-13 15:37:12 -07:00
|
|
|
_t: PhantomData,
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T: Address> Service for Resolver<T> {
|
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = Connect<T>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Error = ConnectError;
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = Either<ResolverFuture<T>, Ready<Result<Connect<T>, Self::Error>>>;
|
2018-09-10 19:16:46 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
fn call(&mut self, mut req: Connect<T>) -> Self::Future {
|
|
|
|
if req.addr.is_some() {
|
2019-11-14 18:38:24 +06:00
|
|
|
Either::Right(ok(req))
|
2019-08-17 05:15:51 +09:00
|
|
|
} else if let Ok(ip) = req.host().parse() {
|
|
|
|
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
2019-11-14 18:38:24 +06:00
|
|
|
Either::Right(ok(req))
|
2019-03-13 15:37:12 -07:00
|
|
|
} else {
|
2019-08-17 05:15:51 +09:00
|
|
|
trace!("DNS resolver: resolving host {:?}", req.host());
|
|
|
|
if self.resolver.is_none() {
|
|
|
|
self.resolver = Some(get_default_resolver());
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
Either::Left(ResolverFuture::new(req, self.resolver.as_ref().unwrap()))
|
2018-12-10 18:08:07 -08:00
|
|
|
}
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
/// Resolver future
|
2019-03-13 15:37:12 -07:00
|
|
|
pub struct ResolverFuture<T: Address> {
|
|
|
|
req: Option<Connect<T>>,
|
2019-03-13 22:51:31 -07:00
|
|
|
lookup: Background<LookupIpFuture>,
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T: Address> ResolverFuture<T> {
|
|
|
|
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
|
2019-03-13 22:51:31 -07:00
|
|
|
let lookup = if let Some(host) = req.host().splitn(2, ':').next() {
|
|
|
|
resolver.lookup_ip(host)
|
|
|
|
} else {
|
|
|
|
resolver.lookup_ip(req.host())
|
|
|
|
};
|
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
ResolverFuture {
|
2019-03-13 22:51:31 -07:00
|
|
|
lookup,
|
2019-03-13 15:37:12 -07:00
|
|
|
req: Some(req),
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T: Address> Future for ResolverFuture<T> {
|
2019-11-14 18:38:24 +06:00
|
|
|
type Output = Result<Connect<T>, ConnectError>;
|
2018-09-10 19:16:46 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
2019-11-18 18:28:54 +06:00
|
|
|
let this = self.get_mut();
|
2019-07-17 02:17:51 +02:00
|
|
|
|
2019-11-18 18:28:54 +06:00
|
|
|
match Pin::new(&mut this.lookup).poll(cx) {
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
Poll::Ready(Ok(ips)) => {
|
|
|
|
let req = this.req.take().unwrap();
|
2019-07-17 02:17:51 +02:00
|
|
|
let port = req.port();
|
|
|
|
let req = req.set_addrs(ips.iter().map(|ip| SocketAddr::new(ip, port)));
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
trace!(
|
|
|
|
"DNS resolver: host {:?} resolved to {:?}",
|
|
|
|
req.host(),
|
2019-07-17 02:17:51 +02:00
|
|
|
req.addrs()
|
2019-03-13 15:37:12 -07:00
|
|
|
);
|
2019-07-17 02:17:51 +02:00
|
|
|
|
|
|
|
if req.addr.is_none() {
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Ready(Err(ConnectError::NoRecords))
|
2019-03-13 12:40:11 -07:00
|
|
|
} else {
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Ready(Ok(req))
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Ready(Err(e)) => {
|
|
|
|
trace!(
|
|
|
|
"DNS resolver: failed to resolve host {:?} err: {}",
|
|
|
|
this.req.as_ref().unwrap().host(),
|
|
|
|
e
|
|
|
|
);
|
|
|
|
Poll::Ready(Err(e.into()))
|
|
|
|
}
|
2018-09-10 19:16:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|