summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/tunnel.rs
blob: 5349174d4cc6638551a0e3ce2ad0cb8884b1ef2e (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::{new_rpc_client, Command, Result};
use clap::value_t;

use mullvad_types::settings::TunnelOptions;

pub struct Tunnel;

impl Command for Tunnel {
    fn name(&self) -> &'static str {
        "tunnel"
    }

    fn clap_subcommand(&self) -> clap::App<'static, 'static> {
        clap::SubCommand::with_name(self.name())
            .about("Manage tunnel specific options")
            .setting(clap::AppSettings::SubcommandRequiredElseHelp)
            .subcommand(create_openvpn_subcommand())
            .subcommand(create_wireguard_subcommand())
            .subcommand(create_ipv6_subcommand())
    }

    fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()> {
        match matches.subcommand() {
            ("openvpn", Some(openvpn_matches)) => Self::handle_openvpn_cmd(openvpn_matches),
            ("wireguard", Some(wg_matches)) => Self::handle_wireguard_cmd(wg_matches),
            ("ipv6", Some(ipv6_matches)) => Self::handle_ipv6_cmd(ipv6_matches),
            _ => {
                unreachable!("unhandled comand");
            }
        }
    }
}

fn create_wireguard_subcommand() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("wireguard")
        .about("Manage options for Wireguard tunnels")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(create_wireguard_mtu_subcommand())
        .subcommand(create_wireguard_keys_subcommand())
}

fn create_wireguard_mtu_subcommand() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("mtu")
        .about("Configure the MTU of the wireguard tunnel")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(clap::SubCommand::with_name("get"))
        .subcommand(clap::SubCommand::with_name("unset"))
        .subcommand(
            clap::SubCommand::with_name("set").arg(clap::Arg::with_name("mtu").required(true)),
        )
}

fn create_wireguard_keys_subcommand() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("key")
        .about("Manage your wireguard key")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(clap::SubCommand::with_name("check"))
        .subcommand(clap::SubCommand::with_name("regenerate"))
        .subcommand(create_wireguard_keys_rotation_interval_subcommand())
}

fn create_wireguard_keys_rotation_interval_subcommand() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("rotation-interval")
        .about("Manage automatic key rotation (specified in hours; 0 = disabled)")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(clap::SubCommand::with_name("get"))
        .subcommand(clap::SubCommand::with_name("reset").about("Use the default rotation interval"))
        .subcommand(
            clap::SubCommand::with_name("set").arg(clap::Arg::with_name("interval").required(true)),
        )
}


fn create_openvpn_subcommand() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("openvpn")
        .about("Manage options for OpenVPN tunnels")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(create_openvpn_mssfix_subcommand())
}

fn create_openvpn_mssfix_subcommand() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("mssfix")
        .about("Configure the optional mssfix parameter")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(clap::SubCommand::with_name("get"))
        .subcommand(clap::SubCommand::with_name("unset"))
        .subcommand(
            clap::SubCommand::with_name("set").arg(clap::Arg::with_name("mssfix").required(true)),
        )
}

fn create_ipv6_subcommand() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("ipv6")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(clap::SubCommand::with_name("get"))
        .subcommand(
            clap::SubCommand::with_name("set").arg(
                clap::Arg::with_name("enable")
                    .required(true)
                    .takes_value(true)
                    .possible_values(&["on", "off"]),
            ),
        )
}

