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
|
use super::{Error, Result};
use mullvad_types::settings::SettingsVersion;
use talpid_types::net::TunnelType;
/// Automatic tunnel protocol has been removed. If the tunnel protocol is set to `any`, it will be
/// migrated to `wireguard`, unless the location is an openvpn relay, in which case it will be
/// migrated to `openvpn`.
pub fn migrate(settings: &mut serde_json::Value) -> Result<()> {
if !(version(settings) == Some(SettingsVersion::V10)) {
return Ok(());
}
log::info!("Migrating settings format to v11");
migrate_tunnel_type(settings)?;
settings["settings_version"] = serde_json::json!(SettingsVersion::V11);
Ok(())
}
fn version(settings: &serde_json::Value) -> Option<SettingsVersion> {
settings
.get("settings_version")
.and_then(|version| serde_json::from_value(version.clone()).ok())
}
fn relay_settings(settings: &mut serde_json::Value) -> Option<&mut serde_json::Value> {
settings.get_mut("relay_settings")?.get_mut("normal")
}
fn migrate_tunnel_type(settings: &mut serde_json::Value) -> Result<()> {
if let Some(ref mut normal) = relay_settings(settings) {
migrate_tunnel_type_inner(normal)?;
}
Ok(())
}
fn migrate_tunnel_type_inner(normal: &mut serde_json::Value) -> Result<()> {
match normal.get_mut("tunnel_protocol") {
// Migrate tunnel protocol "any"
Some(serde_json::Value::String(s)) if s == "any" => {
// If openvpn is selected, migrate to openvpn tunnel type
// Otherwise, select wireguard
let hostname = normal
.get_mut("location")
.and_then(|location| location.get_mut("only"))
.and_then(|only| only.get_mut("location"))
.and_then(|only| only.get_mut("hostname").cloned());
let protocol = if let Some(serde_json::Value::String(s)) = hostname {
if s.split('-').any(|token| token == "ovpn") {
TunnelType::OpenVpn
} else {
TunnelType::Wireguard
}
} else {
TunnelType::Wireguard
};
normal["tunnel_protocol"] = serde_json::json!(protocol);
}
// Migrate '"only": { "tunnel_protocol": $tunnel_protocol }'
// to '"tunnel_protocol": $tunnel_protocol'
Some(serde_json::Value::Object(constraint)) => {
if let Some(tunnel_type) = constraint.get("only") {
let tunnel_type: TunnelType = serde_json::from_value(tunnel_type.clone())
.map_err(|_| Error::InvalidSettingsContent)?;
normal["tunnel_protocol"] = serde_json::json!(tunnel_type);
} else {
return Err(Error::InvalidSettingsContent);
}
}
Some(_) => {
return Err(Error::InvalidSettingsContent);
}
// Unexpected result. Do nothing.
None => (),
};
Ok(())
}
#[cfg(test)]
mod test {
use serde_json::json;
use crate::migrations::v10::migrate_tunnel_type_inner;
/// Tunnel protocol "any" is migrated to wireguard
#[test]
fn test_v10_to_v11_migration() {
let mut old_settings = json!({
"tunnel_protocol": "any",
"location": {
"only": {
"location": {
"country": "se"
}
}
}
});
migrate_tunnel_type_inner(&mut old_settings).unwrap();
let new_settings: serde_json::Value = json!({
"tunnel_protocol": "wireguard",
"location": {
"only": {
"location": {
"country": "se"
}
}
}
});
assert_eq!(&old_settings, &new_settings);
}
/// Tunnel protocol "any" is migrated to openvpn, since the location is an openvpn relay
#[test]
fn test_v10_to_v11_migration_openvpn_location() {
let mut old_settings = json!({
"tunnel_protocol": "any",
"location": {
"only": {
"location": {
"hostname": "at-vie-ovpn-001"
}
}
}
});
migrate_tunnel_type_inner(&mut old_settings).unwrap();
let new_settings: serde_json::Value = json!({
"tunnel_protocol": "openvpn",
"location": {
"only": {
"location": {
"hostname": "at-vie-ovpn-001"
}
}
}
});
assert_eq!(&old_settings, &new_settings);
}
/// Tunnel protocol '"only" = { "tunnel_protocol" = '$tunnel_protocol' } is migrated to
/// '"tunnel_protocol" = '$tunnel_protocol'
#[test]
fn test_v10_to_v11_migration_only_protocol() {
let mut old_settings = json!({
"tunnel_protocol": {
"only": "wireguard"
},
"location": {
"only": {
"location": {
"country": "se"
}
}
}
});
migrate_tunnel_type_inner(&mut old_settings).unwrap();
let new_settings: serde_json::Value = json!({
"tunnel_protocol": "wireguard",
"location": {
"only": {
"location": {
"country": "se"
}
}
}
});
assert_eq!(&old_settings, &new_settings);
}
}
|