diff options
| author | David Lönnhager <david.l@mullvad.net> | 2023-10-25 13:02:13 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-10-31 00:45:17 +0100 |
| commit | e02d812d55064fc114d38edbdcbbeda5aca6a6b7 (patch) | |
| tree | 6d96b45d7b617161525497dd637a572e89bdfac7 | |
| parent | 9b33f4440fd35d01eea663df5de7d83f65d96918 (diff) | |
| download | mullvadvpn-e02d812d55064fc114d38edbdcbbeda5aca6a6b7.tar.xz mullvadvpn-e02d812d55064fc114d38edbdcbbeda5aca6a6b7.zip | |
Replace UpdateRelaySettings with SetRelaySettings
21 files changed, 132 insertions, 418 deletions
diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/RelayConstraintsUpdate.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/RelayConstraintsUpdate.kt deleted file mode 100644 index 8a382a87d9..0000000000 --- a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/RelayConstraintsUpdate.kt +++ /dev/null @@ -1,8 +0,0 @@ -package net.mullvad.mullvadvpn.model - -data class RelayConstraintsUpdate( - val location: Constraint<LocationConstraint>?, - val providers: Constraint<Providers>?, - val ownership: Constraint<Ownership>?, - val wireguardConstraints: WireguardConstraints? -) diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/RelaySettingsUpdate.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/RelaySettingsUpdate.kt deleted file mode 100644 index f8d27f115e..0000000000 --- a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/RelaySettingsUpdate.kt +++ /dev/null @@ -1,7 +0,0 @@ -package net.mullvad.mullvadvpn.model - -sealed class RelaySettingsUpdate { - object CustomTunnelEndpoint : RelaySettingsUpdate() - - data class Normal(val constraints: RelayConstraintsUpdate) : RelaySettingsUpdate() -} diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadDaemon.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadDaemon.kt index ceb95a48b7..9110ac08ab 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadDaemon.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadDaemon.kt @@ -19,7 +19,7 @@ import net.mullvad.mullvadvpn.model.PlayPurchaseInitResult import net.mullvad.mullvadvpn.model.PlayPurchaseVerifyResult import net.mullvad.mullvadvpn.model.QuantumResistantState import net.mullvad.mullvadvpn.model.RelayList -import net.mullvad.mullvadvpn.model.RelaySettingsUpdate +import net.mullvad.mullvadvpn.model.RelaySettings import net.mullvad.mullvadvpn.model.RemoveDeviceEvent import net.mullvad.mullvadvpn.model.RemoveDeviceResult import net.mullvad.mullvadvpn.model.Settings @@ -182,8 +182,8 @@ class MullvadDaemon( return verifyPlayPurchase(daemonInterfaceAddress, playPurchase) } - fun updateRelaySettings(update: RelaySettingsUpdate) { - updateRelaySettings(daemonInterfaceAddress, update) + fun setRelaySettings(update: RelaySettings) { + setRelaySettings(daemonInterfaceAddress, update) } fun setObfuscationSettings(settings: ObfuscationSettings?) { @@ -291,7 +291,7 @@ class MullvadDaemon( private external fun updateRelaySettings( daemonInterfaceAddress: Long, - update: RelaySettingsUpdate + update: RelaySettings ) private external fun setObfuscationSettings( diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt index 6974d804a1..1884b98737 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt @@ -12,9 +12,9 @@ import net.mullvad.mullvadvpn.lib.ipc.Request import net.mullvad.mullvadvpn.model.Constraint import net.mullvad.mullvadvpn.model.GeographicLocationConstraint import net.mullvad.mullvadvpn.model.LocationConstraint -import net.mullvad.mullvadvpn.model.RelayConstraintsUpdate +import net.mullvad.mullvadvpn.model.RelayConstraints import net.mullvad.mullvadvpn.model.RelayList -import net.mullvad.mullvadvpn.model.RelaySettingsUpdate +import net.mullvad.mullvadvpn.model.RelaySettings import net.mullvad.mullvadvpn.model.WireguardConstraints import net.mullvad.mullvadvpn.service.MullvadDaemon @@ -95,8 +95,8 @@ class RelayListListener(endpoint: ServiceEndpoint) { val wireguardConstraints: WireguardConstraints? = selectedWireguardConstraints val update = - RelaySettingsUpdate.Normal( - RelayConstraintsUpdate( + RelaySettings.Normal( + RelayConstraints( location = location, wireguardConstraints = wireguardConstraints, ownership = Constraint.Any(), @@ -104,7 +104,7 @@ class RelayListListener(endpoint: ServiceEndpoint) { ) ) - daemon.await().updateRelaySettings(update) + daemon.await().setRelaySettings(update) } companion object { diff --git a/mullvad-cli/src/cmds/relay.rs b/mullvad-cli/src/cmds/relay.rs index 2bccbbc24e..6d1b487126 100644 --- a/mullvad-cli/src/cmds/relay.rs +++ b/mullvad-cli/src/cmds/relay.rs @@ -6,8 +6,8 @@ use mullvad_types::{ location::Location, relay_constraints::{ Constraint, GeographicLocationConstraint, LocationConstraint, LocationConstraintFormatter, - Match, OpenVpnConstraints, Ownership, Provider, Providers, RelayConstraintsUpdate, - RelaySettings, RelaySettingsUpdate, TransportPort, WireguardConstraints, + Match, OpenVpnConstraints, Ownership, Provider, Providers, RelayConstraints, RelaySettings, + TransportPort, WireguardConstraints, }, relay_list::{RelayEndpointData, RelayListCountry}, ConnectionConfig, CustomTunnelEndpoint, @@ -339,9 +339,21 @@ impl Relay { /// Get active relays which are not bridges. - async fn update_constraints(update: RelaySettingsUpdate) -> Result<()> { + async fn update_constraints(update_fn: impl FnOnce(&mut RelayConstraints)) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; - rpc.update_relay_settings(update).await?; + let settings = rpc.get_settings().await?; + + let relay_settings = settings.get_relay_settings(); + let mut constraints = match relay_settings { + RelaySettings::Normal(normal) => normal, + RelaySettings::CustomTunnelEndpoint(_custom) => { + println!("Removing custom relay settings"); + RelayConstraints::default() + } + }; + update_fn(&mut constraints); + rpc.set_relay_settings(RelaySettings::Normal(constraints)) + .await?; println!("Relay constraints updated"); Ok(()) } @@ -408,7 +420,11 @@ impl Relay { .await? } }; - Self::update_constraints(RelaySettingsUpdate::CustomTunnelEndpoint(custom_endpoint)).await + let mut rpc = MullvadProxyClient::new().await?; + rpc.set_relay_settings(RelaySettings::CustomTunnelEndpoint(custom_endpoint)) + .await?; + println!("Relay constraints updated"); + Ok(()) } fn read_custom_openvpn_relay( @@ -507,10 +523,9 @@ impl Relay { location_constraint.map(LocationConstraint::Location) }; - Self::update_constraints(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - location: Some(constraint), - ..Default::default() - })) + Self::update_constraints(|constraints| { + constraints.location = constraint; + }) .await } @@ -519,12 +534,10 @@ impl Relay { let list_id = super::custom_list::find_list_by_name(&mut rpc, &custom_list_name) .await? .id; - rpc.update_relay_settings(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - location: Some(Constraint::Only(LocationConstraint::CustomList { list_id })), - ..Default::default() - })) - .await?; - Ok(()) + Self::update_constraints(|constraints| { + constraints.location = Constraint::Only(LocationConstraint::CustomList { list_id }); + }) + .await } async fn set_providers(providers: Vec<String>) -> Result<()> { @@ -533,18 +546,16 @@ impl Relay { } else { Constraint::Only(Providers::new(providers.into_iter()).unwrap()) }; - Self::update_constraints(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - providers: Some(providers), - ..Default::default() - })) + Self::update_constraints(|constraints| { + constraints.providers = providers; + }) .await } async fn set_ownership(ownership: Constraint<Ownership>) -> Result<()> { - Self::update_constraints(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - ownership: Some(ownership), - ..Default::default() - })) + Self::update_constraints(|constraints| { + constraints.ownership = ownership; + }) .await } @@ -558,10 +569,9 @@ impl Relay { }; openvpn_constraints.port = parse_transport_port(port, protocol, &openvpn_constraints.port); - Self::update_constraints(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - openvpn_constraints: Some(openvpn_constraints), - ..Default::default() - })) + Self::update_constraints(|constraints| { + constraints.openvpn_constraints = openvpn_constraints; + }) .await } @@ -628,10 +638,9 @@ impl Relay { None => (), } - Self::update_constraints(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - wireguard_constraints: Some(wireguard_constraints), - ..Default::default() - })) + Self::update_constraints(|constraints| { + constraints.wireguard_constraints = wireguard_constraints; + }) .await } @@ -648,10 +657,9 @@ impl Relay { } async fn set_tunnel_protocol(protocol: Constraint<TunnelType>) -> Result<()> { - Self::update_constraints(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - tunnel_protocol: Some(protocol), - ..Default::default() - })) + Self::update_constraints(|constraints| { + constraints.tunnel_protocol = protocol; + }) .await } } diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index 1077185ca3..8ee8e58fd6 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -47,7 +47,7 @@ use mullvad_types::{ custom_list::CustomList, device::{Device, DeviceEvent, DeviceEventCause, DeviceId, DeviceState, RemoveDeviceEvent}, location::GeoIpLocation, - relay_constraints::{BridgeSettings, BridgeState, ObfuscationSettings, RelaySettingsUpdate}, + relay_constraints::{BridgeSettings, BridgeState, ObfuscationSettings, RelaySettings}, relay_list::RelayList, settings::{DnsOptions, Settings}, states::{TargetState, TunnelState}, @@ -232,7 +232,7 @@ pub enum DaemonCommand { /// Remove device from a given account. RemoveDevice(ResponseTx<(), Error>, AccountToken, DeviceId), /// Place constraints on the type of tunnel and relay - UpdateRelaySettings(ResponseTx<(), settings::Error>, RelaySettingsUpdate), + SetRelaySettings(ResponseTx<(), settings::Error>, RelaySettings), /// Set the allow LAN setting. SetAllowLan(ResponseTx<(), settings::Error>, bool), /// Set the beta program setting. @@ -1055,7 +1055,7 @@ where } GetAccountHistory(tx) => self.on_get_account_history(tx), ClearAccountHistory(tx) => self.on_clear_account_history(tx).await, - UpdateRelaySettings(tx, update) => self.on_update_relay_settings(tx, update).await, + SetRelaySettings(tx, update) => self.on_set_relay_settings(tx, update).await, SetAllowLan(tx, allow_lan) => self.on_set_allow_lan(tx, allow_lan).await, SetShowBetaReleases(tx, enabled) => self.on_set_show_beta_releases(tx, enabled).await, SetBlockWhenDisconnected(tx, block_when_disconnected) => { @@ -1817,18 +1817,18 @@ where } } - async fn on_update_relay_settings( + async fn on_set_relay_settings( &mut self, tx: ResponseTx<(), settings::Error>, - update: RelaySettingsUpdate, + update: RelaySettings, ) { match self .settings - .update(move |settings| settings.update_relay_settings(update)) + .update(move |settings| settings.set_relay_settings(update)) .await { Ok(settings_changed) => { - Self::oneshot_send(tx, Ok(()), "update_relay_settings response"); + Self::oneshot_send(tx, Ok(()), "set_relay_settings response"); if settings_changed { self.event_listener .notify_settings(self.settings.to_settings()); @@ -1840,7 +1840,7 @@ where } Err(e) => { log::error!("{}", e.display_chain_with_msg("Unable to save settings")); - Self::oneshot_send(tx, Err(e), "update_relay_settings response"); + Self::oneshot_send(tx, Err(e), "set_relay_settings response"); } } } diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index b494c53fbb..79466d8878 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -13,7 +13,7 @@ use mullvad_paths; use mullvad_types::settings::DnsOptions; use mullvad_types::{ account::AccountToken, - relay_constraints::{BridgeSettings, BridgeState, ObfuscationSettings, RelaySettingsUpdate}, + relay_constraints::{BridgeSettings, BridgeState, ObfuscationSettings, RelaySettings}, relay_list::RelayList, settings::Settings, states::{TargetState, TunnelState}, @@ -164,16 +164,16 @@ impl ManagementService for ManagementServiceImpl { Ok(Response::new(())) } - async fn update_relay_settings( + async fn set_relay_settings( &self, - request: Request<types::RelaySettingsUpdate>, + request: Request<types::RelaySettings>, ) -> ServiceResult<()> { - log::debug!("update_relay_settings"); + log::debug!("set_relay_settings"); let (tx, rx) = oneshot::channel(); let constraints_update = - RelaySettingsUpdate::try_from(request.into_inner()).map_err(map_protobuf_type_err)?; + RelaySettings::try_from(request.into_inner()).map_err(map_protobuf_type_err)?; - let message = DaemonCommand::UpdateRelaySettings(tx, constraints_update); + let message = DaemonCommand::SetRelaySettings(tx, constraints_update); self.send_command_to_daemon(message)?; self.wait_for_result(rx) .await? diff --git a/mullvad-jni/src/classes.rs b/mullvad-jni/src/classes.rs index 45dc21a4a2..d39cb156d8 100644 --- a/mullvad-jni/src/classes.rs +++ b/mullvad-jni/src/classes.rs @@ -55,9 +55,6 @@ pub const CLASSES: &[&str] = &[ "net/mullvad/mullvadvpn/model/RelayListCountry", "net/mullvad/mullvadvpn/model/RelaySettings$CustomTunnelEndpoint", "net/mullvad/mullvadvpn/model/RelaySettings$Normal", - "net/mullvad/mullvadvpn/model/RelaySettingsUpdate$CustomTunnelEndpoint", - "net/mullvad/mullvadvpn/model/RelaySettingsUpdate$Normal", - "net/mullvad/mullvadvpn/model/RelayConstraintsUpdate", "net/mullvad/mullvadvpn/model/SelectedObfuscation", "net/mullvad/mullvadvpn/model/Settings", "net/mullvad/mullvadvpn/model/TunnelState$Error", diff --git a/mullvad-jni/src/daemon_interface.rs b/mullvad-jni/src/daemon_interface.rs index c64c94041e..236c864a01 100644 --- a/mullvad-jni/src/daemon_interface.rs +++ b/mullvad-jni/src/daemon_interface.rs @@ -4,7 +4,7 @@ use mullvad_types::{ account::{AccountData, AccountToken, PlayPurchase, VoucherSubmission}, device::{Device, DeviceState}, location::GeoIpLocation, - relay_constraints::{ObfuscationSettings, RelaySettingsUpdate}, + relay_constraints::{ObfuscationSettings, RelaySettings}, relay_list::RelayList, settings::{DnsOptions, Settings}, states::{TargetState, TunnelState}, @@ -327,10 +327,10 @@ impl DaemonInterface { .map_err(Error::from) } - pub fn update_relay_settings(&self, update: RelaySettingsUpdate) -> Result<()> { + pub fn set_relay_settings(&self, update: RelaySettings) -> Result<()> { let (tx, rx) = oneshot::channel(); - self.send_command(DaemonCommand::UpdateRelaySettings(tx, update))?; + self.send_command(DaemonCommand::SetRelaySettings(tx, update))?; block_on(rx) .map_err(|_| Error::NoResponse)? diff --git a/mullvad-jni/src/lib.rs b/mullvad-jni/src/lib.rs index a40bf73fc1..a2c3586431 100644 --- a/mullvad-jni/src/lib.rs +++ b/mullvad-jni/src/lib.rs @@ -1306,19 +1306,19 @@ pub extern "system" fn Java_net_mullvad_mullvadvpn_service_MullvadDaemon_verifyP #[no_mangle] #[allow(non_snake_case)] -pub extern "system" fn Java_net_mullvad_mullvadvpn_service_MullvadDaemon_updateRelaySettings( +pub extern "system" fn Java_net_mullvad_mullvadvpn_service_MullvadDaemon_setRelaySettings( env: JNIEnv<'_>, _: JObject<'_>, daemon_interface_address: jlong, - relaySettingsUpdate: JObject<'_>, + relaySettings: JObject<'_>, ) { let env = JnixEnv::from(env); // SAFETY: The address points to an instance valid for the duration of this function call if let Some(daemon_interface) = unsafe { get_daemon_interface(daemon_interface_address) } { - let update = FromJava::from_java(&env, relaySettingsUpdate); + let update = FromJava::from_java(&env, relaySettings); - if let Err(error) = daemon_interface.update_relay_settings(update) { + if let Err(error) = daemon_interface.set_relay_settings(update) { log::error!( "{}", error.display_chain_with_msg("Failed to update relay settings") diff --git a/mullvad-management-interface/proto/management_interface.proto b/mullvad-management-interface/proto/management_interface.proto index 4846f896e2..35417584b3 100644 --- a/mullvad-management-interface/proto/management_interface.proto +++ b/mullvad-management-interface/proto/management_interface.proto @@ -28,9 +28,9 @@ service ManagementService { // Relays and tunnel constraints rpc UpdateRelayLocations(google.protobuf.Empty) returns (google.protobuf.Empty) {} - rpc UpdateRelaySettings(RelaySettingsUpdate) returns (google.protobuf.Empty) {} rpc GetRelayLocations(google.protobuf.Empty) returns (RelayList) {} rpc GetCurrentLocation(google.protobuf.Empty) returns (GeoIpLocation) {} + rpc SetRelaySettings(RelaySettings) returns (google.protobuf.Empty) {} rpc SetBridgeSettings(BridgeSettings) returns (google.protobuf.Empty) {} rpc SetBridgeState(BridgeState) returns (google.protobuf.Empty) {} rpc SetObfuscationSettings(ObfuscationSettings) returns (google.protobuf.Empty) {} @@ -101,13 +101,6 @@ service ManagementService { message UUID { string value = 1; } -message RelaySettingsUpdate { - oneof type { - CustomRelaySettings custom = 1; - NormalRelaySettingsUpdate normal = 2; - } -} - message AccountData { google.protobuf.Timestamp expiry = 1; } message AccountHistory { google.protobuf.StringValue token = 1; } @@ -419,20 +412,6 @@ message NormalRelaySettings { Ownership ownership = 6; } -// Constraints are only updated for fields that are provided -message NormalRelaySettingsUpdate { - LocationConstraint location = 1; - ProviderUpdate providers = 2; - TunnelTypeUpdate tunnel_type = 3; - WireguardConstraints wireguard_constraints = 4; - OpenvpnConstraints openvpn_constraints = 5; - OwnershipUpdate ownership = 6; -} - -message ProviderUpdate { repeated string providers = 1; } - -message TunnelTypeUpdate { optional TunnelType tunnel_type = 2; } - message TransportPort { TransportProtocol protocol = 1; optional uint32 port = 2; @@ -440,8 +419,6 @@ message TransportPort { message OpenvpnConstraints { TransportPort port = 1; } -message OwnershipUpdate { Ownership ownership = 1; } - enum IpVersion { V4 = 0; V6 = 1; diff --git a/mullvad-management-interface/src/client.rs b/mullvad-management-interface/src/client.rs index 417083e161..1c15a169cd 100644 --- a/mullvad-management-interface/src/client.rs +++ b/mullvad-management-interface/src/client.rs @@ -8,7 +8,7 @@ use mullvad_types::{ custom_list::{CustomList, Id}, device::{Device, DeviceEvent, DeviceId, DeviceState, RemoveDeviceEvent}, location::GeoIpLocation, - relay_constraints::{BridgeSettings, BridgeState, ObfuscationSettings, RelaySettingsUpdate}, + relay_constraints::{BridgeSettings, BridgeState, ObfuscationSettings, RelaySettings}, relay_list::RelayList, settings::{DnsOptions, Settings}, states::TunnelState, @@ -221,10 +221,10 @@ impl MullvadProxyClient { Ok(()) } - pub async fn update_relay_settings(&mut self, update: RelaySettingsUpdate) -> Result<()> { - let update = types::RelaySettingsUpdate::from(update); + pub async fn set_relay_settings(&mut self, update: RelaySettings) -> Result<()> { + let update = types::RelaySettings::from(update); self.0 - .update_relay_settings(update) + .set_relay_settings(update) .await .map_err(Error::Rpc)?; Ok(()) diff --git a/mullvad-management-interface/src/types/conversions/relay_constraints.rs b/mullvad-management-interface/src/types/conversions/relay_constraints.rs index b33bf3c165..cff2bb5847 100644 --- a/mullvad-management-interface/src/types/conversions/relay_constraints.rs +++ b/mullvad-management-interface/src/types/conversions/relay_constraints.rs @@ -1,10 +1,9 @@ use crate::types::{conversions::net::try_tunnel_type_from_i32, proto, FromProtobufTypeError}; use mullvad_types::{ custom_list::Id, - relay_constraints::{Constraint, GeographicLocationConstraint, RelaySettingsUpdate}, + relay_constraints::{Constraint, GeographicLocationConstraint}, }; use std::str::FromStr; -use talpid_types::net::TunnelType; impl TryFrom<&proto::WireguardConstraints> for mullvad_types::relay_constraints::WireguardConstraints @@ -131,176 +130,6 @@ impl TryFrom<proto::RelaySettings> for mullvad_types::relay_constraints::RelaySe } } -impl From<RelaySettingsUpdate> for proto::RelaySettingsUpdate { - fn from(relay_settings_update: RelaySettingsUpdate) -> Self { - match relay_settings_update { - RelaySettingsUpdate::Normal(constraints) => proto::RelaySettingsUpdate { - r#type: Some(proto::relay_settings_update::Type::Normal( - proto::NormalRelaySettingsUpdate { - location: constraints - .location - .and_then(|constraint| match constraint { - Constraint::Any => None, - Constraint::Only(location) => { - Some(proto::LocationConstraint::from(location)) - } - }), - providers: constraints - .providers - .map(|constraint| proto::ProviderUpdate { - providers: convert_providers_constraint(&constraint), - }), - ownership: constraints - .ownership - .map(|ownership| proto::OwnershipUpdate { - ownership: i32::from(convert_ownership_constraint(&ownership)), - }), - tunnel_type: constraints.tunnel_protocol.map(|protocol| { - proto::TunnelTypeUpdate { - tunnel_type: protocol - .map(|protocol| { - i32::from(match protocol { - TunnelType::Wireguard => proto::TunnelType::Wireguard, - TunnelType::OpenVpn => proto::TunnelType::Openvpn, - }) - }) - .option(), - } - }), - wireguard_constraints: constraints.wireguard_constraints.map( - |wireguard_constraints| proto::WireguardConstraints { - port: wireguard_constraints.port.map(u32::from).option(), - ip_version: wireguard_constraints - .ip_version - .option() - .map(|ipv| i32::from(proto::IpVersion::from(ipv))), - use_multihop: wireguard_constraints.use_multihop, - entry_location: wireguard_constraints - .entry_location - .option() - .map(proto::LocationConstraint::from), - }, - ), - openvpn_constraints: constraints.openvpn_constraints.map( - |openvpn_constraints| proto::OpenvpnConstraints { - port: openvpn_constraints - .port - .option() - .map(proto::TransportPort::from), - }, - ), - }, - )), - }, - RelaySettingsUpdate::CustomTunnelEndpoint(endpoint) => proto::RelaySettingsUpdate { - r#type: Some(proto::relay_settings_update::Type::Custom( - proto::CustomRelaySettings { - host: endpoint.host.to_string(), - config: Some(proto::ConnectionConfig::from(endpoint.config)), - }, - )), - }, - } - } -} - -impl TryFrom<proto::RelaySettingsUpdate> for mullvad_types::relay_constraints::RelaySettingsUpdate { - type Error = FromProtobufTypeError; - - fn try_from( - settings: proto::RelaySettingsUpdate, - ) -> Result<mullvad_types::relay_constraints::RelaySettingsUpdate, Self::Error> { - use mullvad_types::{relay_constraints as mullvad_constraints, CustomTunnelEndpoint}; - - let update_value = settings - .r#type - .ok_or(FromProtobufTypeError::InvalidArgument( - "missing relay settings", - ))?; - - match update_value { - proto::relay_settings_update::Type::Custom(settings) => { - let config = settings - .config - .ok_or(FromProtobufTypeError::InvalidArgument( - "missing relay connection config", - ))?; - let config = mullvad_types::ConnectionConfig::try_from(config)?; - Ok( - mullvad_constraints::RelaySettingsUpdate::CustomTunnelEndpoint( - CustomTunnelEndpoint { - host: settings.host, - config, - }, - ), - ) - } - - proto::relay_settings_update::Type::Normal(settings) => { - // If `location` isn't provided, no changes are made. - // If `location` is provided, but is an empty vector, - // then the constraint is set to `Constraint::Any`. - let location = settings.location.and_then(|loc| { - Constraint::<mullvad_types::relay_constraints::LocationConstraint>::try_from( - loc, - ) - .ok() - }); - let providers = if let Some(ref provider_update) = settings.providers { - Some(try_providers_constraint_from_proto( - &provider_update.providers, - )?) - } else { - None - }; - let ownership = if let Some(ref ownership_update) = settings.ownership { - Some(try_ownership_constraint_from_i32( - ownership_update.ownership, - )?) - } else { - None - }; - let tunnel_protocol = if let Some(update) = settings.tunnel_type { - Some(Constraint::from( - update - .tunnel_type - .map(try_tunnel_type_from_i32) - .transpose()?, - )) - } else { - None - }; - let openvpn_constraints = - if let Some(ref constraints) = settings.openvpn_constraints { - Some(mullvad_constraints::OpenVpnConstraints::try_from( - constraints, - )?) - } else { - None - }; - let wireguard_constraints = - if let Some(ref constraints) = settings.wireguard_constraints { - Some(mullvad_constraints::WireguardConstraints::try_from( - constraints, - )?) - } else { - None - }; - Ok(mullvad_constraints::RelaySettingsUpdate::Normal( - mullvad_constraints::RelayConstraintsUpdate { - location, - providers, - ownership, - tunnel_protocol, - wireguard_constraints, - openvpn_constraints, - }, - )) - } - } - } -} - impl From<mullvad_types::relay_constraints::BridgeState> for proto::BridgeState { fn from(state: mullvad_types::relay_constraints::BridgeState) -> Self { use mullvad_types::relay_constraints::BridgeState; diff --git a/mullvad-relay-selector/src/lib.rs b/mullvad-relay-selector/src/lib.rs index 0ce93f7fcd..1046624af7 100644 --- a/mullvad-relay-selector/src/lib.rs +++ b/mullvad-relay-selector/src/lib.rs @@ -1292,8 +1292,8 @@ mod test { use mullvad_types::{ custom_list::CustomListsSettings, relay_constraints::{ - BridgeConstraints, GeographicLocationConstraint, RelayConstraints, - RelayConstraintsUpdate, RelaySettingsUpdate, WireguardConstraints, + BridgeConstraints, GeographicLocationConstraint, RelayConstraints, RelaySettings, + WireguardConstraints, }, relay_list::{ OpenVpnEndpoint, OpenVpnEndpointData, Relay, RelayListCity, RelayListCountry, @@ -2180,15 +2180,13 @@ mod test { ] { { let mut config = relay_selector.config.lock(); - config.relay_settings = config.relay_settings.merge(RelaySettingsUpdate::Normal( - RelayConstraintsUpdate { - tunnel_protocol: Some(tunnel_protocol), - location: Some(Constraint::Only(LocationConstraint::from( - GeographicLocationConstraint::Country("se".to_string()), - ))), - ..Default::default() - }, - )); + config.relay_settings = RelaySettings::Normal(RelayConstraints { + tunnel_protocol, + location: Constraint::Only(LocationConstraint::from( + GeographicLocationConstraint::Country("se".to_string()), + )), + ..RelayConstraints::default() + }); } let mut actual_ports = HashSet::new(); @@ -2267,13 +2265,10 @@ mod test { // Verify that bridges are ignored when tunnel protocol is WireGuard { let mut config = relay_selector.config.lock(); - config.relay_settings = - config - .relay_settings - .merge(RelaySettingsUpdate::Normal(RelayConstraintsUpdate { - tunnel_protocol: Some(Constraint::Only(TunnelType::Wireguard)), - ..Default::default() - })); + config.relay_settings = RelaySettings::Normal(RelayConstraints { + tunnel_protocol: Constraint::Only(TunnelType::Wireguard), + ..RelayConstraints::default() + }); } for i in 0..20 { let (_relay, bridge, _obfs) = relay_selector.get_relay(i).unwrap(); diff --git a/mullvad-types/src/relay_constraints.rs b/mullvad-types/src/relay_constraints.rs index d0178c3c98..ebc2b627b3 100644 --- a/mullvad-types/src/relay_constraints.rs +++ b/mullvad-types/src/relay_constraints.rs @@ -205,17 +205,22 @@ pub enum RelaySettings { } impl RelaySettings { - pub fn merge(&self, update: RelaySettingsUpdate) -> Self { - match update { - RelaySettingsUpdate::CustomTunnelEndpoint(relay) => { - RelaySettings::CustomTunnelEndpoint(relay) + /// Returns false if the specified relay settings update explicitly do not allow for bridging + /// (i.e. use UDP instead of TCP) + pub fn supports_bridge(&self) -> bool { + match &self { + RelaySettings::CustomTunnelEndpoint(endpoint) => { + endpoint.endpoint().protocol == TransportProtocol::Tcp } - RelaySettingsUpdate::Normal(constraint_update) => RelaySettings::Normal(match *self { - RelaySettings::CustomTunnelEndpoint(_) => { - RelayConstraints::default().merge(constraint_update) + RelaySettings::Normal(update) => !matches!( + &update.openvpn_constraints, + OpenVpnConstraints { + port: Constraint::Only(TransportPort { + protocol: TransportProtocol::Udp, + .. + }) } - RelaySettings::Normal(ref constraint) => constraint.merge(constraint_update), - }), + ), } } } @@ -362,9 +367,8 @@ impl<'a> fmt::Display for LocationConstraintFormatter<'a> { } /// Limits the set of [`crate::relay_list::Relay`]s that a `RelaySelector` may select. -#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] +#[derive(Default, Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] #[serde(default)] -#[cfg_attr(not(target_os = "android"), derive(Default))] #[cfg_attr(target_os = "android", derive(IntoJava))] #[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))] pub struct RelayConstraints { @@ -378,37 +382,6 @@ pub struct RelayConstraints { pub openvpn_constraints: OpenVpnConstraints, } -#[cfg(target_os = "android")] -impl Default for RelayConstraints { - fn default() -> Self { - RelayConstraints { - tunnel_protocol: Constraint::Only(TunnelType::Wireguard), - location: Constraint::default(), - providers: Constraint::default(), - ownership: Constraint::default(), - wireguard_constraints: WireguardConstraints::default(), - openvpn_constraints: OpenVpnConstraints::default(), - } - } -} - -impl RelayConstraints { - pub fn merge(&self, update: RelayConstraintsUpdate) -> Self { - RelayConstraints { - location: update.location.unwrap_or_else(|| self.location.clone()), - providers: update.providers.unwrap_or_else(|| self.providers.clone()), - ownership: update.ownership.unwrap_or(self.ownership), - tunnel_protocol: update.tunnel_protocol.unwrap_or(self.tunnel_protocol), - wireguard_constraints: update - .wireguard_constraints - .unwrap_or_else(|| self.wireguard_constraints.clone()), - openvpn_constraints: update - .openvpn_constraints - .unwrap_or(self.openvpn_constraints), - } - } -} - pub struct RelayConstraintsFormatter<'a> { pub constraints: &'a RelayConstraints, pub custom_lists: &'a CustomListsSettings, @@ -947,51 +920,3 @@ pub struct InternalBridgeConstraints { pub ownership: Constraint<Ownership>, pub transport_protocol: Constraint<TransportProtocol>, } - -/// Used to update the [`RelaySettings`] used in `mullvad-daemon`. -#[derive(Debug, Deserialize, Serialize)] -#[cfg_attr(target_os = "android", derive(FromJava))] -#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))] -#[serde(rename_all = "snake_case")] -pub enum RelaySettingsUpdate { - #[cfg_attr(target_os = "android", jnix(deny))] - CustomTunnelEndpoint(CustomTunnelEndpoint), - Normal(RelayConstraintsUpdate), -} - -impl RelaySettingsUpdate { - /// Returns false if the specified relay settings update explicitly do not allow for bridging - /// (i.e. use UDP instead of TCP) - pub fn supports_bridge(&self) -> bool { - match &self { - RelaySettingsUpdate::CustomTunnelEndpoint(endpoint) => { - endpoint.endpoint().protocol == TransportProtocol::Tcp - } - RelaySettingsUpdate::Normal(update) => !matches!( - &update.openvpn_constraints, - Some(OpenVpnConstraints { - port: Constraint::Only(TransportPort { - protocol: TransportProtocol::Udp, - .. - }) - }) - ), - } - } -} - -/// Used in [`RelaySettings`] to change relay constraints in the daemon. -#[derive(Debug, Default, Deserialize, Serialize)] -#[cfg_attr(target_os = "android", derive(FromJava))] -#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))] -#[serde(default)] -pub struct RelayConstraintsUpdate { - pub location: Option<Constraint<LocationConstraint>>, - pub providers: Option<Constraint<Providers>>, - pub ownership: Option<Constraint<Ownership>>, - #[cfg_attr(target_os = "android", jnix(default))] - pub tunnel_protocol: Option<Constraint<TunnelType>>, - pub wireguard_constraints: Option<WireguardConstraints>, - #[cfg_attr(target_os = "android", jnix(default))] - pub openvpn_constraints: Option<OpenVpnConstraints>, -} diff --git a/mullvad-types/src/settings/mod.rs b/mullvad-types/src/settings/mod.rs index 6ade7dea32..5978c6a736 100644 --- a/mullvad-types/src/settings/mod.rs +++ b/mullvad-types/src/settings/mod.rs @@ -4,7 +4,7 @@ use crate::{ relay_constraints::{ BridgeConstraints, BridgeSettings, BridgeState, Constraint, GeographicLocationConstraint, LocationConstraint, ObfuscationSettings, RelayConstraints, RelaySettings, - RelaySettingsFormatter, RelaySettingsUpdate, SelectedObfuscation, WireguardConstraints, + RelaySettingsFormatter, SelectedObfuscation, WireguardConstraints, }, wireguard, }; @@ -150,11 +150,9 @@ impl Settings { self.relay_settings.clone() } - pub fn update_relay_settings(&mut self, update: RelaySettingsUpdate) { - let update_supports_bridge = update.supports_bridge(); - let new_settings = self.relay_settings.merge(update); + pub fn set_relay_settings(&mut self, new_settings: RelaySettings) { if self.relay_settings != new_settings { - if !update_supports_bridge && BridgeState::On == self.bridge_state { + if !new_settings.supports_bridge() && BridgeState::On == self.bridge_state { self.bridge_state = BridgeState::Auto; } diff --git a/test/test-manager/src/tests/dns.rs b/test/test-manager/src/tests/dns.rs index 69de367b88..cd30df2c88 100644 --- a/test/test-manager/src/tests/dns.rs +++ b/test/test-manager/src/tests/dns.rs @@ -24,7 +24,7 @@ use crate::vm::network::{ CUSTOM_TUN_REMOTE_TUN_ADDR, NON_TUN_GATEWAY, }; -use super::helpers::update_relay_settings; +use super::helpers::set_relay_settings; /// How long to wait for expected "DNS queries" to appear const MONITOR_TIMEOUT: Duration = Duration::from_secs(5); @@ -652,7 +652,7 @@ async fn connect_local_wg_relay(mullvad_client: &mut ManagementServiceClient) -> }), }); - update_relay_settings(mullvad_client, relay_settings) + set_relay_settings(mullvad_client, relay_settings) .await .expect("failed to update relay settings"); diff --git a/test/test-manager/src/tests/helpers.rs b/test/test-manager/src/tests/helpers.rs index c51f6875da..9ad3df3cdb 100644 --- a/test/test-manager/src/tests/helpers.rs +++ b/test/test-manager/src/tests/helpers.rs @@ -346,7 +346,7 @@ pub async fn reset_relay_settings( let bridge_state = BridgeState::Auto; let obfuscation_settings = ObfuscationSettings::default(); - update_relay_settings(mullvad_client, relay_settings) + set_relay_settings(mullvad_client, relay_settings) .await .map_err(|error| { Error::DaemonError(format!("Failed to reset relay settings: {}", error)) @@ -365,14 +365,14 @@ pub async fn reset_relay_settings( Ok(()) } -pub async fn update_relay_settings( +pub async fn set_relay_settings( mullvad_client: &mut ManagementServiceClient, relay_settings_update: RelaySettingsUpdate, ) -> Result<(), Error> { let update = types::RelaySettingsUpdate::from(relay_settings_update); mullvad_client - .update_relay_settings(update) + .set_relay_settings(update) .await .map_err(|error| Error::DaemonError(format!("Failed to set relay settings: {}", error)))?; Ok(()) diff --git a/test/test-manager/src/tests/tunnel.rs b/test/test-manager/src/tests/tunnel.rs index 99e30b087c..79a6ab3575 100644 --- a/test/test-manager/src/tests/tunnel.rs +++ b/test/test-manager/src/tests/tunnel.rs @@ -1,4 +1,4 @@ -use super::helpers::{self, connect_and_wait, disconnect_and_wait, update_relay_settings}; +use super::helpers::{self, connect_and_wait, disconnect_and_wait, set_relay_settings}; use super::{Error, TestContext}; use std::net::IpAddr; @@ -54,7 +54,7 @@ pub async fn test_openvpn_tunnel( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -97,7 +97,7 @@ pub async fn test_wireguard_tunnel( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -148,7 +148,7 @@ pub async fn test_udp2tcp_tunnel( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -246,7 +246,7 @@ pub async fn test_bridge( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -325,7 +325,7 @@ pub async fn test_multihop( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -386,7 +386,7 @@ pub async fn test_wireguard_autoconnect( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -428,7 +428,7 @@ pub async fn test_openvpn_autoconnect( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -485,7 +485,7 @@ pub async fn test_quantum_resistant_tunnel( tunnel_protocol: Some(Constraint::Only(TunnelType::Wireguard)), ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("Failed to update relay settings"); @@ -564,7 +564,7 @@ pub async fn test_quantum_resistant_multihop_udp2tcp_tunnel( .expect("Failed to enable obfuscation"); mullvad_client - .update_relay_settings(types::RelaySettingsUpdate::from( + .set_relay_settings(types::RelaySettingsUpdate::from( RelaySettingsUpdate::Normal(RelayConstraintsUpdate { wireguard_constraints: WireguardConstraints { use_multihop: true, diff --git a/test/test-manager/src/tests/tunnel_state.rs b/test/test-manager/src/tests/tunnel_state.rs index 55b1d2074e..4cd0f54f1d 100644 --- a/test/test-manager/src/tests/tunnel_state.rs +++ b/test/test-manager/src/tests/tunnel_state.rs @@ -1,6 +1,6 @@ use super::helpers::{ self, connect_and_wait, disconnect_and_wait, get_tunnel_state, send_guest_probes, - unreachable_wireguard_tunnel, update_relay_settings, wait_for_tunnel_state, + unreachable_wireguard_tunnel, set_relay_settings, wait_for_tunnel_state, }; use super::{ui, Error, TestContext}; use crate::assert_tunnel_state; @@ -93,7 +93,7 @@ pub async fn test_connecting_state( config: mullvad_types::ConnectionConfig::Wireguard(unreachable_wireguard_tunnel()), }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -159,7 +159,7 @@ pub async fn test_connecting_state( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -200,7 +200,7 @@ pub async fn test_error_state( .await .expect("failed to disable LAN sharing"); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -249,7 +249,7 @@ pub async fn test_error_state( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); @@ -291,7 +291,7 @@ pub async fn test_connected_state( ..Default::default() }); - update_relay_settings(&mut mullvad_client, relay_settings) + set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); diff --git a/test/test-manager/src/tests/ui.rs b/test/test-manager/src/tests/ui.rs index 1e97430061..b0308fb9f7 100644 --- a/test/test-manager/src/tests/ui.rs +++ b/test/test-manager/src/tests/ui.rs @@ -100,7 +100,7 @@ pub async fn test_ui_tunnel_settings( ..Default::default() }); - helpers::update_relay_settings(&mut mullvad_client, relay_settings) + helpers::set_relay_settings(&mut mullvad_client, relay_settings) .await .expect("failed to update relay settings"); |
