1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-02-26 06:03:51 +01:00
actix-net/src/ssl/openssl.rs

198 lines
5.0 KiB
Rust
Raw Normal View History

2018-08-25 09:02:14 -07:00
use std::marker::PhantomData;
2018-08-19 10:47:04 -07:00
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
2018-09-08 10:16:43 -07:00
use openssl::ssl::{Error, SslAcceptor, SslConnector};
2018-08-19 10:47:04 -07:00
use tokio_io::{AsyncRead, AsyncWrite};
2018-08-25 09:02:14 -07:00
use tokio_openssl::{AcceptAsync, ConnectAsync, SslAcceptorExt, SslConnectorExt, SslStream};
2018-08-19 10:47:04 -07:00
2018-09-07 11:35:25 -07:00
use super::MAX_CONN_COUNTER;
2018-10-23 22:14:02 -07:00
use connector::Connect;
use counter::{Counter, CounterGuard};
2018-09-11 09:30:22 -07:00
use service::{NewService, Service};
2018-08-19 10:47:04 -07:00
/// Support `SSL` connections via openssl package
///
2018-08-25 09:02:14 -07:00
/// `ssl` feature enables `OpensslAcceptor` type
pub struct OpensslAcceptor<T> {
2018-08-19 10:47:04 -07:00
acceptor: SslAcceptor,
io: PhantomData<T>,
}
2018-10-03 10:34:14 -07:00
impl<T> OpensslAcceptor<T> {
2018-08-25 09:02:14 -07:00
/// Create default `OpensslAcceptor`
2018-09-08 10:16:43 -07:00
pub fn new(acceptor: SslAcceptor) -> Self {
2018-08-25 09:02:14 -07:00
OpensslAcceptor {
2018-09-08 10:16:43 -07:00
acceptor,
2018-08-19 10:47:04 -07:00
io: PhantomData,
}
}
}
2018-08-25 09:02:14 -07:00
impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
2018-08-19 10:47:04 -07:00
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
io: PhantomData,
}
}
}
2018-08-25 09:02:14 -07:00
impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
2018-08-19 10:47:04 -07:00
type Request = T;
type Response = SslStream<T>;
2018-08-25 09:02:14 -07:00
type Error = Error;
type Service = OpensslAcceptorService<T>;
2018-09-08 12:52:43 -07:00
type InitError = ();
2018-09-08 12:58:08 -07:00
type Future = FutureResult<Self::Service, Self::InitError>;
2018-08-19 10:47:04 -07:00
2018-08-23 15:42:34 -07:00
fn new_service(&self) -> Self::Future {
2018-09-08 10:16:43 -07:00
MAX_CONN_COUNTER.with(|conns| {
2018-09-07 11:35:25 -07:00
ok(OpensslAcceptorService {
acceptor: self.acceptor.clone(),
2018-09-08 10:16:43 -07:00
conns: conns.clone(),
2018-09-07 11:35:25 -07:00
io: PhantomData,
})
2018-08-19 10:47:04 -07:00
})
}
}
2018-08-25 09:02:14 -07:00
pub struct OpensslAcceptorService<T> {
2018-08-19 10:47:04 -07:00
acceptor: SslAcceptor,
io: PhantomData<T>,
conns: Counter,
2018-08-19 10:47:04 -07:00
}
2018-08-25 09:02:14 -07:00
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
2018-08-19 10:47:04 -07:00
type Request = T;
type Response = SslStream<T>;
2018-08-25 09:02:14 -07:00
type Error = Error;
type Future = OpensslAcceptorServiceFut<T>;
2018-08-19 10:47:04 -07:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
2018-09-14 13:12:55 -07:00
if self.conns.available() {
Ok(Async::Ready(()))
} else {
Ok(Async::NotReady)
}
2018-08-19 10:47:04 -07:00
}
fn call(&mut self, req: Self::Request) -> Self::Future {
OpensslAcceptorServiceFut {
2018-09-08 10:16:43 -07:00
_guard: self.conns.get(),
fut: SslAcceptorExt::accept_async(&self.acceptor, req),
}
}
}
pub struct OpensslAcceptorServiceFut<T>
where
T: AsyncRead + AsyncWrite,
{
fut: AcceptAsync<T>,
_guard: CounterGuard,
}
impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> {
type Item = SslStream<T>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
2018-09-07 11:35:25 -07:00
self.fut.poll()
2018-08-19 10:47:04 -07:00
}
}
2018-08-25 09:02:14 -07:00
/// Openssl connector factory
2018-10-23 22:14:02 -07:00
pub struct OpensslConnector<T, E> {
2018-08-25 09:02:14 -07:00
connector: SslConnector,
2018-10-23 22:14:02 -07:00
_t: PhantomData<(T, E)>,
2018-08-25 09:02:14 -07:00
}
2018-08-19 10:47:04 -07:00
2018-10-23 22:14:02 -07:00
impl<T, E> OpensslConnector<T, E> {
2018-08-25 09:02:14 -07:00
pub fn new(connector: SslConnector) -> Self {
OpensslConnector {
connector,
2018-10-23 22:14:02 -07:00
_t: PhantomData,
2018-08-25 09:02:14 -07:00
}
}
}
2018-08-19 10:47:04 -07:00
2018-10-23 22:14:02 -07:00
impl<T: AsyncRead + AsyncWrite> OpensslConnector<T, ()> {
pub fn service(
connector: SslConnector,
2018-10-23 22:14:02 -07:00
) -> impl Service<Request = (Connect, T), Response = (Connect, SslStream<T>), Error = Error>
{
OpensslConnectorService {
connector: connector,
2018-10-23 22:14:02 -07:00
_t: PhantomData,
}
}
}
2018-10-23 22:14:02 -07:00
impl<T, E> Clone for OpensslConnector<T, E> {
2018-08-25 09:02:14 -07:00
fn clone(&self) -> Self {
Self {
connector: self.connector.clone(),
2018-10-23 22:14:02 -07:00
_t: PhantomData,
2018-08-25 09:02:14 -07:00
}
2018-08-19 10:47:04 -07:00
}
}
2018-10-23 22:14:02 -07:00
impl<T: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<T, E> {
type Request = (Connect, T);
type Response = (Connect, SslStream<T>);
2018-08-25 09:02:14 -07:00
type Error = Error;
2018-10-23 22:14:02 -07:00
type Service = OpensslConnectorService<T>;
type InitError = E;
2018-08-25 09:02:14 -07:00
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
ok(OpensslConnectorService {
2018-08-25 09:02:14 -07:00
connector: self.connector.clone(),
2018-10-23 22:14:02 -07:00
_t: PhantomData,
2018-08-25 09:02:14 -07:00
})
}
}
2018-10-23 22:14:02 -07:00
pub struct OpensslConnectorService<T> {
2018-08-25 09:02:14 -07:00
connector: SslConnector,
2018-10-23 22:14:02 -07:00
_t: PhantomData<T>,
2018-08-25 09:02:14 -07:00
}
2018-10-23 22:14:02 -07:00
impl<T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<T> {
type Request = (Connect, T);
type Response = (Connect, SslStream<T>);
2018-08-25 09:02:14 -07:00
type Error = Error;
2018-10-23 22:14:02 -07:00
type Future = ConnectAsyncExt<T>;
2018-08-25 09:02:14 -07:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
2018-10-23 22:14:02 -07:00
fn call(&mut self, (req, stream): Self::Request) -> Self::Future {
2018-08-27 21:53:20 -07:00
ConnectAsyncExt {
2018-10-23 22:14:02 -07:00
fut: SslConnectorExt::connect_async(&self.connector, &req.host, stream),
2018-08-29 15:15:24 -07:00
req: Some(req),
}
2018-08-25 09:02:14 -07:00
}
}
2018-10-23 22:14:02 -07:00
pub struct ConnectAsyncExt<T> {
fut: ConnectAsync<T>,
req: Option<Connect>,
}
2018-10-23 22:14:02 -07:00
impl<T> Future for ConnectAsyncExt<T>
where
2018-10-23 22:14:02 -07:00
T: AsyncRead + AsyncWrite,
{
2018-10-23 22:14:02 -07:00
type Item = (Connect, SslStream<T>);
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.fut.poll()? {
2018-10-23 22:14:02 -07:00
Async::Ready(stream) => Ok(Async::Ready((self.req.take().unwrap(), stream))),
2018-08-27 21:53:20 -07:00
Async::NotReady => Ok(Async::NotReady),
}
}
2018-08-27 21:53:20 -07:00
}