diff --git a/clients/client-core/src/config/mod.rs b/clients/client-core/src/config/mod.rs index 978f832e13..429b9588d0 100644 --- a/clients/client-core/src/config/mod.rs +++ b/clients/client-core/src/config/mod.rs @@ -345,8 +345,8 @@ pub struct Client { nym_root_directory: PathBuf, #[serde(skip)] - super_struct: PhantomData<*const T>, - // super_struct: PhantomData, + //super_struct: PhantomData<*const T>, + super_struct: PhantomData, } impl Default for Client { diff --git a/clients/native/src/client/mod.rs b/clients/native/src/client/mod.rs index 1ba55c183f..c9899af37c 100644 --- a/clients/native/src/client/mod.rs +++ b/clients/native/src/client/mod.rs @@ -36,7 +36,7 @@ use nymsphinx::receiver::ReconstructedMessage; use crate::client::config::{Config, SocketType}; use crate::websocket; -pub mod config; +pub(crate) mod config; pub struct NymClient { /// Client configuration options, including, among other things, packet sending rates, diff --git a/clients/native/src/commands/init.rs b/clients/native/src/commands/init.rs index ab764a1e89..b2cfd95a4d 100644 --- a/clients/native/src/commands/init.rs +++ b/clients/native/src/commands/init.rs @@ -88,7 +88,7 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { app } -pub async fn register_with_gateway( +async fn register_with_gateway( gateway: &gateway::Node, our_identity: Arc, ) -> Arc { @@ -110,7 +110,7 @@ pub async fn register_with_gateway( .expect("failed to register with the gateway!") } -pub async fn gateway_details( +async fn gateway_details( validator_servers: Vec, chosen_gateway_id: Option<&str>, ) -> gateway::Node { @@ -144,7 +144,7 @@ pub async fn gateway_details( } } -pub fn show_address(config: &Config) { +fn show_address(config: &Config) { fn load_identity_keys(pathfinder: &ClientKeyPathfinder) -> identity::KeyPair { let identity_keypair: identity::KeyPair = pemstore::load_keypair(&pemstore::KeyPairPath::new( diff --git a/clients/native/src/commands/mod.rs b/clients/native/src/commands/mod.rs index 3e7731d469..c4d0f07eaa 100644 --- a/clients/native/src/commands/mod.rs +++ b/clients/native/src/commands/mod.rs @@ -11,13 +11,13 @@ pub(crate) const ETH_ENDPOINT_ARG_NAME: &str = "eth_endpoint"; #[cfg(not(feature = "coconut"))] pub(crate) const ETH_PRIVATE_KEY_ARG_NAME: &str = "eth_private_key"; #[cfg(not(feature = "coconut"))] -pub const DEFAULT_ETH_ENDPOINT: &str = +pub(crate) const DEFAULT_ETH_ENDPOINT: &str = "https://rinkeby.infura.io/v3/00000000000000000000000000000000"; #[cfg(not(feature = "coconut"))] -pub const DEFAULT_ETH_PRIVATE_KEY: &str = +pub(crate) const DEFAULT_ETH_PRIVATE_KEY: &str = "0000000000000000000000000000000000000000000000000000000000000001"; -pub mod init; +pub(crate) mod init; pub(crate) mod run; pub(crate) mod upgrade; diff --git a/clients/native/src/lib.rs b/clients/native/src/lib.rs index 9830241223..2adec5a73d 100644 --- a/clients/native/src/lib.rs +++ b/clients/native/src/lib.rs @@ -2,5 +2,4 @@ // SPDX-License-Identifier: Apache-2.0 pub mod client; -pub mod commands; pub mod websocket; diff --git a/clients/socks5/src/client/mod.rs b/clients/socks5/src/client/mod.rs index 967ded9ba8..2e344435e1 100644 --- a/clients/socks5/src/client/mod.rs +++ b/clients/socks5/src/client/mod.rs @@ -48,8 +48,12 @@ pub struct NymClient { impl NymClient { pub fn new(config: Config) -> Self { + info!("NymClient::new() 1"); let pathfinder = ClientKeyPathfinder::new_from_config(config.get_base()); + info!("NymClient::new() 2"); + dbg!(&pathfinder); let key_manager = KeyManager::load_keys(&pathfinder).expect("failed to load stored keys"); + info!("NymClient::new() 3"); NymClient { config, diff --git a/clients/socks5/src/commands/init.rs b/clients/socks5/src/commands/init.rs index 911438c3d3..5f7fed4718 100644 --- a/clients/socks5/src/commands/init.rs +++ b/clients/socks5/src/commands/init.rs @@ -88,7 +88,7 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { app } -async fn register_with_gateway( +pub async fn register_with_gateway( gateway: &gateway::Node, our_identity: Arc, ) -> Arc { @@ -110,10 +110,12 @@ async fn register_with_gateway( .expect("failed to register with the gateway!") } -async fn gateway_details( +pub async fn gateway_details( validator_servers: Vec, chosen_gateway_id: Option<&str>, ) -> gateway::Node { + dbg!(&validator_servers); + dbg!(&chosen_gateway_id); let validator_api = validator_servers .choose(&mut thread_rng()) .expect("The list of validator apis is empty"); @@ -144,7 +146,7 @@ async fn gateway_details( } } -fn show_address(config: &Config) { +pub fn show_address(config: &Config) { fn load_identity_keys(pathfinder: &ClientKeyPathfinder) -> identity::KeyPair { let identity_keypair: identity::KeyPair = pemstore::load_keypair(&pemstore::KeyPairPath::new( diff --git a/clients/socks5/src/commands/mod.rs b/clients/socks5/src/commands/mod.rs index 66813410ef..37bdc9d675 100644 --- a/clients/socks5/src/commands/mod.rs +++ b/clients/socks5/src/commands/mod.rs @@ -5,7 +5,7 @@ use crate::client::config::Config; use clap::ArgMatches; use url::Url; -pub(crate) mod init; +pub mod init; pub(crate) mod run; pub(crate) mod upgrade; diff --git a/clients/socks5/src/commands/run.rs b/clients/socks5/src/commands/run.rs index 87bdc1a30f..b50d13c788 100644 --- a/clients/socks5/src/commands/run.rs +++ b/clients/socks5/src/commands/run.rs @@ -106,5 +106,6 @@ pub async fn execute(matches: ArgMatches<'static>) { return; } + dbg!(&config); NymClient::new(config).run_forever().await; } diff --git a/clients/socks5/src/lib.rs b/clients/socks5/src/lib.rs index 2faec9d4e8..d23a471d69 100644 --- a/clients/socks5/src/lib.rs +++ b/clients/socks5/src/lib.rs @@ -3,3 +3,4 @@ pub mod client; pub mod socks; +pub mod commands; diff --git a/hrycyszynvpn/Cargo.lock b/hrycyszynvpn/Cargo.lock index 430a044223..3ec47e5994 100644 --- a/hrycyszynvpn/Cargo.lock +++ b/hrycyszynvpn/Cargo.lock @@ -2553,6 +2553,7 @@ dependencies = [ "nym-network-requester", "nym-socks5-client", "nymsphinx", + "pretty_env_logger", "rand 0.7.3", "serde", "serde_json", diff --git a/hrycyszynvpn/src-tauri/Cargo.toml b/hrycyszynvpn/src-tauri/Cargo.toml index 15d38c8665..d3db7a0474 100644 --- a/hrycyszynvpn/src-tauri/Cargo.toml +++ b/hrycyszynvpn/src-tauri/Cargo.toml @@ -59,6 +59,7 @@ network-defaults = { path = "../../common/network-defaults" } fix-path-env = { git = "https://github.com/tauri-apps/fix-path-env-rs", branch = "release"} log = "0.4" +pretty_env_logger = "0.4.0" [dev-dependencies] ts-rs = "6.1.2" diff --git a/hrycyszynvpn/src-tauri/src/config/mod.rs b/hrycyszynvpn/src-tauri/src/config/mod.rs index 15419bdbde..4bbbdc44c9 100644 --- a/hrycyszynvpn/src-tauri/src/config/mod.rs +++ b/hrycyszynvpn/src-tauri/src/config/mod.rs @@ -4,15 +4,14 @@ use rand::rngs::OsRng; use client_core::client::key_manager::KeyManager; use client_core::config::persistence::key_pathfinder::ClientKeyPathfinder; use config::NymConfig; -#[cfg(not(feature = "coconut"))] -use nym_client::commands::{DEFAULT_ETH_ENDPOINT, DEFAULT_ETH_PRIVATE_KEY}; -use nymsphinx::addressing::clients::Recipient; -pub static NATIVE_CLIENT_CONFIG_ID: &str = "hrycyszynvpn"; pub static SOCKS5_CONFIG_ID: &str = "hrycyszynvpn"; +// TODO +pub static PROVIDER_ADDRESS: &str = "HL2ufyQ875JNkro17ubKQB4gJyy2ozG8SMrUvHbYXYca.E7iz8hixuWE763h6MdaSnXAXobDqEHuq3Bzvi9XLQhW@BNjYZPxzcJwczXHHgBxCAyVJKxN6LPteDRrKapxWmexv"; + // TODO: move to config file -static GATEWAY_ID: &str = "83x9YyNkQ5QEY84ZU6Wmq8XHqfwf9SUtR7g5PAYB1FRY"; // sandbox +//static GATEWAY_ID: &str = "83x9YyNkQ5QEY84ZU6Wmq8XHqfwf9SUtR7g5PAYB1FRY"; // sandbox pub struct Config {} @@ -20,104 +19,45 @@ impl Config { pub async fn init() { info!("Initialising..."); - let native_client_address = init_native_client(GATEWAY_ID).await; - init_socks5(native_client_address, GATEWAY_ID).await; + init_socks5(PROVIDER_ADDRESS, None).await; info!("Configuration saved 🚀"); } } -pub async fn init_native_client(chosen_gateway_id: &str) -> Recipient { - let mut config = nym_client::client::config::Config::new(NATIVE_CLIENT_CONFIG_ID); - - let mut rng = OsRng; - - // create identity, encryption and ack keys. - let mut key_manager = KeyManager::new(&mut rng); - - let gateway_details = nym_client::commands::init::gateway_details( - config.get_base().get_validator_api_endpoints(), - Some(chosen_gateway_id), - ) - .await; - - config - .get_base_mut() - .with_gateway_id(gateway_details.identity_key.to_base58_string()); - - config.get_base_mut().with_testnet_mode(true); - config - .get_base_mut() - .with_eth_endpoint(DEFAULT_ETH_ENDPOINT); - config - .get_base_mut() - .with_eth_private_key(DEFAULT_ETH_PRIVATE_KEY); - - let shared_keys = nym_client::commands::init::register_with_gateway( - &gateway_details, - key_manager.identity_keypair(), - ) - .await; - - config - .get_base_mut() - .with_gateway_listener(gateway_details.clients_address()); - key_manager.insert_gateway_shared_key(shared_keys); - - let pathfinder = ClientKeyPathfinder::new_from_config(config.get_base()); - key_manager - .store_keys(&pathfinder) - .expect("Failed to generated keys"); - info!("Saved all generated keys"); - - let config_save_location = config.get_config_file_save_location(); - config - .save_to_file(None) - .expect("Failed to save the config file"); - info!("Saved configuration file to {:?}", config_save_location); - info!("Using gateway: {}", config.get_base().get_gateway_id(),); - info!("Client configuration completed.\n\n\n"); - - nym_client::commands::init::show_address(&config) -} - -pub async fn init_socks5(provider_address: Recipient, chosen_gateway_id: &str) { +pub async fn init_socks5(provider_address: &str, chosen_gateway_id: Option<&str>) { let id = SOCKS5_CONFIG_ID; - - let mut config = nym_socks5::client::config::Config::new(id, &format!("{}", provider_address)); - - let mut rng = OsRng; + let mut config = nym_socks5::client::config::Config::new(id, provider_address); // create identity, encryption and ack keys. + let mut rng = OsRng; let mut key_manager = KeyManager::new(&mut rng); - config.get_base_mut().with_testnet_mode(true); - config - .get_base_mut() - .with_eth_endpoint(DEFAULT_ETH_ENDPOINT); - config - .get_base_mut() - .with_eth_private_key(DEFAULT_ETH_PRIVATE_KEY); - - let gateway_details = nym_client::commands::init::gateway_details( + info!("Getting gateway details"); + let gateway_details = nym_socks5::commands::init::gateway_details( config.get_base().get_validator_api_endpoints(), - Some(chosen_gateway_id), + chosen_gateway_id, ) .await; - config - .get_base_mut() - .with_gateway_id(gateway_details.identity_key.to_base58_string()); - let shared_keys = nym_client::commands::init::register_with_gateway( + + info!("Registering with gateway"); + let shared_keys = nym_socks5::commands::init::register_with_gateway( &gateway_details, key_manager.identity_keypair(), ) .await; - config - .get_base_mut() - .with_gateway_listener(gateway_details.clients_address()); + info!("Setting gateway endpoint"); + config.get_base_mut().with_gateway_endpoint( + gateway_details.identity_key.to_base58_string(), + gateway_details.owner.clone(), + gateway_details.clients_address(), + ); + + info!("Insert gateway shared key"); key_manager.insert_gateway_shared_key(shared_keys); + info!("Creating client key path finder"); let pathfinder = ClientKeyPathfinder::new_from_config(config.get_base()); key_manager .store_keys(&pathfinder) @@ -131,4 +71,6 @@ pub async fn init_socks5(provider_address: Recipient, chosen_gateway_id: &str) { info!("Saved configuration file to {:?}", config_save_location); info!("Using gateway: {}", config.get_base().get_gateway_id(),); info!("Client configuration completed.\n\n\n"); + + nym_socks5::commands::init::show_address(&config); } diff --git a/hrycyszynvpn/src-tauri/src/main.rs b/hrycyszynvpn/src-tauri/src/main.rs index 52389a7bcc..7fa1012cf8 100644 --- a/hrycyszynvpn/src-tauri/src/main.rs +++ b/hrycyszynvpn/src-tauri/src/main.rs @@ -25,6 +25,7 @@ mod state; mod window; fn main() { + setup_logging(); println!("Starting up..."); // as per breaking change description here: https://github.com/tauri-apps/tauri/blob/feac1d193c6d618e49916ad0707201f43d5cdd36/tooling/bundler/CHANGELOG.md @@ -44,6 +45,20 @@ fn main() { .expect("error while running tauri application"); } +fn setup_logging() { + let mut log_builder = pretty_env_logger::formatted_timed_builder(); + if let Ok(s) = ::std::env::var("RUST_LOG") { + log_builder.parse_filters(&s); + } else { + // default to 'Info' + log_builder.filter(None, log::LevelFilter::Info); + } + + log_builder + .filter_module("handlebars", log::LevelFilter::Warn) + .init(); +} + #[cfg(test)] mod test { ts_rs::export! { diff --git a/hrycyszynvpn/src-tauri/src/state.rs b/hrycyszynvpn/src-tauri/src/state.rs index 55a0b875b7..c9ef29162e 100644 --- a/hrycyszynvpn/src-tauri/src/state.rs +++ b/hrycyszynvpn/src-tauri/src/state.rs @@ -1,196 +1,80 @@ -//use crate::models::{ -// AppEventConnectionStatusChangedPayload, ConnectionStatusKind, APP_EVENT_CONNECTION_STATUS_CHANGED, -//}; -//use tauri::Manager; -//use tokio::time::{sleep, Duration}; -// -//type StatusChangedHandler = fn(status: &ConnectionStatusKind) -> (); -// -//pub struct State { -// status: ConnectionStatusKind, -//} -// -//impl State { -// pub fn new() -> Self { -// State { -// status: ConnectionStatusKind::Disconnected, -// } -// } -// -// pub fn get_status(&self) -> ConnectionStatusKind { -// self.status.clone() -// } -// -// fn set_state(&mut self, status: ConnectionStatusKind, window: &tauri::Window) { -// self.status = status.clone(); -// window.emit_all( -// APP_EVENT_CONNECTION_STATUS_CHANGED, -// AppEventConnectionStatusChangedPayload { status }, -// ); -// } -// -// pub async fn init_config() { -// // crate::config::Config::init().await; -// } -// -// pub async fn start_connecting(&mut self, window: &tauri::Window) { -// self.set_state(ConnectionStatusKind::Connecting, window); -// sleep(Duration::from_secs(2)).await; -// self.set_state(ConnectionStatusKind::Connected, window); -// } -// -// pub async fn start_disconnecting(&mut self, window: &tauri::Window) { -// self.set_state(ConnectionStatusKind::Disconnecting, window); -// sleep(Duration::from_secs(2)).await; -// self.set_state(ConnectionStatusKind::Disconnected, window); -// } -//} - -use log::{error, info}; +use log::info; use config::NymConfig; -use nym_client::client::NymClient as NativeNymClient; #[cfg(not(feature = "coconut"))] use nym_socks5::client::NymClient as Socks5NymClient; -use nymsphinx::addressing::clients::Recipient; -use std::sync::Arc; -use tokio::sync::RwLock; -use crate::config::{NATIVE_CLIENT_CONFIG_ID, SOCKS5_CONFIG_ID}; -use crate::models::ConnectionStatusKind; +use crate::config::SOCKS5_CONFIG_ID; +use crate::models::{ + AppEventConnectionStatusChangedPayload, ConnectionStatusKind, APP_EVENT_CONNECTION_STATUS_CHANGED, +}; +use tauri::Manager; pub struct State { status: ConnectionStatusKind, - nym_client: Arc>>, - socks5_client: Arc>>, - nym_network_requester: Arc>>, + //socks5_client: Arc>>, } impl State { pub fn new() -> Self { State { status: ConnectionStatusKind::Disconnected, - nym_client: Arc::new(RwLock::new(None)), - socks5_client: Arc::new(RwLock::new(None)), - nym_network_requester: Arc::new(RwLock::new(None)), + //socks5_client: Arc::new(RwLock::new(None)), } } + #[allow(unused)] pub fn get_status(&self) -> ConnectionStatusKind { self.status.clone() } + fn set_state(&mut self, status: ConnectionStatusKind, window: &tauri::Window) { + self.status = status.clone(); + window + .emit_all( + APP_EVENT_CONNECTION_STATUS_CHANGED, + AppEventConnectionStatusChangedPayload { status }, + ) + .unwrap(); + } + pub async fn init_config() { crate::config::Config::init().await; } - pub async fn start_connecting(&mut self) { + pub async fn start_connecting(&mut self, window: &tauri::Window) { + info!("Connecting"); + self.set_state(ConnectionStatusKind::Connecting, window); self.status = ConnectionStatusKind::Connecting; - let mut nym_client_guard = self.nym_client.write().await; - let mut socks5_client_guard = self.socks5_client.write().await; - let mut nym_network_requester_guard = self.nym_network_requester.write().await; + Self::init_config().await; + tokio::spawn(async move { start_nym_socks5_client().await }); - *nym_client_guard = start_nym_native_client().await; - if nym_client_guard.is_none() { - self.status = ConnectionStatusKind::Disconnected; - return; - } - - let address = nym_client_guard.unwrap().as_mix_recipient(); - info!("Nym client address is {}", address); - - *socks5_client_guard = self.start_nym_socks5_client(&address).await; - if socks5_client_guard.is_none() { - self.status = ConnectionStatusKind::Disconnected; - return; - } - - *nym_network_requester_guard = self.start_network_requester().await; self.status = ConnectionStatusKind::Connected; + self.set_state(ConnectionStatusKind::Connected, window); } - pub async fn start_disconnecting(&mut self) { + pub async fn start_disconnecting(&mut self, window: &tauri::Window) { + info!("Disconnecting"); + self.set_state(ConnectionStatusKind::Disconnecting, window); self.status = ConnectionStatusKind::Disconnecting; - let nym_client_guard = self.nym_client.write().await; - let socks5_client_guard = self.socks5_client.write().await; - let nym_network_requester_guard = self.nym_network_requester.write().await; - - if nym_network_requester_guard.is_some() { - // TODO: implement - // nym_network_requester_guard.unwrap().stop().await; - *nym_network_requester_guard = None; - } - - if socks5_client_guard.is_some() { - // TODO: implement - // socks5_client_guard.unwrap().stop().await; - *socks5_client_guard = None; - } - - if nym_client_guard.is_some() { - // TODO: implement - // nym_client_guard.unwrap().stop().await; - *nym_client_guard = None; - } + // TODO: implement + // socks5_client_guard.unwrap().stop().await; + // *socks5_client_guard = None; self.status = ConnectionStatusKind::Disconnected; + self.set_state(ConnectionStatusKind::Disconnected, window); } } -async fn start_nym_native_client() -> Option { - let id = NATIVE_CLIENT_CONFIG_ID; - - let config = match nym_client::client::config::Config::load_from_file(Some(id)) { - Ok(cfg) => cfg, - Err(err) => { - error!( - "Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", - id, err - ); - return None; - } - }; - - // let base_config = config.get_base_mut(); - // base_config.with_gateway_id("83x9YyNkQ5QEY84ZU6Wmq8XHqfwf9SUtR7g5PAYB1FRY"); - - let mut nym_client = NativeNymClient::new(config); - nym_client.start().await; - Some(nym_client) -} - -async fn start_nym_socks5_client(recipient: &Recipient) -> Option { +async fn start_nym_socks5_client() { let id = SOCKS5_CONFIG_ID; - let mut config = match nym_socks5::client::config::Config::load_from_file(Some(id)) { - Ok(cfg) => cfg, - Err(err) => { - error!( - "Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", - id, err - ); - return None; - } - }; - - // let base_config = config.get_base_mut(); - // base_config.with_gateway_id("83x9YyNkQ5QEY84ZU6Wmq8XHqfwf9SUtR7g5PAYB1FRY"); - - config = config.with_provider_mix_address(recipient.to_string()); + info!("Loading config from file"); + let config = nym_socks5::client::config::Config::load_from_file(Some(id)).unwrap(); let mut socks5_client = Socks5NymClient::new(config); + info!("Starting socks5 client"); socks5_client.start().await; - Some(socks5_client) -} - -// TODO: use remote network requester -async fn start_network_requester() -> nym_network_requester::core::ServiceProvider { - let open_proxy = true; - let uri = "ws://localhost:1977"; - info!("Starting socks5 service provider:"); - let mut server = nym_network_requester::core::ServiceProvider::new(uri.into(), open_proxy); - server.run().await; - server } diff --git a/service-providers/network-requester/Cargo.toml b/service-providers/network-requester/Cargo.toml index 263ce746d1..7a24cb7ec2 100644 --- a/service-providers/network-requester/Cargo.toml +++ b/service-providers/network-requester/Cargo.toml @@ -7,10 +7,6 @@ version = "1.0.0-rc.2" authors = ["Dave Hrycyszyn ", "Jędrzej Stuczyński "] edition = "2021" -[lib] -name = "nym_network_requester" -path = "src/lib.rs" - # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/service-providers/network-requester/src/lib.rs b/service-providers/network-requester/src/lib.rs deleted file mode 100644 index 2c89e7337d..0000000000 --- a/service-providers/network-requester/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2021 - Nym Technologies SA -// SPDX-License-Identifier: Apache-2.0 - -mod allowed_hosts; -mod connection; -pub mod core; -mod websocket; diff --git a/service-providers/network-requester/src/main.rs b/service-providers/network-requester/src/main.rs index c1cf7c13e2..1780521a53 100644 --- a/service-providers/network-requester/src/main.rs +++ b/service-providers/network-requester/src/main.rs @@ -5,7 +5,7 @@ use clap::{App, Arg, ArgMatches}; mod allowed_hosts; mod connection; -pub mod core; +mod core; mod websocket; const OPEN_PROXY_ARG: &str = "open-proxy";