-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathmod.rs
83 lines (71 loc) · 2.48 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::{ffi::CStr, sync::Arc};
use mullvad_api::{
proxy::{ApiConnectionMode, StaticConnectionModeProvider},
rest::MullvadRestHandle,
ApiEndpoint, Runtime,
};
mod api;
mod cancellation;
mod completion;
mod response;
mod retry_strategy;
#[repr(C)]
pub struct SwiftApiContext(*const ApiContext);
impl SwiftApiContext {
pub fn new(context: ApiContext) -> SwiftApiContext {
SwiftApiContext(Arc::into_raw(Arc::new(context)))
}
pub unsafe fn into_rust_context(self) -> Arc<ApiContext> {
Arc::increment_strong_count(self.0);
Arc::from_raw(self.0)
}
}
pub struct ApiContext {
_api_client: Runtime,
rest_client: MullvadRestHandle,
}
impl ApiContext {
pub fn rest_handle(&self) -> MullvadRestHandle {
self.rest_client.clone()
}
}
/// # Safety
///
/// `host` must be a pointer to a null terminated string representing a hostname for Mullvad API host.
/// This hostname will be used for TLS validation but not used for domain name resolution.
///
/// `address` must be a pointer to a null terminated string representing a socket address through which
/// the Mullvad API can be reached directly.
///
/// If a context cannot be constructed this function will panic since the call site would not be able
/// to proceed in a meaningful way anyway.
///
/// This function is safe.
#[no_mangle]
pub extern "C" fn mullvad_api_init_new(host: *const u8, address: *const u8) -> SwiftApiContext {
let host = unsafe { CStr::from_ptr(host.cast()) };
let address = unsafe { CStr::from_ptr(address.cast()) };
let host = host.to_str().unwrap();
let address = address.to_str().unwrap();
let endpoint = ApiEndpoint {
host: Some(String::from(host)),
address: Some(address.parse().unwrap()),
#[cfg(feature = "api-override")]
disable_tls: false,
#[cfg(feature = "api-override")]
force_direct: false,
};
let tokio_handle = crate::mullvad_ios_runtime().unwrap();
let api_context = tokio_handle.clone().block_on(async move {
// It is imperative that the REST runtime is created within an async context, otherwise
// ApiAvailability panics.
let api_client = mullvad_api::Runtime::new(tokio_handle, &endpoint);
let rest_client = api_client
.mullvad_rest_handle(StaticConnectionModeProvider::new(ApiConnectionMode::Direct));
ApiContext {
_api_client: api_client,
rest_client,
}
});
SwiftApiContext::new(api_context)
}