2019-07-24 11:14:26 +03:00
|
|
|
use std::fmt;
|
2019-11-14 18:38:24 +06:00
|
|
|
use std::future::Future;
|
2019-07-24 11:14:26 +03:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 18:38:24 +06:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::task::{Context, Poll};
|
2019-07-24 11:14:26 +03:00
|
|
|
|
2019-12-05 23:09:34 +06:00
|
|
|
pub use rust_tls::Session;
|
|
|
|
pub use tokio_rustls::{client::TlsStream, rustls::ClientConfig};
|
|
|
|
|
2019-07-24 11:14:26 +03:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
2019-11-14 18:38:24 +06:00
|
|
|
use actix_service::{Service, ServiceFactory};
|
2020-03-12 04:22:38 +09:00
|
|
|
use futures_util::future::{ok, Ready};
|
2019-12-05 23:09:34 +06:00
|
|
|
use tokio_rustls::{Connect, TlsConnector};
|
2019-07-24 14:16:02 +06:00
|
|
|
use webpki::DNSNameRef;
|
2019-07-24 11:14:26 +03:00
|
|
|
|
|
|
|
use crate::{Address, Connection};
|
|
|
|
|
|
|
|
/// Rustls connector factory
|
|
|
|
pub struct RustlsConnector<T, U> {
|
|
|
|
connector: Arc<ClientConfig>,
|
|
|
|
_t: PhantomData<(T, U)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> RustlsConnector<T, U> {
|
|
|
|
pub fn new(connector: Arc<ClientConfig>) -> Self {
|
|
|
|
RustlsConnector {
|
|
|
|
connector,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> RustlsConnector<T, U>
|
|
|
|
where
|
2019-11-19 14:51:40 +06:00
|
|
|
T: Address,
|
2019-11-14 18:38:24 +06:00
|
|
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
2019-07-24 11:14:26 +03:00
|
|
|
{
|
2019-11-18 20:20:56 +06:00
|
|
|
pub fn service(connector: Arc<ClientConfig>) -> RustlsConnectorService<T, U> {
|
2019-07-24 11:14:26 +03:00
|
|
|
RustlsConnectorService {
|
2020-01-28 20:27:33 +09:00
|
|
|
connector,
|
2019-07-24 11:14:26 +03:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> Clone for RustlsConnector<T, U> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
connector: self.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:51:40 +06:00
|
|
|
impl<T: Address, U> ServiceFactory for RustlsConnector<T, U>
|
2019-07-24 11:14:26 +03:00
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
2019-07-24 11:14:26 +03:00
|
|
|
{
|
|
|
|
type Request = Connection<T, U>;
|
2019-10-03 14:32:32 -04:00
|
|
|
type Response = Connection<T, TlsStream<U>>;
|
2019-07-24 11:14:26 +03:00
|
|
|
type Error = std::io::Error;
|
|
|
|
type Config = ();
|
|
|
|
type Service = RustlsConnectorService<T, U>;
|
|
|
|
type InitError = ();
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
2019-07-24 11:14:26 +03:00
|
|
|
|
2019-12-05 16:40:24 +06:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2019-07-24 11:14:26 +03:00
|
|
|
ok(RustlsConnectorService {
|
|
|
|
connector: self.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RustlsConnectorService<T, U> {
|
|
|
|
connector: Arc<ClientConfig>,
|
|
|
|
_t: PhantomData<(T, U)>,
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:20:56 +06:00
|
|
|
impl<T, U> Clone for RustlsConnectorService<T, U> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
connector: self.connector.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:51:40 +06:00
|
|
|
impl<T: Address, U> Service for RustlsConnectorService<T, U>
|
2019-07-24 11:14:26 +03:00
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
2019-07-24 11:14:26 +03:00
|
|
|
{
|
|
|
|
type Request = Connection<T, U>;
|
2019-10-03 14:32:32 -04:00
|
|
|
type Response = Connection<T, TlsStream<U>>;
|
2019-07-24 11:14:26 +03:00
|
|
|
type Error = std::io::Error;
|
|
|
|
type Future = ConnectAsyncExt<T, U>;
|
|
|
|
|
2020-12-27 14:15:42 +00:00
|
|
|
actix_service::always_ready!();
|
2019-07-24 11:14:26 +03:00
|
|
|
|
|
|
|
fn call(&mut self, stream: Connection<T, U>) -> Self::Future {
|
|
|
|
trace!("SSL Handshake start for: {:?}", stream.host());
|
|
|
|
let (io, stream) = stream.replace(());
|
2019-10-04 07:30:13 +02:00
|
|
|
let host = DNSNameRef::try_from_ascii_str(stream.host())
|
|
|
|
.expect("rustls currently only handles hostname-based connections. See https://github.com/briansmith/webpki/issues/54");
|
2019-07-24 11:14:26 +03:00
|
|
|
ConnectAsyncExt {
|
|
|
|
fut: TlsConnector::from(self.connector.clone()).connect(host, io),
|
|
|
|
stream: Some(stream),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConnectAsyncExt<T, U> {
|
|
|
|
fut: Connect<U>,
|
|
|
|
stream: Option<Connection<T, ()>>,
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:51:40 +06:00
|
|
|
impl<T: Address, U> Future for ConnectAsyncExt<T, U>
|
2019-07-24 11:14:26 +03:00
|
|
|
where
|
2019-11-14 18:38:24 +06:00
|
|
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
2019-07-24 11:14:26 +03:00
|
|
|
{
|
2019-11-14 18:38:24 +06:00
|
|
|
type Output = Result<Connection<T, TlsStream<U>>, std::io::Error>;
|
2019-07-24 11:14:26 +03:00
|
|
|
|
2019-12-05 16:40:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-14 18:38:24 +06:00
|
|
|
let this = self.get_mut();
|
|
|
|
Poll::Ready(
|
2020-03-12 04:22:38 +09:00
|
|
|
futures_util::ready!(Pin::new(&mut this.fut).poll(cx)).map(|stream| {
|
2019-11-14 18:38:24 +06:00
|
|
|
let s = this.stream.take().unwrap();
|
2019-07-24 11:14:26 +03:00
|
|
|
trace!("SSL Handshake success: {:?}", s.host());
|
2019-11-14 18:38:24 +06:00
|
|
|
s.replace(stream).1
|
|
|
|
}),
|
|
|
|
)
|
2019-07-24 11:14:26 +03:00
|
|
|
}
|
|
|
|
}
|