2019-03-13 12:40:11 -07:00
|
|
|
use std::fmt;
|
2018-12-10 16:16:40 -08:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
|
|
|
use actix_service::{NewService, Service};
|
|
|
|
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
|
|
|
use openssl::ssl::{HandshakeError, SslConnector};
|
|
|
|
use tokio_openssl::{ConnectAsync, SslConnectorExt, SslStream};
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
use crate::Stream;
|
2018-12-10 16:16:40 -08:00
|
|
|
|
|
|
|
/// Openssl connector factory
|
2019-03-13 12:40:11 -07:00
|
|
|
pub struct OpensslConnector<T, P, E> {
|
2018-12-10 16:16:40 -08:00
|
|
|
connector: SslConnector,
|
2019-03-13 12:40:11 -07:00
|
|
|
_t: PhantomData<(T, P, E)>,
|
2018-12-10 16:16:40 -08:00
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
impl<T, P, E> OpensslConnector<T, P, E> {
|
2018-12-10 16:16:40 -08:00
|
|
|
pub fn new(connector: SslConnector) -> Self {
|
|
|
|
OpensslConnector {
|
|
|
|
connector,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
impl<T: AsyncRead + AsyncWrite + fmt::Debug, P> OpensslConnector<T, P, ()> {
|
2018-12-10 16:16:40 -08:00
|
|
|
pub fn service(
|
|
|
|
connector: SslConnector,
|
2019-03-13 12:40:11 -07:00
|
|
|
) -> impl Service<
|
|
|
|
Request = Stream<T, P>,
|
|
|
|
Response = Stream<SslStream<T>, P>,
|
|
|
|
Error = HandshakeError<T>,
|
|
|
|
> {
|
2018-12-10 16:16:40 -08:00
|
|
|
OpensslConnectorService {
|
|
|
|
connector: connector,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
impl<T, P, E> Clone for OpensslConnector<T, P, E> {
|
2018-12-10 16:16:40 -08:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
connector: self.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
impl<T, P, E> NewService<()> for OpensslConnector<T, P, E>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + fmt::Debug,
|
2019-02-22 12:44:37 -08:00
|
|
|
{
|
2019-03-13 12:40:11 -07:00
|
|
|
type Request = Stream<T, P>;
|
|
|
|
type Response = Stream<SslStream<T>, P>;
|
2018-12-10 16:16:40 -08:00
|
|
|
type Error = HandshakeError<T>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Service = OpensslConnectorService<T, P>;
|
2018-12-10 16:16:40 -08:00
|
|
|
type InitError = E;
|
|
|
|
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 12:40:11 -07:00
|
|
|
pub struct OpensslConnectorService<T, P> {
|
2018-12-10 16:16:40 -08:00
|
|
|
connector: SslConnector,
|
2019-03-13 12:40:11 -07:00
|
|
|
_t: PhantomData<(T, P)>,
|
2018-12-10 16:16:40 -08:00
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
impl<T, P> Service for OpensslConnectorService<T, P>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + fmt::Debug,
|
|
|
|
{
|
|
|
|
type Request = Stream<T, P>;
|
|
|
|
type Response = Stream<SslStream<T>, P>;
|
2018-12-10 16:16:40 -08:00
|
|
|
type Error = HandshakeError<T>;
|
2019-03-13 12:40:11 -07:00
|
|
|
type Future = ConnectAsyncExt<T, P>;
|
2018-12-10 16:16:40 -08:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
fn call(&mut self, stream: Stream<T, P>) -> Self::Future {
|
|
|
|
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 12:40:11 -07:00
|
|
|
pub struct ConnectAsyncExt<T, P> {
|
2018-12-10 16:16:40 -08:00
|
|
|
fut: ConnectAsync<T>,
|
2019-03-13 12:40:11 -07:00
|
|
|
stream: Option<Stream<(), P>>,
|
2018-12-10 16:16:40 -08:00
|
|
|
}
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
impl<T, P> Future for ConnectAsyncExt<T, P>
|
2018-12-10 16:16:40 -08:00
|
|
|
where
|
2019-03-13 12:40:11 -07:00
|
|
|
T: AsyncRead + AsyncWrite + fmt::Debug,
|
2018-12-10 16:16:40 -08:00
|
|
|
{
|
2019-03-13 12:40:11 -07:00
|
|
|
type Item = Stream<SslStream<T>, P>;
|
2018-12-10 16:16:40 -08:00
|
|
|
type Error = HandshakeError<T>;
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|