summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/dns.rs
blob: 90ae609ae45dbc4c8d00ddbd0b35b29e6e3f21d0 (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
use anyhow::Result;
use clap::Subcommand;
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::settings::{CustomDnsOptions, DefaultDnsOptions, DnsOptions, DnsState};
use std::net::IpAddr;

#[derive(Subcommand, Debug)]
pub enum Dns {
    /// Display the current DNS settings
    Get,

    /// Set DNS servers to use
    Set {
        #[clap(subcommand)]
        cmd: DnsSet,
    },
}

#[derive(Subcommand, Debug, Clone)]
pub enum DnsSet {
    /// Use a default DNS server, with or without content
    /// blocking.
    Default {
        /// Block domains known to be used for ads
        #[arg(long)]
        block_ads: bool,

        /// Block domains known to be used for tracking
        #[arg(long)]
        block_trackers: bool,

        /// Block domains known to be used by malware
        #[arg(long)]
        block_malware: bool,

        /// Block domains known to be used for adult content
        #[arg(long)]
        block_adult_content: bool,

        /// Block domains known to be used for gambling
        #[arg(long)]
        block_gambling: bool,

        /// Block domains related to social media
        #[arg(long)]
        block_social_media: bool,
    },

    /// Set a list of custom DNS servers
    Custom {
        /// One or more IP addresses pointing to DNS resolvers
        #[arg(required(true), num_args = 1..)]
        servers: Vec<IpAddr>,
    },
}

impl Dns {
    pub async fn handle(self) -> Result<()> {
        match self {
            Dns::Get => Self::get().await,
            Dns::Set {
                cmd:
                    DnsSet::Default {
                        block_ads,
                        block_trackers,
                        block_malware,
                        block_adult_content,
                        block_gambling,
                        block_social_media,
                    },
            } => {
                Self::set_default(
                    block_ads,
                    block_trackers,
                    block_malware,
                    block_adult_content,
                    block_gambling,
                    block_social_media,
                )
                .await
            }
            Dns::Set {
                cmd: DnsSet::Custom { servers },
            } => Self::set_custom(servers).await,
        }
    }

    async fn get() -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let options = rpc.get_settings().await?.tunnel_options.dns_options;

        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);
                println!(
                    "Block social media: {}",
                    options.default_options.block_social_media
                );
            }
            DnsState::Custom => {
                println!("Custom DNS: yes\nServers:");
                for server in &options.custom_options.addresses {
                    println!("{server}");
                }
            }
        }

        Ok(())
    }

    async fn set_default(
        block_ads: bool,
        block_trackers: bool,
        block_malware: bool,
        block_adult_content: bool,
        block_gambling: bool,
        block_social_media: bool,
    ) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let settings = rpc.get_settings().await?;
        rpc.set_dns_options(DnsOptions {
            state: DnsState::Default,
            default_options: DefaultDnsOptions {
                block_ads,
                block_trackers,
                block_malware,
                block_adult_content,
                block_gambling,
                block_social_media,
            },
            ..settings.tunnel_options.dns_options
        })
        .await?;
        println!("Updated DNS settings");
        Ok(())
    }

    async fn set_custom(servers: Vec<IpAddr>) -> Result<()> {
        let mut rpc = MullvadProxyClient::new().await?;
        let settings = rpc.get_settings().await?;
        rpc.set_dns_options(DnsOptions {
            state: DnsState::Custom,
            custom_options: CustomDnsOptions { addresses: servers },
            ..settings.tunnel_options.dns_options
        })
        .await?;
        println!("Updated DNS settings");
        Ok(())
    }
}