Skip to content

Commit 4fa6699

Browse files
tobias-jarvelovMarkusPettersson98
authored andcommitted
Add v13 settings migration
1 parent e14e194 commit 4fa6699

9 files changed

+216
-1
lines changed

mullvad-daemon/src/migrations/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ mod v1;
4949
mod v10;
5050
mod v11;
5151
mod v12;
52+
mod v13;
5253
mod v2;
5354
mod v3;
5455
mod v4;
@@ -216,6 +217,7 @@ async fn migrate_settings(
216217
v10::migrate(settings)?;
217218
v11::migrate(settings)?;
218219
v12::migrate(settings)?;
220+
v13::migrate(settings)?;
219221

220222
Ok(migration_data)
221223
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: mullvad-daemon/src/migrations/v13.rs
3+
expression: "serde_json::to_string_pretty(&old_settings).unwrap()"
4+
---
5+
{
6+
"obfuscation_settings": {
7+
"selected_obfuscation": "auto",
8+
"wireguard_port": {
9+
"port": "any"
10+
}
11+
},
12+
"relay_settings": {
13+
"normal": {
14+
"wireguard_constraints": {}
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: mullvad-daemon/src/migrations/v13.rs
3+
expression: "serde_json::to_string_pretty(&old_settings).unwrap()"
4+
---
5+
{
6+
"obfuscation_settings": {
7+
"selected_obfuscation": "auto"
8+
},
9+
"relay_settings": {
10+
"normal": {
11+
"wireguard_constraints": {
12+
"port": "any"
13+
}
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: mullvad-daemon/src/migrations/v13.rs
3+
expression: "serde_json::to_string_pretty(&old_settings).unwrap()"
4+
---
5+
{
6+
"obfuscation_settings": {
7+
"selected_obfuscation": "shadowsocks",
8+
"wireguard_port": {
9+
"port": "any"
10+
}
11+
},
12+
"relay_settings": {
13+
"normal": {
14+
"wireguard_constraints": {}
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: mullvad-daemon/src/migrations/v13.rs
3+
expression: "serde_json::to_string_pretty(&old_settings).unwrap()"
4+
---
5+
{
6+
"obfuscation_settings": {
7+
"selected_obfuscation": "shadowsocks"
8+
},
9+
"relay_settings": {
10+
"normal": {
11+
"wireguard_constraints": {
12+
"port": "any"
13+
}
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: mullvad-daemon/src/migrations/v13.rs
3+
expression: "serde_json::to_string_pretty(&old_settings).unwrap()"
4+
---
5+
{
6+
"obfuscation_settings": {
7+
"wireguard_port": {
8+
"port": {
9+
"only": 53
10+
}
11+
}
12+
},
13+
"relay_settings": {
14+
"normal": {
15+
"wireguard_constraints": {}
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: mullvad-daemon/src/migrations/v13.rs
3+
expression: "serde_json::to_string_pretty(&old_settings).unwrap()"
4+
---
5+
{
6+
"obfuscation_settings": {},
7+
"relay_settings": {
8+
"normal": {
9+
"wireguard_constraints": {
10+
"port": {
11+
"only": 53
12+
}
13+
}
14+
}
15+
}
16+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use super::Result;
2+
use mullvad_types::settings::SettingsVersion;
3+
use serde_json::json;
4+
5+
const WIREGUARD_PORT_OLD_KEY: &str = "port";
6+
const WIREGUARD_PORT_NEW_KEY: &str = "wireguard_port";
7+
8+
/// This migration handles:
9+
/// - Migrates the WireGuard port from WireGuard constraints in relay settings
10+
/// to obfuscation settings.
11+
pub fn migrate(settings: &mut serde_json::Value) -> Result<()> {
12+
if !version_matches(settings) {
13+
return Ok(());
14+
}
15+
16+
log::info!("Migrating settings format to V14");
17+
18+
migrate_wireguard_port(settings);
19+
20+
settings["settings_version"] = serde_json::json!(SettingsVersion::V14);
21+
22+
Ok(())
23+
}
24+
25+
fn migrate_wireguard_port(settings: &mut serde_json::Value) -> Option<()> {
26+
let wireguard_constraints = settings
27+
.get_mut("relay_settings")
28+
.and_then(|relay_settings| relay_settings.get_mut("normal"))
29+
.and_then(|normal_relay_settings| normal_relay_settings.get_mut("wireguard_constraints"))
30+
.and_then(|wireguard_constraints| wireguard_constraints.as_object_mut())?;
31+
32+
let port = wireguard_constraints.remove(WIREGUARD_PORT_OLD_KEY)?;
33+
34+
let obfuscation_settings = settings
35+
.get_mut("obfuscation_settings")
36+
.and_then(|obfuscation_settings| obfuscation_settings.as_object_mut())?;
37+
obfuscation_settings.insert(WIREGUARD_PORT_NEW_KEY.to_string(), json!({"port": port}));
38+
39+
Some(())
40+
}
41+
42+
fn version_matches(settings: &serde_json::Value) -> bool {
43+
settings
44+
.get("settings_version")
45+
.map(|version| version == SettingsVersion::V13 as u64)
46+
.unwrap_or(false)
47+
}
48+
49+
#[cfg(test)]
50+
mod test {
51+
use serde_json::json;
52+
53+
use crate::migrations::v13::migrate_wireguard_port;
54+
55+
#[test]
56+
fn test_v13_to_v14_migration_wireguard_port_any_selected_obfuscation_custom() {
57+
let mut old_settings = json!({
58+
"obfuscation_settings": {
59+
"selected_obfuscation": "shadowsocks"
60+
},
61+
"relay_settings": {
62+
"normal": {
63+
"wireguard_constraints": {
64+
"port": "any"
65+
}
66+
}
67+
}
68+
});
69+
insta::assert_snapshot!(serde_json::to_string_pretty(&old_settings).unwrap());
70+
migrate_wireguard_port(&mut old_settings).unwrap();
71+
insta::assert_snapshot!(serde_json::to_string_pretty(&old_settings).unwrap());
72+
}
73+
74+
#[test]
75+
fn test_v13_to_v14_migration_wireguard_port_any_selected_obfuscation_auto() {
76+
let mut old_settings = json!({
77+
"obfuscation_settings": {
78+
"selected_obfuscation": "auto"
79+
},
80+
"relay_settings": {
81+
"normal": {
82+
"wireguard_constraints": {
83+
"port": "any"
84+
}
85+
}
86+
}
87+
});
88+
insta::assert_snapshot!(serde_json::to_string_pretty(&old_settings).unwrap());
89+
migrate_wireguard_port(&mut old_settings).unwrap();
90+
insta::assert_snapshot!(serde_json::to_string_pretty(&old_settings).unwrap());
91+
}
92+
93+
#[test]
94+
fn test_v13_to_v14_migration_wireguard_port_value() {
95+
let mut old_settings = json!({
96+
"obfuscation_settings": {},
97+
"relay_settings": {
98+
"normal": {
99+
"wireguard_constraints": {
100+
"port": {
101+
"only": 53
102+
}
103+
}
104+
}
105+
}
106+
});
107+
insta::assert_snapshot!(serde_json::to_string_pretty(&old_settings).unwrap());
108+
migrate_wireguard_port(&mut old_settings).unwrap();
109+
insta::assert_snapshot!(serde_json::to_string_pretty(&old_settings).unwrap());
110+
}
111+
}

mullvad-types/src/settings/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod dns;
2020
/// latest version that exists in `SettingsVersion`.
2121
/// This should be bumped when a new version is introduced along with a migration
2222
/// being added to `mullvad-daemon`.
23-
pub const CURRENT_SETTINGS_VERSION: SettingsVersion = SettingsVersion::V13;
23+
pub const CURRENT_SETTINGS_VERSION: SettingsVersion = SettingsVersion::V14;
2424

2525
#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Copy)]
2626
#[repr(u32)]
@@ -37,6 +37,7 @@ pub enum SettingsVersion {
3737
V11 = 11,
3838
V12 = 12,
3939
V13 = 13,
40+
V14 = 14,
4041
}
4142

4243
impl<'de> Deserialize<'de> for SettingsVersion {
@@ -57,6 +58,7 @@ impl<'de> Deserialize<'de> for SettingsVersion {
5758
v if v == SettingsVersion::V11 as u32 => Ok(SettingsVersion::V11),
5859
v if v == SettingsVersion::V12 as u32 => Ok(SettingsVersion::V12),
5960
v if v == SettingsVersion::V13 as u32 => Ok(SettingsVersion::V13),
61+
v if v == SettingsVersion::V14 as u32 => Ok(SettingsVersion::V14),
6062
v => Err(serde::de::Error::custom(format!(
6163
"{v} is not a valid SettingsVersion"
6264
))),

0 commit comments

Comments
 (0)