diff options
Diffstat (limited to 'mullvad-cli/src/cmds')
| -rw-r--r-- | mullvad-cli/src/cmds/custom_dns.rs | 156 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/dns.rs | 136 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/mod.rs | 9 |
3 files changed, 140 insertions, 161 deletions
diff --git a/mullvad-cli/src/cmds/custom_dns.rs b/mullvad-cli/src/cmds/custom_dns.rs deleted file mode 100644 index 6213a8f6ea..0000000000 --- a/mullvad-cli/src/cmds/custom_dns.rs +++ /dev/null @@ -1,156 +0,0 @@ -use crate::{new_rpc_client, Command, Result}; -use clap::value_t_or_exit; -use mullvad_management_interface::types; - -pub struct CustomDns; - -#[mullvad_management_interface::async_trait] -impl Command for CustomDns { - fn name(&self) -> &'static str { - "custom-dns" - } - - fn clap_subcommand(&self) -> clap::App<'static, 'static> { - clap::SubCommand::with_name(self.name()) - .about("Configure custom DNS servers to use when connected") - .setting(clap::AppSettings::SubcommandRequiredElseHelp) - .subcommand( - clap::SubCommand::with_name("servers") - .about("Set custom DNS servers to use") - .setting(clap::AppSettings::SubcommandRequiredElseHelp) - .subcommand( - clap::SubCommand::with_name("set") - .about("Set custom DNS servers to use") - .arg( - clap::Arg::with_name("servers") - .multiple(true) - .help("One or more IP addresses pointing to DNS resolvers.") - .required(true), - ), - ) - .subcommand( - clap::SubCommand::with_name("clear").about("Remove all custom DNS servers"), - ), - ) - .subcommand( - clap::SubCommand::with_name("get").about("Display the current custom DNS settings"), - ) - .subcommand( - clap::SubCommand::with_name("set") - .about("Enable or disable custom DNS") - .arg( - clap::Arg::with_name("policy") - .required(true) - .possible_values(&["on", "off"]), - ), - ) - } - - async fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()> { - match matches.subcommand() { - ("servers", Some(matches)) => match matches.subcommand() { - ("set", Some(matches)) => { - self.set_servers(matches.values_of_lossy("servers")).await - } - ("clear", _) => self.clear_servers().await, - _ => unreachable!("No custom-dns server command given"), - }, - ("set", Some(matches)) => { - let policy = value_t_or_exit!(matches.value_of("policy"), String); - self.set_state(policy == "on").await - } - ("get", _) => self.get().await, - _ => unreachable!("No custom-dns command given"), - } - } -} - -impl CustomDns { - async fn set_state(&self, enabled: bool) -> Result<()> { - let mut rpc = new_rpc_client().await?; - let options = rpc - .get_settings(()) - .await? - .into_inner() - .tunnel_options - .unwrap() - .dns_options - .unwrap(); - rpc.set_dns_options(types::DnsOptions { - custom: enabled, - addresses: options.addresses, - }) - .await?; - println!("Updated custom DNS settings"); - Ok(()) - } - - async fn set_servers(&self, servers: Option<Vec<String>>) -> Result<()> { - let mut rpc = new_rpc_client().await?; - let options = rpc - .get_settings(()) - .await? - .into_inner() - .tunnel_options - .unwrap() - .dns_options - .unwrap(); - rpc.set_dns_options(types::DnsOptions { - custom: options.custom, - addresses: servers.unwrap_or_default(), - }) - .await?; - println!("Updated custom DNS settings"); - Ok(()) - } - - async fn clear_servers(&self) -> Result<()> { - let mut rpc = new_rpc_client().await?; - let options = rpc - .get_settings(()) - .await? - .into_inner() - .tunnel_options - .unwrap() - .dns_options - .unwrap(); - rpc.set_dns_options(types::DnsOptions { - custom: options.custom, - addresses: vec![], - }) - .await?; - println!("Cleared list of custom DNS servers"); - Ok(()) - } - - async fn get(&self) -> Result<()> { - let mut rpc = new_rpc_client().await?; - let options = rpc - .get_settings(()) - .await? - .into_inner() - .tunnel_options - .unwrap() - .dns_options - .unwrap(); - - let state = if options.custom { - "enabled" - } else { - "disabled" - }; - println!("Custom DNS: {}", state); - - match options.addresses.len() { - 0 => println!("No DNS servers are configured"), - _ => { - println!("Servers:"); - for server in &options.addresses { - println!("\t{}", server); - } - } - } - - Ok(()) - } -} diff --git a/mullvad-cli/src/cmds/dns.rs b/mullvad-cli/src/cmds/dns.rs new file mode 100644 index 0000000000..9ffa0ec09c --- /dev/null +++ b/mullvad-cli/src/cmds/dns.rs @@ -0,0 +1,136 @@ +use crate::{new_rpc_client, Command, Result}; +use mullvad_management_interface::types; +use mullvad_types::settings::{DnsOptions, DnsState}; +use std::convert::TryInto; + +pub struct Dns; + +#[mullvad_management_interface::async_trait] +impl Command for Dns { + fn name(&self) -> &'static str { + "dns" + } + + fn clap_subcommand(&self) -> clap::App<'static, 'static> { + 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( + 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<()> { + match matches.subcommand() { + ("set", Some(matches)) => match matches.subcommand() { + ("default", Some(matches)) => { + self.set_default( + matches.is_present("block ads"), + matches.is_present("block trackers"), + ) + .await + } + ("custom", Some(matches)) => { + self.set_custom(matches.values_of_lossy("servers")).await + } + _ => unreachable!("No custom-dns server command given"), + }, + ("get", _) => self.get().await, + _ => unreachable!("No custom-dns command given"), + } + } +} + +impl Dns { + async fn set_default(&self, block_ads: bool, block_trackers: bool) -> Result<()> { + let mut rpc = new_rpc_client().await?; + let settings = rpc.get_settings(()).await?.into_inner(); + rpc.set_dns_options(types::DnsOptions { + state: types::dns_options::DnsState::Default as i32, + default_options: Some(types::DefaultDnsOptions { + block_ads, + block_trackers, + }), + ..settings.tunnel_options.unwrap().dns_options.unwrap() + }) + .await?; + println!("Updated DNS settings"); + Ok(()) + } + + async fn set_custom(&self, servers: Option<Vec<String>>) -> Result<()> { + let mut rpc = new_rpc_client().await?; + let settings = rpc.get_settings(()).await?.into_inner(); + rpc.set_dns_options(types::DnsOptions { + state: types::dns_options::DnsState::Custom as i32, + custom_options: Some(types::CustomDnsOptions { + addresses: servers.unwrap_or_default(), + }), + ..settings.tunnel_options.unwrap().dns_options.unwrap() + }) + .await?; + println!("Updated DNS settings"); + Ok(()) + } + + async fn get(&self) -> Result<()> { + let mut rpc = new_rpc_client().await?; + let options: DnsOptions = rpc + .get_settings(()) + .await? + .into_inner() + .tunnel_options + .unwrap() + .dns_options + .unwrap() + .try_into() + .unwrap(); + + match options.state { + DnsState::Default => { + println!("Custom DNS: no"); + println!("Block ads: {}", options.default_options.block_ads); + println!("Block trackers: {}", options.default_options.block_trackers); + } + DnsState::Custom => { + println!("Custom DNS: yes\nServers:"); + for server in &options.custom_options.addresses { + println!("{}", server); + } + } + } + + Ok(()) + } +} diff --git a/mullvad-cli/src/cmds/mod.rs b/mullvad-cli/src/cmds/mod.rs index eb999a2936..bd7ca97372 100644 --- a/mullvad-cli/src/cmds/mod.rs +++ b/mullvad-cli/src/cmds/mod.rs @@ -22,12 +22,12 @@ pub use self::connect::Connect; mod disconnect; pub use self::disconnect::Disconnect; +mod dns; +pub use self::dns::Dns; + mod lan; pub use self::lan::Lan; -mod custom_dns; -pub use self::custom_dns::CustomDns; - mod reconnect; pub use self::reconnect::Reconnect; @@ -61,10 +61,9 @@ pub fn get_commands() -> HashMap<&'static str, Box<dyn Command>> { Box::new(Bridge), Box::new(Connect), Box::new(Disconnect), + Box::new(Dns), Box::new(Reconnect), Box::new(Lan), - #[cfg(not(target_os = "android"))] - Box::new(CustomDns), Box::new(Relay), Box::new(Reset), #[cfg(target_os = "linux")] |
