2019-11-14 18:38:24 +06:00
|
|
|
use std::convert::Infallible;
|
2018-09-27 20:23:30 -07:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 18:38:24 +06:00
|
|
|
use std::task::{Context, Poll};
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use actix_service::{Service, ServiceFactory};
|
|
|
|
use futures::future::{self, FutureExt as _, LocalBoxFuture, TryFutureExt as _};
|
|
|
|
use native_tls::Error;
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
|
|
|
use tokio_tls::{TlsAcceptor, TlsStream};
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use crate::counter::Counter;
|
2019-03-08 19:43:13 -08:00
|
|
|
use crate::ssl::MAX_CONN_COUNTER;
|
2019-11-14 18:38:24 +06:00
|
|
|
use crate::{Io, ServerConfig};
|
2018-09-27 20:23:30 -07:00
|
|
|
|
|
|
|
/// Support `SSL` connections via native-tls package
|
|
|
|
///
|
|
|
|
/// `tls` feature enables `NativeTlsAcceptor` type
|
2019-03-11 13:48:55 -07:00
|
|
|
pub struct NativeTlsAcceptor<T, P = ()> {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: TlsAcceptor,
|
2019-03-11 12:01:55 -07:00
|
|
|
io: PhantomData<(T, P)>,
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<T, P> NativeTlsAcceptor<T, P>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
{
|
2018-09-27 20:23:30 -07:00
|
|
|
/// Create `NativeTlsAcceptor` instance
|
2019-11-14 18:38:24 +06:00
|
|
|
#[inline]
|
2018-09-27 20:23:30 -07:00
|
|
|
pub fn new(acceptor: TlsAcceptor) -> Self {
|
|
|
|
NativeTlsAcceptor {
|
2018-12-09 22:14:29 -08:00
|
|
|
acceptor,
|
2018-09-27 20:23:30 -07:00
|
|
|
io: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<T, P> Clone for NativeTlsAcceptor<T, P> {
|
|
|
|
#[inline]
|
2018-09-27 20:23:30 -07:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
io: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<T, P> ServiceFactory for NativeTlsAcceptor<T, P>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
P: 'static,
|
|
|
|
{
|
2019-03-11 12:01:55 -07:00
|
|
|
type Request = Io<T, P>;
|
|
|
|
type Response = Io<TlsStream<T>, P>;
|
2018-09-27 20:23:30 -07:00
|
|
|
type Error = Error;
|
2019-05-12 06:03:50 -07:00
|
|
|
|
|
|
|
type Config = ServerConfig;
|
2019-03-11 12:01:55 -07:00
|
|
|
type Service = NativeTlsAcceptorService<T, P>;
|
2019-11-14 18:38:24 +06:00
|
|
|
type InitError = Infallible;
|
|
|
|
type Future = future::Ready<Result<Self::Service, Self::InitError>>;
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2019-03-09 07:27:56 -08:00
|
|
|
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
|
|
|
cfg.set_secure();
|
|
|
|
|
2018-09-27 20:23:30 -07:00
|
|
|
MAX_CONN_COUNTER.with(|conns| {
|
2019-11-14 18:38:24 +06:00
|
|
|
future::ok(NativeTlsAcceptorService {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
conns: conns.clone(),
|
|
|
|
io: PhantomData,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 12:01:55 -07:00
|
|
|
pub struct NativeTlsAcceptorService<T, P> {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: TlsAcceptor,
|
2019-03-11 12:01:55 -07:00
|
|
|
io: PhantomData<(T, P)>,
|
2018-09-27 20:23:30 -07:00
|
|
|
conns: Counter,
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<T, P> Clone for NativeTlsAcceptorService<T, P> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
io: PhantomData,
|
|
|
|
conns: self.conns.clone(),
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
impl<T, P> Service for NativeTlsAcceptorService<T, P>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
P: 'static,
|
|
|
|
{
|
|
|
|
type Request = Io<T, P>;
|
|
|
|
type Response = Io<TlsStream<T>, P>;
|
2018-09-27 20:23:30 -07:00
|
|
|
type Error = Error;
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Io<TlsStream<T>, P>, Error>>;
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
if self.conns.available(cx) {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
let guard = self.conns.get();
|
|
|
|
let this = self.clone();
|
|
|
|
let (io, params, proto) = req.into_parts();
|
|
|
|
async move { this.acceptor.accept(io).await }
|
|
|
|
.map_ok(move |stream| Io::from_parts(stream, params, proto))
|
|
|
|
.map_ok(move |io| {
|
|
|
|
// Required to preserve `CounterGuard` until `Self::Future`
|
|
|
|
// is completely resolved.
|
|
|
|
let _ = guard;
|
|
|
|
io
|
|
|
|
})
|
|
|
|
.boxed_local()
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|