From 64c3009aa947e0908065b72b94ec2a552f934b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Tue, 28 Jun 2022 16:32:15 +0200 Subject: [PATCH] connect: reuse the same id (#1412) * connect: reuse the same id * changelog: add note * rustfmt * clippy --- CHANGELOG.md | 1 + nym-connect/Cargo.lock | 1 - nym-connect/src-tauri/Cargo.toml | 1 - nym-connect/src-tauri/src/config/mod.rs | 118 +++++++++++++++++------- nym-connect/src-tauri/src/state.rs | 2 +- 5 files changed, 87 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35fa7289c2..1f354e83fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ Post 1.0.0 release, the changelog format is based on [Keep a Changelog](https:// ### Changed +- nym-connect: reuse config id instead of creating a new id on each connection. - validator-client: created internal `Coin` type that replaces coins from `cosmrs` and `cosmwasm` for API entrypoints [[#1295]] - all: updated all `cosmwasm`-related dependencies to `1.0.0` and `cw-storage-plus` to `0.13.4` [[#1318]] - all: updated `rocket` to `0.5.0-rc.2`. diff --git a/nym-connect/Cargo.lock b/nym-connect/Cargo.lock index 33c4434c63..87cfbcd0df 100644 --- a/nym-connect/Cargo.lock +++ b/nym-connect/Cargo.lock @@ -3392,7 +3392,6 @@ dependencies = [ "futures", "log", "nym-socks5-client", - "once_cell", "pretty_env_logger", "rand 0.7.3", "serde", diff --git a/nym-connect/src-tauri/Cargo.toml b/nym-connect/src-tauri/Cargo.toml index 1a4a844df0..92e555b322 100644 --- a/nym-connect/src-tauri/Cargo.toml +++ b/nym-connect/src-tauri/Cargo.toml @@ -24,7 +24,6 @@ bip39 = "1.0" dirs = "4.0" eyre = "0.6.5" futures = "0.3" -once_cell = "1.12" rand = "0.7" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/nym-connect/src-tauri/src/config/mod.rs b/nym-connect/src-tauri/src/config/mod.rs index 1db949b799..ef726edb9e 100644 --- a/nym-connect/src-tauri/src/config/mod.rs +++ b/nym-connect/src-tauri/src/config/mod.rs @@ -1,19 +1,11 @@ use client_core::config::GatewayEndpoint; use log::info; -use once_cell::sync::Lazy; -use rand::Rng; use client_core::config::Config as BaseConfig; use config::NymConfig; use nym_socks5::client::config::Config as Socks5Config; -// Generate a random id used for the config, since we need to init a new configuration each time -// due to not being able to reuse gateway registration. This is probably something we should -// improve. -pub static SOCKS5_CONFIG_ID: Lazy = Lazy::new(|| { - let mut rng = rand::thread_rng(); - format!("{}{:08}", "nym-connect-", rng.gen::()) -}); +pub static SOCKS5_CONFIG_ID: &str = "nym-connect"; // This is an open-proxy network-requester for testing // TODO: make this configurable from the UI @@ -62,13 +54,27 @@ impl Config { } pub async fn init_socks5(provider_address: &str, chosen_gateway_id: Option<&str>) { - let id: &str = &SOCKS5_CONFIG_ID; + log::info!("Initialising client..."); + + let id: &str = SOCKS5_CONFIG_ID; + + let already_init = Socks5Config::default_config_file_path(Some(id)).exists(); + if already_init { + log::info!( + "SOCKS5 client \"{}\" was already initialised before! \ + Config information will be overwritten (but keys will be kept)!", + id + ); + } + + // Future proofing. This flag exists for the other clients + let user_wants_force_register = false; + + 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 gateway = setup_gateway(chosen_gateway_id, config.get_socks5()).await; - config.get_base_mut().with_gateway_endpoint(gateway); - // 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. config @@ -78,40 +84,86 @@ 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; + config.get_base_mut().with_gateway_endpoint(gateway); + let config_save_location = config.get_socks5().get_config_file_save_location(); config .get_socks5() .save_to_file(None) .expect("Failed to save the config file"); - info!("Saved configuration file to {:?}", config_save_location); - info!( - "Using gateway: {}", - config.get_socks5().get_base().get_gateway_id(), + + log::info!("Saved configuration file to {:?}", config_save_location); + log::info!("Gateway id: {}", config.get_base().get_gateway_id()); + log::info!("Gateway owner: {}", config.get_base().get_gateway_owner()); + log::info!( + "Gateway listener: {}", + config.get_base().get_gateway_listener() + ); + + log::info!( + "Service provider address: {}", + config.get_socks5().get_provider_mix_address() + ); + log::info!( + "Service provider port: {}", + config.get_socks5().get_listening_port() ); info!("Client configuration completed."); client_core::init::show_address(config.get_base()); } +// TODO: deduplicate with same functions in other client async fn setup_gateway( + id: &str, + register: bool, user_chosen_gateway_id: Option<&str>, - config: &nym_socks5::client::config::Config, + config: &Socks5Config, ) -> GatewayEndpoint { - // Get the gateway details by querying the validator-api. Either pick one at random or use - // the chosen one if it's among the available ones. - log::info!("Configuring gateway"); - let gateway = client_core::init::query_gateway_details( - config.get_base().get_validator_api_endpoints(), - user_chosen_gateway_id, - ) - .await; - log::debug!("Querying gateway gives: {}", gateway); - - // Registering with gateway by setting up and writing shared keys to disk - log::trace!("Registering gateway"); - client_core::init::register_with_gateway_and_store_keys(gateway.clone(), config.get_base()) + if register { + // Get the gateway details by querying the validator-api. Either pick one at random or use + // the chosen one if it's among the available ones. + println!("Configuring gateway"); + let gateway = client_core::init::query_gateway_details( + config.get_base().get_validator_api_endpoints(), + user_chosen_gateway_id, + ) .await; - log::info!("Saved all generated keys"); + log::debug!("Querying gateway gives: {}", gateway); - gateway.into() + // Registering with gateway by setting up and writing shared keys to disk + log::trace!("Registering gateway"); + client_core::init::register_with_gateway_and_store_keys(gateway.clone(), config.get_base()) + .await; + println!("Saved all generated keys"); + + gateway.into() + } else if user_chosen_gateway_id.is_some() { + // Just set the config, don't register or create any keys + // This assumes that the user knows what they are doing, and that the existing keys are + // valid for the gateway being used + println!("Using gateway provided by user, keeping existing keys"); + let gateway = client_core::init::query_gateway_details( + config.get_base().get_validator_api_endpoints(), + user_chosen_gateway_id, + ) + .await; + log::debug!("Querying gateway gives: {}", gateway); + gateway.into() + } else { + println!("Not registering gateway, will reuse existing config and keys"); + match Socks5Config::load_from_file(Some(id)) { + Ok(existing_config) => existing_config.get_base().get_gateway_endpoint().clone(), + Err(err) => { + panic!( + "Unable to configure gateway: {err}. \n + Seems like the client was already initialized but it was not possible to read \ + the existing configuration file. \n + CAUTION: Consider backing up your gateway keys and try force gateway registration, or \ + removing the existing configuration and starting over." + ) + } + } + } } diff --git a/nym-connect/src-tauri/src/state.rs b/nym-connect/src-tauri/src/state.rs index 80a288954b..d5f7464b9e 100644 --- a/nym-connect/src-tauri/src/state.rs +++ b/nym-connect/src-tauri/src/state.rs @@ -88,7 +88,7 @@ impl State { } fn start_nym_socks5_client() -> Socks5ControlMessageSender { - let id: &str = &SOCKS5_CONFIG_ID; + let id: &str = SOCKS5_CONFIG_ID; info!("Loading config from file"); let config = nym_socks5::client::config::Config::load_from_file(Some(id)).unwrap();