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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
use crate::{format::print_keygen_event, new_rpc_client, Command, Error, Result};
use clap::value_t;
use mullvad_management_interface::types::{self, Timestamp, TunnelOptions};
use mullvad_types::wireguard::DEFAULT_ROTATION_INTERVAL;
use std::{convert::TryFrom, time::Duration};
pub struct Tunnel;
#[mullvad_management_interface::async_trait]
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())
}
async fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()> {
match matches.subcommand() {
("openvpn", Some(openvpn_matches)) => Self::handle_openvpn_cmd(openvpn_matches).await,
("wireguard", Some(wg_matches)) => Self::handle_wireguard_cmd(wg_matches).await,
("ipv6", Some(ipv6_matches)) => Self::handle_ipv6_cmd(ipv6_matches).await,
_ => {
unreachable!("unhandled comand");
}
}
}
}
fn create_wireguard_subcommand() -> clap::App<'static, 'static> {
let subcmd = 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());
#[cfg(windows)]
{
subcmd.subcommand(create_wireguard_use_wg_nt_subcommand())
}
#[cfg(not(windows))]
{
subcmd
}
}
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())
}
#[cfg(windows)]
fn create_wireguard_use_wg_nt_subcommand() -> clap::App<'static, 'static> {
clap::SubCommand::with_name("use-wireguard-nt")
.about("Enable or disable wireguard-nt")
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.subcommand(clap::SubCommand::with_name("get"))
.subcommand(
clap::SubCommand::with_name("set").arg(
clap::Arg::with_name("policy")
.required(true)
.takes_value(true)
.possible_values(&["on", "off"]),
),
)
}
fn create_wireguard_keys_rotation_interval_subcommand() -> clap::App<'static, 'static> {
clap::SubCommand::with_name("rotation-interval")
.about("Manage automatic key rotation (given in hours)")
.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("policy")
.required(true)
.takes_value(true)
.possible_values(&["on", "off"]),
),
)
}
impl Tunnel {
async fn handle_openvpn_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
match matches.subcommand() {
("mssfix", Some(mssfix_matches)) => {
Self::handle_openvpn_mssfix_cmd(mssfix_matches).await
}
_ => unreachable!("unhandled command"),
}
}
async fn handle_openvpn_mssfix_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
match matches.subcommand() {
("get", Some(_)) => Self::process_openvpn_mssfix_get().await,
("unset", Some(_)) => Self::process_openvpn_mssfix_unset().await,
("set", Some(set_matches)) => Self::process_openvpn_mssfix_set(set_matches).await,
_ => unreachable!("unhandled command"),
}
}
async fn handle_wireguard_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
match matches.subcommand() {
("mtu", Some(matches)) => match matches.subcommand() {
("get", _) => Self::process_wireguard_mtu_get().await,
("set", Some(matches)) => Self::process_wireguard_mtu_set(matches).await,
("unset", _) => Self::process_wireguard_mtu_unset().await,
_ => unreachable!("unhandled command"),
},
("key", Some(matches)) => match matches.subcommand() {
("check", _) => Self::process_wireguard_key_check().await,
("regenerate", _) => Self::process_wireguard_key_generate().await,
("rotation-interval", Some(matches)) => match matches.subcommand() {
("get", _) => Self::process_wireguard_rotation_interval_get().await,
("set", Some(matches)) => {
Self::process_wireguard_rotation_interval_set(matches).await
}
("reset", _) => Self::process_wireguard_rotation_interval_reset().await,
_ => unreachable!("unhandled command"),
},
_ => unreachable!("unhandled command"),
},
#[cfg(windows)]
("use-wireguard-nt", Some(matches)) => match matches.subcommand() {
("get", _) => Self::process_wireguard_use_wg_nt_get().await,
("set", Some(matches)) => Self::process_wireguard_use_wg_nt_set(matches).await,
_ => unreachable!("unhandled command"),
},
_ => unreachable!("unhandled command"),
}
}
async fn process_wireguard_mtu_get() -> Result<()> {
let tunnel_options = Self::get_tunnel_options().await?;
let mtu = tunnel_options.wireguard.unwrap().mtu;
println!(
"mtu: {}",
if mtu != 0 {
mtu.to_string()
} else {
"unset".to_string()
},
);
Ok(())
}
async 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().await?;
rpc.set_wireguard_mtu(mtu as u32).await?;
println!("Wireguard MTU has been updated");
Ok(())
}
async fn process_wireguard_mtu_unset() -> Result<()> {
let mut rpc = new_rpc_client().await?;
rpc.set_wireguard_mtu(0).await?;
println!("Wireguard MTU has been unset");
Ok(())
}
#[cfg(windows)]
async fn process_wireguard_use_wg_nt_get() -> Result<()> {
let tunnel_options = Self::get_tunnel_options().await?;
if tunnel_options.wireguard.unwrap().use_wireguard_nt {
println!("enabled");
} else {
println!("disabled");
}
Ok(())
}
#[cfg(windows)]
async fn process_wireguard_use_wg_nt_set(matches: &clap::ArgMatches<'_>) -> Result<()> {
let new_state = matches.value_of("policy").unwrap() == "on";
let mut rpc = new_rpc_client().await?;
rpc.set_use_wireguard_nt(new_state).await?;
println!("Updated wireguard-nt setting");
Ok(())
}
async fn process_wireguard_key_check() -> Result<()> {
let mut rpc = new_rpc_client().await?;
let key = rpc.get_wireguard_key(()).await;
let key = match key {
Ok(response) => Some(response.into_inner()),
Err(status) => {
if status.code() == mullvad_management_interface::Code::NotFound {
None
} else {
return Err(Error::RpcFailedExt("Failed to obtain key", status));
}
}
};
if let Some(key) = key {
println!("Current key : {}", base64::encode(&key.key));
println!(
"Key created on : {}",
Self::format_key_timestamp(&key.created.unwrap())
);
} else {
println!("No key is set");
return Ok(());
}
let is_valid = rpc
.verify_wireguard_key(())
.await
.map_err(|error| Error::RpcFailedExt("Failed to verify key", error))?
.into_inner();
println!("Key is valid for use with current account: {}", is_valid);
Ok(())
}
async fn process_wireguard_key_generate() -> Result<()> {
let mut rpc = new_rpc_client().await?;
let keygen_event = rpc.generate_wireguard_key(()).await?;
print_keygen_event(&keygen_event.into_inner());
Ok(())
}
async fn process_wireguard_rotation_interval_get() -> Result<()> {
let tunnel_options = Self::get_tunnel_options().await?;
match tunnel_options.wireguard.unwrap().rotation_interval {
Some(interval) => {
let hours = duration_hours(&Duration::try_from(interval).unwrap());
println!("Rotation interval: {} hour(s)", hours);
}
None => println!(
"Rotation interval: default ({} hours)",
duration_hours(&DEFAULT_ROTATION_INTERVAL)
),
}
Ok(())
}
async fn process_wireguard_rotation_interval_set(matches: &clap::ArgMatches<'_>) -> Result<()> {
let rotate_interval =
value_t!(matches.value_of("interval"), u64).unwrap_or_else(|e| e.exit());
let mut rpc = new_rpc_client().await?;
rpc.set_wireguard_rotation_interval(types::Duration::from(Duration::from_secs(
60 * 60 * rotate_interval,
)))
.await?;
println!("Set key rotation interval: {} hour(s)", rotate_interval);
Ok(())
}
async fn process_wireguard_rotation_interval_reset() -> Result<()> {
let mut rpc = new_rpc_client().await?;
rpc.reset_wireguard_rotation_interval(()).await?;
println!(
"Set key rotation interval: default ({} hours)",
duration_hours(&DEFAULT_ROTATION_INTERVAL)
);
Ok(())
}
async fn handle_ipv6_cmd(matches: &clap::ArgMatches<'_>) -> Result<()> {
if matches.subcommand_matches("get").is_some() {
Self::process_ipv6_get().await
} else if let Some(m) = matches.subcommand_matches("set") {
Self::process_ipv6_set(m).await
} else {
unreachable!("unhandled command");
}
}
async fn process_openvpn_mssfix_get() -> Result<()> {
let tunnel_options = Self::get_tunnel_options().await?;
let mssfix = tunnel_options.openvpn.unwrap().mssfix;
println!(
"mssfix: {}",
if mssfix != 0 {
mssfix.to_string()
} else {
"unset".to_string()
},
);
Ok(())
}
async fn get_tunnel_options() -> Result<TunnelOptions> {
let mut rpc = new_rpc_client().await?;
Ok(rpc
.get_settings(())
.await?
.into_inner()
.tunnel_options
.unwrap())
}
async fn process_openvpn_mssfix_unset() -> Result<()> {
let mut rpc = new_rpc_client().await?;
rpc.set_openvpn_mssfix(0).await?;
println!("mssfix parameter has been unset");
Ok(())
}
async 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().await?;
rpc.set_openvpn_mssfix(new_value as u32).await?;
println!("mssfix parameter has been updated");
Ok(())
}
async fn process_ipv6_get() -> Result<()> {
let tunnel_options = Self::get_tunnel_options().await?;
println!(
"IPv6: {}",
if tunnel_options.generic.unwrap().enable_ipv6 {
"on"
} else {
"off"
}
);
Ok(())
}
async fn process_ipv6_set(matches: &clap::ArgMatches<'_>) -> Result<()> {
let enabled = matches.value_of("policy").unwrap() == "on";
let mut rpc = new_rpc_client().await?;
rpc.set_enable_ipv6(enabled).await?;
if enabled {
println!("Enabled IPv6");
} else {
println!("Disabled IPv6");
}
Ok(())
}
fn format_key_timestamp(timestamp: &Timestamp) -> String {
let ndt = chrono::NaiveDateTime::from_timestamp(timestamp.seconds, timestamp.nanos as u32);
let utc = chrono::DateTime::<chrono::Utc>::from_utc(ndt, chrono::Utc);
utc.with_timezone(&chrono::Local).to_string()
}
}
fn duration_hours(duration: &Duration) -> u64 {
duration.as_secs() / 60 / 60
}
|