2018-12-10 16:16:40 -08:00
|
|
|
use std::marker::PhantomData;
|
2019-08-05 09:52:50 -07:00
|
|
|
use std::{fmt, io};
|
2018-12-10 16:16:40 -08:00
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
|
|
|
use actix_service::{NewService, Service};
|
2019-08-05 09:52:50 -07:00
|
|
|
use futures::{future::ok, future::FutureResult, try_ready, Async, Future, Poll};
|
2018-12-10 16:16:40 -08:00
|
|
|
use openssl::ssl::{HandshakeError, SslConnector};
|
|
|
|
use tokio_openssl::{ConnectAsync, SslConnectorExt, SslStream};
|
2019-08-05 09:52:50 -07:00
|
|
|
use tokio_tcp::TcpStream;
|
|
|
|
use trust_dns_resolver::AsyncResolver;
|
2018-12-10 16:16:40 -08:00
|
|
|
|
2019-08-05 09:52:50 -07:00
|
|
|
use crate::{
|
|
|
|
Address, Connect, ConnectError, ConnectService, ConnectServiceFactory, Connection,
|
|
|
|
};
|
2018-12-10 16:16:40 -08:00
|
|
|
|
|
|
|
/// Openssl connector factory
|
2019-03-13 15:51:21 -07:00
|
|
|
pub struct OpensslConnector<T, U> {
|
2018-12-10 16:16:40 -08:00
|
|
|
connector: SslConnector,
|
2019-03-13 15:51:21 -07:00
|
|
|
_t: PhantomData<(T, U)>,
|
2018-12-10 16:16:40 -08:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:51:21 -07:00
|
|
|
impl<T, U> OpensslConnector<T, U> {
|
2018-12-10 16:16:40 -08:00
|
|
|
pub fn new(connector: SslConnector) -> Self {
|
|
|
|
OpensslConnector {
|
|
|
|
connector,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:51:21 -07:00
|
|
|
impl<T, U> OpensslConnector<T, U>
|
2019-03-13 15:37:12 -07:00
|
|
|
where
|
|
|
|
T: Address,
|
|
|
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
|
|
|
{
|
2018-12-10 16:16:40 -08:00
|
|
|
pub fn service(
|
|
|
|
connector: SslConnector,
|
2019-03-13 12:40:11 -07:00
|
|
|
) -> impl Service<
|
2019-03-13 15:37:12 -07:00
|
|
|
Request = Connection<T, U>,
|
|
|
|
Response = Connection<T, SslStream<U>>,
|
|
|
|
Error = HandshakeError<U>,
|
2019-03-13 12:40:11 -07:00
|
|
|
> {
|
2018-12-10 16:16:40 -08:00
|
|
|
OpensslConnectorService {
|
|
|
|
connector: connector,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:51:21 -07:00
|
|
|
impl<T, U> Clone for OpensslConnector<T, U> {
|
2018-12-10 16:16:40 -08:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
connector: self.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:03:50 -07:00
|
|
|
impl<T: Address, U> NewService for OpensslConnector<T, U>
|
2019-03-13 12:40:11 -07:00
|
|
|
where
|
2019-03-13 15:37:12 -07:00
|
|
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
2019-02-22 12:44:37 -08:00
|
|
|
{
|
2019-03-13 15:37:12 -07:00
|
|
|
type Request = Connection<T, U>;
|
|
|
|
type Response = Connection<T, SslStream<U>>;
|
|
|
|
type Error = HandshakeError<U>;
|
2019-05-12 06:03:50 -07:00
|
|
|
type Config = ();
|
2019-03-13 15:37:12 -07:00
|
|
|
type Service = OpensslConnectorService<T, U>;
|
2019-03-13 15:51:21 -07:00
|
|
|
type InitError = ();
|
2018-12-10 16:16:40 -08:00
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
2019-02-22 12:44:37 -08:00
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
2018-12-10 16:16:40 -08:00
|
|
|
ok(OpensslConnectorService {
|
|
|
|
connector: self.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
pub struct OpensslConnectorService<T, U> {
|
2018-12-10 16:16:40 -08:00
|
|
|
connector: SslConnector,
|
2019-03-13 15:37:12 -07:00
|
|
|
_t: PhantomData<(T, U)>,
|
2018-12-10 16:16:40 -08:00
|
|
|
}
|
|
|
|
|
2019-08-05 09:52:50 -07:00
|
|
|
impl<T, U> Clone for OpensslConnectorService<T, U> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
connector: self.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T: Address, U> Service for OpensslConnectorService<T, U>
|
2019-03-13 12:40:11 -07:00
|
|
|
where
|
2019-03-13 15:37:12 -07:00
|
|
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
2019-03-13 12:40:11 -07:00
|
|
|
{
|
2019-03-13 15:37:12 -07:00
|
|
|
type Request = Connection<T, U>;
|
|
|
|
type Response = Connection<T, SslStream<U>>;
|
|
|
|
type Error = HandshakeError<U>;
|
|
|
|
type Future = ConnectAsyncExt<T, U>;
|
2018-12-10 16:16:40 -08:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
fn call(&mut self, stream: Connection<T, U>) -> Self::Future {
|
2019-03-13 12:40:11 -07:00
|
|
|
trace!("SSL Handshake start for: {:?}", stream.host());
|
|
|
|
let (io, stream) = stream.replace(());
|
2018-12-10 16:16:40 -08:00
|
|
|
ConnectAsyncExt {
|
2019-03-13 12:40:11 -07:00
|
|
|
fut: SslConnectorExt::connect_async(&self.connector, stream.host(), io),
|
|
|
|
stream: Some(stream),
|
2018-12-10 16:16:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
pub struct ConnectAsyncExt<T, U> {
|
|
|
|
fut: ConnectAsync<U>,
|
|
|
|
stream: Option<Connection<T, ()>>,
|
2018-12-10 16:16:40 -08:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
impl<T: Address, U> Future for ConnectAsyncExt<T, U>
|
2018-12-10 16:16:40 -08:00
|
|
|
where
|
2019-03-13 15:37:12 -07:00
|
|
|
U: AsyncRead + AsyncWrite + fmt::Debug,
|
2018-12-10 16:16:40 -08:00
|
|
|
{
|
2019-03-13 15:37:12 -07:00
|
|
|
type Item = Connection<T, SslStream<U>>;
|
|
|
|
type Error = HandshakeError<U>;
|
2018-12-10 16:16:40 -08:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-03-13 12:40:11 -07:00
|
|
|
match self.fut.poll().map_err(|e| {
|
|
|
|
trace!("SSL Handshake error: {:?}", e);
|
|
|
|
e
|
|
|
|
})? {
|
|
|
|
Async::Ready(stream) => {
|
|
|
|
let s = self.stream.take().unwrap();
|
|
|
|
trace!("SSL Handshake success: {:?}", s.host());
|
|
|
|
Ok(Async::Ready(s.replace(stream).1))
|
|
|
|
}
|
2018-12-10 16:16:40 -08:00
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-05 09:52:50 -07:00
|
|
|
|
|
|
|
pub struct OpensslConnectServiceFactory<T> {
|
|
|
|
tcp: ConnectServiceFactory<T>,
|
|
|
|
openssl: OpensslConnector<T, TcpStream>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> OpensslConnectServiceFactory<T> {
|
|
|
|
/// Construct new OpensslConnectService factory
|
|
|
|
pub fn new(connector: SslConnector) -> Self {
|
|
|
|
OpensslConnectServiceFactory {
|
|
|
|
tcp: ConnectServiceFactory::default(),
|
|
|
|
openssl: OpensslConnector::new(connector),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct new connect service with custom dns resolver
|
|
|
|
pub fn with_resolver(connector: SslConnector, resolver: AsyncResolver) -> Self {
|
|
|
|
OpensslConnectServiceFactory {
|
|
|
|
tcp: ConnectServiceFactory::with_resolver(resolver),
|
|
|
|
openssl: OpensslConnector::new(connector),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct openssl connect service
|
|
|
|
pub fn service(&self) -> OpensslConnectService<T> {
|
|
|
|
OpensslConnectService {
|
|
|
|
tcp: self.tcp.service(),
|
|
|
|
openssl: OpensslConnectorService {
|
|
|
|
connector: self.openssl.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for OpensslConnectServiceFactory<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
OpensslConnectServiceFactory {
|
|
|
|
tcp: self.tcp.clone(),
|
|
|
|
openssl: self.openssl.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Address> NewService for OpensslConnectServiceFactory<T> {
|
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = SslStream<TcpStream>;
|
|
|
|
type Error = ConnectError;
|
|
|
|
type Config = ();
|
|
|
|
type Service = OpensslConnectService<T>;
|
|
|
|
type InitError = ();
|
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
ok(self.service())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct OpensslConnectService<T> {
|
|
|
|
tcp: ConnectService<T>,
|
|
|
|
openssl: OpensslConnectorService<T, TcpStream>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Address> Service for OpensslConnectService<T> {
|
|
|
|
type Request = Connect<T>;
|
|
|
|
type Response = SslStream<TcpStream>;
|
|
|
|
type Error = ConnectError;
|
|
|
|
type Future = OpensslConnectServiceResponse<T>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
|
|
|
OpensslConnectServiceResponse {
|
|
|
|
fut1: Some(self.tcp.call(req)),
|
|
|
|
fut2: None,
|
|
|
|
openssl: self.openssl.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OpensslConnectServiceResponse<T: Address> {
|
|
|
|
fut1: Option<<ConnectService<T> as Service>::Future>,
|
|
|
|
fut2: Option<<OpensslConnectorService<T, TcpStream> as Service>::Future>,
|
|
|
|
openssl: OpensslConnectorService<T, TcpStream>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Address> Future for OpensslConnectServiceResponse<T> {
|
|
|
|
type Item = SslStream<TcpStream>;
|
|
|
|
type Error = ConnectError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Some(ref mut fut) = self.fut1 {
|
|
|
|
let res = try_ready!(fut.poll());
|
|
|
|
let _ = self.fut1.take();
|
|
|
|
self.fut2 = Some(self.openssl.call(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref mut fut) = self.fut2 {
|
|
|
|
let connect = try_ready!(fut
|
|
|
|
.poll()
|
|
|
|
.map_err(|e| ConnectError::Io(io::Error::new(io::ErrorKind::Other, e))));
|
|
|
|
Ok(Async::Ready(connect.into_parts().0))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|