Skip to content

Commit e05216c

Browse files
committed
TMP: work
1 parent 8fc35ff commit e05216c

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

mullvad-api/src/ffi/error.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ impl MullvadApiError {
4848
}
4949
}
5050

51-
#[cfg(test)]
5251
pub fn unwrap(&self) {
53-
if ! matches!(self.kind, MullvadApiErrorKind::NoError) {
52+
if !matches!(self.kind, MullvadApiErrorKind::NoError) {
5453
let desc = unsafe { std::ffi::CStr::from_ptr(self.description) };
5554
panic!("API ERROR - {:?} - {}", self.kind, desc.to_str().unwrap());
5655
}

mullvad-api/src/ffi/mod.rs

+47-7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl FfiClient {
5555
unsafe fn new(
5656
api_address_ptr: *const libc::c_char,
5757
hostname: *const libc::c_char,
58+
#[cfg(debug_assertions)] disable_tls: bool,
5859
) -> Result<Self, MullvadApiError> {
5960
// SAFETY: addr_str must be a valid pointer to a null-terminated string.
6061
let addr_str = unsafe { string_from_raw_ptr(api_address_ptr)? };
@@ -71,6 +72,8 @@ impl FfiClient {
7172
let endpoint = ApiEndpoint {
7273
host: Some(api_hostname.clone()),
7374
address: Some(api_address),
75+
#[cfg(debug_assertions)]
76+
disable_tls,
7477
};
7578

7679
let mut runtime_builder = tokio::runtime::Builder::new_multi_thread();
@@ -238,8 +241,16 @@ pub unsafe extern "C" fn mullvad_api_client_initialize(
238241
client_ptr: *mut MullvadApiClient,
239242
api_address_ptr: *const libc::c_char,
240243
hostname: *const libc::c_char,
244+
#[cfg(debug_assertions)] disable_tls: bool,
241245
) -> MullvadApiError {
242-
match unsafe { FfiClient::new(api_address_ptr, hostname) } {
246+
match unsafe {
247+
FfiClient::new(
248+
api_address_ptr,
249+
hostname,
250+
#[cfg(debug_assertions)]
251+
disable_tls,
252+
)
253+
} {
243254
Ok(client) => {
244255
unsafe {
245256
std::ptr::write(client_ptr, MullvadApiClient::new(client));
@@ -445,24 +456,29 @@ unsafe fn string_from_raw_ptr(ptr: *const libc::c_char) -> Result<String, Mullva
445456

446457
#[cfg(test)]
447458
mod test {
448-
use std::mem::MaybeUninit;
459+
use super::*;
460+
use std::time::Duration;
461+
use httpmock::prelude::*;
462+
use std::{mem::MaybeUninit, net::Ipv4Addr, thread};
449463

450464
use super::*;
451465
const STAGING_SOCK_ADDR: &[u8] = b"185.217.116.129:443\0";
452466
const STAGING_HOSTNAME: &[u8] = b"api-app.stagemole.eu\0";
453467

454468
#[test]
455469
fn test_initialization() {
456-
let _ = create_client();
470+
let _ = create_client(&SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 1));
457471
}
458472

459-
fn create_client() -> MullvadApiClient {
473+
fn create_client(addr: &SocketAddr) -> MullvadApiClient {
460474
let mut client = MaybeUninit::<MullvadApiClient>::uninit();
475+
let cstr_address = CString::new(addr.to_string()).unwrap();
461476
let _client = unsafe {
462477
mullvad_api_client_initialize(
463478
client.as_mut_ptr(),
464-
STAGING_SOCK_ADDR.as_ptr().cast(),
479+
cstr_address.as_ptr().cast(),
465480
STAGING_HOSTNAME.as_ptr().cast(),
481+
true,
466482
)
467483
.unwrap();
468484
};
@@ -471,10 +487,34 @@ mod test {
471487

472488
#[test]
473489
fn test_create_delete_account() {
474-
let client = create_client();
490+
let server = test_server();
491+
let client = create_client(server.address());
492+
493+
println!("SERVER ADDR: {}", server.address());
475494

476-
let mut account_buf = vec![(0 as libc::c_char); 100];
495+
let mut account_buf = vec![0 as libc::c_char; 100];
477496

478497
unsafe { mullvad_api_create_account(client, (&account_buf.as_mut_ptr()).cast()).unwrap() };
479498
}
499+
500+
fn test_server() -> MockServer {
501+
let expected_create_account_response = br#"{"id":"085df870-0fc2-47cb-9e8c-cb43c1bdaac0","expiry":"2024-12-11T12:56:32+00:00","max_ports":0,"can_add_ports":false,"max_devices":5,"can_add_devices":true,"number":"6705749539195318"}"}"#;
502+
let server = MockServer::start();
503+
let mock = server.mock(|when, then| {
504+
// when.method(POST)
505+
// .header("content-type", "application/json")
506+
// .path(crate::ACCOUNTS_URL_PREFIX.to_owned() + "/accounts");
507+
when.any_request();
508+
then.status(201)
509+
.body(expected_create_account_response)
510+
.header("content-type", "application/json")
511+
.header(
512+
"content-length",
513+
&format!("{}", expected_create_account_response.len()),
514+
)
515+
;
516+
});
517+
518+
server
519+
}
480520
}

mullvad-api/src/https_client_with_sni.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use tokio::{
4141
};
4242
use tower::Service;
4343

44-
#[cfg(feature = "api-override")]
44+
#[cfg(debug_assertions)]
4545
use crate::{proxy::ConnectionDecorator, API};
4646

4747
const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
@@ -206,7 +206,7 @@ impl InnerConnectionMode {
206206

207207
let proxy = make_proxy_stream(socket).await?;
208208

209-
#[cfg(feature = "api-override")]
209+
#[cfg(debug_assertions)]
210210
if API.disable_tls {
211211
return Ok(ApiConnection::new(Box::new(ConnectionDecorator(proxy))));
212212
}

mullvad-api/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub struct ApiEndpoint {
133133
pub address: Option<SocketAddr>,
134134
#[cfg(feature = "api-override")]
135135
pub disable_address_cache: bool,
136-
#[cfg(feature = "api-override")]
136+
#[cfg(debug_assertions)]
137137
pub disable_tls: bool,
138138
#[cfg(feature = "api-override")]
139139
/// Whether bridges/proxies can be used to access the API or not. This is
@@ -268,6 +268,8 @@ impl ApiEndpoint {
268268
ApiEndpoint {
269269
host: None,
270270
address: None,
271+
#[cfg(debug_assertions)]
272+
disable_tls: false
271273
}
272274
}
273275

mullvad-api/src/rest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ where
449449
.insert(header::AUTHORIZATION, auth);
450450
}
451451

452+
println!("rest - {:?}", self.request.uri());
452453
// Make request to hyper client
453454
let response = hyper_client
454455
.request(self.request)
@@ -461,7 +462,6 @@ where
461462
}
462463

463464
// Parse unexpected responses and errors
464-
465465
let response = response?;
466466

467467
if !self.expected_status.contains(&response.status()) {

0 commit comments

Comments
 (0)