diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2023-09-11 15:52:15 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-10-09 14:40:03 +0200 |
| commit | 2db6e2ecc64c0ddb9dba0b0947a31b0fbefe9f53 (patch) | |
| tree | 943d1a981f74d9fbe792eb5932532e247bcd668c /mullvad-cli/src | |
| parent | c899c1b63858c12c9367318c19120fe899b31394 (diff) | |
| download | mullvadvpn-2db6e2ecc64c0ddb9dba0b0947a31b0fbefe9f53.tar.xz mullvadvpn-2db6e2ecc64c0ddb9dba0b0947a31b0fbefe9f53.zip | |
Code cleanup
- Add a new datastructures for distinguishing between built-in & custom
api access methods
- Implement `TryFrom` instead of `From` for fallible conversions
- Do not panic if a protobuf-message is ill-formatted
- Do not allow removal of built-in api access methods
- Refactor notification logic in `access_methods.rs`
- Rename `mullvad proxy api` to simply `mullvad proxy`
- Since there are no other kinds of proxies at the moment, the
subcommand `proxy api` does not make much sense.
- Remove left-over comments
Diffstat (limited to 'mullvad-cli/src')
| -rw-r--r-- | mullvad-cli/src/cmds/proxy.rs | 84 |
1 files changed, 41 insertions, 43 deletions
diff --git a/mullvad-cli/src/cmds/proxy.rs b/mullvad-cli/src/cmds/proxy.rs index 5a4bf2fdd8..4826a571af 100644 --- a/mullvad-cli/src/cmds/proxy.rs +++ b/mullvad-cli/src/cmds/proxy.rs @@ -1,41 +1,47 @@ use anyhow::{anyhow, Result}; use mullvad_management_interface::MullvadProxyClient; -use mullvad_types::api_access_method::{AccessMethod, ApiAccessMethodReplace}; +use mullvad_types::api_access_method::{ + daemon::ApiAccessMethodReplace, AccessMethod, ObfuscationProtocol, +}; use std::net::IpAddr; use clap::{Args, Subcommand}; use talpid_types::net::openvpn::SHADOWSOCKS_CIPHERS; -#[derive(Subcommand, Debug)] +#[derive(Subcommand, Debug, Clone)] pub enum Proxy { - /// Get current api settings + /// List the configured API proxies + List, + /// Add a custom API proxy #[clap(subcommand)] - Api(ApiCommands), + Add(AddCustomCommands), + /// Edit an API proxy + Edit(EditCustomCommands), + /// Remove an API proxy + Remove(RemoveCustomCommands), } impl Proxy { pub async fn handle(self) -> Result<()> { match self { - Proxy::Api(cmd) => match cmd { - ApiCommands::List => { - //println!("Listing the API access methods: .."); - Self::list().await?; - } - ApiCommands::Add(cmd) => { - //println!("Adding custom proxy"); - Self::add(cmd).await?; - } - ApiCommands::Edit(cmd) => { - // Transform human-readable index to 0-based indexing. - let index = Self::zero_to_one_based_index(cmd.index)?; - Self::edit(EditCustomCommands { index, ..cmd }).await? - } - ApiCommands::Remove(cmd) => { - // Transform human-readable index to 0-based indexing. - let index = Self::zero_to_one_based_index(cmd.index)?; - Self::remove(RemoveCustomCommands { index }).await? - } - }, + Proxy::List => { + //println!("Listing the API access methods: .."); + Self::list().await?; + } + Proxy::Add(cmd) => { + //println!("Adding custom proxy"); + Self::add(cmd).await?; + } + Proxy::Edit(cmd) => { + // Transform human-readable index to 0-based indexing. + let index = Self::zero_to_one_based_index(cmd.index)?; + Self::edit(EditCustomCommands { index, ..cmd }).await? + } + Proxy::Remove(cmd) => { + // Transform human-readable index to 0-based indexing. + let index = Self::zero_to_one_based_index(cmd.index)?; + Self::remove(RemoveCustomCommands { index }).await? + } }; Ok(()) } @@ -88,8 +94,13 @@ impl Proxy { .clone(); // Create a new access method combining the new params with the previous values - let edited_access_method: AccessMethod = match access_method { - AccessMethod::Shadowsocks(shadowsocks) => { + let access_method = match access_method { + AccessMethod::BuiltIn(_) => Err(anyhow!("Can not edit built-in access method")), + AccessMethod::Custom(custom_access_method) => Ok(custom_access_method), + }?; + + let edited_access_method: AccessMethod = match access_method.access_method { + ObfuscationProtocol::Shadowsocks(shadowsocks) => { let ip = cmd.params.ip.unwrap_or(shadowsocks.peer.ip()).to_string(); let port = cmd.params.port.unwrap_or(shadowsocks.peer.port()); let password = cmd.params.password.unwrap_or(shadowsocks.password); @@ -97,7 +108,7 @@ impl Proxy { mullvad_types::api_access_method::Shadowsocks::from_args(ip, port, cipher, password) .map(|x| x.into()) } - AccessMethod::Socks5(socks) => match socks { + ObfuscationProtocol::Socks5(socks) => match socks { mullvad_types::api_access_method::Socks5::Local(local) => { let ip = cmd.params.ip.unwrap_or(local.peer.ip()).to_string(); let port = cmd.params.port.unwrap_or(local.peer.port()); @@ -135,19 +146,6 @@ impl Proxy { } #[derive(Subcommand, Debug, Clone)] -pub enum ApiCommands { - /// List the configured API proxies - List, - /// Add a custom API proxy - #[clap(subcommand)] - Add(AddCustomCommands), - /// Edit an API proxy - Edit(EditCustomCommands), - /// Remove an API proxy - Remove(RemoveCustomCommands), -} - -#[derive(Subcommand, Debug, Clone)] pub enum AddCustomCommands { /// Configure SOCKS5 proxy #[clap(subcommand)] @@ -262,7 +260,7 @@ mod conversions { ) .ok_or(anyhow!("Could not create a local Socks5 api proxy"))?, ); - daemon_types::AccessMethod::Socks5(socks_proxy) + socks_proxy.into() } Socks5AddCommands::Remote { remote_ip, @@ -278,7 +276,7 @@ mod conversions { ) .ok_or(anyhow!("Could not create a remote Socks5 api proxy"))?, ); - daemon_types::AccessMethod::Socks5(socks_proxy) + socks_proxy.into() } }, AddCustomCommands::Shadowsocks { @@ -297,7 +295,7 @@ mod conversions { password, ) .ok_or(anyhow!("Could not create a Shadowsocks api proxy"))?; - daemon_types::AccessMethod::Shadowsocks(shadowsocks_proxy) + shadowsocks_proxy.into() } }) } |
