From 00dda8a63d8c217ba0da41085d53fe0e3c174d8e Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Fri, 13 Oct 2023 22:04:58 +0300 Subject: [PATCH] fix wss: on tauri --- Cargo.lock | 15 +++++++++++++++ ng-app/src-tauri/Cargo.toml | 1 + ng-app/src-tauri/src/lib.rs | 2 +- ng-wallet/src/lib.rs | 31 ++++++++++++++++++++++++------- p2p-client-ws/src/remote_ws.rs | 11 +++++------ p2p-net/src/types.rs | 1 + 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1568866..c47beb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -322,6 +322,18 @@ dependencies = [ "event-listener", ] +[[package]] +name = "async-native-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9343dc5acf07e79ff82d0c37899f079db3534d99f189a1837c8e549c99405bec" +dependencies = [ + "futures-util", + "native-tls", + "thiserror", + "url", +] + [[package]] name = "async-oneshot" version = "0.5.0" @@ -410,6 +422,7 @@ name = "async-tungstenite" version = "0.22.2" source = "git+https://git.nextgraph.org/NextGraph/async-tungstenite.git?branch=nextgraph#979de8e77d365af4630607dfdc721d5d9aeea42e" dependencies = [ + "async-native-tls", "async-std", "futures-io", "futures-util", @@ -3042,6 +3055,7 @@ name = "ng-app" version = "0.1.0" dependencies = [ "async-std", + "async-tungstenite", "ng-wallet", "p2p-client-ws", "p2p-net", @@ -5588,6 +5602,7 @@ dependencies = [ "http", "httparse", "log", + "native-tls", "rand 0.8.5", "sha1", "thiserror", diff --git a/ng-app/src-tauri/Cargo.toml b/ng-app/src-tauri/Cargo.toml index 412c048..9c84936 100644 --- a/ng-app/src-tauri/Cargo.toml +++ b/ng-app/src-tauri/Cargo.toml @@ -32,6 +32,7 @@ ng-wallet = { path = "../../ng-wallet" } async-std = { version = "1.12.0", features = ["attributes", "unstable"] } # tauri-plugin-window = { git = "https://git.nextgraph.org/NextGraph/plugins-workspace.git", branch="window-alpha.1-nextgraph" } tauri-plugin-window = "2.0.0-alpha.1" +async-tungstenite = { git = "https://git.nextgraph.org/NextGraph/async-tungstenite.git", branch = "nextgraph", features = ["async-std-runtime", "async-native-tls"] } [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/ng-app/src-tauri/src/lib.rs b/ng-app/src-tauri/src/lib.rs index 42c6ecc..d0e228e 100644 --- a/ng-app/src-tauri/src/lib.rs +++ b/ng-app/src-tauri/src/lib.rs @@ -433,7 +433,7 @@ async fn broker_connect( info, session, opened_wallet, - location, + None, Box::new(ConnectionWebSocket {}), ) .await?; diff --git a/ng-wallet/src/lib.rs b/ng-wallet/src/lib.rs index 759f73f..f94cecc 100644 --- a/ng-wallet/src/lib.rs +++ b/ng-wallet/src/lib.rs @@ -458,7 +458,7 @@ pub async fn connect_wallet( let arc_cnx = Arc::new(cnx); match opened_wallet { EncryptedWallet::V0(wallet) => { - log_debug!("XXXX {} {:?}", client.to_string(), wallet); + log_info!("XXXX {} {:?}", client.to_string(), wallet); let auto_open = &wallet .clients .get(&client.to_string()) @@ -501,10 +501,12 @@ pub async fn connect_wallet( continue; } let broker = broker.unwrap(); + let mut tried: Option<(String, String, String, Option, f64)> = None; for broker_info in broker { match broker_info { BrokerInfoV0::ServerV0(server) => { let url = server.get_ws_url(&location).await; + log_debug!("URL {:?}", url); //Option<(String, Vec)> if url.is_some() { let url = url.unwrap(); @@ -515,13 +517,13 @@ pub async fn connect_wallet( .await .connect( arc_cnx.clone(), - peer_key, + peer_key.clone(), peer_id, server_key, StartConfig::Client(ClientConfig { url: url.0.clone(), user: *user, - user_priv, + user_priv: user_priv.clone(), client, info: info.clone(), registration: Some(core.1), @@ -530,23 +532,38 @@ pub async fn connect_wallet( .await; log_debug!("broker.connect : {:?}", res); - result.push(( - user_id, + tried = Some(( + user_id.clone(), core.0.to_string(), url.0.into(), - match res { + match &res { Ok(_) => None, Err(e) => Some(e.to_string()), }, get_unix_time(), )); } - break; // TODO implement failover + if tried.is_some() && tried.as_ref().unwrap().3.is_none() { + // successful. we can stop here + break; + } else { + log_debug!("Failed connection {:?}", tried); + } } } _ => {} } } + if tried.is_none() { + tried = Some(( + user_id, + core.0.to_string(), + "".into(), + Some("No broker found".into()), + get_unix_time(), + )); + } + result.push(tried.unwrap()); } _ => unimplemented!(), } diff --git a/p2p-client-ws/src/remote_ws.rs b/p2p-client-ws/src/remote_ws.rs index 2b1bc01..7a3c1f6 100644 --- a/p2p-client-ws/src/remote_ws.rs +++ b/p2p-client-ws/src/remote_ws.rs @@ -13,7 +13,6 @@ use std::sync::Arc; -use async_std::net::TcpStream; use async_tungstenite::tungstenite::protocol::frame::coding::CloseCode; use async_tungstenite::tungstenite::protocol::CloseFrame; use async_tungstenite::WebSocketStream; @@ -33,7 +32,7 @@ use p2p_repo::log::*; use p2p_repo::types::*; use p2p_repo::utils::{generate_keypair, now_timestamp}; -use async_tungstenite::async_std::connect_async; +use async_tungstenite::async_std::{connect_async, ConnectStream}; use async_tungstenite::tungstenite::{Error, Message}; pub struct ConnectionWebSocket {} @@ -125,7 +124,7 @@ impl IConnect for ConnectionWebSocket { #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] impl IAccept for ConnectionWebSocket { - type Socket = WebSocketStream; + type Socket = WebSocketStream; async fn accept( &self, remote_bind_address: BindAddress, @@ -161,7 +160,7 @@ impl IAccept for ConnectionWebSocket { } async fn close_ws( - stream: &mut WebSocketStream, + stream: &mut WebSocketStream, receiver: &mut Sender, code: u16, reason: &str, @@ -191,12 +190,12 @@ async fn close_ws( } async fn ws_loop( - mut ws: WebSocketStream, + mut ws: WebSocketStream, sender: Receiver, mut receiver: Sender, ) -> Result<(), NetError> { async fn inner_loop( - stream: &mut WebSocketStream, + stream: &mut WebSocketStream, mut sender: Receiver, receiver: &mut Sender, ) -> Result { diff --git a/p2p-net/src/types.rs b/p2p-net/src/types.rs index 98903ce..1660ee0 100644 --- a/p2p-net/src/types.rs +++ b/p2p-net/src/types.rs @@ -20,6 +20,7 @@ use crate::utils::{ use crate::{actor::EActor, actors::*, errors::ProtocolError}; use core::fmt; use p2p_repo::errors::NgError; +use p2p_repo::log::*; use p2p_repo::types::*; use serde::{Deserialize, Serialize}; use std::collections::HashMap;