-
Notifications
You must be signed in to change notification settings - Fork 328
/
mod.rs
293 lines (267 loc) · 10.2 KB
/
mod.rs
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
use crate::{
access_method,
constraints::Constraint,
custom_list::CustomListsSettings,
relay_constraints::{
BridgeSettings, BridgeState, GeographicLocationConstraint, LocationConstraint,
ObfuscationSettings, RelayConstraints, RelayOverride, RelaySettings,
RelaySettingsFormatter, SelectedObfuscation, WireguardConstraints,
},
wireguard,
};
#[cfg(target_os = "android")]
use jnix::IntoJava;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(any(windows, target_os = "android"))]
use std::collections::HashSet;
use talpid_types::net::{openvpn, GenericTunnelOptions};
mod dns;
/// The version used by the current version of the code. Should always be the
/// latest version that exists in `SettingsVersion`.
/// This should be bumped when a new version is introduced along with a migration
/// being added to `mullvad-daemon`.
pub const CURRENT_SETTINGS_VERSION: SettingsVersion = SettingsVersion::V9;
#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Copy)]
#[repr(u32)]
pub enum SettingsVersion {
V2 = 2,
V3 = 3,
V4 = 4,
V5 = 5,
V6 = 6,
V7 = 7,
V8 = 8,
V9 = 9,
}
impl<'de> Deserialize<'de> for SettingsVersion {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
match <u32>::deserialize(deserializer)? {
v if v == SettingsVersion::V2 as u32 => Ok(SettingsVersion::V2),
v if v == SettingsVersion::V3 as u32 => Ok(SettingsVersion::V3),
v if v == SettingsVersion::V4 as u32 => Ok(SettingsVersion::V4),
v if v == SettingsVersion::V5 as u32 => Ok(SettingsVersion::V5),
v if v == SettingsVersion::V6 as u32 => Ok(SettingsVersion::V6),
v if v == SettingsVersion::V7 as u32 => Ok(SettingsVersion::V7),
v if v == SettingsVersion::V8 as u32 => Ok(SettingsVersion::V8),
v if v == SettingsVersion::V9 as u32 => Ok(SettingsVersion::V9),
v => Err(serde::de::Error::custom(format!(
"{v} is not a valid SettingsVersion"
))),
}
}
}
impl Serialize for SettingsVersion {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u32(*self as u32)
}
}
/// Mullvad daemon settings.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(default)]
pub struct Settings {
pub relay_settings: RelaySettings,
pub bridge_settings: BridgeSettings,
pub obfuscation_settings: ObfuscationSettings,
pub bridge_state: BridgeState,
/// All of the custom relay lists
pub custom_lists: CustomListsSettings,
/// API access methods
pub api_access_methods: access_method::Settings,
/// If the daemon should allow communication with private (LAN) networks.
pub allow_lan: bool,
/// Extra level of kill switch. When this setting is on, the disconnected state will block
/// the firewall to not allow any traffic in or out.
pub block_when_disconnected: bool,
/// If the daemon should connect the VPN tunnel directly on start or not.
pub auto_connect: bool,
/// Options that should be applied to tunnels of a specific type regardless of where the relays
/// might be located.
pub tunnel_options: TunnelOptions,
/// Overrides for relays
pub relay_overrides: Vec<RelayOverride>,
/// Whether to notify users of beta updates.
pub show_beta_releases: bool,
/// Split tunneling settings
#[cfg(any(windows, target_os = "android"))]
pub split_tunnel: SplitTunnelSettings,
/// Specifies settings schema version
pub settings_version: SettingsVersion,
}
#[cfg(any(windows, target_os = "android"))]
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq)]
pub struct SplitTunnelSettings {
/// Toggles split tunneling on or off
pub enable_exclusions: bool,
/// Set of applications to exclude from the tunnel.
pub apps: HashSet<SplitApp>,
}
/// An application whose traffic should be excluded from any active tunnel.
#[cfg(target_os = "android")]
#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct SplitApp(String);
/// An application whose traffic should be excluded from any active tunnel.
#[cfg(windows)]
#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct SplitApp(std::path::PathBuf);
#[cfg(any(windows, target_os = "android"))]
impl SplitApp {
/// Convert the underlying app name to a [`String`].
///
/// # Note
/// This function is fallible due to the Window's dito being fallible, and it is convenient to have the same API across all platforms.
#[cfg(target_os = "android")]
pub fn to_string(self) -> Option<String> {
Some(self.0)
}
/// Convert the underlying path to a [`String`].
/// This function will fail if the underlying path string is not valid UTF-8. See [`std::ffi::OsStr::to_str`] for details.
#[cfg(windows)]
pub fn to_string(self) -> Option<String> {
self.0.as_os_str().to_str().map(str::to_string)
}
/// This is the String-representation as expected by [`SetExcludedApps`].
#[cfg(windows)]
pub fn to_tunnel_command_repr(self) -> std::ffi::OsString {
self.0.as_os_str().to_owned()
}
/// This is the String-representation as expected by [`SetExcludedApps`].
#[cfg(target_os = "android")]
pub fn to_tunnel_command_repr(self) -> String {
self.0
}
#[cfg(windows)]
pub fn display(&self) -> std::path::Display<'_> {
self.0.display()
}
}
#[cfg(target_os = "android")]
impl From<String> for SplitApp {
fn from(value: String) -> Self {
SplitApp(value)
}
}
#[cfg(windows)]
impl From<String> for SplitApp {
fn from(value: String) -> Self {
SplitApp::from(std::path::PathBuf::from(value))
}
}
#[cfg(windows)]
impl From<std::path::PathBuf> for SplitApp {
fn from(value: std::path::PathBuf) -> Self {
SplitApp(value)
}
}
impl Default for Settings {
fn default() -> Self {
Settings {
relay_settings: RelaySettings::Normal(RelayConstraints {
location: Constraint::Only(LocationConstraint::Location(
GeographicLocationConstraint::Country("se".to_owned()),
)),
wireguard_constraints: WireguardConstraints {
entry_location: Constraint::Only(LocationConstraint::Location(
GeographicLocationConstraint::Country("se".to_owned()),
)),
..Default::default()
},
..Default::default()
}),
bridge_settings: BridgeSettings::default(),
obfuscation_settings: ObfuscationSettings {
selected_obfuscation: SelectedObfuscation::Auto,
..Default::default()
},
bridge_state: BridgeState::Auto,
custom_lists: CustomListsSettings::default(),
api_access_methods: access_method::Settings::default(),
allow_lan: false,
block_when_disconnected: false,
auto_connect: false,
tunnel_options: TunnelOptions::default(),
relay_overrides: vec![],
show_beta_releases: false,
#[cfg(any(windows, target_os = "android"))]
split_tunnel: SplitTunnelSettings::default(),
settings_version: CURRENT_SETTINGS_VERSION,
}
}
}
impl Settings {
pub fn get_relay_settings(&self) -> RelaySettings {
self.relay_settings.clone()
}
pub fn set_relay_settings(&mut self, new_settings: RelaySettings) {
if self.relay_settings != new_settings {
if !new_settings.supports_bridge() && BridgeState::On == self.bridge_state {
self.bridge_state = BridgeState::Auto;
}
log::debug!(
"Changing relay settings:\n\tfrom: {}\n\tto: {}",
RelaySettingsFormatter {
settings: &self.relay_settings,
custom_lists: &self.custom_lists,
},
RelaySettingsFormatter {
settings: &new_settings,
custom_lists: &self.custom_lists,
},
);
self.relay_settings = new_settings;
}
}
pub fn set_relay_override(&mut self, relay_override: RelayOverride) {
let existing_override = self
.relay_overrides
.iter_mut()
.enumerate()
.find(|(_, elem)| elem.hostname == relay_override.hostname);
match existing_override {
None => self.relay_overrides.push(relay_override),
Some((index, elem)) => {
if relay_override.is_empty() {
self.relay_overrides.swap_remove(index);
} else {
*elem = relay_override;
}
}
}
}
}
/// TunnelOptions holds configuration data that applies to all kinds of tunnels.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(default)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
pub struct TunnelOptions {
/// openvpn holds OpenVPN specific tunnel options.
#[cfg_attr(target_os = "android", jnix(skip))]
pub openvpn: openvpn::TunnelOptions,
/// Contains wireguard tunnel options.
pub wireguard: wireguard::TunnelOptions,
/// Contains generic tunnel options that may apply to more than a single tunnel type.
#[cfg_attr(target_os = "android", jnix(skip))]
pub generic: GenericTunnelOptions,
/// DNS options.
pub dns_options: DnsOptions,
}
pub use dns::{CustomDnsOptions, DefaultDnsOptions, DnsOptions, DnsState};
impl Default for TunnelOptions {
fn default() -> Self {
TunnelOptions {
openvpn: openvpn::TunnelOptions::default(),
wireguard: wireguard::TunnelOptions::default(),
generic: GenericTunnelOptions {
// Enable IPv6 be default on Android
enable_ipv6: cfg!(target_os = "android"),
},
dns_options: DnsOptions::default(),
}
}
}