From 166e7ef5e790e32029aa279dbe2545c84b93efd9 Mon Sep 17 00:00:00 2001 From: Bug Magnet Date: Thu, 5 Sep 2024 15:39:55 +0200 Subject: [PATCH] Refactor rust --- .../ios_runtime.rs | 57 +++++++------------ .../ios_tcp_connection.rs | 0 .../mod.rs | 42 +++++++++++--- mullvad-ios/src/lib.rs | 2 +- 4 files changed, 56 insertions(+), 45 deletions(-) rename mullvad-ios/src/{post_quantum_proxy => ephemeral_peer_proxy}/ios_runtime.rs (82%) rename mullvad-ios/src/{post_quantum_proxy => ephemeral_peer_proxy}/ios_tcp_connection.rs (100%) rename mullvad-ios/src/{post_quantum_proxy => ephemeral_peer_proxy}/mod.rs (85%) diff --git a/mullvad-ios/src/post_quantum_proxy/ios_runtime.rs b/mullvad-ios/src/ephemeral_peer_proxy/ios_runtime.rs similarity index 82% rename from mullvad-ios/src/post_quantum_proxy/ios_runtime.rs rename to mullvad-ios/src/ephemeral_peer_proxy/ios_runtime.rs index fc73bce08642..7dbf5c21e1c4 100644 --- a/mullvad-ios/src/post_quantum_proxy/ios_runtime.rs +++ b/mullvad-ios/src/ephemeral_peer_proxy/ios_runtime.rs @@ -1,4 +1,6 @@ -use super::{ios_tcp_connection::*, EphemeralPeerCancelToken}; +use super::{ + ios_tcp_connection::*, EphemeralPeerCancelToken, EphemeralPeerParameters, PacketTunnelBridge, +}; use libc::c_void; use std::{ future::Future, @@ -16,37 +18,23 @@ use tower::util::service_fn; /// # Safety /// packet_tunnel and tcp_connection must be valid pointers to a packet tunnel and a TCP connection /// instances. -pub unsafe fn run_post_quantum_psk_exchange( +pub unsafe fn run_ephemeral_peer_exchange( pub_key: [u8; 32], ephemeral_key: [u8; 32], - packet_tunnel: *const c_void, - tcp_connection: *const c_void, - peer_exchange_timeout: u64, - enable_post_quantum: bool, - enable_daita: bool, + packet_tunnel_bridge: PacketTunnelBridge, + peer_parameters: EphemeralPeerParameters, + tokio_handle: TokioHandle, ) -> Result { match unsafe { IOSRuntime::new( pub_key, ephemeral_key, - packet_tunnel, - tcp_connection, - peer_exchange_timeout, - enable_post_quantum, - enable_daita, + packet_tunnel_bridge, + peer_parameters, ) } { Ok(runtime) => { let token = runtime.packet_tunnel.tcp_connection.clone(); - - let tokio_handle = match crate::mullvad_ios_runtime() { - Ok(handle) => handle, - Err(err) => { - log::error!("Failed to obtain a handle to a tokio runtime: {err}"); - return Err(Error::UnableToCreateRuntime); - } - }; - runtime.run(tokio_handle); Ok(EphemeralPeerCancelToken { context: Arc::into_raw(token) as *mut _, @@ -72,33 +60,28 @@ struct IOSRuntime { pub_key: [u8; 32], ephemeral_key: [u8; 32], packet_tunnel: SwiftContext, - peer_exchange_timeout: u64, - enable_post_quantum: bool, - enable_daita: bool, + peer_parameters: EphemeralPeerParameters, } impl IOSRuntime { pub unsafe fn new( pub_key: [u8; 32], ephemeral_key: [u8; 32], - packet_tunnel: *const libc::c_void, - tcp_connection: *const c_void, - post_quantum_key_exchange_timeout: u64, - enable_post_quantum: bool, - enable_daita: bool, + packet_tunnel_bridge: PacketTunnelBridge, + peer_parameters: EphemeralPeerParameters, ) -> io::Result { let context = SwiftContext { - packet_tunnel, - tcp_connection: Arc::new(Mutex::new(ConnectionContext::new(tcp_connection))), + packet_tunnel: packet_tunnel_bridge.packet_tunnel, + tcp_connection: Arc::new(Mutex::new(ConnectionContext::new( + packet_tunnel_bridge.tcp_connection, + ))), }; Ok(Self { pub_key, ephemeral_key, packet_tunnel: context, - peer_exchange_timeout: post_quantum_key_exchange_timeout, - enable_post_quantum, - enable_daita, + peer_parameters, }) } @@ -158,8 +141,8 @@ impl IOSRuntime { async_provider, PublicKey::from(self.pub_key), ephemeral_pub_key, - self.enable_post_quantum, - self.enable_daita, + self.peer_parameters.enable_post_quantum, + self.peer_parameters.enable_daita, ) => { shutdown_handle.shutdown(); if let Ok(mut connection) = self.packet_tunnel.tcp_connection.lock() { @@ -195,7 +178,7 @@ impl IOSRuntime { } } - _ = tokio::time::sleep(std::time::Duration::from_secs(self.peer_exchange_timeout)) => { + _ = tokio::time::sleep(std::time::Duration::from_secs(self.peer_parameters.peer_exchange_timeout)) => { if let Ok(mut connection) = self.packet_tunnel.tcp_connection.lock() { connection.shutdown(); }; diff --git a/mullvad-ios/src/post_quantum_proxy/ios_tcp_connection.rs b/mullvad-ios/src/ephemeral_peer_proxy/ios_tcp_connection.rs similarity index 100% rename from mullvad-ios/src/post_quantum_proxy/ios_tcp_connection.rs rename to mullvad-ios/src/ephemeral_peer_proxy/ios_tcp_connection.rs diff --git a/mullvad-ios/src/post_quantum_proxy/mod.rs b/mullvad-ios/src/ephemeral_peer_proxy/mod.rs similarity index 85% rename from mullvad-ios/src/post_quantum_proxy/mod.rs rename to mullvad-ios/src/ephemeral_peer_proxy/mod.rs index a4090e58db7d..0cb85f2177b2 100644 --- a/mullvad-ios/src/post_quantum_proxy/mod.rs +++ b/mullvad-ios/src/ephemeral_peer_proxy/mod.rs @@ -1,7 +1,7 @@ pub mod ios_runtime; pub mod ios_tcp_connection; -use ios_runtime::run_post_quantum_psk_exchange; +use ios_runtime::run_ephemeral_peer_exchange; use ios_tcp_connection::ConnectionContext; use libc::c_void; use std::sync::{Arc, Mutex, Weak}; @@ -16,6 +16,17 @@ pub struct EphemeralPeerCancelToken { pub context: *mut c_void, } +pub struct PacketTunnelBridge { + pub packet_tunnel: *const c_void, + pub tcp_connection: *const c_void, +} + +pub struct EphemeralPeerParameters { + pub peer_exchange_timeout: u64, + pub enable_post_quantum: bool, + pub enable_daita: bool, +} + impl EphemeralPeerCancelToken { /// # Safety /// This function can only be called when the context pointer is valid. @@ -137,15 +148,32 @@ pub unsafe extern "C" fn request_ephemeral_peer( let pub_key: [u8; 32] = unsafe { std::ptr::read(public_key as *const [u8; 32]) }; let eph_key: [u8; 32] = unsafe { std::ptr::read(ephemeral_key as *const [u8; 32]) }; + let handle = match crate::mullvad_ios_runtime() { + Ok(handle) => handle, + Err(err) => { + log::error!("Failed to obtain a handle to a tokio runtime: {err}"); + + return -1; + } + }; + + let packet_tunnel_bridge = PacketTunnelBridge { + packet_tunnel, + tcp_connection, + }; + let peer_parameters = EphemeralPeerParameters { + peer_exchange_timeout, + enable_post_quantum, + enable_daita, + }; + match unsafe { - run_post_quantum_psk_exchange( + run_ephemeral_peer_exchange( pub_key, eph_key, - packet_tunnel, - tcp_connection, - peer_exchange_timeout, - enable_post_quantum, - enable_daita, + packet_tunnel_bridge, + peer_parameters, + handle, ) } { Ok(token) => { diff --git a/mullvad-ios/src/lib.rs b/mullvad-ios/src/lib.rs index 663284ca07bd..d36a925788d0 100644 --- a/mullvad-ios/src/lib.rs +++ b/mullvad-ios/src/lib.rs @@ -1,5 +1,5 @@ #![cfg(target_os = "ios")] -mod post_quantum_proxy; +mod ephemeral_peer_proxy; mod shadowsocks_proxy; mod tunnel_obfuscator_proxy;