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

124 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 12:40:11 -07:00
use crate::Stream;
/// Openssl connector factory
2019-03-13 12:40:11 -07:00
pub struct OpensslConnector<T, P, E> {
connector: SslConnector,
2019-03-13 12:40:11 -07:00
_t: PhantomData<(T, P, E)>,
}
2019-03-13 12:40:11 -07:00
impl<T, P, E> OpensslConnector<T, P, E> {
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, ()> {
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>,
> {
OpensslConnectorService {
connector: connector,
_t: PhantomData,
}
}
}
2019-03-13 12:40:11 -07:00
impl<T, P, E> Clone for OpensslConnector<T, P, E> {
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>;
type Error = HandshakeError<T>;
2019-03-13 12:40:11 -07:00
type Service = OpensslConnectorService<T, P>;
type InitError = E;
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 12:40:11 -07:00
pub struct OpensslConnectorService<T, P> {
connector: SslConnector,
2019-03-13 12:40:11 -07:00
_t: PhantomData<(T, P)>,
}
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>;
type Error = HandshakeError<T>;
2019-03-13 12:40:11 -07:00
type Future = ConnectAsyncExt<T, P>;
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(());
ConnectAsyncExt {
2019-03-13 12:40:11 -07:00
fut: SslConnectorExt::connect_async(&self.connector, stream.host(), io),
stream: Some(stream),
}
}
}
2019-03-13 12:40:11 -07:00
pub struct ConnectAsyncExt<T, P> {
fut: ConnectAsync<T>,
2019-03-13 12:40:11 -07:00
stream: Option<Stream<(), P>>,
}
2019-03-13 12:40:11 -07:00
impl<T, P> Future for ConnectAsyncExt<T, P>
where
2019-03-13 12:40:11 -07:00
T: AsyncRead + AsyncWrite + fmt::Debug,
{
2019-03-13 12:40:11 -07:00
type Item = Stream<SslStream<T>, P>;
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))
}
Async::NotReady => Ok(Async::NotReady),
}
}
}