Skip to content

Commit 71b927c

Browse files
committed
It passes now
1 parent de413a3 commit 71b927c

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

mullvad-api/src/address_cache.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This module keeps track of the last known good API IP address and reads and stores it on disk.
22
33
use super::API;
4-
use crate::DnsResolver;
4+
use crate::{ApiEndpoint, DnsResolver};
55
use async_trait::async_trait;
66
use std::{io, net::SocketAddr, path::Path, sync::Arc};
77
use tokio::{
@@ -40,6 +40,7 @@ impl DnsResolver for AddressCache {
4040
pub struct AddressCache {
4141
inner: Arc<Mutex<AddressCacheInner>>,
4242
write_path: Option<Arc<Path>>,
43+
custom_endpoint: Option<ApiEndpoint>,
4344
}
4445

4546
impl AddressCache {
@@ -52,6 +53,16 @@ impl AddressCache {
5253
Self::new_inner(address, None)
5354
}
5455

56+
pub fn with_custom_endpoint(endpoint: ApiEndpoint) -> Self {
57+
let cache = AddressCacheInner::from_address(endpoint.address());
58+
59+
Self {
60+
inner: Arc::new(Mutex::new(cache)),
61+
write_path: None,
62+
custom_endpoint: Some(endpoint),
63+
}
64+
}
65+
5566
// #[cfg(target_os = "ios")]
5667
// pub fn with_static_hostname_addr(address: SocketAddr, hostname: &str) -> {
5768

@@ -73,12 +84,14 @@ impl AddressCache {
7384
Self {
7485
inner: Arc::new(Mutex::new(cache)),
7586
write_path: write_path.map(Arc::from),
87+
custom_endpoint: None,
7688
}
7789
}
7890

7991
/// Returns the address if the hostname equals `API.host`. Otherwise, returns `None`.
8092
async fn resolve_hostname(&self, hostname: &str) -> Option<SocketAddr> {
81-
if hostname.eq_ignore_ascii_case(API.host()) {
93+
let host = self.endpoint().host();
94+
if hostname.eq_ignore_ascii_case(host) {
8295
Some(self.get_address().await)
8396
} else {
8497
None
@@ -115,6 +128,10 @@ impl AddressCache {
115128
.map_err(Error::Write)?;
116129
file.finalize().await.map_err(Error::Write)
117130
}
131+
132+
fn endpoint(&self) -> &ApiEndpoint {
133+
self.custom_endpoint.as_ref().unwrap_or(&*API)
134+
}
118135
}
119136

120137
#[derive(Clone, PartialEq, Eq)]

mullvad-api/src/ffi/mod.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use crate::{
99
rest::{self, MullvadRestHandle},
10-
AccountsProxy, DevicesProxy,
10+
AccountsProxy, ApiEndpoint, DevicesProxy,
1111
};
1212

1313
mod device;
@@ -68,12 +68,11 @@ impl FfiClient {
6868
)
6969
})?;
7070

71-
// The call site guarantees that
72-
// api_hostname and api_address will never change after the first call to new.
73-
std::env::set_var(crate::env::API_HOST_VAR, &api_hostname);
74-
std::env::set_var(crate::env::API_ADDR_VAR, &addr_str);
75-
std::env::set_var(crate::env::API_FORCE_DIRECT_VAR, "0");
76-
std::env::set_var(crate::env::DISABLE_TLS_VAR, "0");
71+
let endpoint = ApiEndpoint {
72+
host: Some(api_hostname.clone()),
73+
address: Some(api_address),
74+
};
75+
7776
let mut runtime_builder = tokio::runtime::Builder::new_multi_thread();
7877

7978
runtime_builder.worker_threads(2).enable_all();
@@ -84,7 +83,7 @@ impl FfiClient {
8483
// It is imperative that the REST runtime is created within an async context, otherwise
8584
// ApiAvailability panics.
8685
let api_runtime = tokio_runtime.block_on(async {
87-
crate::Runtime::with_static_addr(tokio_runtime.handle().clone(), api_address)
86+
crate::Runtime::with_custom_endpoint(tokio_runtime.handle().clone(), endpoint)
8887
});
8988

9089
let context = FfiClient {
@@ -455,7 +454,6 @@ mod test {
455454
#[test]
456455
fn test_initialization() {
457456
let _ = create_client();
458-
459457
}
460458

461459
fn create_client() -> MullvadApiClient {
@@ -468,18 +466,15 @@ mod test {
468466
)
469467
.unwrap();
470468
};
471-
unsafe {
472-
client.assume_init()
473-
}
469+
unsafe { client.assume_init() }
474470
}
475471

476472
#[test]
477473
fn test_create_delete_account() {
478474
let client = create_client();
479475

480-
let mut account_buf_ptr = ptr::null_mut();
481-
476+
let mut account_buf = vec![(0 as libc::c_char); 100];
482477

483-
unsafe { mullvad_api_create_account(client, account_buf_ptr).unwrap() };
478+
unsafe { mullvad_api_create_account(client, (&account_buf.as_mut_ptr()).cast()).unwrap() };
484479
}
485480
}

mullvad-api/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub mod env {
112112
}
113113

114114
/// A hostname and socketaddr to reach the Mullvad REST API over.
115-
#[derive(Debug)]
115+
#[derive(Debug,Clone)]
116116
pub struct ApiEndpoint {
117117
/// An overriden API hostname. Initialized with the value of the environment
118118
/// variable `MULLVAD_API_HOST` if it has been set.
@@ -370,10 +370,10 @@ impl Runtime {
370370
)
371371
}
372372

373-
pub fn with_static_addr(handle: tokio::runtime::Handle, address: SocketAddr) -> Self {
373+
pub fn with_custom_endpoint(handle: tokio::runtime::Handle, api_endpoint: ApiEndpoint) -> Self {
374374
Runtime {
375375
handle,
376-
address_cache: AddressCache::with_static_addr(address),
376+
address_cache: AddressCache::with_custom_endpoint(api_endpoint),
377377
api_availability: ApiAvailability::default(),
378378
}
379379
}

0 commit comments

Comments
 (0)