From d9bc659b7d90b344c616f472b21592857be23161 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Tue, 9 Apr 2024 16:30:55 +0200 Subject: [PATCH] Add ClearCustomApiAccessMethods rpc call --- mullvad-daemon/src/access_method.rs | 11 +++++++++++ mullvad-daemon/src/lib.rs | 10 ++++++++++ mullvad-daemon/src/management_interface.rs | 10 ++++++++++ .../proto/management_interface.proto | 1 + mullvad-management-interface/src/client.rs | 8 ++++++++ mullvad-types/src/access_method.rs | 5 +++++ test/test-manager/src/tests/mod.rs | 6 ++++++ 7 files changed, 51 insertions(+) diff --git a/mullvad-daemon/src/access_method.rs b/mullvad-daemon/src/access_method.rs index bc843f529e2b..edae229ae00f 100644 --- a/mullvad-daemon/src/access_method.rs +++ b/mullvad-daemon/src/access_method.rs @@ -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 { diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index c3c64ebacfa6..e8d7a4fadcba 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -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), /// Test an API access method @@ -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, @@ -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) { let handle = self.access_mode_handler.clone(); tokio::spawn(async move { diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index ee0fadaa0eb4..af4f6124cc41 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -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( diff --git a/mullvad-management-interface/proto/management_interface.proto b/mullvad-management-interface/proto/management_interface.proto index 7fbdb6eba672..cd3e6afaac0c 100644 --- a/mullvad-management-interface/proto/management_interface.proto +++ b/mullvad-management-interface/proto/management_interface.proto @@ -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) {} diff --git a/mullvad-management-interface/src/client.rs b/mullvad-management-interface/src/client.rs index 847150c47731..e8327d2b1173 100644 --- a/mullvad-management-interface/src/client.rs +++ b/mullvad-management-interface/src/client.rs @@ -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. /// diff --git a/mullvad-types/src/access_method.rs b/mullvad-types/src/access_method.rs index c5f0b4185971..de788bfe1985 100644 --- a/mullvad-types/src/access_method.rs +++ b/mullvad-types/src/access_method.rs @@ -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) { diff --git a/test/test-manager/src/tests/mod.rs b/test/test-manager/src/tests/mod.rs index 0ccc900c5fe7..51c5a146708b 100644 --- a/test/test-manager/src/tests/mod.rs +++ b/test/test-manager/src/tests/mod.rs @@ -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