Skip to content
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

Add support for native-tls when using websocket transport #742

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions rumqttc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["use-rustls"]
use-rustls = ["dep:tokio-rustls", "dep:rustls-webpki", "dep:rustls-pemfile", "dep:rustls-native-certs"]
use-native-tls = ["dep:tokio-native-tls", "dep:native-tls"]
use-rustls = ["dep:tokio-rustls", "dep:rustls-webpki", "dep:rustls-pemfile", "dep:rustls-native-certs",
"async-tungstenite?/tokio-rustls-webpki-roots", "async-tungstenite?/tokio-rustls-native-certs"]
use-native-tls = ["dep:tokio-native-tls", "dep:native-tls", "async-tungstenite?/tokio-native-tls"]
websocket = ["dep:async-tungstenite", "dep:ws_stream_tungstenite", "dep:http"]
proxy = ["dep:async-http-proxy"]

Expand Down
14 changes: 12 additions & 2 deletions rumqttc/src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ async fn network_connect(
let (domain, port) = match options.transport() {
#[cfg(feature = "websocket")]
Transport::Ws => split_url(&options.broker_addr)?,
#[cfg(all(feature = "use-rustls", feature = "websocket"))]
#[cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
))]
Transport::Wss(_) => split_url(&options.broker_addr)?,
_ => options.broker_address(),
};
Expand Down Expand Up @@ -415,7 +418,10 @@ async fn network_connect(

Network::new(WsStream::new(socket), options.max_incoming_packet_size)
}
#[cfg(all(feature = "use-rustls", feature = "websocket"))]
#[cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
))]
Transport::Wss(tls_config) => {
let mut request = options.broker_addr.as_str().into_client_request()?;
request
Expand All @@ -426,14 +432,18 @@ async fn network_connect(
request = request_modifier(request).await;
}

#[cfg(feature = "use-rustls")]
let connector = tls::rustls_connector(&tls_config).await?;
#[cfg(feature = "use-native-tls")]
let connector = tls::native_tls_connector(&tls_config).await?;

let (socket, response) = async_tungstenite::tokio::client_async_tls_with_connector(
request,
tcp_stream,
Some(connector),
)
.await?;

validate_response_headers(response)?;

Network::new(WsStream::new(socket), options.max_incoming_packet_size)
Expand Down
38 changes: 33 additions & 5 deletions rumqttc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ pub enum Transport {
#[cfg(feature = "websocket")]
#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
Ws,
#[cfg(all(feature = "use-rustls", feature = "websocket"))]
#[cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "use-rustls", feature = "websocket"))))]
Wss(TlsConfiguration),
}
Expand Down Expand Up @@ -320,14 +323,32 @@ impl Transport {
Self::wss_with_config(config)
}

#[cfg(all(feature = "use-rustls", feature = "websocket"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "use-rustls", feature = "websocket"))))]
#[cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
))]
#[cfg_attr(
docsrs,
doc(cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
)))
)]
pub fn wss_with_config(tls_config: TlsConfiguration) -> Self {
Self::Wss(tls_config)
}

#[cfg(all(feature = "use-rustls", feature = "websocket"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "use-rustls", feature = "websocket"))))]
#[cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
))]
#[cfg_attr(
docsrs,
doc(cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
)))
)]
pub fn wss_with_default_config() -> Self {
Self::Wss(Default::default())
}
Expand Down Expand Up @@ -376,6 +397,13 @@ impl Default for TlsConfiguration {
}
}

#[cfg(feature = "use-native-tls")]
impl Default for TlsConfiguration {
fn default() -> Self {
Self::Native
}
}

#[cfg(feature = "use-rustls")]
impl From<ClientConfig> for TlsConfiguration {
fn from(config: ClientConfig) -> Self {
Expand Down
13 changes: 11 additions & 2 deletions rumqttc/src/v5/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ async fn network_connect(options: &MqttOptions) -> Result<Network, ConnectionErr
let (domain, port) = match options.transport() {
#[cfg(feature = "websocket")]
Transport::Ws => split_url(&options.broker_addr)?,
#[cfg(all(feature = "use-rustls", feature = "websocket"))]
#[cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
))]
Transport::Wss(_) => split_url(&options.broker_addr)?,
_ => options.broker_address(),
};
Expand Down Expand Up @@ -354,7 +357,10 @@ async fn network_connect(options: &MqttOptions) -> Result<Network, ConnectionErr

Network::new(WsStream::new(socket), max_incoming_pkt_size)
}
#[cfg(all(feature = "use-rustls", feature = "websocket"))]
#[cfg(all(
any(feature = "use-rustls", feature = "use-native-tls"),
feature = "websocket"
))]
Transport::Wss(tls_config) => {
let mut request = options.broker_addr.as_str().into_client_request()?;
request
Expand All @@ -365,7 +371,10 @@ async fn network_connect(options: &MqttOptions) -> Result<Network, ConnectionErr
request = request_modifier(request).await;
}

#[cfg(feature = "use-rustls")]
let connector = tls::rustls_connector(&tls_config).await?;
#[cfg(feature = "use-native-tls")]
let connector = tls::native_tls_connector(&tls_config).await?;

let (socket, response) = async_tungstenite::tokio::client_async_tls_with_connector(
request,
Expand Down
Loading