diff --git a/nym-connect/src-tauri/src/config/mod.rs b/nym-connect/src-tauri/src/config/mod.rs index 93acea98e0..7b3009632e 100644 --- a/nym-connect/src-tauri/src/config/mod.rs +++ b/nym-connect/src-tauri/src/config/mod.rs @@ -2,31 +2,51 @@ use std::path::PathBuf; use client_core::config::GatewayEndpoint; use log::info; +use std::sync::Arc; +use tokio::sync::RwLock; use client_core::config::Config as BaseConfig; use config::NymConfig; use nym_socks5::client::config::Config as Socks5Config; -pub static SOCKS5_CONFIG_ID: &str = "nym-connect"; +use crate::{error::BackendError, state::State}; -// This is an open-proxy network-requester for testing -// TODO: make this configurable from the UI -// TODO: once we can set this is the UI, consider just removing it, and put in guards to halt if -// user hasn't chosen the provider -pub static PROVIDER_ADDRESS: &str = "8CrdmK4mYgZ5caMxGU4AvNeT1dXL8VSbgMYAjSFvnfut.2GLdZ1Jn9vkTBMf858evGNGDsPoeivUPw7zFNceLiLX3@BNjYZPxzcJwczXHHgBxCAyVJKxN6LPteDRrKapxWmexv"; +pub static SOCKS5_CONFIG_ID: &str = "nym-connect"; const DEFAULT_ETH_ENDPOINT: &str = "https://rinkeby.infura.io/v3/00000000000000000000000000000000"; const DEFAULT_ETH_PRIVATE_KEY: &str = "0000000000000000000000000000000000000000000000000000000000000001"; -#[tauri::command] -pub fn get_config_file_location() -> String { - let id: &str = SOCKS5_CONFIG_ID; - Config::config_file_location(id) - .to_string_lossy() - .to_string() +pub fn append_config_id(gateway_id: &str) -> String { + let mut id = SOCKS5_CONFIG_ID.to_string(); + id.push_str(&format!("-{}", gateway_id)); + id } +#[tauri::command] +pub async fn get_config_id( + state: tauri::State<'_, Arc>>, +) -> Result { + let guard = state.read().await; + // TODO: return error instead + let gateway_id = guard + .get_gateway() + .as_ref() + .expect("The config id can not be determined before setting the gateway"); + Ok(append_config_id(gateway_id)) +} + +#[tauri::command] +pub async fn get_config_file_location( + state: tauri::State<'_, Arc>>, +) -> Result { + let id = get_config_id(state).await?; + Ok(Config::config_file_location(&id) + .to_string_lossy() + .to_string()) +} + +#[derive(Debug)] pub struct Config { socks5: Socks5Config, } @@ -55,9 +75,7 @@ impl Config { self.socks5.get_base_mut() } - pub async fn init(service_provider: Option<&String>, chosen_gateway_id: Option<&String>) { - let service_provider = service_provider.map_or(PROVIDER_ADDRESS, String::as_str); - let chosen_gateway_id = chosen_gateway_id.map(String::as_str); + pub async fn init(service_provider: &str, chosen_gateway_id: &str) { info!("Initialising..."); init_socks5(service_provider, chosen_gateway_id).await; info!("Configuration saved 🚀"); @@ -68,16 +86,17 @@ impl Config { } } -pub async fn init_socks5(provider_address: &str, chosen_gateway_id: Option<&str>) { +pub async fn init_socks5(provider_address: &str, chosen_gateway_id: &str) { log::info!("Initialising client..."); - let id: &str = SOCKS5_CONFIG_ID; + // Append the gateway id to the name id that we store the config under + let id = append_config_id(chosen_gateway_id); log::debug!( "Attempting to use config file location: {}", - Config::config_file_location(id).to_string_lossy(), + Config::config_file_location(&id).to_string_lossy(), ); - let already_init = Config::config_file_location(id).exists(); + let already_init = Config::config_file_location(&id).exists(); if already_init { log::info!( "SOCKS5 client \"{}\" was already initialised before! \ @@ -92,7 +111,7 @@ pub async fn init_socks5(provider_address: &str, chosen_gateway_id: Option<&str> let register_gateway = !already_init || user_wants_force_register; log::trace!("Creating config for id: {}", id); - let mut config = Config::new(id, provider_address); + let mut config = Config::new(id.as_str(), provider_address); // As far as I'm aware, these two are not used, they are only set because the socks5 init code // requires them for initialising the bandwidth controller. @@ -103,7 +122,13 @@ pub async fn init_socks5(provider_address: &str, chosen_gateway_id: Option<&str> .get_base_mut() .with_eth_private_key(DEFAULT_ETH_PRIVATE_KEY); - let gateway = setup_gateway(id, register_gateway, chosen_gateway_id, config.get_socks5()).await; + let gateway = setup_gateway( + &id, + register_gateway, + Some(chosen_gateway_id), + config.get_socks5(), + ) + .await; config.get_base_mut().with_gateway_endpoint(gateway); let config_save_location = config.get_socks5().get_config_file_save_location(); diff --git a/nym-connect/src-tauri/src/main.rs b/nym-connect/src-tauri/src/main.rs index 8f74d60429..1496aa6f4b 100644 --- a/nym-connect/src-tauri/src/main.rs +++ b/nym-connect/src-tauri/src/main.rs @@ -35,6 +35,7 @@ fn main() { .manage(Arc::new(RwLock::new(State::new()))) .invoke_handler(tauri::generate_handler![ crate::config::get_config_file_location, + crate::config::get_config_id, crate::operations::connection::connect::get_gateway, crate::operations::connection::connect::get_service_provider, crate::operations::connection::connect::set_gateway, @@ -62,5 +63,9 @@ fn setup_logging() { log_builder .filter_module("handlebars", log::LevelFilter::Warn) + .filter_module("mio", log::LevelFilter::Warn) + .filter_module("sled", log::LevelFilter::Warn) + .filter_module("tokio_tungstenite", log::LevelFilter::Warn) + .filter_module("tungstenite", log::LevelFilter::Warn) .init(); } diff --git a/nym-connect/src-tauri/src/operations/connection/connect.rs b/nym-connect/src-tauri/src/operations/connection/connect.rs index 6ffb583be3..b28913472f 100644 --- a/nym-connect/src-tauri/src/operations/connection/connect.rs +++ b/nym-connect/src-tauri/src/operations/connection/connect.rs @@ -11,6 +11,9 @@ pub async fn start_connecting( ) -> Result { let mut guard = state.write().await; + log::trace!("Start connecting with:"); + log::trace!(" service_provider: {:?}", guard.get_service_provider()); + log::trace!(" gateway: {:?}", guard.get_gateway()); guard.start_connecting(&window).await; Ok(ConnectResult { @@ -35,6 +38,7 @@ pub async fn set_service_provider( service_provider: String, state: tauri::State<'_, Arc>>, ) -> Result<(), BackendError> { + log::trace!("Setting service_provider: {service_provider}"); let mut guard = state.write().await; guard.set_service_provider(service_provider); Ok(()) @@ -56,6 +60,7 @@ pub async fn set_gateway( gateway: String, state: tauri::State<'_, Arc>>, ) -> Result<(), BackendError> { + log::trace!("Setting gateway: {gateway}"); let mut guard = state.write().await; guard.set_gateway(gateway); Ok(()) diff --git a/nym-connect/src-tauri/src/state.rs b/nym-connect/src-tauri/src/state.rs index b52d7fd31b..a367cb01ff 100644 --- a/nym-connect/src-tauri/src/state.rs +++ b/nym-connect/src-tauri/src/state.rs @@ -8,10 +8,12 @@ use config::NymConfig; use nym_socks5::client::NymClient as Socks5NymClient; use nym_socks5::client::{Socks5ControlMessage, Socks5ControlMessageSender}; -use crate::config::SOCKS5_CONFIG_ID; -use crate::models::{ - AppEventConnectionStatusChangedPayload, ConnectionStatusKind, - APP_EVENT_CONNECTION_STATUS_CHANGED, +use crate::{ + config::append_config_id, + models::{ + AppEventConnectionStatusChangedPayload, ConnectionStatusKind, + APP_EVENT_CONNECTION_STATUS_CHANGED, + }, }; use tauri::Manager; @@ -64,7 +66,15 @@ impl State { } pub async fn init_config(&self) { - crate::config::Config::init(self.service_provider.as_ref(), self.gateway.as_ref()).await; + let service_provider = self + .service_provider + .as_ref() + .expect("Attempting to init without service provider"); + let gateway = self + .gateway + .as_ref() + .expect("Attempting to init without gateway"); + crate::config::Config::init(service_provider, gateway).await; } pub async fn start_connecting(&mut self, window: &tauri::Window) { @@ -76,7 +86,12 @@ impl State { self.init_config().await; // Kick of the main task and get the channel for controlling it - let (sender, used_gateway) = start_nym_socks5_client(); + let id = append_config_id( + self.gateway + .as_ref() + .expect("Attempting to start without gateway"), + ); + let (sender, used_gateway) = start_nym_socks5_client(&id); self.gateway = Some(used_gateway.gateway_id); self.socks5_client_sender = Some(sender); @@ -99,10 +114,9 @@ impl State { } } -fn start_nym_socks5_client() -> (Socks5ControlMessageSender, GatewayEndpoint) { - let id: &str = SOCKS5_CONFIG_ID; - - info!("Loading config from file"); +fn start_nym_socks5_client(id: &str) -> (Socks5ControlMessageSender, GatewayEndpoint) { + info!("Loading config from file: {id}"); + // TODO: handle this gracefully! let config = nym_socks5::client::config::Config::load_from_file(Some(id)).unwrap(); let used_gateway = config.get_base().get_gateway_endpoint().clone();