impl Tunnel {
    fn handle_openvpn_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
        match matches.subcommand() {
            ("mssfix", Some(mssfix_matches)) => Self::handle_openvpn_mssfix_cmd(mssfix_matches),
            _ => unreachable!("unhandled command"),
        }
    }

    fn handle_openvpn_mssfix_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
        match matches.subcommand() {
            ("get", Some(_)) => Self::process_openvpn_mssfix_get(),
            ("unset", Some(_)) => Self::process_openvpn_mssfix_unset(),
            ("set", Some(set_matches)) => Self::process_openvpn_mssfix_set(set_matches),
            _ => unreachable!("unhandled command"),
        }
    }

    fn handle_wireguard_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
        match matches.subcommand() {
            ("mtu", Some(matches)) => match matches.subcommand() {
                ("get", _) => Self::process_wireguard_mtu_get(),
                ("set", Some(matches)) => Self::process_wireguard_mtu_set(matches),
                ("unset", _) => Self::process_wireguard_mtu_unset(),
                _ => unreachable!("unhandled command"),
            },

            ("key", Some(matches)) => match matches.subcommand() {
                ("check", _) => Self::process_wireguard_key_check(),
                ("regenerate", _) => Self::process_wireguard_key_generate(),
                ("rotation-interval", Some(matches)) => match matches.subcommand() {
                    ("get", _) => Self::process_wireguard_rotation_interval_get(),
                    ("set", Some(matches)) => {
                        Self::process_wireguard_rotation_interval_set(matches)
                    }
                    ("reset", _) => Self::process_wireguard_rotation_interval_reset(),
                    _ => unreachable!("unhandled command"),
                },
                _ => unreachable!("unhandled command"),
            },

            _ => unreachable!("unhandled command"),
        }
    }

    fn process_wireguard_mtu_get() -> Result<()> {
        let tunnel_options = Self::get_tunnel_options()?;
        println!(
            "mtu: {}",
            tunnel_options
                .wireguard
                .mtu
                .map(|mtu| mtu.to_string())
                .unwrap_or_else(|| "unset".to_owned())
        );
        Ok(())
    }

    fn process_wireguard_mtu_set(matches: &clap::ArgMatches<'_>) -> Result<()> {
        let mtu = value_t!(matches.value_of("mtu"), u16).unwrap_or_else(|e| e.exit());
        let mut rpc = new_rpc_client()?;
        rpc.set_wireguard_mtu(Some(mtu))?;
        println!("Wireguard MTU has been updated");
        Ok(())
    }

    fn process_wireguard_mtu_unset() -> Result<()> {
        let mut rpc = new_rpc_client()?;
        rpc.set_wireguard_mtu(None)?;
        println!("Wireguard MTU has been unset");
        Ok(())
    }

    fn process_wireguard_key_check() -> Result<()> {
        let mut rpc = new_rpc_client()?;
        match rpc.get_wireguard_key()? {
            Some(key) => {
                println!("Current key    : {}", &key.key);
                println!(
                    "Key created on : {}",
                    &key.created.with_timezone(&chrono::offset::Local)
                );
            }
            None => {
                println!("No key is set");
                return Ok(());
            }
        };

        let is_valid = rpc.verify_wireguard_key()?;
        println!("Key is valid for use with current account: {}", is_valid);
        Ok(())
    }

    fn process_wireguard_key_generate() -> Result<()> {
        let mut rpc = new_rpc_client()?;
        let result = rpc
            .generate_wireguard_key()
            .map_err(|e| crate::Error::RpcClientError(e))?;
        println!("{}", result);
        Ok(())
    }

    fn process_wireguard_rotation_interval_get() -> Result<()> {
        let tunnel_options = Self::get_tunnel_options()?;
        println!(
            "Rotation interval: {} hour(s)",
            tunnel_options
                .wireguard
                .automatic_rotation
                .map(|interval| interval.to_string())
                .unwrap_or_else(|| "default".to_owned())
        );
        Ok(())
    }

    fn process_wireguard_rotation_interval_set(matches: &clap::ArgMatches<'_>) -> Result<()> {
        let rotate_interval =
            value_t!(matches.value_of("interval"), u32).unwrap_or_else(|e| e.exit());
        let mut rpc = new_rpc_client()?;
        rpc.set_wireguard_rotation_interval(Some(rotate_interval))?;
        println!("Set key rotation interval: {} hour(s)", rotate_interval);
        Ok(())
    }

    fn process_wireguard_rotation_interval_reset() -> Result<()> {
        let mut rpc = new_rpc_client()?;
        rpc.set_wireguard_rotation_interval(None)?;
        println!("Set key rotation interval: default");
        Ok(())
    }

    fn handle_ipv6_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
        if matches.subcommand_matches("get").is_some() {
            Self::process_ipv6_get()
        } else if let Some(m) = matches.subcommand_matches("set") {
            Self::process_ipv6_set(m)
        } else {
            unreachable!("unhandled command");
        }
    }

    fn process_openvpn_mssfix_get() -> Result<()> {
        let tunnel_options = Self::get_tunnel_options()?;
        println!(
            "mssfix: {}",
            tunnel_options
                .openvpn
                .mssfix
                .map_or_else(|| "unset".to_owned(), |v| v.to_string())
        );
        Ok(())
    }

    fn get_tunnel_options() -> Result<TunnelOptions> {
        let mut rpc = new_rpc_client()?;
        Ok(rpc.get_settings()?.tunnel_options)
    }

    fn process_openvpn_mssfix_unset() -> Result<()> {
        let mut rpc = new_rpc_client()?;
        rpc.set_openvpn_mssfix(None)?;
        println!("mssfix parameter has been unset");
        Ok(())
    }

    fn process_openvpn_mssfix_set(matches: &clap::ArgMatches<'_>) -> Result<()> {
        let new_value = value_t!(matches.value_of("mssfix"), u16).unwrap_or_else(|e| e.exit());
        let mut rpc = new_rpc_client()?;
        rpc.set_openvpn_mssfix(Some(new_value))?;
        println!("mssfix parameter has been updated");
        Ok(())
    }

    fn process_ipv6_get() -> Result<()> {
        let tunnel_options = Self::get_tunnel_options()?;
        println!(
            "IPv6: {}",
            if tunnel_options.generic.enable_ipv6 {
                "on"
            } else {
                "off"
            }
        );
        Ok(())
    }

    fn process_ipv6_set(matches: &clap::ArgMatches<'_>) -> Result<()> {
        let enabled = matches.value_of("enable").unwrap() == "on";

        let mut rpc = new_rpc_client()?;
        rpc.set_enable_ipv6(enabled)?;
        println!("IPv6 setting has been updated");
        Ok(())
    }
}