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-26 08:26:22 +06:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
2019-11-14 18:38:24 +06:00
|
|
|
use actix_service::{Service, ServiceFactory};
|
2019-12-05 20:52:37 +06:00
|
|
|
use actix_utils::counter::Counter;
|
2020-03-12 04:30:32 +09:00
|
|
|
use futures_util::future::{self, FutureExt, LocalBoxFuture, TryFutureExt};
|
2020-09-08 18:00:07 +01:00
|
|
|
|
2019-12-05 20:52:37 +06:00
|
|
|
pub use native_tls::Error;
|
|
|
|
pub use tokio_tls::{TlsAcceptor, TlsStream};
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2019-12-05 20:52:37 +06:00
|
|
|
use crate::MAX_CONN_COUNTER;
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2020-09-08 18:00:07 +01:00
|
|
|
/// Accept TLS connections via `native-tls` package.
|
2018-09-27 20:23:30 -07:00
|
|
|
///
|
2020-09-08 18:00:07 +01:00
|
|
|
/// `nativetls` feature enables this `Acceptor` type.
|
|
|
|
pub struct Acceptor<T> {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: TlsAcceptor,
|
2019-12-05 20:52:37 +06:00
|
|
|
io: PhantomData<T>,
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
|
2020-09-08 18:00:07 +01:00
|
|
|
impl<T> Acceptor<T>
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
|
|
|
{
|
2020-09-08 18:00:07 +01:00
|
|
|
/// Create `native-tls` based `Acceptor` service factory.
|
2019-11-14 18:38:24 +06:00
|
|
|
#[inline]
|
2018-09-27 20:23:30 -07:00
|
|
|
pub fn new(acceptor: TlsAcceptor) -> Self {
|
2020-09-08 18:00:07 +01:00
|
|
|
Acceptor {
|
2018-12-09 22:14:29 -08:00
|
|
|
acceptor,
|
2018-09-27 20:23:30 -07:00
|
|
|
io: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:00:07 +01:00
|
|
|
impl<T> Clone for Acceptor<T> {
|
2019-11-14 18:38:24 +06:00
|
|
|
#[inline]
|
2018-09-27 20:23:30 -07:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
io: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:00:07 +01:00
|
|
|
impl<T> ServiceFactory for Acceptor<T>
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
{
|
2019-12-05 20:52:37 +06:00
|
|
|
type Request = T;
|
|
|
|
type Response = TlsStream<T>;
|
2018-09-27 20:23:30 -07:00
|
|
|
type Error = Error;
|
2019-12-05 20:52:37 +06:00
|
|
|
type Service = NativeTlsAcceptorService<T>;
|
2019-05-12 06:03:50 -07:00
|
|
|
|
2019-12-05 20:52:37 +06:00
|
|
|
type Config = ();
|
|
|
|
type InitError = ();
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = future::Ready<Result<Self::Service, Self::InitError>>;
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2019-12-05 20:52:37 +06:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
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-12-05 20:52:37 +06:00
|
|
|
pub struct NativeTlsAcceptorService<T> {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: TlsAcceptor,
|
2019-12-05 20:52:37 +06:00
|
|
|
io: PhantomData<T>,
|
2018-09-27 20:23:30 -07:00
|
|
|
conns: Counter,
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:52:37 +06:00
|
|
|
impl<T> Clone for NativeTlsAcceptorService<T> {
|
2019-11-14 18:38:24 +06:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
io: PhantomData,
|
|
|
|
conns: self.conns.clone(),
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 20:52:37 +06:00
|
|
|
impl<T> Service for NativeTlsAcceptorService<T>
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
{
|
2019-12-05 20:52:37 +06:00
|
|
|
type Request = T;
|
|
|
|
type Response = TlsStream<T>;
|
2018-09-27 20:23:30 -07:00
|
|
|
type Error = Error;
|
2019-12-05 20:52:37 +06:00
|
|
|
type Future = LocalBoxFuture<'static, Result<TlsStream<T>, 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();
|
2019-12-05 20:52:37 +06:00
|
|
|
async move { this.acceptor.accept(req).await }
|
2019-11-14 18:38:24 +06:00
|
|
|
.map_ok(move |io| {
|
2020-09-08 18:00:07 +01:00
|
|
|
// Required to preserve `CounterGuard` until `Self::Future` is completely resolved.
|
2019-11-14 18:38:24 +06:00
|
|
|
let _ = guard;
|
|
|
|
io
|
|
|
|
})
|
|
|
|
.boxed_local()
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|