-
Notifications
You must be signed in to change notification settings - Fork 11
Support both rustls and native-tls #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/target | ||
**/*.rs.bk | ||
Cargo.lock | ||
.idea/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Changelog | ||
|
||
## 0.4.0 - 2022-02-22 | ||
|
||
### Added | ||
|
||
- Support for [`native-tls`](https://github.com/sfackler/rust-native-tls). | ||
|
||
### Changed | ||
|
||
- Either one of the `rustls` or `native-tls` features must now be enabled. | ||
- The `TlsListener` stream now returns a `tls_listener::Error` instead of `std::io::Error` type. | ||
- Signatures of `TcpListener::new()` and `builder()` have changed to now take an argument `T: Into<TlsAcceptor>`. | ||
When passing a `rustls::ServerConfig` it should therefore be wrapped in an `Arc` first. | ||
|
||
### Fixed | ||
|
||
- Crate will now compile when linked against a target that doesn't explicitly enable the `tokio/time` and `hyper/tcp` | ||
features. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
[package] | ||
name = "tls-listener" | ||
description = "wrap incoming Stream of connections in TLS" | ||
version = "0.3.0" | ||
version = "0.4.0" | ||
authors = ["Thayne McCombs <[email protected]>"] | ||
repository = "https://github.com/tmccombs/tls-listener" | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
|
||
[features] | ||
default = ["tokio-net"] | ||
rustls = ["tokio-rustls"] | ||
native-tls = ["tokio-native-tls"] | ||
|
||
tokio-net = ["tokio/net"] | ||
hyper-h1 = ["hyper", "hyper/http1"] | ||
hyper-h2 = ["hyper", "hyper/http2"] | ||
|
||
[dependencies] | ||
futures-util = "0.3.8" | ||
tokio = "1.0" | ||
tokio-rustls = "0.23.0" | ||
hyper = { version = "0.14.1", features = ["server", "tcp"], optional = true } | ||
pin-project-lite = "0.2.8" | ||
#tokio-native-tls = "0.3.0" | ||
|
||
[dependencies.hyper] | ||
version = "0.14.1" | ||
features = ["server"] | ||
optional = true | ||
thiserror = "1.0.30" | ||
tokio = { version = "1.0", features = ["time"] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a bug here such that if you try to use this crate in another project that doesn't itself also import tokio/time, then it will fail to compile. |
||
tokio-native-tls = { version = "0.3.0", optional = true } | ||
tokio-rustls = { version = "0.23.0", optional = true } | ||
|
||
[dev-dependencies] | ||
hyper = { version = "0.14.1", features = ["server", "http1", "tcp", "stream"] } | ||
hyper = { version = "0.14.1", features = ["http1", "stream"] } | ||
tokio = { version = "1.0", features = ["rt", "macros", "net", "io-util"] } | ||
|
||
[[example]] | ||
|
@@ -51,5 +50,5 @@ path = "examples/http-low-level.rs" | |
required-features = ["hyper-h1"] | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
features = ["rustls", "hyper-h1", "hyper-h2"] | ||
rustdoc-args = ["--cfg", "docsrs"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,32 @@ | ||
use tokio_rustls::rustls::{Certificate, PrivateKey, ServerConfig}; | ||
#[cfg(feature = "rustls")] | ||
mod cert { | ||
pub const CERT: &[u8] = include_bytes!("local.cert"); | ||
pub const PKEY: &[u8] = include_bytes!("local.key"); | ||
} | ||
#[cfg(feature = "native-tls")] | ||
const PFX: &[u8] = include_bytes!("local.pfx"); | ||
|
||
#[cfg(feature = "rustls")] | ||
pub fn tls_acceptor() -> std::sync::Arc<tokio_rustls::rustls::ServerConfig> { | ||
use std::sync::Arc; | ||
use tokio_rustls::rustls::{Certificate, PrivateKey, ServerConfig}; | ||
|
||
const CERT: &[u8] = include_bytes!("local.cert"); | ||
const PKEY: &[u8] = include_bytes!("local.key"); | ||
let key = PrivateKey(cert::PKEY.into()); | ||
let cert = Certificate(cert::CERT.into()); | ||
|
||
Arc::new( | ||
ServerConfig::builder() | ||
.with_safe_defaults() | ||
.with_no_client_auth() | ||
.with_single_cert(vec![cert], key) | ||
.unwrap(), | ||
) | ||
} | ||
|
||
pub fn tls_config() -> ServerConfig { | ||
let key = PrivateKey(PKEY.into()); | ||
let cert = Certificate(CERT.into()); | ||
#[cfg(feature = "native-tls")] | ||
pub fn tls_acceptor() -> tokio_native_tls::native_tls::TlsAcceptor { | ||
use tokio_native_tls::native_tls::{Identity, TlsAcceptor}; | ||
|
||
ServerConfig::builder() | ||
.with_safe_defaults() | ||
.with_no_client_auth() | ||
.with_single_cert(vec![cert], key) | ||
.unwrap() | ||
let identity = Identity::from_pkcs12(PFX, "").unwrap(); | ||
TlsAcceptor::builder(identity).build().unwrap() | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you would like rustls to be in the defaults instead? I think this way is more consistent with the rust ecosystem from what I have seen of other libraries