2021-01-22 17:33:50 -08:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2019-12-02 11:30:27 +06:00
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
|
|
|
use actix_service::{Service, ServiceFactory};
|
|
|
|
use actix_utils::counter::{Counter, CounterGuard};
|
2021-01-22 17:33:50 -08:00
|
|
|
use futures_core::{future::LocalBoxFuture, ready};
|
2019-12-02 11:30:27 +06:00
|
|
|
|
2020-12-29 00:38:41 +00:00
|
|
|
pub use openssl::ssl::{
|
|
|
|
AlpnError, Error as SslError, HandshakeError, Ssl, SslAcceptor, SslAcceptorBuilder,
|
|
|
|
};
|
|
|
|
pub use tokio_openssl::SslStream;
|
2020-09-08 18:00:07 +01:00
|
|
|
|
2020-12-29 00:38:41 +00:00
|
|
|
use super::MAX_CONN_COUNTER;
|
2019-12-02 11:30:27 +06:00
|
|
|
|
2020-09-08 18:00:07 +01:00
|
|
|
/// Accept TLS connections via `openssl` package.
|
2019-12-02 11:30:27 +06:00
|
|
|
///
|
2020-09-08 18:00:07 +01:00
|
|
|
/// `openssl` feature enables this `Acceptor` type.
|
2020-12-29 19:36:17 +08:00
|
|
|
pub struct Acceptor {
|
2019-12-02 11:30:27 +06:00
|
|
|
acceptor: SslAcceptor,
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl Acceptor {
|
2020-09-08 18:00:07 +01:00
|
|
|
/// Create OpenSSL based `Acceptor` service factory.
|
|
|
|
#[inline]
|
2019-12-02 11:30:27 +06:00
|
|
|
pub fn new(acceptor: SslAcceptor) -> Self {
|
2020-12-29 19:36:17 +08:00
|
|
|
Acceptor { acceptor }
|
2019-12-02 11:30:27 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl Clone for Acceptor {
|
2020-09-08 18:00:07 +01:00
|
|
|
#[inline]
|
2019-12-02 11:30:27 +06:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl<T> ServiceFactory<T> for Acceptor
|
2020-12-29 00:38:41 +00:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
{
|
2019-12-02 11:30:27 +06:00
|
|
|
type Response = SslStream<T>;
|
2020-12-29 00:38:41 +00:00
|
|
|
type Error = SslError;
|
2019-12-02 11:30:27 +06:00
|
|
|
type Config = ();
|
2020-12-29 19:36:17 +08:00
|
|
|
type Service = AcceptorService;
|
2019-12-02 11:30:27 +06:00
|
|
|
type InitError = ();
|
2021-01-22 17:33:50 -08:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
2019-12-02 11:30:27 +06:00
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2021-01-22 17:33:50 -08:00
|
|
|
let res = MAX_CONN_COUNTER.with(|conns| {
|
|
|
|
Ok(AcceptorService {
|
2019-12-02 11:30:27 +06:00
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
conns: conns.clone(),
|
2021-01-22 17:33:50 -08:00
|
|
|
})
|
|
|
|
});
|
|
|
|
Box::pin(async { res })
|
2019-12-02 11:30:27 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
pub struct AcceptorService {
|
2019-12-02 11:30:27 +06:00
|
|
|
acceptor: SslAcceptor,
|
|
|
|
conns: Counter,
|
|
|
|
}
|
|
|
|
|
2020-12-29 19:36:17 +08:00
|
|
|
impl<T> Service<T> for AcceptorService
|
2020-12-29 00:38:41 +00:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
{
|
2019-12-02 11:30:27 +06:00
|
|
|
type Response = SslStream<T>;
|
2020-12-29 00:38:41 +00:00
|
|
|
type Error = SslError;
|
2019-12-02 11:30:27 +06:00
|
|
|
type Future = AcceptorServiceResponse<T>;
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-12-02 11:30:27 +06:00
|
|
|
if self.conns.available(ctx) {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:06:22 -08:00
|
|
|
fn call(&self, io: T) -> Self::Future {
|
2020-12-31 06:11:50 +08:00
|
|
|
let ssl_ctx = self.acceptor.context();
|
|
|
|
let ssl = Ssl::new(ssl_ctx).expect("Provided SSL acceptor was invalid.");
|
2019-12-02 11:30:27 +06:00
|
|
|
AcceptorServiceResponse {
|
|
|
|
_guard: self.conns.get(),
|
2020-12-29 00:38:41 +00:00
|
|
|
stream: Some(SslStream::new(ssl, io).unwrap()),
|
2019-12-02 11:30:27 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AcceptorServiceResponse<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
{
|
2020-12-29 00:38:41 +00:00
|
|
|
stream: Option<SslStream<T>>,
|
2019-12-02 11:30:27 +06:00
|
|
|
_guard: CounterGuard,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncRead + AsyncWrite + Unpin> Future for AcceptorServiceResponse<T> {
|
2020-12-29 00:38:41 +00:00
|
|
|
type Output = Result<SslStream<T>, SslError>;
|
2019-12-02 11:30:27 +06:00
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2020-12-29 00:38:41 +00:00
|
|
|
ready!(Pin::new(self.stream.as_mut().unwrap()).poll_accept(cx))?;
|
|
|
|
Poll::Ready(Ok(self.stream.take().expect("SSL connect has resolved.")))
|
2019-12-02 11:30:27 +06:00
|
|
|
}
|
|
|
|
}
|