Tungstenite: Match scheme for "ws" or "wss" (#554)
Match scheme for "ws" or "wss" and choose whether to connect with TLS connector for tungstenite
This commit is contained in:
parent
5bee907733
commit
9926f8ab94
|
@ -9,8 +9,10 @@ use futures_util::{
|
||||||
};
|
};
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
use tokio_tungstenite::{
|
use tokio_tungstenite::{
|
||||||
connect_async_tls_with_config, tungstenite, Connector, MaybeTlsStream, WebSocketStream,
|
connect_async_tls_with_config, connect_async_with_config, tungstenite, Connector,
|
||||||
|
MaybeTlsStream, WebSocketStream,
|
||||||
};
|
};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use crate::gateway::{GatewayMessage, RawGatewayMessage};
|
use crate::gateway::{GatewayMessage, RawGatewayMessage};
|
||||||
|
|
||||||
|
@ -32,38 +34,60 @@ impl TungsteniteBackend {
|
||||||
pub async fn connect(
|
pub async fn connect(
|
||||||
websocket_url: &str,
|
websocket_url: &str,
|
||||||
) -> Result<(TungsteniteSink, TungsteniteStream), TungsteniteBackendError> {
|
) -> Result<(TungsteniteSink, TungsteniteStream), TungsteniteBackendError> {
|
||||||
let certs = webpki_roots::TLS_SERVER_ROOTS;
|
let websocket_url_parsed =
|
||||||
let roots = rustls::RootCertStore {
|
Url::parse(websocket_url).map_err(|_| TungsteniteBackendError::TungsteniteError {
|
||||||
roots: certs
|
error: tungstenite::error::Error::Url(
|
||||||
.iter()
|
tungstenite::error::UrlError::UnsupportedUrlScheme,
|
||||||
.map(|cert| {
|
),
|
||||||
rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
|
})?;
|
||||||
cert.subject.to_vec(),
|
if websocket_url_parsed.scheme() == "ws" {
|
||||||
cert.subject_public_key_info.to_vec(),
|
let (websocket_stream, _) =
|
||||||
cert.name_constraints.as_ref().map(|der| der.to_vec()),
|
match connect_async_with_config(websocket_url, None, false).await {
|
||||||
)
|
Ok(websocket_stream) => websocket_stream,
|
||||||
})
|
Err(e) => return Err(TungsteniteBackendError::TungsteniteError { error: e }),
|
||||||
.collect(),
|
};
|
||||||
};
|
|
||||||
let (websocket_stream, _) = match connect_async_tls_with_config(
|
|
||||||
websocket_url,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
Some(Connector::Rustls(
|
|
||||||
rustls::ClientConfig::builder()
|
|
||||||
.with_safe_defaults()
|
|
||||||
.with_root_certificates(roots)
|
|
||||||
.with_no_client_auth()
|
|
||||||
.into(),
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(websocket_stream) => websocket_stream,
|
|
||||||
Err(e) => return Err(TungsteniteBackendError::TungsteniteError { error: e }),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(websocket_stream.split())
|
Ok(websocket_stream.split())
|
||||||
|
} else if websocket_url_parsed.scheme() == "wss" {
|
||||||
|
let certs = webpki_roots::TLS_SERVER_ROOTS;
|
||||||
|
let roots = rustls::RootCertStore {
|
||||||
|
roots: certs
|
||||||
|
.iter()
|
||||||
|
.map(|cert| {
|
||||||
|
rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
|
||||||
|
cert.subject.to_vec(),
|
||||||
|
cert.subject_public_key_info.to_vec(),
|
||||||
|
cert.name_constraints.as_ref().map(|der| der.to_vec()),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
let (websocket_stream, _) = match connect_async_tls_with_config(
|
||||||
|
websocket_url,
|
||||||
|
None,
|
||||||
|
false,
|
||||||
|
Some(Connector::Rustls(
|
||||||
|
rustls::ClientConfig::builder()
|
||||||
|
.with_safe_defaults()
|
||||||
|
.with_root_certificates(roots)
|
||||||
|
.with_no_client_auth()
|
||||||
|
.into(),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(websocket_stream) => websocket_stream,
|
||||||
|
Err(e) => return Err(TungsteniteBackendError::TungsteniteError { error: e }),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(websocket_stream.split())
|
||||||
|
} else {
|
||||||
|
Err(TungsteniteBackendError::TungsteniteError {
|
||||||
|
error: tungstenite::error::Error::Url(
|
||||||
|
tungstenite::error::UrlError::UnsupportedUrlScheme,
|
||||||
|
),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue