diff options
| author | David Lönnhager <david.l@mullvad.net> | 2025-03-04 15:12:24 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2025-03-05 10:04:14 +0100 |
| commit | 8c1abc6d0df834da38c6c2be56f1cc38802c7e83 (patch) | |
| tree | c25930d60fb52b5254685de43860b7bc30620ac8 | |
| parent | a25c36c55045bb0b65c76c19a0211b795103ffb8 (diff) | |
| download | mullvadvpn-8c1abc6d0df834da38c6c2be56f1cc38802c7e83.tar.xz mullvadvpn-8c1abc6d0df834da38c6c2be56f1cc38802c7e83.zip | |
Migrate settings for tunnel type
Co-authored-by: Markus Pettersson <markus.pettersson@mullvad.net>
| -rw-r--r-- | mullvad-daemon/src/migrations/v9.rs | 63 | ||||
| -rw-r--r-- | mullvad-daemon/src/settings/mod.rs | 4 |
2 files changed, 64 insertions, 3 deletions
diff --git a/mullvad-daemon/src/migrations/v9.rs b/mullvad-daemon/src/migrations/v9.rs index 1275c488e1..c117905f7f 100644 --- a/mullvad-daemon/src/migrations/v9.rs +++ b/mullvad-daemon/src/migrations/v9.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; #[cfg(target_os = "android")] use serde_json::json; #[cfg(target_os = "android")] @@ -29,6 +30,16 @@ const SPLIT_TUNNELING_APPS: &str = "split-tunnelling.txt"; #[cfg(target_os = "android")] const SPLIT_TUNNELING_STATE: &str = "split-tunnelling-enabled.txt"; +/// Tunnel protocol +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename = "tunnel_type")] +pub enum TunnelType { + #[serde(rename = "openvpn")] + OpenVpn, + #[serde(rename = "wireguard")] + Wireguard, +} + // ====================================================== /// This is an open migration @@ -73,12 +84,64 @@ pub fn migrate( } } + migrate_tunnel_type(settings)?; + // TODO: Uncomment this when closing the migration: // json_blob["settings_version"] = serde_json::json!(SettingsVersion::V10); Ok(()) } +fn migrate_tunnel_type(settings: &mut serde_json::Value) -> Result<()> { + let Some(ref mut normal) = relay_settings(settings) else { + return Ok(()); + }; + match normal.get_mut("tunnel_protocol") { + // Already migrated + Some(serde_json::Value::String(_s)) => (), + // Migrate + Some(serde_json::Value::Object(ref mut constraint)) => { + if constraint.get("any").is_some() { + // 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); + } else 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(()) +} + +fn relay_settings(settings: &mut serde_json::Value) -> Option<&mut serde_json::Value> { + settings.get_mut("relay_settings")?.get_mut("normal") +} + fn version_matches(settings: &serde_json::Value) -> bool { settings .get("settings_version") diff --git a/mullvad-daemon/src/settings/mod.rs b/mullvad-daemon/src/settings/mod.rs index 0b0d914569..ec97f4e52b 100644 --- a/mullvad-daemon/src/settings/mod.rs +++ b/mullvad-daemon/src/settings/mod.rs @@ -554,9 +554,7 @@ mod test { } } }, - "tunnel_protocol": { - "only": "wireguard" - }, + "tunnel_protocol": "wireguard", "wireguard_constraints": { "port": "any" }, |
