Skip to content

Commit

Permalink
Refactor rust
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet committed Sep 5, 2024
1 parent f8638f9 commit 166e7ef
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<EphemeralPeerCancelToken, Error> {
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 _,
Expand All @@ -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<Self> {
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,
})
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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.
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion mullvad-ios/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(target_os = "ios")]
mod post_quantum_proxy;
mod ephemeral_peer_proxy;
mod shadowsocks_proxy;
mod tunnel_obfuscator_proxy;

Expand Down

0 comments on commit 166e7ef

Please sign in to comment.