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]
default = ["connect", "tls", "async_std_runtime"]
connect = ["stream"]
async_std_runtime = ["connect", "tls", "async-std"]
tls = ["async-tls", "stream"]
async_std_runtime = ["connect", "async-std"]
tls-base = ["stream"]
tls = ["async-tls", "tls-base"]
native-tls = ["async-native-tls", "real-native-tls", "tls-base", "tungstenite/tls"]
stream = []
[dependencies]
@ -35,6 +37,15 @@ version = "1.0"
optional = true
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]
url = "2.0.0"
env_logger = "0.7"

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

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

Loading…
Cancel
Save