Skip to content

Commit

Permalink
Merge pull request museun#219 from museun/tokio-openssl
Browse files Browse the repository at this point in the history
add tokio-openssl
  • Loading branch information
museun authored Oct 16, 2020
2 parents ac26cbd + ec729ec commit b232ed0
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 16 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ async-std = { version = "1.6", optional = true }
tokio = { version = "0.2", features = ["net"], optional = true }
tokio-util = { version = "0.3", features = ["compat"], optional = true }

# rustls
tokio-rustls = { version = "0.14", optional = true }
webpki-roots = { version = "0.20", optional = true }

# native-tls
tokio-native-tls = { version = "0.1", optional = true }
native-tls = { version = "0.2", optional = true }

# openssl
tokio-openssl = { version = "0.4", optional = true }
openssl = { version = "0.10", optional = true, features = ["v110"] }

# for some test utilities
async-mutex = { version = "1.4.0", optional = true }
async-mutex = { version = "1.4", optional = true }


[dev-dependencies]
anyhow = "1.0"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ This crate is runtime agonostic. To use..
| [`async_io`](https://docs.rs/async-io/latest/async_io/) | `async-io` |
| [`smol`](https://docs.rs/smol/latest/smol/) | `smol` |
| [`async_std`](https://docs.rs/async-std/latest/async_std/) | `async-std` |
| [`tokio`](https://docs.rs/tokio/latest/tokio/) | `tokio` and `tokio-util` |
| [`tokio`](https://docs.rs/tokio/0.2/tokio/) | `tokio` and `tokio-util` |

### TLS

Expand All @@ -48,11 +48,13 @@ If you want TLS supports, enable the above runtime and also enable the coorespon
| [`async_io`](https://docs.rs/async-io/latest/async_io/) | `async_io` | `"async-tls"` | [`rustls`][rustls] |
| [`smol`](https://docs.rs/smol/latest/smol/) | `smol` | `"async-tls"` | [`rustls`][rustls] |
| [`async_std`](https://docs.rs/async-std/latest/async_std/) | `async_std` | `"async-tls"` | [`rustls`][rustls] |
| [`tokio`](https://docs.rs/tokio/latest/tokio/) | `tokio` | `"tokio-util"`, `"tokio-rustls"`, `"webpki-roots"` | [`rustls`][rustls] |
| [`tokio`](https://docs.rs/tokio/latest/tokio/) | `tokio` | `"tokio-util"`, `"tokio-native-tls"`, `"native-tls"` | [`native-tls`][native-tls] |
| [`tokio`](https://docs.rs/tokio/0.2/tokio/) | `tokio` | `"tokio-util"`, `"tokio-rustls"`, `"webpki-roots"` | [`rustls`][rustls] |
| [`tokio`](https://docs.rs/tokio/0.2/tokio/) | `tokio` | `"tokio-util"`, `"tokio-native-tls"`, `"native-tls"` | [`native-tls`][native-tls] |
| [`tokio`](https://docs.rs/tokio/0.2/tokio/) | `tokio` | `"tokio-util"`, `"tokio-openssl"`, `"openssl"` | [`openssl`][openssl] |

[rustls]: https://docs.rs/rustls/0.18.1/rustls/
[native-tls]: https://docs.rs/native-tls/0.2.4/native_tls/
[openssl]: https://docs.rs/openssl/0.10/openssl/

## Examples

Expand Down
37 changes: 28 additions & 9 deletions src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@
//! | [`async_io`](https://docs.rs/async-io/latest/async_io/) |`async-io` |
//! | [`smol`](https://docs.rs/smol/latest/smol/) |`smol` |
//! | [`async_std`](https://docs.rs/async-std/latest/async_std/) |`async-std` |
//! | [`tokio`](https://docs.rs/tokio/latest/tokio/) |`tokio` and `tokio-util` |
//! | [`tokio`](https://docs.rs/tokio/0.2/tokio/) |`tokio` and `tokio-util` |
//!
//! ## TLS
//!
//! If you want TLS supports, enable the above runtime and also enable the cooresponding features:
//!
//! | Read/Write provider | Runtime | Features | TLS backend |
//! | --- | --- | --- | --- |
//! | [`async_io`](https://docs.rs/async-io/latest/async_io/) | `async_io` | `"async-tls"` | [`rustls`][rustls] |
//! | [`smol`](https://docs.rs/smol/latest/smol/) | `smol` | `"async-tls"` | [`rustls`][rustls] |
//! | [`async_std`](https://docs.rs/async-std/latest/async_std/) | `async_std` | `"async-tls"` | [`rustls`][rustls] |
//! | [`tokio`](https://docs.rs/tokio/latest/tokio/) | `tokio` | `"tokio-util"`, `"tokio-rustls"`, `"webpki-roots"` | [`rustls`][rustls] |
//! | [`tokio`](https://docs.rs/tokio/latest/tokio/) | `tokio` | `"tokio-util"`, `"tokio-native-tls"`, `"native-tls"` | [`native-tls`][native-tls] |
//! | Read/Write provider | Runtime | Features | TLS backend |
//! | ---------------------------------------------------------- | ----------- | ---------------------------------------------------- | -------------------------- |
//! | [`async_io`](https://docs.rs/async-io/latest/async_io/) | `async_io` | `"async-tls"` | [`rustls`][rustls] |
//! | [`smol`](https://docs.rs/smol/latest/smol/) | `smol` | `"async-tls"` | [`rustls`][rustls] |
//! | [`async_std`](https://docs.rs/async-std/latest/async_std/) | `async_std` | `"async-tls"` | [`rustls`][rustls] |
//! | [`tokio`](https://docs.rs/tokio/0.2/tokio/) | `tokio` | `"tokio-util"`, `"tokio-rustls"`, `"webpki-roots"` | [`rustls`][rustls] |
//! | [`tokio`](https://docs.rs/tokio/0.2/tokio/) | `tokio` | `"tokio-util"`, `"tokio-native-tls"`, `"native-tls"` | [`native-tls`][native-tls] |
//! | [`tokio`](https://docs.rs/tokio/0.2/tokio/) | `tokio` | `"tokio-util"`, `"tokio-openssl"`, `"openssl"` | [`openssl`][openssl] |
//!
//! [rustls]: https://docs.rs/rustls/0.18.1/rustls/
//! [native-tls]: https://docs.rs/native-tls/0.2.4/native_tls/
//! [openssl]: https://docs.rs/openssl/0.10/openssl/
//!
use futures_lite::{AsyncRead, AsyncWrite};
use std::{future::Future, io::Result as IoResult, net::SocketAddr};
Expand Down Expand Up @@ -115,7 +117,7 @@ pub use self::smol::Connector as SmolConnector;
pub use self::smol::ConnectorTls as SmolConnectorTls;

#[cfg(all(feature = "tokio", feature = "tokio-util"))]
/// Connector for using a [`tokio::net::TcpStream`](https://docs.rs/tokio/latest/tokio/net/struct.TcpStream.html)
/// Connector for using a [`tokio::net::TcpStream`](https://docs.rs/tokio/0.2/tokio/net/struct.TcpStream.html)
pub mod tokio;

#[cfg(all(feature = "tokio", feature = "tokio-util"))]
Expand All @@ -140,6 +142,15 @@ pub use self::tokio::ConnectorRustTls as TokioConnectorRustTls;
#[doc(inline)]
pub use self::tokio::ConnectorNativeTls as TokioConnectorNativeTls;

#[cfg(all(
feature = "tokio",
feature = "tokio-util",
feature = "tokio-openssl",
feature = "openssl"
))]
#[doc(inline)]
pub use self::tokio::ConnectorOpenSsl as TokioConnectorOpenSsl;

/// The connector trait. This is used to abstract out runtimes.
///
/// You can implement this on your own type to provide a custom connection behavior.
Expand Down Expand Up @@ -212,6 +223,14 @@ mod required {
compile_error! {
"'tokio', 'tokio-util' and 'webpki-roots' must be enabled when 'tokio-rustls' is enabled"
}

#[cfg(all(
feature = "tokio-openssl",
not(all(feature = "tokio", feature = "tokio-util", feature = "openssl"))
))]
compile_error! {
"'tokio', 'tokio-util' and 'openssl' must be enabled when 'tokio-openssl' is enabled"
}
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions src/connector/tokio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ mod rustls;

#[cfg(all(feature = "tokio-rustls", feature = "webpki-roots"))]
pub use rustls::*;

#[cfg(all(feature = "tokio-openssl", feature = "openssl"))]
mod openssl;

#[cfg(all(feature = "tokio-openssl", feature = "openssl"))]
pub use self::openssl::*;
2 changes: 1 addition & 1 deletion src/connector/tokio/native_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ConnectorNativeTls {

impl ConnectorNativeTls {
connector_ctor!(tls:
/// [`tokio`](https://docs.rs/tokio/latest/tokio/) (using [`tokio-native-tls`](https://docs.rs/tokio-native-tls/latest/tokio_native_tls/))
/// [`tokio`](https://docs.rs/tokio/0.2/tokio/) (using [`tokio-native-tls`](https://docs.rs/tokio-native-tls/latest/tokio_native_tls/))
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/connector/tokio/non_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Connector {

impl Connector {
connector_ctor!(non_tls:
/// [`tokio`](https://docs.rs/tokio/latest/tokio/)
/// [`tokio`](https://docs.rs/tokio/0.2/tokio/)
);
}

Expand Down
62 changes: 62 additions & 0 deletions src/connector/tokio/openssl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use super::*;

use std::io::{Error, ErrorKind};

/// A `tokio` connector that uses `tokio-openssl` (an `openssl` wrapper). This uses TLS.
///
/// To use this type, ensure you set up the 'TLS Domain' in the configuration.
///
/// The crate provides the 'TLS domain' for Twitch in the root of this crate.
#[derive(Debug, Clone, PartialEq)]
pub struct ConnectorOpenSsl {
addrs: Vec<std::net::SocketAddr>,
tls_domain: String,
}

impl ConnectorOpenSsl {
connector_ctor!(tls:
/// [`tokio`](https://docs.rs/tokio/0.2/tokio/) (using [`tokio-openssl`](https://docs.rs/tokio_openssl/latest/tokio_openssl/))
);
}

type CloneStream<T> = async_dup::Mutex<tokio_util::compat::Compat<T>>;
type Stream = tokio_openssl::SslStream<tokio::net::TcpStream>;

impl crate::connector::Connector for ConnectorOpenSsl {
type Output = CloneStream<Stream>;

fn connect(&mut self) -> BoxedFuture<std::io::Result<Self::Output>> {
let this = self.clone();

let fut = async move {
use tokio_util::compat::Tokio02AsyncReadCompatExt as _;

let config = ::openssl::ssl::SslConnector::builder(::openssl::ssl::SslMethod::tls())
.and_then(|c| c.build().configure())
.map_err(|err| Error::new(ErrorKind::Other, err))?;

let stream = tokio::net::TcpStream::connect(&*this.addrs).await?;
let stream = tokio_openssl::connect(config, &this.tls_domain, stream)
.await
.map_err(|err| Error::new(ErrorKind::Other, err))?;

Ok(async_dup::Mutex::new(stream.compat()))
};
Box::pin(fut)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn assert_connector_trait_is_fulfilled() {
use crate::connector::testing::*;
use crate::connector::Connector as C;

assert_connector::<ConnectorOpenSsl>();
assert_type_is_read_write::<<ConnectorOpenSsl as C>::Output>();
assert_obj_is_sane(ConnectorOpenSsl::twitch().unwrap());
}
}
2 changes: 1 addition & 1 deletion src/connector/tokio/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ConnectorRustTls {

impl ConnectorRustTls {
connector_ctor!(tls:
/// [`tokio`](https://docs.rs/tokio/latest/tokio/) (using [`tokio-rustls`](https://docs.rs/tokio-rustls/latest/tokio_rustls/))
/// [`tokio`](https://docs.rs/tokio/0.2/tokio/) (using [`tokio-rustls`](https://docs.rs/tokio-rustls/latest/tokio_rustls/))
);
}

Expand Down

0 comments on commit b232ed0

Please sign in to comment.