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;
|
2021-01-22 17:33:50 -08:00
|
|
|
use futures_core::future::LocalBoxFuture;
|
2020-09-08 18:00:07 +01:00
|
|
|
|
2021-02-04 15:22:38 +00:00
|
|
|
pub use tokio_native_tls::native_tls::Error;
|
2020-12-29 00:38:41 +00:00
|
|
|
pub use tokio_native_tls::{TlsAcceptor, TlsStream};
|
2018-09-27 20:23:30 -07:00
|
|
|
|
2020-12-29 00:38:41 +00:00
|
|
|
use super::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-12-29 00:38:41 +00:00
|
|
|
/// `native-tls` feature enables this `Acceptor` type.
|
2020-12-29 19:36:17 +08:00
|
|
|
pub struct Acceptor {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: TlsAcceptor,
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl Acceptor {
|
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-12-29 19:36:17 +08:00
|
|
|
Acceptor { acceptor }
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl Clone for Acceptor {
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl<T> ServiceFactory<T> for Acceptor
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
{
|
2019-12-05 20:52:37 +06:00
|
|
|
type Response = TlsStream<T>;
|
2018-09-27 20:23:30 -07:00
|
|
|
type Error = Error;
|
2019-12-05 20:52:37 +06:00
|
|
|
type Config = ();
|
2020-12-29 19:36:17 +08:00
|
|
|
|
|
|
|
type Service = NativeTlsAcceptorService;
|
2019-12-05 20:52:37 +06:00
|
|
|
type InitError = ();
|
2021-01-22 17:33:50 -08:00
|
|
|
type Future = LocalBoxFuture<'static, 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 {
|
2021-01-22 17:33:50 -08:00
|
|
|
let res = MAX_CONN_COUNTER.with(|conns| {
|
|
|
|
Ok(NativeTlsAcceptorService {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
conns: conns.clone(),
|
2021-01-22 17:33:50 -08:00
|
|
|
})
|
|
|
|
});
|
|
|
|
Box::pin(async { res })
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
pub struct NativeTlsAcceptorService {
|
2018-09-27 20:23:30 -07:00
|
|
|
acceptor: TlsAcceptor,
|
|
|
|
conns: Counter,
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl Clone for NativeTlsAcceptorService {
|
2019-11-14 18:38:24 +06:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
conns: self.conns.clone(),
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl<T> Service<T> for NativeTlsAcceptorService
|
2019-11-14 18:38:24 +06:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
{
|
2019-12-05 20:52:37 +06:00
|
|
|
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
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-14 18:38:24 +06:00
|
|
|
if self.conns.available(cx) {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&self, io: T) -> Self::Future {
|
2019-11-14 18:38:24 +06:00
|
|
|
let guard = self.conns.get();
|
|
|
|
let this = self.clone();
|
2020-12-29 19:36:17 +08:00
|
|
|
Box::pin(async move {
|
|
|
|
let io = this.acceptor.accept(io).await;
|
|
|
|
drop(guard);
|
|
|
|
io
|
|
|
|
})
|
2018-09-27 20:23:30 -07:00
|
|
|
}
|
|
|
|
}
|