summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/dns.rs
blob: 300e1c395518bad9ffae81589b406fc1dea8144b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::{new_rpc_client, Command, Result};
use mullvad_management_interface::types;
use mullvad_types::settings::{DnsOptions, DnsState};
use std::{convert::TryInto, net::IpAddr};

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> {
        clap::App::new(self.name())
            .about("Configure DNS servers to use when connected")
            .setting(clap::AppSettings::SubcommandRequiredElseHelp)
            .subcommand(clap::App::new("get").about("Display the current DNS settings"))
            .subcommand(
                clap::App::new("set")
                    .about("Set DNS servers to use")
                    .setting(clap::AppSettings::SubcommandRequiredElseHelp)
                    .subcommand(
                        clap::App::new("default")
                            .about("Use default DNS servers")
                            .arg(
                                clap::Arg::new("block ads")
                                    .long("block-ads")
                                    .takes_value(false)
                                    .help("Block domain names used for ads"),
                            )
                            .arg(
                                clap::Arg::new("block trackers")
                                    .long("block-trackers")
                                    .takes_value(false)
                                    .help("Block domain names used for tracking"),
                            )
                            .arg(
                                clap::Arg::new("block malware")
                                    .long("block-malware")
                                    .takes_value(false)
                                    .help("Block domains known to be used by malware"),
                            )
                            .arg(
                                clap::Arg::new("block adult content")
                                    .long("block-adult-content")
                                    .takes_value(false)
                                    .help("Block domains known to be used for adult content"),
                            )
                            .arg(
                                clap::Arg::new("block gambling")
                                    .long("block-gambling")
                                    .takes_value(false)
                                    .help("Block domains known to be used for gambling"),
                            ),
                    )
                    .subcommand(
                        clap::App::new("custom")
                            .about("Set a list of custom DNS servers")
                            .arg(
                                clap::Arg::new("servers")
                                    .multiple_occurrences(true)
                                    .help("One or more IP addresses pointing to DNS resolvers.")
                                    .required(true),
                            ),
                    ),
            )
    }

    async fn run(&self, matches: &clap::ArgMatches) -> Result<()> {
        match matches.subcommand() {
            Some(("set", matches)) => match matches.subcommand() {
                Some(("default", matches)) => {
                    self.set_default(
                        matches.is_present("block ads"),
                        matches.is_present("block trackers"),
                        matches.is_present("block malware"),
                        matches.is_present("block adult content"),
                        matches.is_present("block gambling"),
                    )
                    .await
                }
                Some(("custom", matches)) => {
                    let servers = match matches.values_of_t::<IpAddr>("servers") {
                        Ok(servers) => Some(servers),
                        Err(e) => match e.kind {
                            clap::ErrorKind::ArgumentNotFound => None,
                            _ => e.exit(),
                        },
                    };
                    self.set_custom(servers).await
                }
                _ => unreachable!("No custom-dns server command given"),
            },
            Some(("get", _)) => self.get().await,
            _ => unreachable!("No custom-dns command given"),
        }
    }
}

impl Dns {
    async fn set_default(
        &self,
        block_ads: bool,
        block_trackers: bool,
        block_malware: bool,
        block_adult_content: bool,
        block_gambling: 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,
                block_malware,
                block_adult_content,
                block_gambling,
            }),
            ..settings.tunnel_options.unwrap().dns_options.unwrap()
        })
        .await?;
        println!("Updated DNS settings");
        Ok(())
    }

    async fn set_custom(&self, servers: Option<Vec<IpAddr>>) -> 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()
                    .into_iter()
                    .map(|a| a.to_string())
                    .collect(),
            }),
            ..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);
                println!("Block malware: {}", options.default_options.block_malware);
                println!(
                    "Block adult content: {}",
                    options.default_options.block_adult_content
                );
                println!("Block gambling: {}", options.default_options.block_gambling);
            }
            DnsState::Custom => {
                println!("Custom DNS: yes\nServers:");
                for server in &options.custom_options.addresses {
                    println!("{}", server);
                }
            }
        }

        Ok(())
    }
}