diff options
| author | Emīls <emils@mullvad.net> | 2021-12-06 16:45:48 +0000 |
|---|---|---|
| committer | Emīls <emils@mullvad.net> | 2021-12-10 09:58:51 +0000 |
| commit | 523089c5c5d4f9fecd2bf518a26216a8a0bd4e54 (patch) | |
| tree | 120d7d7821fc198324a4e4577554def409876205 /mullvad-cli | |
| parent | e74723e7aae8106ccbe3c6548e62258f023520b4 (diff) | |
| download | mullvadvpn-523089c5c5d4f9fecd2bf518a26216a8a0bd4e54.tar.xz mullvadvpn-523089c5c5d4f9fecd2bf518a26216a8a0bd4e54.zip | |
Use network-check subcommand in CLI
Diffstat (limited to 'mullvad-cli')
| -rw-r--r-- | mullvad-cli/src/cmds/dns.rs | 106 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/mod.rs | 7 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/network_check.rs | 73 | ||||
| -rw-r--r-- | mullvad-cli/src/format.rs | 4 |
4 files changed, 115 insertions, 75 deletions
diff --git a/mullvad-cli/src/cmds/dns.rs b/mullvad-cli/src/cmds/dns.rs index d0a6c7954e..9ffa0ec09c 100644 --- a/mullvad-cli/src/cmds/dns.rs +++ b/mullvad-cli/src/cmds/dns.rs @@ -12,55 +12,43 @@ impl Command for Dns { } fn clap_subcommand(&self) -> clap::App<'static, 'static> { - let set_subcommand = clap::SubCommand::with_name("set") - .about("Set DNS servers to use") - .setting(clap::AppSettings::SubcommandRequiredElseHelp) - .subcommand( - clap::SubCommand::with_name("default") - .about("Use default DNS servers") - .arg( - clap::Arg::with_name("block ads") - .long("block-ads") - .takes_value(false) - .help("Block domain names used for ads"), - ) - .arg( - clap::Arg::with_name("block trackers") - .long("block-trackers") - .takes_value(false) - .help("Block domain names used for tracking"), - ), - ) - .subcommand( - clap::SubCommand::with_name("custom") - .about("Set a list of custom DNS servers") - .arg( - clap::Arg::with_name("servers") - .multiple(true) - .help("One or more IP addresses pointing to DNS resolvers.") - .required(true), - ), - ); - #[cfg(target_os = "macos")] - let set_subcommand = set_subcommand.subcommand( - clap::SubCommand::with_name("custom-resolver") - .about("Toggle custom resolver") - .arg( - clap::Arg::with_name("state") - .help("Whether to turn on custom resolver to aid macOS's captive portal check") - .takes_value(true) - .possible_values(&["on", "off"]) - .required(true), - ), - ); - clap::SubCommand::with_name(self.name()) .about("Configure DNS servers to use when connected") .setting(clap::AppSettings::SubcommandRequiredElseHelp) .subcommand( clap::SubCommand::with_name("get").about("Display the current DNS settings"), ) - .subcommand(set_subcommand) + .subcommand( + clap::SubCommand::with_name("set") + .about("Set DNS servers to use") + .setting(clap::AppSettings::SubcommandRequiredElseHelp) + .subcommand( + clap::SubCommand::with_name("default") + .about("Use default DNS servers") + .arg( + clap::Arg::with_name("block ads") + .long("block-ads") + .takes_value(false) + .help("Block domain names used for ads"), + ) + .arg( + clap::Arg::with_name("block trackers") + .long("block-trackers") + .takes_value(false) + .help("Block domain names used for tracking"), + ), + ) + .subcommand( + clap::SubCommand::with_name("custom") + .about("Set a list of custom DNS servers") + .arg( + clap::Arg::with_name("servers") + .multiple(true) + .help("One or more IP addresses pointing to DNS resolvers.") + .required(true), + ), + ), + ) } async fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()> { @@ -76,11 +64,6 @@ impl Command for Dns { ("custom", Some(matches)) => { self.set_custom(matches.values_of_lossy("servers")).await } - #[cfg(target_os = "macos")] - ("custom-resolver", Some(matches)) => { - self.set_custom_resolver(matches.value_of("state") == Some("on")) - .await - } _ => unreachable!("No custom-dns server command given"), }, ("get", _) => self.get().await, @@ -121,26 +104,12 @@ impl Dns { Ok(()) } - #[cfg(target_os = "macos")] - async fn set_custom_resolver(&self, enable: bool) -> Result<()> { - let mut rpc = new_rpc_client().await?; - let result = rpc.set_custom_resolver(enable).await; - let action = if enable { "enabled" } else { "disabled" }; - match result { - Ok(_) => { - println!("Successfully {} custom resolver", action); - } - Err(err) => { - println!("Failed to {} custom resolver: {}", action, err); - } - } - Ok(()) - } - async fn get(&self) -> Result<()> { let mut rpc = new_rpc_client().await?; - let settings = rpc.get_settings(()).await?.into_inner(); - let options: DnsOptions = settings + let options: DnsOptions = rpc + .get_settings(()) + .await? + .into_inner() .tunnel_options .unwrap() .dns_options @@ -162,11 +131,6 @@ impl Dns { } } - #[cfg(target_os = "macos")] - { - println!("Custom resolver: {}", settings.enable_custom_resolver); - } - Ok(()) } } diff --git a/mullvad-cli/src/cmds/mod.rs b/mullvad-cli/src/cmds/mod.rs index 2ceb3bfdcf..1f0e98f8ed 100644 --- a/mullvad-cli/src/cmds/mod.rs +++ b/mullvad-cli/src/cmds/mod.rs @@ -28,6 +28,11 @@ pub use self::dns::Dns; mod lan; pub use self::lan::Lan; +#[cfg(target_os = "macos")] +mod network_check; +#[cfg(target_os = "macos")] +pub use self::network_check::NetworkCheck; + mod reconnect; pub use self::reconnect::Reconnect; @@ -64,6 +69,8 @@ pub fn get_commands() -> HashMap<&'static str, Box<dyn Command>> { Box::new(Dns), Box::new(Reconnect), Box::new(Lan), + #[cfg(any(target_os = "macos"))] + Box::new(NetworkCheck), Box::new(Relay), Box::new(Reset), #[cfg(any(target_os = "linux", windows))] diff --git a/mullvad-cli/src/cmds/network_check.rs b/mullvad-cli/src/cmds/network_check.rs new file mode 100644 index 0000000000..323e5eb512 --- /dev/null +++ b/mullvad-cli/src/cmds/network_check.rs @@ -0,0 +1,73 @@ +use crate::{new_rpc_client, Command, Result}; +use clap::value_t_or_exit; + +pub struct NetworkCheck; + +const SUBCOMMAND_DESCRIPTION: &'static str = +"Control the macOS network check setting. Allowing the check leaks DNS queries for `captive.apple.com`. Allowing the +connectivity check allows macOS to get online quicker after sleep and after connecting to new WiFi networks"; + +#[mullvad_management_interface::async_trait] +impl Command for NetworkCheck { + fn name(&self) -> &'static str { + "macos-network-check" + } + + fn clap_subcommand(&self) -> clap::App<'static, 'static> { + clap::SubCommand::with_name(self.name()) + .about(SUBCOMMAND_DESCRIPTION) + .setting(clap::AppSettings::SubcommandRequiredElseHelp) + .subcommand( + clap::SubCommand::with_name("set") + .about("Toggle macOS network check setting") + .arg( + clap::Arg::with_name("policy") + .required(true) + .possible_values(&["allow", "block"]), + ), + ) + .subcommand( + clap::SubCommand::with_name("get") + .about("Display current macOS network check setting"), + ) + } + + async fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()> { + if let Some(set_matches) = matches.subcommand_matches("set") { + let allow_network_check = value_t_or_exit!(set_matches.value_of("policy"), String); + self.set(allow_network_check == "allow").await + } else if let Some(_get_matches) = matches.subcommand_matches("get") { + self.get().await + } else { + unreachable!("No macOS network check given") + } + } +} + +impl NetworkCheck { + async fn set(&self, allow_network_check: bool) -> Result<()> { + let mut rpc = new_rpc_client().await?; + rpc.set_allow_macos_network_check(allow_network_check) + .await?; + println!("Changed macOS network check setting"); + Ok(()) + } + + async fn get(&self) -> Result<()> { + let mut rpc = new_rpc_client().await?; + let allow_network_check = rpc + .get_settings(()) + .await? + .into_inner() + .allow_macos_network_check; + println!( + "macOS network check setting: {}", + if allow_network_check { + "allow" + } else { + "block" + } + ); + Ok(()) + } +} diff --git a/mullvad-cli/src/format.rs b/mullvad-cli/src/format.rs index ac5087ba3c..b056ffff53 100644 --- a/mullvad-cli/src/format.rs +++ b/mullvad-cli/src/format.rs @@ -162,10 +162,6 @@ fn error_state_to_string(error_state: &ErrorState) -> String { VpnPermissionDenied => "The Android VPN permission was denied when creating the tunnel", #[cfg(target_os = "windows")] SplitTunnelError => "The split tunneling module reported an error", - #[cfg(target_os = "macos")] - CustomResolverError => "Failed to start custom resolver", - #[cfg(target_os = "macos")] - ReadSystemDnsConfig => "Failed to read system DNS config", #[cfg(not(target_os = "android"))] _ => unreachable!("unknown error cause"), }; |
