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

174 lines
4.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, future::FutureResult, Async, Poll, Future};
2018-08-25 09:02:14 -07:00
use openssl::ssl::{AlpnError, Error, SslAcceptor, SslAcceptorBuilder, 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-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-08-19 10:47:04 -07:00
pub fn new(builder: SslAcceptorBuilder) -> Self {
2018-08-25 09:02:14 -07:00
OpensslAcceptor {
2018-08-19 10:47:04 -07:00
acceptor: builder.build(),
io: PhantomData,
}
}
/// Create `OpensslWith` with `HTTP1.1` and `HTTP2`.
pub fn for_http(mut builder: SslAcceptorBuilder) -> io::Result<Self> {
let protos = b"\x08http/1.1\x02h2";
builder.set_alpn_select_callback(|_, protos| {
const H2: &[u8] = b"\x02h2";
if protos.windows(3).any(|window| window == H2) {
Ok(b"h2")
} else {
Err(AlpnError::NOACK)
}
});
builder.set_alpn_protos(&protos[..])?;
2018-08-25 09:02:14 -07:00
Ok(OpensslAcceptor {
2018-08-19 10:47:04 -07:00
acceptor: builder.build(),
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-08-25 09:02:14 -07:00
future::ok(OpensslAcceptorService {
2018-08-19 10:47:04 -07:00
acceptor: self.acceptor.clone(),
io: PhantomData,
})
}
}
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-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 = AcceptAsync<T>;
2018-08-19 10:47:04 -07:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, req: Self::Request) -> Self::Future {
2018-08-25 09:02:14 -07:00
SslAcceptorExt::accept_async(&self.acceptor, req)
2018-08-19 10:47:04 -07:00
}
}
2018-08-25 09:02:14 -07:00
/// Openssl connector factory
pub struct OpensslConnector<T> {
connector: SslConnector,
io: PhantomData<T>,
}
2018-08-19 10:47:04 -07:00
2018-08-25 09:02:14 -07:00
impl<T> OpensslConnector<T> {
pub fn new(connector: SslConnector) -> Self {
OpensslConnector {
connector,
io: PhantomData,
}
}
}
2018-08-19 10:47:04 -07:00
2018-08-25 09:02:14 -07:00
impl<T> Clone for OpensslConnector<T> {
fn clone(&self) -> Self {
Self {
connector: self.connector.clone(),
io: PhantomData,
}
2018-08-19 10:47:04 -07:00
}
}
2018-08-25 09:02:14 -07:00
impl<T: AsyncRead + AsyncWrite> NewService for OpensslConnector<T> {
type Request = (String, T);
type Response = (String, SslStream<T>);
2018-08-25 09:02:14 -07:00
type Error = Error;
type Service = OpensslConnectorService<T>;
type InitError = io::Error;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
future::ok(OpensslConnectorService {
connector: self.connector.clone(),
io: PhantomData,
})
}
}
pub struct OpensslConnectorService<T> {
connector: SslConnector,
io: PhantomData<T>,
}
impl<T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<T> {
type Request = (String, T);
type Response = (String, SslStream<T>);
2018-08-25 09:02:14 -07:00
type Error = Error;
type Future = ConnectAsyncExt<T>;
2018-08-25 09:02:14 -07:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, (host, stream): Self::Request) -> Self::Future {
ConnectAsyncExt {
fut: SslConnectorExt::connect_async(&self.connector, &host, stream),
host: Some(host)
}
2018-08-25 09:02:14 -07:00
}
}
pub struct ConnectAsyncExt<T> {
fut: ConnectAsync<T>,
host: Option<String>,
}
impl<T> Future for ConnectAsyncExt<T>
where
T: AsyncRead + AsyncWrite,
{
type Item = (String, SslStream<T>);
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.fut.poll()? {
Async::Ready(stream) => Ok(Async::Ready((self.host.take().unwrap(), stream))),
Async::NotReady => Ok(Async::NotReady)
}
}
}