1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-03-15 22:03:05 +01:00

129 lines
3.3 KiB
Rust
Raw Normal View History

2019-03-13 12:40:11 -07:00
use std::fmt;
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 15:37:12 -07:00
use crate::{Address, Connection};
/// Openssl connector factory
2019-03-13 15:51:21 -07:00
pub struct OpensslConnector<T, U> {
connector: SslConnector,
2019-03-13 15:51:21 -07:00
_t: PhantomData<(T, U)>,
}
2019-03-13 15:51:21 -07:00
impl<T, U> OpensslConnector<T, U> {
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,
{
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
> {
OpensslConnectorService {
connector: connector,
_t: PhantomData,
}
}
}
2019-03-13 15:51:21 -07:00
impl<T, U> Clone for OpensslConnector<T, U> {
fn clone(&self) -> Self {
Self {
connector: self.connector.clone(),
_t: PhantomData,
}
}
}
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>;
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 = ();
type Future = FutureResult<Self::Service, Self::InitError>;
2019-02-22 12:44:37 -08:00
fn new_service(&self, _: &()) -> Self::Future {
ok(OpensslConnectorService {
connector: self.connector.clone(),
_t: PhantomData,
})
}
}
2019-03-13 15:37:12 -07:00
pub struct OpensslConnectorService<T, U> {
connector: SslConnector,
2019-03-13 15:37:12 -07:00
_t: PhantomData<(T, U)>,
}
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>;
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(());
ConnectAsyncExt {
2019-03-13 12:40:11 -07:00
fut: SslConnectorExt::connect_async(&self.connector, stream.host(), io),
stream: Some(stream),
}
}
}
2019-03-13 15:37:12 -07:00
pub struct ConnectAsyncExt<T, U> {
fut: ConnectAsync<U>,
stream: Option<Connection<T, ()>>,
}
2019-03-13 15:37:12 -07:00
impl<T: Address, U> Future for ConnectAsyncExt<T, U>
where
2019-03-13 15:37:12 -07:00
U: AsyncRead + AsyncWrite + fmt::Debug,
{
2019-03-13 15:37:12 -07:00
type Item = Connection<T, SslStream<U>>;
type Error = HandshakeError<U>;
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))
}
Async::NotReady => Ok(Async::NotReady),
}
}
}