Skip to content

Commit

Permalink
Add ClearCustomApiAccessMethods rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe committed Apr 10, 2024
1 parent ad638c2 commit d9bc659
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mullvad-daemon/src/access_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ where
Ok(())
}

/// Remove all custom [`AccessMethodSetting`].
pub async fn clear_custom_api_access_methods(&mut self) -> Result<(), Error> {
self.settings
.update(|settings: &mut Settings| {
settings.api_access_methods.clear_custom();
})
.await?;

Ok(())
}

/// Return the [`AccessMethodSetting`] which is currently used to access the
/// Mullvad API.
pub async fn get_current_access_method(&self) -> Result<AccessMethodSetting, Error> {
Expand Down
10 changes: 10 additions & 0 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ pub enum DaemonCommand {
SetApiAccessMethod(ResponseTx<(), Error>, mullvad_types::access_method::Id),
/// Edit an API access method
UpdateApiAccessMethod(ResponseTx<(), Error>, AccessMethodSetting),
ClearCustomApiAccessMethods(ResponseTx<(), Error>),
/// Get the currently used API access method
GetCurrentAccessMethod(ResponseTx<AccessMethodSetting, Error>),
/// Test an API access method
Expand Down Expand Up @@ -1260,6 +1261,7 @@ where
}
RemoveApiAccessMethod(tx, method) => self.on_remove_api_access_method(tx, method).await,
UpdateApiAccessMethod(tx, method) => self.on_update_api_access_method(tx, method).await,
ClearCustomApiAccessMethods(tx) => self.on_clear_custom_api_access_methods(tx).await,
GetCurrentAccessMethod(tx) => self.on_get_current_api_access_method(tx),
SetApiAccessMethod(tx, method) => self.on_set_api_access_method(tx, method).await,
TestApiAccessMethodById(tx, method) => self.on_test_api_access_method(tx, method).await,
Expand Down Expand Up @@ -2482,6 +2484,14 @@ where
Self::oneshot_send(tx, result, "update_api_access_method response");
}

async fn on_clear_custom_api_access_methods(&mut self, tx: ResponseTx<(), Error>) {
let result = self
.clear_custom_api_access_methods()
.await
.map_err(Error::AccessMethodError);
Self::oneshot_send(tx, result, "update_api_access_method response");
}

fn on_get_current_api_access_method(&mut self, tx: ResponseTx<AccessMethodSetting, Error>) {
let handle = self.access_mode_handler.clone();
tokio::spawn(async move {
Expand Down
10 changes: 10 additions & 0 deletions mullvad-daemon/src/management_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,16 @@ impl ManagementService for ManagementServiceImpl {
.map_err(map_daemon_error)
}

async fn clear_custom_api_access_methods(&self, _: Request<()>) -> ServiceResult<()> {
log::debug!("get_current_api_access_method");
let (tx, rx) = oneshot::channel();
self.send_command_to_daemon(DaemonCommand::ClearCustomApiAccessMethods(tx))?;
self.wait_for_result(rx)
.await?
.map(Response::new)
.map_err(map_daemon_error)
}

/// Return the [`types::AccessMethodSetting`] which the daemon is using to
/// connect to the Mullvad API.
async fn get_current_api_access_method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ service ManagementService {
rpc RemoveApiAccessMethod(UUID) returns (google.protobuf.Empty) {}
rpc SetApiAccessMethod(UUID) returns (google.protobuf.Empty) {}
rpc UpdateApiAccessMethod(AccessMethodSetting) returns (google.protobuf.Empty) {}
rpc ClearCustomApiAccessMethods(google.protobuf.Empty) returns (google.protobuf.Empty) {}
rpc GetCurrentApiAccessMethod(google.protobuf.Empty) returns (AccessMethodSetting) {}
rpc TestCustomApiAccessMethod(CustomProxy) returns (google.protobuf.BoolValue) {}
rpc TestApiAccessMethodById(UUID) returns (google.protobuf.BoolValue) {}
Expand Down
8 changes: 8 additions & 0 deletions mullvad-management-interface/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ impl MullvadProxyClient {
.map(drop)
}

pub async fn clear_custom_access_methods(&mut self) -> Result<()> {
self.0
.clear_custom_api_access_methods(())
.await
.map_err(Error::Rpc)
.map(drop)
}

/// Set the [`AccessMethod`] which [`ApiConnectionModeProvider`] should
/// pick.
///
Expand Down
5 changes: 5 additions & 0 deletions mullvad-types/src/access_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ impl Settings {
updated
}

/// Clear all custom access methods.
pub fn clear_custom(&mut self) {
self.custom.clear();
}

/// Check that `self` contains atleast one enabled access methods. If not,
/// the `Direct` access method is re-enabled.
fn ensure_consistent_state(&mut self) {
Expand Down
6 changes: 6 additions & 0 deletions test/test-manager/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ pub async fn cleanup_after_test(mullvad_client: &mut MullvadProxyClient) -> anyh
settings_version: _, // N/A
} = Default::default();

let _ = api_access_methods;
mullvad_client
.clear_custom_access_methods()
.await
.context("Could not clear custom api access methods")?;

mullvad_client
.set_relay_settings(relay_settings)
.await
Expand Down

0 comments on commit d9bc659

Please sign in to comment.