2018-09-07 10:38:39 -07:00
|
|
|
use std::cell::Cell;
|
2018-08-21 17:08:23 -07:00
|
|
|
use std::io;
|
2018-08-25 09:02:14 -07:00
|
|
|
use std::marker::PhantomData;
|
2018-09-07 10:38:39 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::atomic::Ordering;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
2018-09-07 10:38:39 -07:00
|
|
|
use futures::task::AtomicTask;
|
|
|
|
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
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-09-07 10:38:39 -07:00
|
|
|
use super::MAX_CONN;
|
2018-08-28 16:24:36 -07:00
|
|
|
use connector::ConnectionInfo;
|
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-09-07 10:38:39 -07:00
|
|
|
|
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-07 10:38:39 -07:00
|
|
|
ok(OpensslAcceptorService {
|
2018-08-19 10:47:04 -07:00
|
|
|
acceptor: self.acceptor.clone(),
|
|
|
|
io: PhantomData,
|
2018-09-07 10:38:39 -07:00
|
|
|
inner: Rc::new(Inner::default()),
|
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-07 10:38:39 -07:00
|
|
|
inner: Rc<Inner>,
|
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;
|
2018-09-07 10:38:39 -07:00
|
|
|
type Future = OpensslAcceptorServiceFut<T>;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-09-07 10:38:39 -07:00
|
|
|
if self.inner.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 {
|
2018-09-07 10:38:39 -07:00
|
|
|
self.inner.inc();
|
|
|
|
|
|
|
|
OpensslAcceptorServiceFut {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
fut: SslAcceptorExt::accept_async(&self.acceptor, req),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Inner {
|
|
|
|
maxconn: usize,
|
|
|
|
count: Cell<usize>,
|
|
|
|
task: AtomicTask,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Inner {
|
|
|
|
fn default() -> Inner {
|
|
|
|
Inner {
|
|
|
|
maxconn: MAX_CONN.load(Ordering::Relaxed),
|
|
|
|
count: Cell::new(0),
|
|
|
|
task: AtomicTask::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inner {
|
|
|
|
fn inc(&self) {
|
|
|
|
let num = self.count.get() + 1;
|
|
|
|
self.count.set(num);
|
|
|
|
if num == self.maxconn {
|
|
|
|
self.task.register();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dec(&self) {
|
|
|
|
let num = self.count.get();
|
|
|
|
self.count.set(num - 1);
|
|
|
|
if num == self.maxconn {
|
|
|
|
self.task.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check(&self) -> bool {
|
|
|
|
self.count.get() < self.maxconn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OpensslAcceptorServiceFut<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
|
|
|
{
|
|
|
|
fut: AcceptAsync<T>,
|
|
|
|
inner: Rc<Inner>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> {
|
|
|
|
type Item = SslStream<T>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let res = self.fut.poll();
|
|
|
|
|
|
|
|
if let Ok(Async::NotReady) = res {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
} else {
|
|
|
|
self.inner.dec();
|
|
|
|
res
|
|
|
|
}
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 09:02:14 -07:00
|
|
|
/// Openssl connector factory
|
2018-09-04 12:07:13 -07:00
|
|
|
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>,
|
2018-09-04 12:07:13 -07:00
|
|
|
_e: PhantomData<E>,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
2018-08-19 10:47:04 -07:00
|
|
|
|
2018-09-04 12:07:13 -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,
|
2018-09-04 12:07:13 -07:00
|
|
|
_e: PhantomData,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-19 10:47:04 -07:00
|
|
|
|
2018-09-04 12:07:13 -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,
|
2018-09-04 12:07:13 -07:00
|
|
|
_e: PhantomData,
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 12:07:13 -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>;
|
2018-09-04 12:07:13 -07:00
|
|
|
type InitError = E;
|
2018-08-25 09:02:14 -07:00
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
2018-09-07 10:38:39 -07:00
|
|
|
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-27 14:29:01 -07:00
|
|
|
}
|
2018-08-25 09:02:14 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-27 14:29:01 -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-27 14:29:01 -07:00
|
|
|
}
|
|
|
|
|
2018-08-29 15:15:24 -07:00
|
|
|
impl<T, Io> Future for ConnectAsyncExt<T, Io>
|
2018-08-27 14:29:01 -07:00
|
|
|
where
|
2018-08-29 15:15:24 -07:00
|
|
|
Io: AsyncRead + AsyncWrite,
|
2018-08-27 14:29:01 -07:00
|
|
|
{
|
2018-08-29 15:15:24 -07:00
|
|
|
type Item = (T, ConnectionInfo, SslStream<Io>);
|
2018-08-27 14:29:01 -07:00
|
|
|
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 14:29:01 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-27 21:53:20 -07:00
|
|
|
}
|