Start socks5 client in task

This commit is contained in:
Jon Häggblad
2022-05-23 15:49:21 +02:00
parent 1d6dabd0b5
commit d703e160e3
5 changed files with 82 additions and 94 deletions
+23
View File
@@ -20,6 +20,7 @@ use client_core::client::topology_control::{
use client_core::config::persistence::key_pathfinder::ClientKeyPathfinder;
use crypto::asymmetric::identity;
use futures::channel::mpsc;
use futures::StreamExt;
use gateway_client::bandwidth::BandwidthController;
use gateway_client::{
AcknowledgementReceiver, AcknowledgementSender, GatewayClient, MixnetMessageReceiver,
@@ -37,6 +38,14 @@ use crate::socks::{
pub mod config;
pub type ClientControlMessageSender = mpsc::UnboundedSender<ClientControlMessage>;
pub type ClientControlMessageReceiver = mpsc::UnboundedReceiver<ClientControlMessage>;
#[derive(Debug)]
pub enum ClientControlMessage {
Stop,
}
pub struct NymClient {
/// Client configuration options, including, among other things, packet sending rates,
/// key filepaths, etc.
@@ -272,6 +281,20 @@ impl NymClient {
);
}
// Variant of `run_forever` that listends for message to shutdown
pub async fn run_and_listen(&mut self, mut receiver: ClientControlMessageReceiver) {
self.start().await;
tokio::select! {
message = receiver.next() => match message {
Some(ClientControlMessage::Stop) => {
info!("Received: {:?}", message);
info!("Shutting down");
}
None => info!("none"),
}
}
}
pub async fn start(&mut self) {
info!("Starting nym client");
// channels for inter-component communication
+16 -72
View File
@@ -967,9 +967,9 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.5"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db"
checksum = "0bf124c720b7686e3c2663cf54062ab0f68a88af2fb6a030e87e30bf721fcb38"
dependencies = [
"cfg-if",
"lazy_static",
@@ -1549,15 +1549,6 @@ dependencies = [
"termcolor",
]
[[package]]
name = "error-chain"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc"
dependencies = [
"version_check",
]
[[package]]
name = "ethabi"
version = "14.1.0"
@@ -2539,21 +2530,17 @@ dependencies = [
"base64",
"bip39",
"client-core",
"coconut-interface",
"config",
"cosmrs",
"cosmwasm-std",
"credentials",
"dirs 4.0.0",
"eyre",
"fix-path-env",
"futures",
"log",
"mixnet-contract-common",
"network-defaults",
"nym-client",
"nym-network-requester",
"nym-socks5-client",
"nymsphinx",
"pretty_env_logger",
"rand 0.7.3",
"serde",
@@ -2569,7 +2556,6 @@ dependencies = [
"tokio",
"ts-rs",
"url",
"validator-client",
"version-checker",
"zeroize",
]
@@ -2880,15 +2866,6 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b"
[[package]]
name = "ipnetwork"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02c3eaab3ac0ede60ffa41add21970a7df7d91772c03383aac6c2c3d53cc716b"
dependencies = [
"serde",
]
[[package]]
name = "itertools"
version = "0.10.3"
@@ -3536,31 +3513,6 @@ dependencies = [
"websocket-requests",
]
[[package]]
name = "nym-network-requester"
version = "1.0.1"
dependencies = [
"bincode",
"clap",
"dirs 3.0.2",
"futures",
"ipnetwork",
"log",
"network-defaults",
"nymsphinx",
"ordered-buffer",
"pretty_env_logger",
"proxy-helpers",
"publicsuffix",
"serde",
"socks5-requests",
"sqlx",
"thiserror",
"tokio",
"tokio-tungstenite",
"websocket-requests",
]
[[package]]
name = "nym-socks5-client"
version = "1.0.1"
@@ -4403,20 +4355,6 @@ dependencies = [
"tokio-util 0.6.9",
]
[[package]]
name = "publicsuffix"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bbaa49075179162b49acac1c6aa45fb4dafb5f13cf6794276d77bc7fd95757b"
dependencies = [
"error-chain",
"idna",
"lazy_static",
"native-tls",
"regex",
"url",
]
[[package]]
name = "quick-error"
version = "1.2.3"
@@ -5449,7 +5387,6 @@ dependencies = [
"bitflags",
"byteorder",
"bytes",
"chrono",
"crc",
"crossbeam-channel",
"crossbeam-queue",
@@ -6117,9 +6054,9 @@ dependencies = [
[[package]]
name = "thread_local"
version = "1.1.3"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
dependencies = [
"once_cell",
]
@@ -6321,11 +6258,12 @@ dependencies = [
[[package]]
name = "tracing-core"
version = "0.1.21"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4"
checksum = "f54c8ca710e81886d498c2fd3331b56c93aa248d49de2222ad2742247c60072f"
dependencies = [
"lazy_static",
"valuable",
]
[[package]]
@@ -6341,9 +6279,9 @@ dependencies = [
[[package]]
name = "tracing-subscriber"
version = "0.3.3"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245da694cc7fc4729f3f418b304cb57789f1bed2a78c575407ab8a23f53cb4d3"
checksum = "4bc28f93baff38037f64e6f43d34cfa1605f27a49c34e8a04c5e78b0babf2596"
dependencies = [
"ansi_term",
"lazy_static",
@@ -6574,6 +6512,12 @@ dependencies = [
"vesting-contract-common",
]
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vcpkg"
version = "0.2.15"
+16 -15
View File
@@ -21,18 +21,19 @@ tauri-macros = "=1.0.0-rc.5"
[dependencies]
serde_json = "1.0"
bip39 = "1.0"
dirs = "4.0"
eyre = "0.6.5"
futures = "0.3"
rand = "0.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = { version = "0.23", features = ["derive"] }
tauri = { version = "=1.0.0-rc.8", features = ["ayatana-tray", "shell-open", "system-tray"] }
tokio = { version = "1.10", features = ["sync"] }
dirs = "4.0"
bip39 = "1.0"
thiserror = "1.0"
tendermint-rpc = "0.23.0"
thiserror = "1.0"
tokio = { version = "1.10", features = ["sync"] }
url = "2.2"
rand = "0.7"
eyre = "0.6.5"
aes-gcm = "0.9.4"
argon2 = { version = "0.3.2", features = ["std"] }
@@ -45,15 +46,15 @@ cosmwasm-std = "1.0.0-beta3"
nym-client = { path = "../../clients/native" }
client-core = { path = "../../clients/client-core" }
nym-socks5-client = { path = "../../clients/socks5" }
nym-network-requester = { path = "../../service-providers/network-requester"}
nymsphinx = { path = "../../common/nymsphinx" }
validator-client = { path = "../../common/client-libs/validator-client", features = [
"nymd-client",
] }
mixnet-contract-common = { path = "../../common/cosmwasm-smart-contracts/mixnet-contract" }
#nym-network-requester = { path = "../../service-providers/network-requester"}
#nymsphinx = { path = "../../common/nymsphinx" }
#validator-client = { path = "../../common/client-libs/validator-client", features = [
# "nymd-client",
#] }
#mixnet-contract-common = { path = "../../common/cosmwasm-smart-contracts/mixnet-contract" }
config = { path = "../../common/config" }
coconut-interface = { path = "../../common/coconut-interface" }
credentials = { path = "../../common/credentials" }
#coconut-interface = { path = "../../common/coconut-interface" }
#credentials = { path = "../../common/credentials" }
version-checker = { path = "../../common/version-checker" }
network-defaults = { path = "../../common/network-defaults" }
+2
View File
@@ -25,6 +25,8 @@ mod state;
mod window;
fn main() {
//console_subscriber::init();
//tracing_subscriber::fmt::init();
setup_logging();
println!("Starting up...");
+25 -7
View File
@@ -1,8 +1,15 @@
use std::time::Duration;
use futures::SinkExt;
use log::info;
//use tokio::sync::mpsc;
use futures::channel::mpsc;
use config::NymConfig;
#[cfg(not(feature = "coconut"))]
use nym_socks5::client::NymClient as Socks5NymClient;
use nym_socks5::client::{ClientControlMessageReceiver, ClientControlMessageSender, ClientControlMessage};
use tokio::time::sleep;
use crate::config::SOCKS5_CONFIG_ID;
use crate::models::{
@@ -13,6 +20,7 @@ use tauri::Manager;
pub struct State {
status: ConnectionStatusKind,
//socks5_client: Arc<RwLock<Option<Socks5NymClient>>>,
socks5_client_sender: Option<ClientControlMessageSender>,
}
impl State {
@@ -20,6 +28,7 @@ impl State {
State {
status: ConnectionStatusKind::Disconnected,
//socks5_client: Arc::new(RwLock::new(None)),
socks5_client_sender: None,
}
}
@@ -47,8 +56,9 @@ impl State {
self.set_state(ConnectionStatusKind::Connecting, window);
self.status = ConnectionStatusKind::Connecting;
Self::init_config().await;
tokio::spawn(async move { start_nym_socks5_client().await });
//Self::init_config().await;
let sender = start_nym_socks5_client();
self.socks5_client_sender = Some(sender);
self.status = ConnectionStatusKind::Connected;
self.set_state(ConnectionStatusKind::Connected, window);
@@ -59,16 +69,17 @@ impl State {
self.set_state(ConnectionStatusKind::Disconnecting, window);
self.status = ConnectionStatusKind::Disconnecting;
// TODO: implement
// socks5_client_guard.unwrap().stop().await;
// *socks5_client_guard = None;
// Send shutdown message
if let Some(ref mut sender) = self.socks5_client_sender {
sender.send(ClientControlMessage::Stop).await.unwrap();
}
self.status = ConnectionStatusKind::Disconnected;
self.set_state(ConnectionStatusKind::Disconnected, window);
}
}
async fn start_nym_socks5_client() {
fn start_nym_socks5_client() -> ClientControlMessageSender {
let id = SOCKS5_CONFIG_ID;
info!("Loading config from file");
@@ -76,5 +87,12 @@ async fn start_nym_socks5_client() {
let mut socks5_client = Socks5NymClient::new(config);
info!("Starting socks5 client");
socks5_client.start().await;
let (sender, receiver) = mpsc::unbounded();
tokio::spawn(async move {
socks5_client.run_and_listen(receiver).await;
});
sender
}