From 4fdb9fecb069f6e387865cddabbef2b510539ed3 Mon Sep 17 00:00:00 2001 From: Andrej Mihajlov Date: Tue, 21 Jan 2025 11:09:56 +0100 Subject: [PATCH] Add NymAddress type --- .../nym-vpn-lib-types/src/connection_data.rs | 29 +++++++++++++++++-- .../crates/nym-vpn-lib-types/src/lib.rs | 4 +-- .../src/platform/uniffi_lib_types.rs | 28 +++++++++++++----- .../tunnel_state_machine/tunnel_monitor.rs | 13 ++------- .../conversions/from_proto/tunnel_state.rs | 23 ++++++++------- 5 files changed, 66 insertions(+), 31 deletions(-) diff --git a/nym-vpn-core/crates/nym-vpn-lib-types/src/connection_data.rs b/nym-vpn-core/crates/nym-vpn-lib-types/src/connection_data.rs index 3722833041..336fb8c9cd 100644 --- a/nym-vpn-core/crates/nym-vpn-lib-types/src/connection_data.rs +++ b/nym-vpn-core/crates/nym-vpn-lib-types/src/connection_data.rs @@ -60,10 +60,35 @@ pub enum TunnelConnectionData { Wireguard(WireguardConnectionData), } +// Represents a nym-address of the form id.enc@gateway #[derive(Debug, Clone, Eq, PartialEq)] -pub struct MixnetConnectionData { +pub struct NymAddress { pub nym_address: String, - pub exit_ipr: String, +} + +impl NymAddress { + pub fn new(nym_address: String) -> Self { + Self { nym_address } + } +} + +impl fmt::Display for NymAddress { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.nym_address.fmt(f) + } +} + +#[cfg(feature = "nym-type-conversions")] +impl From for NymAddress { + fn from(value: nym_gateway_directory::Recipient) -> Self { + Self::new(value.gateway().to_base58_string()) + } +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct MixnetConnectionData { + pub nym_address: NymAddress, + pub exit_ipr: NymAddress, pub ipv4: Ipv4Addr, pub ipv6: Ipv6Addr, } diff --git a/nym-vpn-core/crates/nym-vpn-lib-types/src/lib.rs b/nym-vpn-core/crates/nym-vpn-lib-types/src/lib.rs index 6d23fd608c..4c16c57504 100644 --- a/nym-vpn-core/crates/nym-vpn-lib-types/src/lib.rs +++ b/nym-vpn-core/crates/nym-vpn-lib-types/src/lib.rs @@ -8,8 +8,8 @@ mod tunnel_event; mod tunnel_state; pub use connection_data::{ - ConnectionData, Gateway, MixnetConnectionData, TunnelConnectionData, WireguardConnectionData, - WireguardNode, + ConnectionData, Gateway, MixnetConnectionData, NymAddress, TunnelConnectionData, + WireguardConnectionData, WireguardNode, }; pub use tunnel_event::{ BandwidthEvent, ConnectionEvent, ConnectionStatisticsEvent, MixnetEvent, SphinxPacketRates, diff --git a/nym-vpn-core/crates/nym-vpn-lib/src/platform/uniffi_lib_types.rs b/nym-vpn-core/crates/nym-vpn-lib/src/platform/uniffi_lib_types.rs index 3c44787a6c..6eb7a963fc 100644 --- a/nym-vpn-core/crates/nym-vpn-lib/src/platform/uniffi_lib_types.rs +++ b/nym-vpn-core/crates/nym-vpn-lib/src/platform/uniffi_lib_types.rs @@ -11,9 +11,10 @@ use nym_vpn_lib_types::{ ConnectionStatisticsEvent as CoreConnectionStatisticsEvent, ErrorStateReason as CoreErrorStateReason, Gateway as CoreGateway, MixnetConnectionData as CoreMixnetConnectionData, MixnetEvent as CoreMixnetEvent, - SphinxPacketRates as CoreSphinxPacketRates, TunnelConnectionData as CoreTunnelConnectionData, - TunnelEvent as CoreTunnelEvent, TunnelState as CoreTunnelState, - WireguardConnectionData as CoreWireguardConnectionData, WireguardNode as CoreWireguardNode, + NymAddress as CoreNymAddress, SphinxPacketRates as CoreSphinxPacketRates, + TunnelConnectionData as CoreTunnelConnectionData, TunnelEvent as CoreTunnelEvent, + TunnelState as CoreTunnelState, WireguardConnectionData as CoreWireguardConnectionData, + WireguardNode as CoreWireguardNode, }; use time::OffsetDateTime; @@ -257,6 +258,19 @@ impl From for Gateway { } } +#[derive(uniffi::Record)] +pub struct NymAddress { + pub nym_address: String, +} + +impl From for NymAddress { + fn from(value: CoreNymAddress) -> Self { + Self { + nym_address: value.nym_address, + } + } +} + #[derive(uniffi::Record)] pub struct ConnectionData { pub entry_gateway: Gateway, @@ -292,8 +306,8 @@ impl From for TunnelConnectionData { impl From for MixnetConnectionData { fn from(value: CoreMixnetConnectionData) -> Self { Self { - nym_address: value.nym_address, - exit_ipr: value.exit_ipr, + nym_address: NymAddress::from(value.nym_address), + exit_ipr: NymAddress::from(value.exit_ipr), ipv4: value.ipv4, ipv6: value.ipv6, } @@ -317,8 +331,8 @@ pub enum TunnelConnectionData { #[derive(uniffi::Record)] pub struct MixnetConnectionData { - pub nym_address: String, - pub exit_ipr: String, + pub nym_address: NymAddress, + pub exit_ipr: NymAddress, pub ipv4: Ipv4Addr, pub ipv6: Ipv6Addr, } diff --git a/nym-vpn-core/crates/nym-vpn-lib/src/tunnel_state_machine/tunnel_monitor.rs b/nym-vpn-core/crates/nym-vpn-lib/src/tunnel_state_machine/tunnel_monitor.rs index de2f6fb0c5..6c9e862735 100644 --- a/nym-vpn-core/crates/nym-vpn-lib/src/tunnel_state_machine/tunnel_monitor.rs +++ b/nym-vpn-core/crates/nym-vpn-lib/src/tunnel_state_machine/tunnel_monitor.rs @@ -36,7 +36,7 @@ use super::{ Error, NymConfig, Result, TunnelSettings, }; use nym_vpn_lib_types::{ - ConnectionData, ErrorStateReason, Gateway, MixnetConnectionData, MixnetEvent, + ConnectionData, ErrorStateReason, Gateway, MixnetConnectionData, MixnetEvent, NymAddress, TunnelConnectionData, TunnelType, WireguardConnectionData, WireguardNode, }; @@ -444,15 +444,8 @@ impl TunnelMonitor { } let tunnel_conn_data = TunnelConnectionData::Mixnet(MixnetConnectionData { - nym_address: assigned_addresses - .mixnet_client_address - .gateway() - .to_base58_string(), - exit_ipr: assigned_addresses - .exit_mix_addresses - .0 - .gateway() - .to_base58_string(), + nym_address: NymAddress::from(assigned_addresses.mixnet_client_address), + exit_ipr: NymAddress::from(assigned_addresses.exit_mix_addresses.0), ipv4: assigned_addresses.interface_addresses.ipv4, ipv6: assigned_addresses.interface_addresses.ipv6, }); diff --git a/nym-vpn-core/crates/nym-vpn-proto/src/conversions/from_proto/tunnel_state.rs b/nym-vpn-core/crates/nym-vpn-proto/src/conversions/from_proto/tunnel_state.rs index ee25a9789b..d3376e0b69 100644 --- a/nym-vpn-core/crates/nym-vpn-proto/src/conversions/from_proto/tunnel_state.rs +++ b/nym-vpn-core/crates/nym-vpn-proto/src/conversions/from_proto/tunnel_state.rs @@ -8,7 +8,7 @@ use std::{ use nym_vpn_lib_types::{ ActionAfterDisconnect, ConnectionData, ErrorStateReason, Gateway, MixnetConnectionData, - TunnelConnectionData, TunnelState, WireguardConnectionData, WireguardNode, + NymAddress, TunnelConnectionData, TunnelState, WireguardConnectionData, WireguardNode, }; use crate::{ @@ -23,7 +23,7 @@ use crate::{ Disconnecting as ProtoDisconnecting, Error as ProtoError, ErrorStateReason as ProtoErrorStateReason, Offline as ProtoOffline, State as ProtoState, }, - ConnectionData as ProtoConnectionData, Gateway as ProtoGateway, + Address as ProtoAddress, ConnectionData as ProtoConnectionData, Gateway as ProtoGateway, MixnetConnectionData as ProtoMixnetConnectionData, TunnelConnectionData as ProtoTunnelConnectionData, TunnelState as ProtoTunnelState, WireguardConnectionData as ProtoWireguardConnectionData, WireguardNode as ProtoWireguardNode, @@ -158,16 +158,13 @@ impl TryFrom for MixnetConnectionData { fn try_from(value: ProtoMixnetConnectionData) -> Result { Ok(Self { - nym_address: value - .nym_address - .ok_or(ConversionError::NoValueSet( - "MixnetConnectionData.nym_address", - ))? - .nym_address, + nym_address: value.nym_address.map(NymAddress::from).ok_or( + ConversionError::NoValueSet("MixnetConnectionData.nym_address"), + )?, exit_ipr: value .exit_ipr - .ok_or(ConversionError::NoValueSet("MixnetConnectionData.exit_ipr"))? - .nym_address, + .map(NymAddress::from) + .ok_or(ConversionError::NoValueSet("MixnetConnectionData.exit_ipr"))?, ipv4: Ipv4Addr::from_str(&value.ipv4) .map_err(|e| ConversionError::ParseAddr("MixnetConnectionData.ipv4", e))?, ipv6: Ipv6Addr::from_str(&value.ipv6) @@ -217,4 +214,10 @@ impl From for Gateway { } } +impl From for NymAddress { + fn from(value: ProtoAddress) -> Self { + Self::new(value.nym_address) + } +} + pub type Result = std::result::Result;