2018-08-23 20:47:41 -07:00
|
|
|
use std::collections::VecDeque;
|
2018-11-11 21:12:30 -08:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::net::{IpAddr, SocketAddr};
|
2018-10-23 22:14:02 -07:00
|
|
|
use std::time::Duration;
|
2018-10-29 14:04:53 -07:00
|
|
|
use std::{fmt, io};
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2018-08-28 16:24:36 -07:00
|
|
|
use futures::{
|
|
|
|
future::{ok, FutureResult},
|
|
|
|
Async, Future, Poll,
|
|
|
|
};
|
2018-08-23 20:47:41 -07:00
|
|
|
use tokio_tcp::{ConnectFuture, TcpStream};
|
|
|
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
2018-08-28 16:24:36 -07:00
|
|
|
use trust_dns_resolver::system_conf::read_system_conf;
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
use super::resolver::{RequestHost, ResolveError, Resolver, ResolverFuture};
|
2018-09-11 09:30:22 -07:00
|
|
|
use super::service::{NewService, Service};
|
2018-08-28 16:24:36 -07:00
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
/// Port of the request
|
|
|
|
pub trait RequestPort {
|
|
|
|
fn port(&self) -> u16;
|
|
|
|
}
|
|
|
|
|
2018-09-10 19:39:55 -07:00
|
|
|
// #[derive(Fail, Debug)]
|
2018-09-10 20:02:55 -07:00
|
|
|
#[derive(Debug)]
|
2018-08-23 20:47:41 -07:00
|
|
|
pub enum ConnectorError {
|
|
|
|
/// Failed to resolve the hostname
|
2018-09-10 19:39:55 -07:00
|
|
|
// #[fail(display = "Failed resolving hostname: {}", _0)]
|
2018-10-29 20:29:47 -07:00
|
|
|
Resolver(ResolveError),
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
/// No dns records
|
2018-11-11 21:12:30 -08:00
|
|
|
// #[fail(display = "No dns records found for the input")]
|
2018-09-10 19:39:55 -07:00
|
|
|
NoRecords,
|
2018-08-23 20:47:41 -07:00
|
|
|
|
2018-10-23 22:14:02 -07:00
|
|
|
/// Connecting took too long
|
|
|
|
// #[fail(display = "Timeout out while establishing connection")]
|
|
|
|
Timeout,
|
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Invalid input
|
|
|
|
InvalidInput,
|
|
|
|
|
2018-08-23 20:47:41 -07:00
|
|
|
/// Connection io error
|
2018-09-10 19:39:55 -07:00
|
|
|
// #[fail(display = "{}", _0)]
|
2018-08-23 20:47:41 -07:00
|
|
|
IoError(io::Error),
|
|
|
|
}
|
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
impl From<ResolveError> for ConnectorError {
|
|
|
|
fn from(err: ResolveError) -> Self {
|
2018-09-10 19:39:55 -07:00
|
|
|
ConnectorError::Resolver(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
impl From<io::Error> for ConnectorError {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
ConnectorError::IoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Connect request
|
2018-10-29 13:27:00 -07:00
|
|
|
#[derive(Eq, PartialEq, Debug, Hash)]
|
2018-10-23 22:14:02 -07:00
|
|
|
pub struct Connect {
|
2018-08-28 16:24:36 -07:00
|
|
|
pub host: String,
|
2018-10-23 22:40:56 -07:00
|
|
|
pub port: u16,
|
2018-10-23 22:14:02 -07:00
|
|
|
pub timeout: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Connect {
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Create new `Connect` instance.
|
2018-10-23 22:40:56 -07:00
|
|
|
pub fn new<T: AsRef<str>>(host: T, port: u16) -> Connect {
|
2018-10-23 22:14:02 -07:00
|
|
|
Connect {
|
2018-10-23 22:40:56 -07:00
|
|
|
port,
|
2018-10-23 22:14:02 -07:00
|
|
|
host: host.as_ref().to_owned(),
|
|
|
|
timeout: Duration::from_secs(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Create `Connect` instance by spliting the string by ':' and convert the second part to u16
|
|
|
|
pub fn with<T: AsRef<str>>(host: T) -> Result<Connect, ConnectorError> {
|
2018-10-29 13:41:54 -07:00
|
|
|
let mut parts_iter = host.as_ref().splitn(2, ':');
|
2018-10-29 20:29:47 -07:00
|
|
|
let host = parts_iter.next().ok_or(ConnectorError::InvalidInput)?;
|
|
|
|
let port_str = parts_iter.next().unwrap_or("");
|
|
|
|
let port = port_str
|
|
|
|
.parse::<u16>()
|
|
|
|
.map_err(|_| ConnectorError::InvalidInput)?;
|
|
|
|
Ok(Connect {
|
|
|
|
port,
|
|
|
|
host: host.to_owned(),
|
|
|
|
timeout: Duration::from_secs(1),
|
|
|
|
})
|
2018-10-29 13:41:54 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:14:02 -07:00
|
|
|
/// Set connect timeout
|
|
|
|
///
|
|
|
|
/// By default timeout is set to a 1 second.
|
|
|
|
pub fn timeout(mut self, timeout: Duration) -> Connect {
|
|
|
|
self.timeout = timeout;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
impl RequestHost for Connect {
|
2018-10-23 22:14:02 -07:00
|
|
|
fn host(&self) -> &str {
|
|
|
|
&self.host
|
|
|
|
}
|
2018-08-28 16:24:36 -07:00
|
|
|
}
|
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
impl RequestPort for Connect {
|
|
|
|
fn port(&self) -> u16 {
|
|
|
|
self.port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:04:53 -07:00
|
|
|
impl fmt::Display for Connect {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}:{}", self.host, self.port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Tcp connector
|
2018-10-23 22:14:02 -07:00
|
|
|
pub struct Connector {
|
|
|
|
resolver: Resolver<Connect>,
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:14:02 -07:00
|
|
|
impl Default for Connector {
|
2018-08-28 16:24:36 -07:00
|
|
|
fn default() -> Self {
|
|
|
|
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
|
|
|
|
(cfg, opts)
|
|
|
|
} else {
|
|
|
|
(ResolverConfig::default(), ResolverOpts::default())
|
2018-08-23 20:47:41 -07:00
|
|
|
};
|
|
|
|
|
2018-08-28 16:24:36 -07:00
|
|
|
Connector::new(cfg, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:14:02 -07:00
|
|
|
impl Connector {
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Create new connector with resolver configuration
|
2018-08-28 16:24:36 -07:00
|
|
|
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
2018-08-29 15:15:24 -07:00
|
|
|
Connector {
|
2018-09-10 19:39:55 -07:00
|
|
|
resolver: Resolver::new(cfg, opts),
|
2018-08-29 15:15:24 -07:00
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
2018-08-27 20:32:49 -07:00
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Create new connector with custom resolver
|
2018-09-10 19:42:51 -07:00
|
|
|
pub fn with_resolver(
|
2018-10-23 22:14:02 -07:00
|
|
|
resolver: Resolver<Connect>,
|
|
|
|
) -> impl Service<Request = Connect, Response = (Connect, TcpStream), Error = ConnectorError>
|
|
|
|
+ Clone {
|
2018-09-10 19:42:51 -07:00
|
|
|
Connector { resolver }
|
|
|
|
}
|
|
|
|
|
2018-10-29 20:29:47 -07:00
|
|
|
/// Create new default connector service
|
2018-08-28 16:24:36 -07:00
|
|
|
pub fn new_service_with_config<E>(
|
2018-10-29 20:29:47 -07:00
|
|
|
cfg: ResolverConfig,
|
|
|
|
opts: ResolverOpts,
|
2018-08-28 16:24:36 -07:00
|
|
|
) -> impl NewService<
|
2018-10-23 22:14:02 -07:00
|
|
|
Request = Connect,
|
|
|
|
Response = (Connect, TcpStream),
|
2018-08-28 16:24:36 -07:00
|
|
|
Error = ConnectorError,
|
|
|
|
InitError = E,
|
|
|
|
> + Clone {
|
2018-10-23 22:14:02 -07:00
|
|
|
move || -> FutureResult<Connector, E> { ok(Connector::new(cfg.clone(), opts)) }
|
2018-08-30 10:06:47 -07:00
|
|
|
}
|
2018-08-27 20:32:49 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:14:02 -07:00
|
|
|
impl Clone for Connector {
|
2018-08-27 20:32:49 -07:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Connector {
|
|
|
|
resolver: self.resolver.clone(),
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:14:02 -07:00
|
|
|
impl Service for Connector {
|
|
|
|
type Request = Connect;
|
|
|
|
type Response = (Connect, TcpStream);
|
2018-08-23 20:47:41 -07:00
|
|
|
type Error = ConnectorError;
|
2018-10-23 22:14:02 -07:00
|
|
|
type Future = ConnectorFuture;
|
2018-08-23 20:47:41 -07:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2018-08-29 15:15:24 -07:00
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
2018-09-10 19:39:55 -07:00
|
|
|
ConnectorFuture {
|
|
|
|
fut: self.resolver.call(req),
|
|
|
|
fut2: None,
|
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:01:51 -07:00
|
|
|
#[doc(hidden)]
|
2018-10-23 22:14:02 -07:00
|
|
|
pub struct ConnectorFuture {
|
|
|
|
fut: ResolverFuture<Connect>,
|
2018-11-11 21:12:30 -08:00
|
|
|
fut2: Option<TcpConnectorResponse<Connect>>,
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:14:02 -07:00
|
|
|
impl Future for ConnectorFuture {
|
|
|
|
type Item = (Connect, TcpStream);
|
2018-08-23 20:47:41 -07:00
|
|
|
type Error = ConnectorError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Some(ref mut fut) = self.fut2 {
|
2018-11-11 21:12:30 -08:00
|
|
|
return fut.poll().map_err(ConnectorError::from);
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
2018-09-10 19:39:55 -07:00
|
|
|
match self.fut.poll().map_err(ConnectorError::from)? {
|
2018-11-11 21:12:30 -08:00
|
|
|
Async::Ready((req, addrs)) => {
|
2018-09-10 19:39:55 -07:00
|
|
|
if addrs.is_empty() {
|
|
|
|
Err(ConnectorError::NoRecords)
|
|
|
|
} else {
|
2018-11-11 21:12:30 -08:00
|
|
|
self.fut2 = Some(TcpConnectorResponse::new(req, addrs));
|
2018-09-10 19:39:55 -07:00
|
|
|
self.poll()
|
|
|
|
}
|
2018-08-23 22:12:10 -07:00
|
|
|
}
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
/// Tcp stream connector service
|
|
|
|
pub struct TcpConnector<T: RequestPort>(PhantomData<T>);
|
|
|
|
|
|
|
|
impl<T: RequestPort> Default for TcpConnector<T> {
|
|
|
|
fn default() -> TcpConnector<T> {
|
|
|
|
TcpConnector(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: RequestPort> Service for TcpConnector<T> {
|
|
|
|
type Request = (T, VecDeque<IpAddr>);
|
|
|
|
type Response = (T, TcpStream);
|
|
|
|
type Error = io::Error;
|
|
|
|
type Future = TcpConnectorResponse<T>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, (req, addrs): Self::Request) -> Self::Future {
|
|
|
|
TcpConnectorResponse::new(req, addrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:01:51 -07:00
|
|
|
#[doc(hidden)]
|
2018-11-11 21:12:30 -08:00
|
|
|
/// Tcp stream connector response future
|
|
|
|
pub struct TcpConnectorResponse<T: RequestPort> {
|
|
|
|
port: u16,
|
|
|
|
req: Option<T>,
|
2018-08-28 16:24:36 -07:00
|
|
|
addr: Option<SocketAddr>,
|
2018-11-11 21:12:30 -08:00
|
|
|
addrs: VecDeque<IpAddr>,
|
2018-08-23 20:47:41 -07:00
|
|
|
stream: Option<ConnectFuture>,
|
|
|
|
}
|
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
impl<T: RequestPort> TcpConnectorResponse<T> {
|
|
|
|
pub fn new(req: T, addrs: VecDeque<IpAddr>) -> TcpConnectorResponse<T> {
|
|
|
|
TcpConnectorResponse {
|
2018-08-23 20:47:41 -07:00
|
|
|
addrs,
|
2018-11-11 21:12:30 -08:00
|
|
|
port: req.port(),
|
2018-08-29 15:15:24 -07:00
|
|
|
req: Some(req),
|
2018-08-28 16:24:36 -07:00
|
|
|
addr: None,
|
2018-08-23 20:47:41 -07:00
|
|
|
stream: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 21:12:30 -08:00
|
|
|
impl<T: RequestPort> Future for TcpConnectorResponse<T> {
|
|
|
|
type Item = (T, TcpStream);
|
|
|
|
type Error = io::Error;
|
2018-08-23 20:47:41 -07:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
// connect
|
|
|
|
loop {
|
|
|
|
if let Some(new) = self.stream.as_mut() {
|
|
|
|
match new.poll() {
|
2018-08-28 16:24:36 -07:00
|
|
|
Ok(Async::Ready(sock)) => {
|
2018-10-23 22:14:02 -07:00
|
|
|
return Ok(Async::Ready((self.req.take().unwrap(), sock)))
|
2018-08-28 16:24:36 -07:00
|
|
|
}
|
2018-08-23 20:47:41 -07:00
|
|
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
|
|
Err(err) => {
|
|
|
|
if self.addrs.is_empty() {
|
2018-11-11 21:12:30 -08:00
|
|
|
return Err(err);
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to connect
|
2018-11-11 21:12:30 -08:00
|
|
|
let addr = SocketAddr::new(self.addrs.pop_front().unwrap(), self.port);
|
2018-08-23 20:47:41 -07:00
|
|
|
self.stream = Some(TcpStream::connect(&addr));
|
2018-08-28 16:24:36 -07:00
|
|
|
self.addr = Some(addr)
|
2018-08-23 20:47:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 22:26:16 -07:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DefaultConnector(Connector);
|
|
|
|
|
|
|
|
impl Default for DefaultConnector {
|
|
|
|
fn default() -> Self {
|
|
|
|
DefaultConnector(Connector::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefaultConnector {
|
|
|
|
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
|
|
|
|
DefaultConnector(Connector::new(cfg, opts))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Service for DefaultConnector {
|
|
|
|
type Request = Connect;
|
|
|
|
type Response = TcpStream;
|
|
|
|
type Error = ConnectorError;
|
|
|
|
type Future = DefaultConnectorFuture;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.0.poll_ready()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
DefaultConnectorFuture {
|
|
|
|
fut: self.0.call(req),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct DefaultConnectorFuture {
|
|
|
|
fut: ConnectorFuture,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for DefaultConnectorFuture {
|
|
|
|
type Item = TcpStream;
|
|
|
|
type Error = ConnectorError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
Ok(Async::Ready(try_ready!(self.fut.poll()).1))
|
|
|
|
}
|
|
|
|
}
|