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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
#[cfg(not(target_os = "android"))]
use futures::TryFutureExt;
use mullvad_types::{
relay_constraints::{RelayConstraints, RelaySettings, WireguardConstraints},
settings::{DnsState, Settings},
};
use std::{
fmt::{self, Display},
ops::Deref,
path::{Path, PathBuf},
};
use talpid_core::firewall::is_local_address;
use talpid_types::ErrorExt;
use tokio::{
fs,
io::{self, AsyncWriteExt},
};
pub mod patch;
const SETTINGS_FILE: &str = "settings.json";
#[derive(err_derive::Error, Debug)]
#[error(no_from)]
pub enum Error {
#[error(display = "Unable to read settings file {}", _0)]
ReadError(String, #[error(source)] io::Error),
#[error(display = "Unable to parse settings file")]
ParseError(#[error(source)] serde_json::Error),
#[error(display = "Unable to remove settings file {}", _0)]
#[cfg(not(target_os = "android"))]
DeleteError(String, #[error(source)] io::Error),
#[error(display = "Unable to serialize settings to JSON")]
SerializeError(#[error(source)] serde_json::Error),
#[error(display = "Unable to write settings to {}", _0)]
WriteError(String, #[error(source)] io::Error),
}
/// Converts an [Error] to a management interface status
#[cfg(not(target_os = "android"))]
impl From<Error> for mullvad_management_interface::Status {
fn from(error: Error) -> mullvad_management_interface::Status {
use mullvad_management_interface::{Code, Status};
match error {
Error::DeleteError(..) | Error::WriteError(..) | Error::ReadError(..) => {
Status::new(Code::FailedPrecondition, error.to_string())
}
Error::SerializeError(..) | Error::ParseError(..) => {
Status::new(Code::Internal, error.to_string())
}
}
}
}
pub struct SettingsPersister {
settings: Settings,
path: PathBuf,
#[allow(clippy::type_complexity)]
on_change_listeners: Vec<Box<dyn Fn(&Settings)>>,
}
pub type MadeChanges = bool;
impl SettingsPersister {
/// Loads user settings from file. If it fails, it returns the defaults.
pub async fn load(settings_dir: &Path) -> Self {
let path = settings_dir.join(SETTINGS_FILE);
let LoadSettingsResult {
mut settings,
mut should_save,
} = Self::load_inner(|| Self::load_from_file(&path)).await;
// Force IPv6 to be enabled on Android
if cfg!(target_os = "android") {
should_save |= !settings.tunnel_options.generic.enable_ipv6;
settings.tunnel_options.generic.enable_ipv6 = true;
}
if crate::version::is_beta_version() {
should_save |= !settings.show_beta_releases;
settings.show_beta_releases = true;
}
let mut persister = SettingsPersister {
settings,
path,
on_change_listeners: vec![],
};
if should_save {
if let Err(error) = persister.save().await {
log::error!(
"{}",
error.display_chain_with_msg("Failed to save updated settings")
);
}
}
persister
}
/// Loads user settings, returning default settings if it should fail.
///
/// `load_settings` allows the caller to decide how to load [`Settings`]
/// from an bitrary resource.
///
/// `load_inner` will always succeed, even in the presence of IO operations.
/// Errors are handled gracefully by returning the default [`Settings`] if
/// necessary.
async fn load_inner<F, R>(load_settings: F) -> LoadSettingsResult
where
F: FnOnce() -> R,
R: std::future::Future<Output = Result<Settings, Error>>,
{
match load_settings().await {
Ok(settings) => LoadSettingsResult {
settings,
should_save: false,
},
Err(Error::ReadError(_, err)) if err.kind() == io::ErrorKind::NotFound => {
log::info!("No settings were found. Using defaults.");
LoadSettingsResult {
settings: Self::default_settings(),
should_save: true,
}
}
Err(error) => {
log::warn!(
"{}",
error.display_chain_with_msg("Failed to load settings. Using defaults.")
);
let mut settings = Self::default_settings();
// Protect the user by blocking the internet by default. Previous settings may
// not have caused the daemon to enter the non-blocking disconnected state.
settings.block_when_disconnected = true;
LoadSettingsResult {
settings,
should_save: true,
}
}
}
}
async fn load_from_file<P>(path: P) -> Result<Settings, Error>
where
P: AsRef<Path> + Clone,
{
let display = path.clone();
log::info!("Loading settings from {}", display.as_ref().display());
let settings_bytes = fs::read(path)
.await
.map_err(|error| Error::ReadError(display.as_ref().display().to_string(), error))?;
let settings = Self::load_from_bytes(&settings_bytes)?;
Ok(settings)
}
fn load_from_bytes(bytes: &[u8]) -> Result<Settings, Error> {
serde_json::from_slice(bytes).map_err(Error::ParseError)
}
async fn save(&mut self) -> Result<(), Error> {
Self::save_inner(&self.path, &self.settings).await
}
/// Serializes the settings and saves them to the given file.
async fn save_inner(path: &Path, settings: &Settings) -> Result<(), Error> {
log::debug!("Writing settings to {}", path.display());
let buffer = serde_json::to_string_pretty(settings).map_err(Error::SerializeError)?;
let mut file = mullvad_fs::AtomicFile::new(path)
.await
.map_err(|e| Error::WriteError(path.display().to_string(), e))?;
file.write_all(&buffer.into_bytes())
.await
.map_err(|e| Error::WriteError(path.display().to_string(), e))?;
file.finalize()
.await
.map_err(|e| Error::WriteError(path.display().to_string(), e))?;
Ok(())
}
/// Resets default settings
#[cfg(not(target_os = "android"))]
pub async fn reset(&mut self) -> Result<(), Error> {
self.settings = Self::default_settings();
let path = self.path.clone();
self.save()
.or_else(|e| async move {
log::error!(
"{}",
e.display_chain_with_msg("Unable to save default settings")
);
log::info!("Will attempt to remove settings file");
fs::remove_file(&path)
.map_err(|e| Error::DeleteError(path.display().to_string(), e))
.await
})
.await?;
self.notify_listeners();
Ok(())
}
pub fn to_settings(&self) -> Settings {
self.settings.clone()
}
/// Modifies `Settings::default()` somewhat, e.g. depending on whether a beta version
/// is being run or not.
fn default_settings() -> Settings {
let mut settings = Settings::default();
if crate::version::is_beta_version() {
settings.show_beta_releases = true;
}
settings
}
/// Edit the settings in a closure, and write the changes, if any, to disk.
///
/// On success, the function returns a boolean indicating whether any settings were changed.
/// If the settings could not be written to disk, all changes are rolled back, and an error is
/// returned.
pub async fn update(
&mut self,
update_fn: impl FnOnce(&mut Settings),
) -> Result<MadeChanges, Error> {
let mut new_settings = self.settings.clone();
update_fn(&mut new_settings);
if self.settings == new_settings {
return Ok(false);
}
Self::save_inner(&self.path, &new_settings).await?;
self.settings = new_settings;
self.notify_listeners();
Ok(true)
}
/// Return a compact summary of important settings
pub fn summary(&self) -> SettingsSummary<'_> {
SettingsSummary {
settings: &self.settings,
}
}
pub fn register_change_listener(&mut self, change_listener: impl Fn(&Settings) + 'static) {
self.on_change_listeners.push(Box::new(change_listener));
}
fn notify_listeners(&self) {
for listener in &self.on_change_listeners {
listener(&self.settings);
}
}
}
struct LoadSettingsResult {
settings: Settings,
should_save: bool,
}
impl Deref for SettingsPersister {
type Target = Settings;
fn deref(&self) -> &Self::Target {
&self.settings
}
}
/// A compact summary of important settings
pub struct SettingsSummary<'a> {
settings: &'a Settings,
}
impl<'a> Display for SettingsSummary<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bool_to_label = |state| {
if state {
"on"
} else {
"off"
}
};
let relay_settings = self.settings.get_relay_settings();
write!(f, "openvpn mssfix: ")?;
Self::fmt_option(f, self.settings.tunnel_options.openvpn.mssfix)?;
write!(f, ", wg mtu: ")?;
Self::fmt_option(f, self.settings.tunnel_options.wireguard.mtu)?;
if let RelaySettings::Normal(RelayConstraints {
wireguard_constraints: WireguardConstraints { ip_version, .. },
..
}) = relay_settings
{
write!(f, ", wg ip version: {ip_version}")?;
}
let multihop = matches!(
relay_settings,
RelaySettings::Normal(RelayConstraints {
wireguard_constraints: WireguardConstraints {
use_multihop: true,
..
},
..
})
);
write!(
f,
", multihop: {}, ipv6 (tun): {}, lan: {}, pq: {}, obfs: {}",
bool_to_label(multihop),
bool_to_label(self.settings.tunnel_options.generic.enable_ipv6),
bool_to_label(self.settings.allow_lan),
self.settings.tunnel_options.wireguard.quantum_resistant,
self.settings.obfuscation_settings.selected_obfuscation,
)?;
// Print DNS options
write!(f, ", dns: ")?;
match self.settings.tunnel_options.dns_options.state {
DnsState::Default => {
let mut content = vec![];
let default_options = &self.settings.tunnel_options.dns_options.default_options;
if default_options.block_ads {
content.push("ads");
}
if default_options.block_trackers {
content.push("trackers");
}
if default_options.block_malware {
content.push("malware");
}
if default_options.block_adult_content {
content.push("adult");
}
if default_options.block_gambling {
content.push("gambling");
}
if default_options.block_social_media {
content.push("social media");
}
if content.is_empty() {
content.push("default");
}
write!(f, "{}", content.join(" "))?;
}
DnsState::Custom => {
// NOTE: Technically inaccurate, as the gateway IP is a local IP but isn't treated
// as one.
let contains_local = self
.settings
.tunnel_options
.dns_options
.custom_options
.addresses
.iter()
.any(is_local_address);
let contains_public = self
.settings
.tunnel_options
.dns_options
.custom_options
.addresses
.iter()
.any(|addr| !is_local_address(addr));
match (contains_public, contains_local) {
(true, true) => f.write_str("custom, public, local")?,
(true, false) => f.write_str("custom, public")?,
(false, false) => f.write_str("custom, no addrs")?,
(false, true) => f.write_str("custom, local")?,
}
}
}
Ok(())
}
}
impl<'a> SettingsSummary<'a> {
fn fmt_option<T: Display>(f: &mut fmt::Formatter<'_>, val: Option<T>) -> fmt::Result {
if let Some(inner) = &val {
inner.fmt(f)
} else {
f.write_str("unset")
}
}
}
#[cfg(test)]
mod test {
use super::*;
use mullvad_types::settings::SettingsVersion;
use serde_json;
#[test]
#[should_panic]
fn test_deserialization_failure_version_too_small() {
let _version: SettingsVersion = serde_json::from_str("1").expect("Version too small");
}
#[test]
#[should_panic]
fn test_deserialization_failure_version_too_big() {
let _version: SettingsVersion = serde_json::from_str("1000").expect("Version too big");
}
#[test]
fn test_deserialization_success() {
let _version: SettingsVersion =
serde_json::from_str("2").expect("Failed to deserialize valid version");
}
#[test]
fn test_serialization_success() {
let version = SettingsVersion::V2;
let s = serde_json::to_string(&version).expect("Failed to serialize");
assert_eq!(s, "2");
}
#[test]
fn test_deserialization() {
let settings = br#"{
"account_token": "0000000000000000",
"relay_settings": {
"normal": {
"location": {
"only": {
"location": {
"country": "gb"
}
}
},
"tunnel_protocol": {
"only": "wireguard"
},
"wireguard_constraints": {
"port": "any"
},
"openvpn_constraints": {
"port": "any",
"protocol": "any"
}
}
},
"bridge_settings": {
"normal": {
"location": "any"
}
},
"bridge_state": "auto",
"allow_lan": true,
"block_when_disconnected": false,
"auto_connect": true,
"tunnel_options": {
"openvpn": {
"mssfix": null
},
"wireguard": {
"mtu": null,
"rotation_interval": null
},
"generic": {
"enable_ipv6": true
}
},
"settings_version": 5,
"show_beta_releases": false,
"custom_lists": {
"custom_lists": []
}
}"#;
let _ = SettingsPersister::load_from_bytes(settings).unwrap();
}
/// The [`SettingsPersister`] should always succeed when deserializing a
/// [`Settings`] object from disk. However, there is a distinction between
/// different error cases.
///
/// If the settings file is missing, it could be because the user starts the
/// app for the first time. As such, we should simply save the default
/// [`Settings`] to disk.
#[tokio::test]
async fn test_deserialize_missing_settings() {
let LoadSettingsResult {
should_save,
settings,
} = SettingsPersister::load_inner(|| async {
Err(Error::ReadError(
"Settings are missing".to_string(),
io::ErrorKind::NotFound.into(),
))
})
.await;
assert!(
should_save,
"Settings should be saved to disk if they didn't exist previously"
);
assert!(
settings.block_when_disconnected == false,
"The daemon should not block the internet if settings are missing"
);
}
/// The [`SettingsPersister`] should always succeed when deserializing a
/// [`Settings`] object from disk. However, there is a distinction between
/// different error cases.
///
/// If the settings file is corrupt, we can assume that the user has started
/// the app previously, but we can't know what settings the user have
/// changed. In this case, we should safeguard against leaks by locking down
/// the network before the user initiates a connection attempt or change
/// these settings.
#[tokio::test]
async fn test_deserialize_invalid_settings() {
let LoadSettingsResult {
should_save,
settings,
} = SettingsPersister::load_inner(|| async {
SettingsPersister::load_from_bytes(b"Not a valid settings file")
})
.await;
assert!(
should_save,
"Settings should be saved to disk if they have become corrupt"
);
assert!(
settings.block_when_disconnected,
"The daemon should block the internet if settings are corrupt"
);
}
}
|