Add optional support for TLS via async-native-tls instead of async-tls (rustls)

Can be enabled with the "native-tls" feature instead of just "tls".
pull/5/head
Sebastian Dröge 5 years ago
parent 21411b2dc1
commit fb8bbca9ed
  1. 15
      Cargo.toml
  2. 32
      src/connect.rs
  3. 2
      src/lib.rs

@ -14,8 +14,10 @@ edition = "2018"
[features] [features]
default = ["connect", "tls", "async_std_runtime"] default = ["connect", "tls", "async_std_runtime"]
connect = ["stream"] connect = ["stream"]
async_std_runtime = ["connect", "tls", "async-std"] async_std_runtime = ["connect", "async-std"]
tls = ["async-tls", "stream"] tls-base = ["stream"]
tls = ["async-tls", "tls-base"]
native-tls = ["async-native-tls", "real-native-tls", "tls-base", "tungstenite/tls"]
stream = [] stream = []
[dependencies] [dependencies]
@ -35,6 +37,15 @@ version = "1.0"
optional = true optional = true
version = "0.6.0" version = "0.6.0"
[dependencies.async-native-tls]
optional = true
version = "0.1.0"
[dependencies.real-native-tls]
optional = true
version = "0.2"
package = "native-tls"
[dev-dependencies] [dev-dependencies]
url = "2.0.0" url = "2.0.0"
env_logger = "0.7" env_logger = "0.7"

@ -7,11 +7,18 @@ use futures::io::{AsyncRead, AsyncWrite};
use super::{client_async, Request, WebSocketStream}; use super::{client_async, Request, WebSocketStream};
#[cfg(feature = "tls")] #[cfg(feature = "tls-base")]
pub(crate) mod encryption { pub(crate) mod encryption {
#[cfg(feature = "tls")]
use async_tls::client::TlsStream; use async_tls::client::TlsStream;
#[cfg(feature = "tls")]
use async_tls::TlsConnector as AsyncTlsConnector; use async_tls::TlsConnector as AsyncTlsConnector;
#[cfg(feature = "native-tls")]
use async_native_tls::TlsConnector as AsyncTlsConnector;
#[cfg(feature = "native-tls")]
use async_native_tls::TlsStream;
use tungstenite::stream::Mode; use tungstenite::stream::Mode;
use tungstenite::Error; use tungstenite::Error;
@ -35,21 +42,28 @@ pub(crate) mod encryption {
match mode { match mode {
Mode::Plain => Ok(StreamSwitcher::Plain(socket)), Mode::Plain => Ok(StreamSwitcher::Plain(socket)),
Mode::Tls => { Mode::Tls => {
let stream = AsyncTlsConnector::new(); #[cfg(feature = "tls")]
let connected = stream.connect(&domain, socket)?.await; let stream = {
match connected { let connector = AsyncTlsConnector::new();
Err(e) => Err(Error::Io(e)), connector.connect(&domain, socket)?.await?
Ok(s) => Ok(StreamSwitcher::Tls(s)), };
} #[cfg(feature = "native-tls")]
let stream = {
let builder = real_native_tls::TlsConnector::builder();
let connector = builder.build()?;
let connector = AsyncTlsConnector::from(connector);
connector.connect(&domain, socket).await?
};
Ok(StreamSwitcher::Tls(stream))
} }
} }
} }
} }
#[cfg(feature = "tls")] #[cfg(feature = "tls-base")]
pub use self::encryption::MaybeTlsStream; pub use self::encryption::MaybeTlsStream;
#[cfg(not(feature = "tls"))] #[cfg(not(feature = "tls-base"))]
pub(crate) mod encryption { pub(crate) mod encryption {
use futures::io::{AsyncRead, AsyncWrite}; use futures::io::{AsyncRead, AsyncWrite};
use futures::{future, Future}; use futures::{future, Future};

@ -49,7 +49,7 @@ pub use connect::client_async_tls;
#[cfg(feature = "async_std_runtime")] #[cfg(feature = "async_std_runtime")]
pub use connect::connect_async; pub use connect::connect_async;
#[cfg(all(feature = "connect", feature = "tls"))] #[cfg(all(feature = "connect", feature = "tls-base"))]
pub use connect::MaybeTlsStream; pub use connect::MaybeTlsStream;
use std::error::Error; use std::error::Error;
use tungstenite::protocol::CloseFrame; use tungstenite::protocol::CloseFrame;

Loading…
Cancel
Save