mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-24 17:20:51 +02:00
fix(websocket): install crypto provider if needed (#3124)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
websocket: patch:bug
|
||||||
|
websocket-js: patch:bug
|
||||||
|
---
|
||||||
|
|
||||||
|
The WebSocket plugin will now install the default crypto provider if needed, preventing panics on WSS connections.
|
||||||
Generated
+1
@@ -6965,6 +6965,7 @@ dependencies = [
|
|||||||
"http",
|
"http",
|
||||||
"log",
|
"log",
|
||||||
"rand 0.9.0",
|
"rand 0.9.0",
|
||||||
|
"rustls",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tauri",
|
"tauri",
|
||||||
|
|||||||
@@ -31,10 +31,16 @@ rand = "0.9"
|
|||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
tokio = { version = "1", features = ["net", "sync"] }
|
tokio = { version = "1", features = ["net", "sync"] }
|
||||||
tokio-tungstenite = { version = "0.28" }
|
tokio-tungstenite = { version = "0.28" }
|
||||||
|
rustls = { version = "0.23", default-features = false, features = [
|
||||||
|
"ring",
|
||||||
|
], optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["rustls-tls"]
|
default = ["rustls-tls"]
|
||||||
native-tls = ["tokio-tungstenite/native-tls"]
|
native-tls = ["tokio-tungstenite/native-tls"]
|
||||||
native-tls-vendored = ["native-tls", "tokio-tungstenite/native-tls-vendored"]
|
native-tls-vendored = ["native-tls", "tokio-tungstenite/native-tls-vendored"]
|
||||||
rustls-tls = ["tokio-tungstenite/rustls-tls-webpki-roots"]
|
rustls-tls = ["tokio-tungstenite/rustls-tls-webpki-roots", "dep:rustls"]
|
||||||
rustls-tls-native-roots = ["tokio-tungstenite/rustls-tls-native-roots"]
|
rustls-tls-native-roots = [
|
||||||
|
"tokio-tungstenite/rustls-tls-native-roots",
|
||||||
|
"dep:rustls",
|
||||||
|
]
|
||||||
|
|||||||
@@ -18,9 +18,17 @@ use tauri::{
|
|||||||
Manager, Runtime, State, Window,
|
Manager, Runtime, State, Window,
|
||||||
};
|
};
|
||||||
use tokio::{net::TcpStream, sync::Mutex};
|
use tokio::{net::TcpStream, sync::Mutex};
|
||||||
#[cfg(any(feature = "rustls-tls", feature = "native-tls"))]
|
#[cfg(any(
|
||||||
|
feature = "rustls-tls",
|
||||||
|
feature = "rustls-tls-native-roots",
|
||||||
|
feature = "native-tls"
|
||||||
|
))]
|
||||||
use tokio_tungstenite::connect_async_tls_with_config;
|
use tokio_tungstenite::connect_async_tls_with_config;
|
||||||
#[cfg(not(any(feature = "rustls-tls", feature = "native-tls")))]
|
#[cfg(not(any(
|
||||||
|
feature = "rustls-tls",
|
||||||
|
feature = "rustls-tls-native-roots",
|
||||||
|
feature = "native-tls"
|
||||||
|
)))]
|
||||||
use tokio_tungstenite::connect_async_with_config;
|
use tokio_tungstenite::connect_async_with_config;
|
||||||
use tokio_tungstenite::{
|
use tokio_tungstenite::{
|
||||||
tungstenite::{
|
tungstenite::{
|
||||||
@@ -63,7 +71,11 @@ impl Serialize for Error {
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct ConnectionManager(Mutex<HashMap<Id, WebSocketWriter>>);
|
struct ConnectionManager(Mutex<HashMap<Id, WebSocketWriter>>);
|
||||||
|
|
||||||
#[cfg(any(feature = "rustls-tls", feature = "native-tls"))]
|
#[cfg(any(
|
||||||
|
feature = "rustls-tls",
|
||||||
|
feature = "rustls-tls-native-roots",
|
||||||
|
feature = "native-tls"
|
||||||
|
))]
|
||||||
struct TlsConnector(Mutex<Option<Connector>>);
|
struct TlsConnector(Mutex<Option<Connector>>);
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -157,17 +169,29 @@ async fn connect<R: Runtime>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "rustls-tls", feature = "native-tls"))]
|
#[cfg(any(
|
||||||
|
feature = "rustls-tls",
|
||||||
|
feature = "rustls-tls-native-roots",
|
||||||
|
feature = "native-tls"
|
||||||
|
))]
|
||||||
let tls_connector = match window.try_state::<TlsConnector>() {
|
let tls_connector = match window.try_state::<TlsConnector>() {
|
||||||
Some(tls_connector) => tls_connector.0.lock().await.clone(),
|
Some(tls_connector) => tls_connector.0.lock().await.clone(),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(any(feature = "rustls-tls", feature = "native-tls"))]
|
#[cfg(any(
|
||||||
|
feature = "rustls-tls",
|
||||||
|
feature = "rustls-tls-native-roots",
|
||||||
|
feature = "native-tls"
|
||||||
|
))]
|
||||||
let (ws_stream, _) =
|
let (ws_stream, _) =
|
||||||
connect_async_tls_with_config(request, config.map(Into::into), false, tls_connector)
|
connect_async_tls_with_config(request, config.map(Into::into), false, tls_connector)
|
||||||
.await?;
|
.await?;
|
||||||
#[cfg(not(any(feature = "rustls-tls", feature = "native-tls")))]
|
#[cfg(not(any(
|
||||||
|
feature = "rustls-tls",
|
||||||
|
feature = "rustls-tls-native-roots",
|
||||||
|
feature = "native-tls"
|
||||||
|
)))]
|
||||||
let (ws_stream, _) = connect_async_with_config(request, config.map(Into::into), false).await?;
|
let (ws_stream, _) = connect_async_with_config(request, config.map(Into::into), false).await?;
|
||||||
|
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
@@ -266,8 +290,21 @@ impl Builder {
|
|||||||
PluginBuilder::new("websocket")
|
PluginBuilder::new("websocket")
|
||||||
.invoke_handler(tauri::generate_handler![connect, send])
|
.invoke_handler(tauri::generate_handler![connect, send])
|
||||||
.setup(|app, _api| {
|
.setup(|app, _api| {
|
||||||
|
#[cfg(any(feature = "rustls-tls", feature = "rustls-tls-native-roots"))]
|
||||||
|
if (self.tls_connector.is_none()
|
||||||
|
|| matches!(self.tls_connector, Some(Connector::Plain)))
|
||||||
|
&& rustls::crypto::CryptoProvider::get_default().is_none()
|
||||||
|
{
|
||||||
|
// This can only fail if there is already a default provider which we checked for already.
|
||||||
|
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||||
|
}
|
||||||
|
|
||||||
app.manage(ConnectionManager::default());
|
app.manage(ConnectionManager::default());
|
||||||
#[cfg(any(feature = "rustls-tls", feature = "native-tls"))]
|
#[cfg(any(
|
||||||
|
feature = "rustls-tls",
|
||||||
|
feature = "rustls-tls-native-roots",
|
||||||
|
feature = "native-tls"
|
||||||
|
))]
|
||||||
app.manage(TlsConnector(Mutex::new(self.tls_connector)));
|
app.manage(TlsConnector(Mutex::new(self.tls_connector)));
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user