From abd50764655cd350f092571b2a3f15f4a89d74ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 22 Jul 2024 18:57:41 +0200 Subject: [PATCH 1/3] Remove old unused mixnet connect code --- .../nym-vpn-lib/src/mixnet_connect.rs | 176 +----------------- 1 file changed, 1 insertion(+), 175 deletions(-) diff --git a/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs b/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs index 3c25373bee..22f57f6fb9 100644 --- a/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs +++ b/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs @@ -2,29 +2,17 @@ // SPDX-License-Identifier: GPL-3.0-only use nym_config::defaults::NymNetworkDetails; -use std::cmp::Ordering; #[cfg(target_family = "unix")] use std::os::fd::RawFd; #[cfg(not(target_family = "unix"))] use std::os::raw::c_int as RawFd; use std::path::PathBuf; use std::sync::Arc; -use std::time::Duration; - -use nym_ip_packet_requests::IpPair; -use nym_ip_packet_requests::{ - request::IpPacketRequest, - response::{ - DynamicConnectResponse, IpPacketResponse, IpPacketResponseData, StaticConnectResponse, - }, -}; use nym_sdk::mixnet::{ MixnetClient, MixnetClientBuilder, MixnetClientSender, MixnetMessageSender, NodeIdentity, Recipient, StoragePaths, }; -use tracing::{debug, error, info}; - -use nym_gateway_directory::IpPacketRouterAddress; +use tracing::{debug, info}; use crate::credentials::check_imported_credential; use crate::error::{Error, Result}; @@ -74,168 +62,6 @@ impl SharedMixnetClient { } } -async fn send_connect_to_ip_packet_router( - mixnet_client: &SharedMixnetClient, - ip_packet_router_address: &IpPacketRouterAddress, - ips: Option, - enable_two_hop: bool, -) -> Result { - let hops = enable_two_hop.then_some(0); - let mixnet_client_address = mixnet_client.nym_address().await; - let (request, request_id) = if let Some(ips) = ips { - debug!("Sending static connect request with ips: {ips}"); - IpPacketRequest::new_static_connect_request(ips, mixnet_client_address, hops, None, None) - } else { - debug!("Sending dynamic connect request"); - IpPacketRequest::new_dynamic_connect_request(mixnet_client_address, hops, None, None) - }; - debug!("Sent connect request with version v{}", request.version); - - mixnet_client - .send(nym_sdk::mixnet::InputMessage::new_regular_with_custom_hops( - ip_packet_router_address.0, - request.to_bytes().unwrap(), - nym_task::connections::TransmissionLane::General, - None, - hops, - )) - .await?; - - Ok(request_id) -} - -async fn wait_for_connect_response( - mixnet_client: &SharedMixnetClient, - request_id: u64, -) -> Result { - let timeout = tokio::time::sleep(Duration::from_secs(5)); - tokio::pin!(timeout); - - // Connecting is basically synchronous from the perspective of the mixnet client, so it's safe - // to just grab ahold of the mutex and keep it until we get the response. - let mut mixnet_client_handle = mixnet_client.lock().await; - let mixnet_client = mixnet_client_handle.as_mut().unwrap(); - - loop { - tokio::select! { - _ = &mut timeout => { - error!("Timed out waiting for reply to connect request"); - return Err(Error::TimeoutWaitingForConnectResponse); - } - msgs = mixnet_client.wait_for_messages() => { - if let Some(msgs) = msgs { - for msg in msgs { - - // Handle if the response is from an IPR running an older or newer version - if let Some(version) = msg.message.first() { - match version.cmp(&nym_ip_packet_requests::CURRENT_VERSION) { - Ordering::Greater => { - log::error!("Received packet with newer version: v{version}, is your client up to date?"); - return Err(Error::ReceivedResponseWithNewVersion { - expected: nym_ip_packet_requests::CURRENT_VERSION, - received: *version, - }); - } - Ordering::Less => { - log::error!("Received packet with older version: v{version}, you client appears to be too new for the exit gateway or exit ip-packet-router?"); - return Err(Error::ReceivedResponseWithOldVersion { - expected: nym_ip_packet_requests::CURRENT_VERSION, - received: *version, - }); - } - Ordering::Equal => { - // We're good - } - } - } - - debug!("MixnetProcessor: Got message while waiting for connect response"); - let Ok(response) = IpPacketResponse::from_reconstructed_message(&msg) else { - // This is ok, it's likely just one of our self-pings - debug!("Failed to deserialize reconstructed message"); - continue; - }; - if response.id() == Some(request_id) { - debug!("Got response with matching id"); - return Ok(response); - } - } - } else { - return Err(Error::NoMixnetMessagesReceived); - } - } - } - } -} - -async fn handle_static_connect_response( - mixnet_client_address: &Recipient, - response: StaticConnectResponse, -) -> Result<()> { - debug!("Handling static connect response"); - if response.reply_to != *mixnet_client_address { - error!("Got reply intended for wrong address"); - return Err(Error::GotReplyIntendedForWrongAddress); - } - match response.reply { - nym_ip_packet_requests::response::StaticConnectResponseReply::Success => Ok(()), - nym_ip_packet_requests::response::StaticConnectResponseReply::Failure(reason) => { - Err(Error::StaticConnectRequestDenied { reason }) - } - } -} - -async fn handle_dynamic_connect_response( - mixnet_client_address: &Recipient, - response: DynamicConnectResponse, -) -> Result { - debug!("Handling dynamic connect response"); - if response.reply_to != *mixnet_client_address { - error!("Got reply intended for wrong address"); - return Err(Error::GotReplyIntendedForWrongAddress); - } - match response.reply { - nym_ip_packet_requests::response::DynamicConnectResponseReply::Success(r) => Ok(r.ips), - nym_ip_packet_requests::response::DynamicConnectResponseReply::Failure(reason) => { - Err(Error::DynamicConnectRequestDenied { reason }) - } - } -} - -pub async fn connect_to_ip_packet_router( - mixnet_client: SharedMixnetClient, - ip_packet_router_address: &IpPacketRouterAddress, - ips: Option, - enable_two_hop: bool, -) -> Result { - debug!("Sending connect request"); - let request_id = send_connect_to_ip_packet_router( - &mixnet_client, - ip_packet_router_address, - ips, - enable_two_hop, - ) - .await?; - - debug!("Waiting for reply..."); - let response = wait_for_connect_response(&mixnet_client, request_id).await?; - - let mixnet_client_address = mixnet_client.nym_address().await; - match response.data { - IpPacketResponseData::StaticConnect(resp) if ips.is_some() => { - handle_static_connect_response(&mixnet_client_address, resp).await?; - Ok(ips.unwrap()) - } - IpPacketResponseData::DynamicConnect(resp) if ips.is_none() => { - handle_dynamic_connect_response(&mixnet_client_address, resp).await - } - response => { - error!("Unexpected response: {:?}", response); - Err(Error::UnexpectedConnectResponse) - } - } -} - fn true_to_enabled(val: bool) -> &'static str { if val { "enabled" From 4cf2e7d24679d091530c7a19d6c839f0dd7a95f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 22 Jul 2024 19:09:15 +0200 Subject: [PATCH 2/3] Remove unused log crate --- nym-vpn-core/Cargo.lock | 1 - nym-vpn-core/nym-vpnd/Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/nym-vpn-core/Cargo.lock b/nym-vpn-core/Cargo.lock index 2c0968e462..968ef9213a 100644 --- a/nym-vpn-core/Cargo.lock +++ b/nym-vpn-core/Cargo.lock @@ -4839,7 +4839,6 @@ dependencies = [ "eventlog", "futures", "http 0.2.12", - "log", "maplit", "nym-bandwidth-controller", "nym-task", diff --git a/nym-vpn-core/nym-vpnd/Cargo.toml b/nym-vpn-core/nym-vpnd/Cargo.toml index a8df00dda3..2c3b34cad9 100644 --- a/nym-vpn-core/nym-vpnd/Cargo.toml +++ b/nym-vpn-core/nym-vpnd/Cargo.toml @@ -17,7 +17,6 @@ clap.workspace = true dirs.workspace = true futures.workspace = true http.workspace = true -log.workspace = true maplit.workspace = true parity-tokio-ipc.workspace = true prost-types.workspace = true From 0ed108889801046dba126b281464171fadb078f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 22 Jul 2024 19:10:51 +0200 Subject: [PATCH 3/3] rustfmt --- nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs b/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs index 22f57f6fb9..8c7346649c 100644 --- a/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs +++ b/nym-vpn-core/nym-vpn-lib/src/mixnet_connect.rs @@ -2,16 +2,16 @@ // SPDX-License-Identifier: GPL-3.0-only use nym_config::defaults::NymNetworkDetails; +use nym_sdk::mixnet::{ + MixnetClient, MixnetClientBuilder, MixnetClientSender, MixnetMessageSender, NodeIdentity, + Recipient, StoragePaths, +}; #[cfg(target_family = "unix")] use std::os::fd::RawFd; #[cfg(not(target_family = "unix"))] use std::os::raw::c_int as RawFd; use std::path::PathBuf; use std::sync::Arc; -use nym_sdk::mixnet::{ - MixnetClient, MixnetClientBuilder, MixnetClientSender, MixnetMessageSender, NodeIdentity, - Recipient, StoragePaths, -}; use tracing::{debug, info}; use crate::credentials::check_imported_credential;