Skip to content

Commit 919de1f

Browse files
committed
Improve mullvad-ios
1 parent 4c01a52 commit 919de1f

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

mullvad-ios/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ rust-version.workspace = true
1010
[lints]
1111
workspace = true
1212

13-
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
13+
[target.'cfg(target_os = "ios")'.dependencies]
1414
libc = "0.2"
1515
log = { workspace = true }
1616
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

mullvad-ios/src/encrypted_dns_proxy.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ pub struct EncryptedDnsProxyState {
1818

1919
#[derive(Debug)]
2020
pub enum Error {
21-
/// Failed to bind a local listening socket, the one that will be forwarded through the proxy
21+
/// Failed to initialize tokio runtime.
22+
TokioRuntime,
23+
/// Failed to bind a local listening socket, the one that will be forwarded through the proxy.
2224
BindLocalSocket(io::Error),
2325
/// Failed to get local listening address of the local listening socket.
2426
GetBindAddr(io::Error),
2527
/// Failed to initialize forwarder.
2628
Forwarder(io::Error),
27-
/// Failed to fetch new.
29+
/// Failed to fetch a proxy configuration over DNS.
2830
FetchConfig(config_resolver::Error),
2931
/// Failed to initialize with a valid configuration.
3032
NoConfigs,
@@ -33,6 +35,7 @@ pub enum Error {
3335
impl From<Error> for i32 {
3436
fn from(err: Error) -> Self {
3537
match err {
38+
Error::TokioRuntime(_) => -1,
3639
Error::BindLocalSocket(_) => -2,
3740
Error::GetBindAddr(_) => -3,
3841
Error::Forwarder(_) => -4,
@@ -59,12 +62,7 @@ impl EncryptedDnsProxyState {
5962
}
6063

6164
async fn start(&mut self) -> Result<ProxyHandle, Error> {
62-
// TODO: Consider strong timeout here
6365
self.fetch_configs().await?;
64-
if self.should_reset() {
65-
self.reset();
66-
}
67-
6866
let proxy_configuration = self.next_configuration().ok_or(Error::NoConfigs)?;
6967

7068
let local_socket = Self::bind_local_addr()
@@ -92,6 +90,10 @@ impl EncryptedDnsProxyState {
9290
}
9391

9492
fn next_configuration(&mut self) -> Option<ProxyConfig> {
93+
if self.should_reset() {
94+
self.reset();
95+
}
96+
9597
if let Some(xor_config) = self
9698
.configurations
9799
.difference(&self.tried_configurations)
@@ -156,7 +158,6 @@ pub unsafe extern "C" fn encrytped_dns_proxy_free(ptr: *mut EncryptedDnsProxySta
156158
///
157159
/// `proxy_handle` will only contain valid values if the return value is zero. It is still valid to
158160
/// deallocate the memory.
159-
///
160161
#[no_mangle]
161162
pub unsafe extern "C" fn encrypted_dns_proxy_start(
162163
encrypted_dns_proxy: *mut EncryptedDnsProxyState,
@@ -166,7 +167,7 @@ pub unsafe extern "C" fn encrypted_dns_proxy_start(
166167
Ok(handle) => handle,
167168
Err(err) => {
168169
log::error!("Cannot instantiate a tokio runtime: {}", err);
169-
return -1;
170+
return Error::TokioRuntime.into();
170171
}
171172
};
172173

@@ -177,6 +178,11 @@ pub unsafe extern "C" fn encrypted_dns_proxy_start(
177178
match proxy_result {
178179
Ok(handle) => unsafe { ptr::write(proxy_handle, handle) },
179180
Err(err) => {
181+
let empty_handle = ProxyHandle {
182+
context: ptr::null(),
183+
port: 0,
184+
};
185+
unsafe { ptr::write(proxy_handle, empty_handle) }
180186
log::error!("Failed to create a proxy connection: {err:?}");
181187
return err.into();
182188
}
@@ -185,12 +191,15 @@ pub unsafe extern "C" fn encrypted_dns_proxy_start(
185191
0
186192
}
187193

188-
/// SAFETY:
194+
/// # Safety
189195
/// `proxy_config` must be a valid pointer to a `ProxyHandle` as initialized by
190196
/// [`encrypted_dns_proxy_start`].
191197
#[no_mangle]
192198
pub unsafe extern "C" fn encrypted_dns_proxy_stop(proxy_config: *mut ProxyHandle) -> i32 {
193-
let handle: Box<JoinHandle<()>> = unsafe { Box::from_raw((*proxy_config).context.cast()) };
194-
handle.abort();
199+
let ptr = unsafe { *proxy_config.context };
200+
if !ptr.is_null() {
201+
let handle: Box<JoinHandle<()>> = unsafe { Box::from_raw(ptr.cast()) };
202+
handle.abort();
203+
}
195204
0i32
196205
}

mullvad-ios/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(any(target_os = "ios", target_os = "macos"))]
1+
#![cfg(target_os = "ios")]
22
mod encrypted_dns_proxy;
33
mod ephemeral_peer_proxy;
44
mod shadowsocks_proxy;

0 commit comments

Comments
 (0)