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

217 lines
5.5 KiB
Rust
Raw Normal View History

2018-08-21 17:08:23 -07:00
use std::io;
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-08-28 16:24:36 -07:00
use connector::ConnectionInfo;
2018-09-08 10:16:43 -07:00
use worker::{Connections, ConnectionsGuard};
2018-08-21 17:08:23 -07:00
use {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-08-25 09:02:14 -07:00
impl<T> OpensslAcceptor<T> {
/// 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-08-19 10:47:04 -07:00
type InitError = io::Error;
type Future = FutureResult<Self::Service, io::Error>;
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>,
2018-09-08 10:16:43 -07:00
conns: Connections,
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-08 10:16:43 -07:00
if self.conns.check() {
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>,
2018-09-08 10:16:43 -07:00
_guard: ConnectionsGuard,
}
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
pub struct OpensslConnector<T, Io, E> {
2018-08-25 09:02:14 -07:00
connector: SslConnector,
2018-08-29 15:15:24 -07:00
t: PhantomData<T>,
io: PhantomData<Io>,
_e: PhantomData<E>,
2018-08-25 09:02:14 -07:00
}
2018-08-19 10:47:04 -07:00
impl<T, Io, E> OpensslConnector<T, Io, E> {
2018-08-25 09:02:14 -07:00
pub fn new(connector: SslConnector) -> Self {
OpensslConnector {
connector,
2018-08-29 15:15:24 -07:00
t: PhantomData,
2018-08-25 09:02:14 -07:00
io: PhantomData,
_e: PhantomData,
2018-08-25 09:02:14 -07:00
}
}
}
2018-08-19 10:47:04 -07:00
impl<T, Io: AsyncRead + AsyncWrite> OpensslConnector<T, Io, ()> {
pub fn service(
connector: SslConnector,
) -> impl Service<
Request = (T, ConnectionInfo, Io),
Response = (T, ConnectionInfo, SslStream<Io>),
Error = Error,
> {
OpensslConnectorService {
connector: connector,
t: PhantomData,
io: PhantomData,
}
}
}
impl<T, Io, E> Clone for OpensslConnector<T, Io, E> {
2018-08-25 09:02:14 -07:00
fn clone(&self) -> Self {
Self {
connector: self.connector.clone(),
2018-08-29 15:15:24 -07:00
t: PhantomData,
2018-08-25 09:02:14 -07:00
io: PhantomData,
_e: PhantomData,
2018-08-25 09:02:14 -07:00
}
2018-08-19 10:47:04 -07:00
}
}
impl<T, Io: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<T, Io, E> {
2018-08-29 15:15:24 -07:00
type Request = (T, ConnectionInfo, Io);
type Response = (T, ConnectionInfo, SslStream<Io>);
2018-08-25 09:02:14 -07:00
type Error = Error;
2018-08-29 15:15:24 -07:00
type Service = OpensslConnectorService<T, Io>;
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-08-29 15:15:24 -07:00
t: PhantomData,
2018-08-25 09:02:14 -07:00
io: PhantomData,
})
}
}
2018-08-29 15:15:24 -07:00
pub struct OpensslConnectorService<T, Io> {
2018-08-25 09:02:14 -07:00
connector: SslConnector,
2018-08-29 15:15:24 -07:00
t: PhantomData<T>,
io: PhantomData<Io>,
2018-08-25 09:02:14 -07:00
}
2018-08-29 15:15:24 -07:00
impl<T, Io: AsyncRead + AsyncWrite> Service for OpensslConnectorService<T, Io> {
type Request = (T, ConnectionInfo, Io);
type Response = (T, ConnectionInfo, SslStream<Io>);
2018-08-25 09:02:14 -07:00
type Error = Error;
2018-08-29 15:15:24 -07:00
type Future = ConnectAsyncExt<T, Io>;
2018-08-25 09:02:14 -07:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
2018-08-29 15:15:24 -07:00
fn call(&mut self, (req, info, stream): Self::Request) -> Self::Future {
2018-08-27 21:53:20 -07:00
ConnectAsyncExt {
2018-08-28 16:24:36 -07:00
fut: SslConnectorExt::connect_async(&self.connector, &info.host, stream),
2018-08-29 15:15:24 -07:00
req: Some(req),
2018-08-28 16:24:36 -07:00
host: Some(info),
}
2018-08-25 09:02:14 -07:00
}
}
2018-08-29 15:15:24 -07:00
pub struct ConnectAsyncExt<T, Io> {
fut: ConnectAsync<Io>,
req: Option<T>,
2018-08-28 16:24:36 -07:00
host: Option<ConnectionInfo>,
}
2018-08-29 15:15:24 -07:00
impl<T, Io> Future for ConnectAsyncExt<T, Io>
where
2018-08-29 15:15:24 -07:00
Io: AsyncRead + AsyncWrite,
{
2018-08-29 15:15:24 -07:00
type Item = (T, ConnectionInfo, SslStream<Io>);
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.fut.poll()? {
2018-08-29 15:15:24 -07:00
Async::Ready(stream) => Ok(Async::Ready((
self.req.take().unwrap(),
self.host.take().unwrap(),
stream,
))),
2018-08-27 21:53:20 -07:00
Async::NotReady => Ok(Async::NotReady),
}
}
2018-08-27 21:53:20 -07:00
}