2019-12-02 11:30:27 +06:00
|
|
|
use std::future::Future;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
|
|
|
use actix_service::{Service, ServiceFactory};
|
|
|
|
use actix_utils::counter::{Counter, CounterGuard};
|
2020-12-29 00:38:41 +00:00
|
|
|
use futures_util::{
|
|
|
|
future::{ok, Ready},
|
|
|
|
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.
|
2019-12-02 11:30:27 +06:00
|
|
|
pub struct Acceptor<T: AsyncRead + AsyncWrite> {
|
|
|
|
acceptor: SslAcceptor,
|
|
|
|
io: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncRead + AsyncWrite> Acceptor<T> {
|
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 {
|
|
|
|
Acceptor {
|
|
|
|
acceptor,
|
|
|
|
io: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncRead + AsyncWrite> Clone for Acceptor<T> {
|
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(),
|
|
|
|
io: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:38:41 +00:00
|
|
|
impl<T> ServiceFactory<T> for Acceptor<T>
|
|
|
|
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 = ();
|
|
|
|
type Service = AcceptorService<T>;
|
|
|
|
type InitError = ();
|
|
|
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2019-12-02 11:30:27 +06:00
|
|
|
MAX_CONN_COUNTER.with(|conns| {
|
|
|
|
ok(AcceptorService {
|
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
conns: conns.clone(),
|
|
|
|
io: PhantomData,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AcceptorService<T> {
|
|
|
|
acceptor: SslAcceptor,
|
|
|
|
conns: Counter,
|
|
|
|
io: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:38:41 +00:00
|
|
|
impl<T> Service<T> for AcceptorService<T>
|
|
|
|
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>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
if self.conns.available(ctx) {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:38:41 +00:00
|
|
|
fn call(&mut self, io: T) -> Self::Future {
|
2019-12-02 11:30:27 +06:00
|
|
|
let acc = self.acceptor.clone();
|
2020-12-29 00:38:41 +00:00
|
|
|
let ssl_ctx = acc.into_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
|
|
|
}
|
|
|
|
}
|