diff options
Diffstat (limited to 'mullvad-cli')
| -rw-r--r-- | mullvad-cli/src/cmds/auto_connect.rs | 15 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/beta_program.rs | 26 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/lan.rs | 18 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/lockdown.rs | 22 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/mod.rs | 64 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/relay.rs | 10 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/split_tunnel/windows.rs | 22 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/tunnel.rs | 18 |
8 files changed, 103 insertions, 92 deletions
diff --git a/mullvad-cli/src/cmds/auto_connect.rs b/mullvad-cli/src/cmds/auto_connect.rs index 4da11dc68f..4fe578f820 100644 --- a/mullvad-cli/src/cmds/auto_connect.rs +++ b/mullvad-cli/src/cmds/auto_connect.rs @@ -2,17 +2,14 @@ use anyhow::Result; use clap::Subcommand; use mullvad_management_interface::MullvadProxyClient; -use super::on_off_parser; +use super::BooleanOption; #[derive(Subcommand, Debug)] pub enum AutoConnect { /// Display the current auto-connect setting Get, /// Change auto-connect setting - Set { - #[arg(value_parser = on_off_parser())] - policy: bool, - }, + Set { policy: BooleanOption }, } impl AutoConnect { @@ -23,17 +20,17 @@ impl AutoConnect { } } - async fn set(policy: bool) -> Result<()> { + async fn set(policy: BooleanOption) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - rpc.set_auto_connect(policy).await?; + rpc.set_auto_connect(*policy).await?; println!("Changed auto-connect setting"); Ok(()) } async fn get() -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - let auto_connect = rpc.get_settings().await?.auto_connect; - println!("Autoconnect: {}", if auto_connect { "on" } else { "off" }); + let auto_connect = BooleanOption::from(rpc.get_settings().await?.auto_connect); + println!("Autoconnect: {auto_connect}"); Ok(()) } } diff --git a/mullvad-cli/src/cmds/beta_program.rs b/mullvad-cli/src/cmds/beta_program.rs index df98efbf79..ba9c72616e 100644 --- a/mullvad-cli/src/cmds/beta_program.rs +++ b/mullvad-cli/src/cmds/beta_program.rs @@ -2,17 +2,14 @@ use anyhow::{anyhow, Result}; use clap::Subcommand; use mullvad_management_interface::MullvadProxyClient; -use super::on_off_parser; +use super::BooleanOption; #[derive(Subcommand, Debug)] pub enum BetaProgram { /// Get beta notifications setting Get, /// Change beta notifications setting - Set { - #[arg(value_parser = on_off_parser())] - policy: bool, - }, + Set { policy: BooleanOption }, } impl BetaProgram { @@ -23,31 +20,24 @@ impl BetaProgram { } } - async fn set(enable: bool) -> Result<()> { - if !enable && mullvad_version::VERSION.contains("beta") { + async fn set(state: BooleanOption) -> Result<()> { + if !*state && mullvad_version::VERSION.contains("beta") { return Err(anyhow!( "The beta program must be enabled while running a beta version", )); } let mut rpc = MullvadProxyClient::new().await?; - rpc.set_show_beta_releases(enable).await?; + rpc.set_show_beta_releases(*state).await?; - if enable { - println!("Beta program: on"); - } else { - println!("Beta program: off"); - } + println!("Beta program: {state}"); Ok(()) } async fn get() -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - if rpc.get_settings().await?.show_beta_releases { - println!("Beta program: on"); - } else { - println!("Beta program: off"); - } + let opt = BooleanOption::from(rpc.get_settings().await?.show_beta_releases); + println!("Beta program: {opt}"); Ok(()) } } diff --git a/mullvad-cli/src/cmds/lan.rs b/mullvad-cli/src/cmds/lan.rs index 21c49ca05c..7bf92063ca 100644 --- a/mullvad-cli/src/cmds/lan.rs +++ b/mullvad-cli/src/cmds/lan.rs @@ -2,7 +2,7 @@ use anyhow::Result; use clap::Subcommand; use mullvad_management_interface::MullvadProxyClient; -use super::on_off_parser_custom; +use super::BooleanOption; #[derive(Subcommand, Debug)] pub enum Lan { @@ -11,8 +11,8 @@ pub enum Lan { /// Change allow LAN setting Set { - #[arg(value_parser = on_off_parser_custom("allow", "block"))] - policy: bool, + #[arg(value_parser = BooleanOption::custom_parser("allow", "block"))] + policy: BooleanOption, }, } @@ -24,20 +24,18 @@ impl Lan { } } - async fn set(policy: bool) -> Result<()> { + async fn set(policy: BooleanOption) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - rpc.set_allow_lan(policy).await?; + rpc.set_allow_lan(*policy).await?; println!("Changed local network sharing setting"); Ok(()) } async fn get() -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - let allow_lan = rpc.get_settings().await?.allow_lan; - println!( - "Local network sharing setting: {}", - if allow_lan { "allow" } else { "block" } - ); + let allow_lan = + BooleanOption::with_labels(rpc.get_settings().await?.allow_lan, "allow", "block"); + println!("Local network sharing setting: {allow_lan}"); Ok(()) } } diff --git a/mullvad-cli/src/cmds/lockdown.rs b/mullvad-cli/src/cmds/lockdown.rs index 12cac6d126..001f195fda 100644 --- a/mullvad-cli/src/cmds/lockdown.rs +++ b/mullvad-cli/src/cmds/lockdown.rs @@ -2,17 +2,14 @@ use anyhow::Result; use clap::Subcommand; use mullvad_management_interface::MullvadProxyClient; -use super::on_off_parser; +use super::BooleanOption; #[derive(Subcommand, Debug)] pub enum LockdownMode { /// Display the current lockdown mode setting Get, /// Change the lockdown mode setting - Set { - #[arg(value_parser = on_off_parser())] - policy: bool, - }, + Set { policy: BooleanOption }, } impl LockdownMode { @@ -23,24 +20,17 @@ impl LockdownMode { } } - async fn set(policy: bool) -> Result<()> { + async fn set(policy: BooleanOption) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - rpc.set_block_when_disconnected(policy).await?; + rpc.set_block_when_disconnected(*policy).await?; println!("Changed lockdown mode setting"); Ok(()) } async fn get() -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - let block_when_disconnected = rpc.get_settings().await?.block_when_disconnected; - println!( - "Network traffic will be {} when the VPN is disconnected", - if block_when_disconnected { - "blocked" - } else { - "allowed" - } - ); + let state = BooleanOption::from(rpc.get_settings().await?.block_when_disconnected); + println!("Block traffic when the VPN is disconnected: {state}"); Ok(()) } } diff --git a/mullvad-cli/src/cmds/mod.rs b/mullvad-cli/src/cmds/mod.rs index b621684ae7..7adcf6c65b 100644 --- a/mullvad-cli/src/cmds/mod.rs +++ b/mullvad-cli/src/cmds/mod.rs @@ -1,4 +1,5 @@ use clap::builder::{PossibleValuesParser, TypedValueParser, ValueParser}; +use std::ops::Deref; pub mod account; pub mod auto_connect; @@ -18,13 +19,62 @@ pub mod tunnel_state; pub mod version; /// A value parser that parses "on" or "off" into a boolean -fn on_off_parser() -> ValueParser { - on_off_parser_custom("on", "off") +#[derive(Debug, Clone, Copy)] +pub struct BooleanOption { + state: bool, + on_label: &'static str, + off_label: &'static str, } -/// A value parser that parses `on_label` into true, and `off_label` into false -fn on_off_parser_custom(on_label: &'static str, off_label: &'static str) -> ValueParser { - ValueParser::new( - PossibleValuesParser::new([on_label, off_label]).map(move |val| val == on_label), - ) +impl Deref for BooleanOption { + type Target = bool; + + fn deref(&self) -> &Self::Target { + &self.state + } +} + +impl clap::builder::ValueParserFactory for BooleanOption { + type Parser = ValueParser; + + /// A value parser that parses "on" or "off" into a `BooleanOption` + fn value_parser() -> Self::Parser { + Self::custom_parser("on", "off") + } +} + +impl BooleanOption { + /// A value parser that parses `on_label` and `off_label` into a `BooleanOption` + fn custom_parser(on_label: &'static str, off_label: &'static str) -> ValueParser { + assert!(on_label != off_label); + + ValueParser::new( + PossibleValuesParser::new([on_label, off_label]) + .map(move |val| Self::with_labels(val == on_label, on_label, off_label)), + ) + } + + fn with_labels(state: bool, on_label: &'static str, off_label: &'static str) -> Self { + Self { + state, + on_label, + off_label, + } + } +} + +impl From<bool> for BooleanOption { + fn from(state: bool) -> Self { + Self::with_labels(state, "on", "off") + } +} + +impl std::fmt::Display for BooleanOption { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.state { + self.on_label.fmt(f) + } else { + self.off_label.fmt(f) + } + } } diff --git a/mullvad-cli/src/cmds/relay.rs b/mullvad-cli/src/cmds/relay.rs index acc99214d4..37b86537e4 100644 --- a/mullvad-cli/src/cmds/relay.rs +++ b/mullvad-cli/src/cmds/relay.rs @@ -20,7 +20,7 @@ use talpid_types::net::{ all_of_the_internet, openvpn, wireguard, Endpoint, IpVersion, TransportProtocol, TunnelType, }; -use super::{on_off_parser, relay_constraints::LocationArgs}; +use super::{relay_constraints::LocationArgs, BooleanOption}; #[derive(Subcommand, Debug)] pub enum Relay { @@ -103,8 +103,8 @@ pub enum SetTunnelCommands { /// Whether to enable multihop. The location constraints are specified with /// 'entry-location'. - #[arg(long, short = 'm', value_parser = on_off_parser())] - use_multihop: Option<bool>, + #[arg(long, short = 'm')] + use_multihop: Option<BooleanOption>, #[clap(subcommand)] entry_location: Option<EntryLocation>, @@ -501,7 +501,7 @@ impl Relay { async fn set_wireguard_constraints( port: Option<Constraint<u16>>, ip_version: Option<Constraint<IpVersion>>, - use_multihop: Option<bool>, + use_multihop: Option<BooleanOption>, entry_location: Option<EntryLocation>, ) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; @@ -528,7 +528,7 @@ impl Relay { wireguard_constraints.ip_version = ipv; } if let Some(use_multihop) = use_multihop { - wireguard_constraints.use_multihop = use_multihop; + wireguard_constraints.use_multihop = *use_multihop; } if let Some(EntryLocation::EntryLocation(entry)) = entry_location { wireguard_constraints.entry_location = Constraint::from(entry); diff --git a/mullvad-cli/src/cmds/split_tunnel/windows.rs b/mullvad-cli/src/cmds/split_tunnel/windows.rs index 3969880bee..f17a3382f3 100644 --- a/mullvad-cli/src/cmds/split_tunnel/windows.rs +++ b/mullvad-cli/src/cmds/split_tunnel/windows.rs @@ -7,7 +7,7 @@ use std::{ use clap::Subcommand; use mullvad_management_interface::MullvadProxyClient; -use super::super::on_off_parser; +use super::super::BooleanOption; /// Set options for applications to exclude from the tunnel. #[derive(Subcommand, Debug)] @@ -22,10 +22,7 @@ pub enum SplitTunnel { }, /// Enable or disable split tunnel - Set { - #[arg(value_parser = on_off_parser())] - policy: bool, - }, + Set { policy: BooleanOption }, /// Manage applications to exclude from the tunnel #[clap(subcommand)] @@ -46,14 +43,9 @@ impl SplitTunnel { let mut rpc = MullvadProxyClient::new().await?; let settings = rpc.get_settings().await?.split_tunnel; - println!( - "Split tunneling state: {}", - if settings.enable_exclusions { - "on" - } else { - "off" - } - ); + let enable_exclusions = BooleanOption::from(settings.enable_exclusions); + + println!("Split tunneling state: {enable_exclusions}"); println!("Excluded applications:"); for path in &settings.apps { @@ -79,8 +71,8 @@ impl SplitTunnel { } SplitTunnel::Set { policy } => { let mut rpc = MullvadProxyClient::new().await?; - rpc.set_split_tunnel_state(policy).await?; - println!("Set split tunnel policy"); + rpc.set_split_tunnel_state(*policy).await?; + println!("Split tunnel policy: {policy}"); Ok(()) } SplitTunnel::App(subcmd) => Self::app(subcmd).await, diff --git a/mullvad-cli/src/cmds/tunnel.rs b/mullvad-cli/src/cmds/tunnel.rs index 8387e053cf..120fad327d 100644 --- a/mullvad-cli/src/cmds/tunnel.rs +++ b/mullvad-cli/src/cmds/tunnel.rs @@ -1,4 +1,3 @@ -use super::on_off_parser; use anyhow::Result; use clap::Subcommand; use mullvad_management_interface::MullvadProxyClient; @@ -7,6 +6,8 @@ use mullvad_types::{ wireguard::{QuantumResistantState, RotationInterval, DEFAULT_ROTATION_INTERVAL}, }; +use super::BooleanOption; + #[derive(Subcommand, Debug)] pub enum Tunnel { /// Show current tunnel options @@ -46,10 +47,7 @@ pub enum TunnelOptions { /// Enable or disable IPv6 in the tunnel #[clap(arg_required_else_help = true)] - Ipv6 { - #[arg(value_parser = on_off_parser())] - state: bool, - }, + Ipv6 { state: BooleanOption }, } #[derive(Subcommand, Debug, Clone)] @@ -144,14 +142,10 @@ impl Tunnel { } } - async fn handle_ipv6(state: bool) -> Result<()> { + async fn handle_ipv6(state: BooleanOption) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - rpc.set_enable_ipv6(state).await?; - if state { - println!("Enabled IPv6"); - } else { - println!("Disabled IPv6"); - } + rpc.set_enable_ipv6(*state).await?; + println!("IPv6: {state}"); Ok(()) } |
