diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2023-09-11 11:55:02 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-10-09 14:40:03 +0200 |
| commit | 411f80d4cd227686a57e9dcc39dd30a01f728575 (patch) | |
| tree | af123ab34b0fef2ca06f8180d3fc3b7b5f6029cc | |
| parent | 585a820373abea33e3338e97fb727028667968bb (diff) | |
| download | mullvadvpn-411f80d4cd227686a57e9dcc39dd30a01f728575.tar.xz mullvadvpn-411f80d4cd227686a57e9dcc39dd30a01f728575.zip | |
Add `mullvad proxy api remove` command
Allow the user to manually remove a custom api proxy.
| -rw-r--r-- | mullvad-cli/src/cmds/proxy.rs | 42 | ||||
| -rw-r--r-- | mullvad-daemon/src/access_methods.rs | 20 | ||||
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 15 | ||||
| -rw-r--r-- | mullvad-management-interface/proto/management_interface.proto | 3 | ||||
| -rw-r--r-- | mullvad-management-interface/src/client.rs | 8 |
5 files changed, 83 insertions, 5 deletions
diff --git a/mullvad-cli/src/cmds/proxy.rs b/mullvad-cli/src/cmds/proxy.rs index fec4a6f966..583accef1e 100644 --- a/mullvad-cli/src/cmds/proxy.rs +++ b/mullvad-cli/src/cmds/proxy.rs @@ -1,9 +1,9 @@ -use anyhow::Result; +use anyhow::{anyhow, Result}; use mullvad_management_interface::MullvadProxyClient; use mullvad_types::api_access_method::AccessMethod; use std::net::IpAddr; -use clap::Subcommand; +use clap::{Args, Subcommand}; use talpid_types::net::openvpn::SHADOWSOCKS_CIPHERS; #[derive(Subcommand, Debug)] @@ -25,6 +25,14 @@ impl Proxy { //println!("Adding custom proxy"); Self::add(cmd).await?; } + ApiCommands::Edit(_) => todo!(), + ApiCommands::Remove(cmd) => { + // Transform human-readable index to 0-based indexing. + match cmd.index.checked_sub(1) { + Some(index) => Self::remove(RemoveCustomCommands { index, ..cmd }).await?, + None => println!("Access method 0 does not exist"), + } + } }, }; Ok(()) @@ -33,9 +41,8 @@ impl Proxy { /// Show all API access methods. async fn list() -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - println!("Calling [rpc::get_api_access_methods] .."); - for api_access_method in rpc.get_api_access_methods().await? { - println!("{:?}", api_access_method); + for (index, api_access_method) in rpc.get_api_access_methods().await?.iter().enumerate() { + println!("{}. {:?}", index + 1, api_access_method); } Ok(()) } @@ -47,6 +54,23 @@ impl Proxy { rpc.add_access_method(proxy).await?; Ok(()) } + + /// Remove an API access method. + async fn remove(cmd: RemoveCustomCommands) -> Result<()> { + let mut rpc = MullvadProxyClient::new().await?; + let access_method = rpc + .get_api_access_methods() + .await? + .get(cmd.index) + .ok_or(anyhow!(format!( + "Access method {} does not exist", + cmd.index + 1 + )))? + .clone(); + rpc.remove_access_method(access_method) + .await + .map_err(Into::<anyhow::Error>::into) + } } #[derive(Subcommand, Debug, Clone)] @@ -56,6 +80,8 @@ pub enum ApiCommands { /// Add a custom API proxy #[clap(subcommand)] Add(AddCustomCommands), + /// Remove an API proxy + Remove(RemoveCustomCommands), } #[derive(Subcommand, Debug, Clone)] @@ -80,6 +106,12 @@ pub enum AddCustomCommands { }, } +#[derive(Args, Debug, Clone)] +pub struct RemoveCustomCommands { + /// Which API proxy to remove + index: usize, +} + #[derive(Subcommand, Debug, Clone)] pub enum Socks5AddCommands { /// Configure a local SOCKS5 proxy diff --git a/mullvad-daemon/src/access_methods.rs b/mullvad-daemon/src/access_methods.rs index 634914d00a..12d8f10f26 100644 --- a/mullvad-daemon/src/access_methods.rs +++ b/mullvad-daemon/src/access_methods.rs @@ -34,4 +34,24 @@ where }) .map_err(Error::Settings) } + + pub async fn remove_access_method(&mut self, access_method: AccessMethod) -> Result<(), Error> { + self.settings + .update(|settings| { + settings + .api_access_methods + .api_access_methods + .retain(|x| *x != access_method); + }) + .await + .map(|changed| { + if changed { + self.event_listener + .notify_settings(self.settings.to_settings()); + self.relay_selector + .set_config(new_selector_config(&self.settings)); + }; + }) + .map_err(Error::Settings) + } } diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index 676e122a64..6ebb7f828e 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -264,6 +264,8 @@ pub enum DaemonCommand { GetApiAccessMethods(ResponseTx<Vec<AccessMethod>, Error>), /// Add API access methods AddApiAccessMethod(ResponseTx<(), Error>, AccessMethod), + /// Remove an API access method + RemoveApiAccessMethod(ResponseTx<(), Error>, AccessMethod), /// Get information about the currently running and latest app versions GetVersionInfo(oneshot::Sender<Option<AppVersionInfo>>), /// Return whether the daemon is performing post-upgrade tasks @@ -1041,6 +1043,7 @@ where GetVersionInfo(tx) => self.on_get_version_info(tx), GetApiAccessMethods(tx) => self.on_get_api_access_methods(tx), AddApiAccessMethod(tx, method) => self.on_add_api_access_method(tx, method).await, + RemoveApiAccessMethod(tx, method) => self.on_remove_api_access_method(tx, method).await, IsPerformingPostUpgrade(tx) => self.on_is_performing_post_upgrade(tx), GetCurrentVersion(tx) => self.on_get_current_version(tx), #[cfg(not(target_os = "android"))] @@ -2228,6 +2231,18 @@ where Self::oneshot_send(tx, result, "add_api_access_method response"); } + async fn on_remove_api_access_method( + &mut self, + tx: ResponseTx<(), Error>, + method: AccessMethod, + ) { + let result = self + .remove_access_method(method) + .await + .map_err(Error::AccessMethodError); + Self::oneshot_send(tx, result, "remove_api_access_method response"); + } + fn on_get_settings(&self, tx: oneshot::Sender<Settings>) { Self::oneshot_send(tx, self.settings.to_settings(), "get_settings response"); } diff --git a/mullvad-management-interface/proto/management_interface.proto b/mullvad-management-interface/proto/management_interface.proto index 03c9a2c02c..9491541dc4 100644 --- a/mullvad-management-interface/proto/management_interface.proto +++ b/mullvad-management-interface/proto/management_interface.proto @@ -78,6 +78,9 @@ service ManagementService { rpc AddApiAccessMethod(ApiAccessMethod) returns (google.protobuf.Empty) { // Can I return something useful here instead of Empty? } + rpc RemoveApiAccessMethod(ApiAccessMethod) returns (google.protobuf.Empty) { + // Can I return something useful here instead of Empty? + } // Split tunneling (Linux) rpc GetSplitTunnelProcesses(google.protobuf.Empty) returns (stream google.protobuf.Int32Value) {} diff --git a/mullvad-management-interface/src/client.rs b/mullvad-management-interface/src/client.rs index e7c14800a5..d4edabc391 100644 --- a/mullvad-management-interface/src/client.rs +++ b/mullvad-management-interface/src/client.rs @@ -479,6 +479,14 @@ impl MullvadProxyClient { .map(drop) } + pub async fn remove_access_method(&mut self, access_method: AccessMethod) -> Result<()> { + self.0 + .remove_api_access_method(types::ApiAccessMethod::from(access_method)) + .await + .map_err(Error::Rpc) + .map(drop) + } + #[cfg(target_os = "linux")] pub async fn get_split_tunnel_processes(&mut self) -> Result<Vec<i32>> { use futures::TryStreamExt; |
