summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2021-08-23 15:30:17 +0200
committerDavid Lönnhager <david.l@mullvad.net>2021-08-25 15:19:19 +0200
commitc4869dc009663347fbca3f5ad87cf8f02acd25b6 (patch)
tree78169a4f2c2794c6fe2f1f0814b4fb386c3a79e4
parente0788bfee8f46404992c520c7f45f907e7bdec03 (diff)
downloadmullvadvpn-c4869dc009663347fbca3f5ad87cf8f02acd25b6.tar.xz
mullvadvpn-c4869dc009663347fbca3f5ad87cf8f02acd25b6.zip
Migrate OpenVPN constraints to new settings format
-rw-r--r--mullvad-types/src/settings/migrations/v1.rs110
-rw-r--r--mullvad-types/src/settings/migrations/v2.rs10
-rw-r--r--mullvad-types/src/settings/migrations/v3.rs10
-rw-r--r--mullvad-types/src/settings/migrations/v4.rs73
4 files changed, 123 insertions, 80 deletions
diff --git a/mullvad-types/src/settings/migrations/v1.rs b/mullvad-types/src/settings/migrations/v1.rs
index 6f7152fec6..484b35ee4e 100644
--- a/mullvad-types/src/settings/migrations/v1.rs
+++ b/mullvad-types/src/settings/migrations/v1.rs
@@ -1,12 +1,5 @@
-use super::{Error, Result};
-use crate::{
- custom_tunnel::CustomTunnelEndpoint,
- relay_constraints::{
- Constraint, LocationConstraint, OpenVpnConstraints, RelaySettings as NewRelaySettings,
- WireguardConstraints,
- },
-};
-use serde::{Deserialize, Serialize};
+use super::Result;
+use crate::relay_constraints::Constraint;
use talpid_types::net::TunnelType;
@@ -20,63 +13,50 @@ impl super::SettingsMigration for Migration {
fn migrate(&self, settings: &mut serde_json::Value) -> Result<()> {
log::info!("Migrating settings format to V2");
- let old_relay_settings: RelaySettings =
- serde_json::from_value(settings["relay_settings"].clone())
- .map_err(Error::ParseError)?;
- let new_relay_settings = migrate_relay_settings(old_relay_settings);
-
- settings["relay_settings"] = serde_json::json!(new_relay_settings);
- settings["show_beta_releases"] = serde_json::json!(false);
- settings["settings_version"] = serde_json::json!(super::SettingsVersion::V2);
-
- Ok(())
- }
-}
+ let openvpn_constraints = || -> Option<serde_json::Value> {
+ settings
+ .get("relay_settings")?
+ .get("normal")?
+ .get("tunnel")?
+ .get("only")?
+ .get("openvpn")
+ .cloned()
+ }();
+ let wireguard_constraints = || -> Option<serde_json::Value> {
+ settings
+ .get("relay_settings")?
+ .get("normal")?
+ .get("tunnel")?
+ .get("only")?
+ .get("wireguard")
+ .cloned()
+ }();
-fn migrate_relay_settings(relay_settings: RelaySettings) -> NewRelaySettings {
- match relay_settings {
- RelaySettings::CustomTunnelEndpoint(endpoint) => {
- crate::relay_constraints::RelaySettings::CustomTunnelEndpoint(endpoint)
- }
- RelaySettings::Normal(old_constraints) => {
- let mut new_constraints = crate::relay_constraints::RelayConstraints {
- location: old_constraints.location,
- ..Default::default()
- };
- match old_constraints.tunnel {
- Constraint::Any => (),
- Constraint::Only(TunnelConstraints::OpenVpn(constraints)) => {
- new_constraints.openvpn_constraints = constraints;
+ if let Some(relay_settings) = settings.get_mut("relay_settings") {
+ if let Some(normal_settings) = relay_settings.get_mut("normal") {
+ if let Some(openvpn_constraints) = openvpn_constraints {
+ normal_settings["openvpn_constraints"] = openvpn_constraints;
+ normal_settings["tunnel_protocol"] =
+ serde_json::json!(Constraint::<TunnelType>::Any);
+ } else if let Some(wireguard_constraints) = wireguard_constraints {
+ normal_settings["wireguard_constraints"] = wireguard_constraints;
+ normal_settings["tunnel_protocol"] =
+ serde_json::json!(Constraint::Only(TunnelType::Wireguard));
+ } else {
+ normal_settings["tunnel_protocol"] =
+ serde_json::json!(Constraint::<TunnelType>::Any);
}
- Constraint::Only(TunnelConstraints::Wireguard(constraints)) => {
- new_constraints.wireguard_constraints = constraints;
- new_constraints.tunnel_protocol = Constraint::Only(TunnelType::Wireguard);
+ if let Some(object) = normal_settings.as_object_mut() {
+ object.remove("tunnel");
}
- };
- crate::relay_constraints::RelaySettings::Normal(new_constraints)
+ }
}
- }
-}
-#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
-#[serde(rename_all = "snake_case")]
-pub enum RelaySettings {
- CustomTunnelEndpoint(CustomTunnelEndpoint),
- Normal(RelayConstraints),
-}
-
-#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
-pub struct RelayConstraints {
- pub location: Constraint<LocationConstraint>,
- pub tunnel: Constraint<TunnelConstraints>,
-}
+ settings["show_beta_releases"] = serde_json::json!(false);
+ settings["settings_version"] = serde_json::json!(super::SettingsVersion::V2);
-#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
-pub enum TunnelConstraints {
- #[serde(rename = "openvpn")]
- OpenVpn(OpenVpnConstraints),
- #[serde(rename = "wireguard")]
- Wireguard(WireguardConstraints),
+ Ok(())
+ }
}
#[cfg(test)]
@@ -100,10 +80,12 @@ mod test {
},
"openvpn_constraints": {
"port": {
- "only": 53
- },
- "protocol": {
- "only": "udp"
+ "only": {
+ "protocol": "udp",
+ "port": {
+ "only": 53
+ }
+ }
}
}
}
diff --git a/mullvad-types/src/settings/migrations/v2.rs b/mullvad-types/src/settings/migrations/v2.rs
index 10a255a44c..badeaacdbc 100644
--- a/mullvad-types/src/settings/migrations/v2.rs
+++ b/mullvad-types/src/settings/migrations/v2.rs
@@ -128,10 +128,12 @@ mod test {
},
"openvpn_constraints": {
"port": {
- "only": 53
- },
- "protocol": {
- "only": "udp"
+ "only": {
+ "protocol": "udp",
+ "port": {
+ "only": 53
+ }
+ }
}
}
}
diff --git a/mullvad-types/src/settings/migrations/v3.rs b/mullvad-types/src/settings/migrations/v3.rs
index 6438b2c981..0aa53e3da4 100644
--- a/mullvad-types/src/settings/migrations/v3.rs
+++ b/mullvad-types/src/settings/migrations/v3.rs
@@ -128,10 +128,12 @@ mod test {
},
"openvpn_constraints": {
"port": {
- "only": 53
- },
- "protocol": {
- "only": "udp"
+ "only": {
+ "protocol": "udp",
+ "port": {
+ "only": 53
+ }
+ }
}
}
}
diff --git a/mullvad-types/src/settings/migrations/v4.rs b/mullvad-types/src/settings/migrations/v4.rs
index a479ae847f..7a9d1a88fb 100644
--- a/mullvad-types/src/settings/migrations/v4.rs
+++ b/mullvad-types/src/settings/migrations/v4.rs
@@ -4,6 +4,7 @@ use talpid_types::net::TransportProtocol;
const WIREGUARD_TCP_PORTS: [u16; 3] = [80, 443, 5001];
+const OPENVPN_TCP_PORTS: [u16; 2] = [80, 443];
pub(super) struct Migration;
@@ -55,13 +56,69 @@ impl super::SettingsMigration for Migration {
.remove("protocol");
}
+ let openvpn_constraints = || -> Option<&serde_json::Value> {
+ settings
+ .get("relay_settings")?
+ .get("normal")?
+ .get("openvpn_constraints")
+ }();
+
+ if let Some(constraints) = openvpn_constraints {
+ let port: Constraint<u16> = if let Some(port) = constraints.get("port") {
+ serde_json::from_value(port.clone()).map_err(Error::ParseError)?
+ } else {
+ Constraint::Any
+ };
+ let transport_constraint: Constraint<TransportProtocol> =
+ if let Some(protocol) = constraints.get("protocol") {
+ serde_json::from_value(protocol.clone()).map_err(Error::ParseError)?
+ } else {
+ Constraint::Any
+ };
+
+ let port = match (port, transport_constraint) {
+ (Constraint::Only(port), Constraint::Any) => Constraint::Only(TransportPort {
+ protocol: openvpn_protocol_from_port(port),
+ port: Constraint::Only(port),
+ }),
+ (Constraint::Only(port), Constraint::Only(protocol)) => {
+ Constraint::Only(TransportPort {
+ protocol,
+ port: Constraint::Only(port),
+ })
+ }
+ (Constraint::Any, Constraint::Only(protocol)) => Constraint::Only(TransportPort {
+ protocol,
+ port: Constraint::Any,
+ }),
+ (Constraint::Any, Constraint::Any) => Constraint::Any,
+ };
+
+ settings["relay_settings"]["normal"]["openvpn_constraints"]["port"] =
+ serde_json::json!(port);
+ settings["relay_settings"]["normal"]["openvpn_constraints"]
+ .as_object_mut()
+ .ok_or(Error::NoMatchingVersion)?
+ .remove("protocol");
+ }
+
settings["settings_version"] = serde_json::json!(SettingsVersion::V5);
Ok(())
}
}
+fn openvpn_protocol_from_port(port: u16) -> TransportProtocol {
+ log::warn!("Inferring transport protocol from port constraint");
+ if OPENVPN_TCP_PORTS.contains(&port) {
+ TransportProtocol::Tcp
+ } else {
+ TransportProtocol::Udp
+ }
+}
+
fn wg_protocol_from_port(port: u16) -> TransportProtocol {
+ log::warn!("Inferring transport protocol from port constraint");
if WIREGUARD_TCP_PORTS.contains(&port) {
TransportProtocol::Tcp
} else {
@@ -93,11 +150,9 @@ mod test {
},
"openvpn_constraints": {
"port": {
- "only": 53
+ "only": 1195
},
- "protocol": {
- "only": "udp"
- }
+ "protocol": "any"
}
}
},
@@ -165,10 +220,12 @@ mod test {
},
"openvpn_constraints": {
"port": {
- "only": 53
- },
- "protocol": {
- "only": "udp"
+ "only": {
+ "protocol": "udp",
+ "port": {
+ "only": 1195
+ }
+ }
}
}
}