diff options
| author | Emīls <emils@mullvad.net> | 2020-07-15 12:37:17 +0100 |
|---|---|---|
| committer | Emīls <emils@mullvad.net> | 2020-07-16 15:57:02 +0100 |
| commit | 598bb3542564d555c7d75fdf49eb458c9c320c7e (patch) | |
| tree | 683b6770be37ebbfb309d63c72fb7cfcf564db3b | |
| parent | 16847ca723bfbd9750136228b3c1b1665a2cbce4 (diff) | |
| download | mullvadvpn-598bb3542564d555c7d75fdf49eb458c9c320c7e.tar.xz mullvadvpn-598bb3542564d555c7d75fdf49eb458c9c320c7e.zip | |
Use talpid_types TunnelType instead of TunnelProtocol
| -rw-r--r-- | mullvad-cli/src/cmds/relay.rs | 10 | ||||
| -rw-r--r-- | mullvad-daemon/src/relays.rs | 40 | ||||
| -rw-r--r-- | mullvad-types/src/relay_constraints.rs | 34 | ||||
| -rw-r--r-- | mullvad-types/src/settings/migrations/v1.rs | 6 |
4 files changed, 35 insertions, 55 deletions
diff --git a/mullvad-cli/src/cmds/relay.rs b/mullvad-cli/src/cmds/relay.rs index e9921b972b..c4e8c72c81 100644 --- a/mullvad-cli/src/cmds/relay.rs +++ b/mullvad-cli/src/cmds/relay.rs @@ -9,11 +9,13 @@ use std::{ use mullvad_types::{ relay_constraints::{ Constraint, OpenVpnConstraints, RelayConstraintsUpdate, RelaySettingsUpdate, - TunnelProtocol, WireguardConstraints, + WireguardConstraints, }, ConnectionConfig, CustomTunnelEndpoint, }; -use talpid_types::net::{all_of_the_internet, openvpn, wireguard, Endpoint, TransportProtocol}; +use talpid_types::net::{ + all_of_the_internet, openvpn, wireguard, Endpoint, TransportProtocol, TunnelType, +}; pub struct Relay; @@ -318,8 +320,8 @@ impl Relay { fn set_tunnel_protocol(&self, matches: &clap::ArgMatches<'_>) -> Result<()> { let tunnel_protocol = match matches.value_of("tunnel protocol").unwrap() { - "wireguard" => Constraint::Only(TunnelProtocol::Wireguard), - "openvpn" => Constraint::Only(TunnelProtocol::OpenVpn), + "wireguard" => Constraint::Only(TunnelType::Wireguard), + "openvpn" => Constraint::Only(TunnelType::OpenVpn), "any" => Constraint::Any, _ => unreachable!(), }; diff --git a/mullvad-daemon/src/relays.rs b/mullvad-daemon/src/relays.rs index 2f8e7a7c61..320994c743 100644 --- a/mullvad-daemon/src/relays.rs +++ b/mullvad-daemon/src/relays.rs @@ -9,7 +9,7 @@ use mullvad_types::{ location::Location, relay_constraints::{ BridgeState, Constraint, InternalBridgeConstraints, LocationConstraint, Match, - OpenVpnConstraints, RelayConstraints, TunnelProtocol, WireguardConstraints, + OpenVpnConstraints, RelayConstraints, WireguardConstraints, }, relay_list::{OpenVpnEndpointData, Relay, RelayList, RelayTunnels, WireguardEndpointData}, }; @@ -24,7 +24,7 @@ use std::{ time::{self, Duration, SystemTime}, }; use talpid_types::{ - net::{all_of_the_internet, openvpn::ProxySettings, wireguard, TransportProtocol}, + net::{all_of_the_internet, openvpn::ProxySettings, wireguard, TransportProtocol, TunnelType}, ErrorExt, }; @@ -252,11 +252,7 @@ impl RelaySelector { wg_key_exists, ) } else { - ( - Constraint::Any, - TransportProtocol::Tcp, - TunnelProtocol::OpenVpn, - ) + (Constraint::Any, TransportProtocol::Tcp, TunnelType::OpenVpn) }; @@ -290,7 +286,7 @@ impl RelaySelector { relay_constraints.tunnel_protocol = Constraint::Only(preferred_tunnel); } - Constraint::Only(TunnelProtocol::OpenVpn) => { + Constraint::Only(TunnelType::OpenVpn) => { let openvpn_constraints = &mut relay_constraints.openvpn_constraints; *openvpn_constraints = original_constraints.openvpn_constraints; if *bridge_state == BridgeState::On && openvpn_constraints.protocol.is_any() { @@ -304,7 +300,7 @@ impl RelaySelector { openvpn_constraints.protocol = Constraint::Only(preferred_protocol); } } - Constraint::Only(TunnelProtocol::Wireguard) => { + Constraint::Only(TunnelType::Wireguard) => { relay_constraints.wireguard_constraints = original_constraints.wireguard_constraints; // This ensures that if after the first 2 failed attempts the daemon does not @@ -381,7 +377,7 @@ impl RelaySelector { retry_attempt: u32, location_constraint: &Constraint<LocationConstraint>, wg_key_exists: bool, - ) -> (Constraint<u16>, TransportProtocol, TunnelProtocol) { + ) -> (Constraint<u16>, TransportProtocol, TunnelType) { #[cfg(not(target_os = "windows"))] { let location_supports_wireguard = @@ -395,7 +391,7 @@ impl RelaySelector { if !location_supports_wireguard || !wg_key_exists { let (preferred_port, preferred_protocol) = Self::preferred_openvpn_constraints(retry_attempt); - return (preferred_port, preferred_protocol, TunnelProtocol::OpenVpn); + return (preferred_port, preferred_protocol, TunnelType::OpenVpn); } @@ -406,17 +402,17 @@ impl RelaySelector { 0 if location_supports_wireguard => ( Constraint::Any, TransportProtocol::Udp, - TunnelProtocol::Wireguard, + TunnelType::Wireguard, ), 1 => ( Constraint::Only(53), TransportProtocol::Udp, - TunnelProtocol::Wireguard, + TunnelType::Wireguard, ), _ => { let (preferred_port, preferred_protocol) = Self::preferred_openvpn_constraints(retry_attempt - 2); - (preferred_port, preferred_protocol, TunnelProtocol::OpenVpn) + (preferred_port, preferred_protocol, TunnelType::OpenVpn) } } } @@ -427,7 +423,7 @@ impl RelaySelector { Self::preferred_openvpn_constraints(retry_attempt); - (preferred_port, preferred_protocol, TunnelProtocol::OpenVpn) + (preferred_port, preferred_protocol, TunnelType::OpenVpn) } } @@ -492,7 +488,7 @@ impl RelaySelector { }; relay } - Constraint::Only(TunnelProtocol::Wireguard) => { + Constraint::Only(TunnelType::Wireguard) => { let mut relay = relay.clone(); relay.tunnels = RelayTunnels { wireguard: Self::matching_wireguard_tunnels( @@ -504,7 +500,7 @@ impl RelaySelector { relay } - Constraint::Only(TunnelProtocol::OpenVpn) => { + Constraint::Only(TunnelType::OpenVpn) => { let mut relay = relay.clone(); relay.tunnels = RelayTunnels { openvpn: Self::matching_openvpn_tunnels( @@ -522,8 +518,8 @@ impl RelaySelector { Constraint::Any => { !relay.tunnels.openvpn.is_empty() || !relay.tunnels.wireguard.is_empty() } - Constraint::Only(TunnelProtocol::OpenVpn) => !relay.tunnels.openvpn.is_empty(), - Constraint::Only(TunnelProtocol::Wireguard) => !relay.tunnels.wireguard.is_empty(), + Constraint::Only(TunnelType::OpenVpn) => !relay.tunnels.openvpn.is_empty(), + Constraint::Only(TunnelType::Wireguard) => !relay.tunnels.wireguard.is_empty(), }; if relay_matches { @@ -657,13 +653,13 @@ impl RelaySelector { // TODO: Handle Constraint::Any case by selecting from both openvpn and wireguard // tunnels once wireguard is mature enough #[cfg(not(target_os = "android"))] - Constraint::Only(TunnelProtocol::OpenVpn) | Constraint::Any => relay + Constraint::Only(TunnelType::OpenVpn) | Constraint::Any => relay .tunnels .openvpn .choose(&mut self.rng) .cloned() .map(|endpoint| endpoint.into_mullvad_endpoint(relay.ipv4_addr_in.into())), - Constraint::Only(TunnelProtocol::Wireguard) => relay + Constraint::Only(TunnelType::Wireguard) => relay .tunnels .wireguard .choose(&mut self.rng) @@ -689,7 +685,7 @@ impl RelaySelector { ) }), #[cfg(target_os = "android")] - Constraint::Only(TunnelProtocol::OpenVpn) => None, + Constraint::Only(TunnelType::OpenVpn) => None, } } diff --git a/mullvad-types/src/relay_constraints.rs b/mullvad-types/src/relay_constraints.rs index fcf81daede..f8907cba9c 100644 --- a/mullvad-types/src/relay_constraints.rs +++ b/mullvad-types/src/relay_constraints.rs @@ -10,7 +10,7 @@ use crate::{ use jnix::{FromJava, IntoJava}; use serde::{Deserialize, Serialize}; use std::fmt; -use talpid_types::net::{openvpn::ProxySettings, TransportProtocol}; +use talpid_types::net::{openvpn::ProxySettings, TransportProtocol, TunnelType}; pub trait Match<T> { @@ -107,7 +107,7 @@ impl RelaySettings { pub(crate) fn ensure_bridge_compatibility(&mut self) { match self { RelaySettings::Normal(ref mut constraints) => { - if constraints.tunnel_protocol == Constraint::Only(TunnelProtocol::Wireguard) { + if constraints.tunnel_protocol == Constraint::Only(TunnelType::Wireguard) { constraints.tunnel_protocol = Constraint::Any; } if constraints.openvpn_constraints.protocol @@ -138,7 +138,7 @@ impl RelaySettings { pub struct RelayConstraints { pub location: Constraint<LocationConstraint>, #[cfg_attr(target_os = "android", jnix(skip))] - pub tunnel_protocol: Constraint<TunnelProtocol>, + pub tunnel_protocol: Constraint<TunnelType>, #[cfg_attr(target_os = "android", jnix(skip))] pub wireguard_constraints: WireguardConstraints, #[cfg_attr(target_os = "android", jnix(skip))] @@ -150,7 +150,7 @@ impl Default for RelayConstraints { fn default() -> Self { RelayConstraints { location: Constraint::Any, - tunnel_protocol: Constraint::Only(TunnelProtocol::Wireguard), + tunnel_protocol: Constraint::Only(TunnelType::Wireguard), wireguard_constraints: WireguardConstraints::default(), openvpn_constraints: OpenVpnConstraints::default(), } @@ -185,10 +185,10 @@ impl fmt::Display for RelayConstraints { Constraint::Only(ref tunnel_protocol) => { tunnel_protocol.fmt(f)?; match tunnel_protocol { - TunnelProtocol::Wireguard => { + TunnelType::Wireguard => { write!(f, " over {}", &self.wireguard_constraints)?; } - TunnelProtocol::OpenVpn => { + TunnelType::OpenVpn => { write!(f, " over {}", &self.openvpn_constraints)?; } }; @@ -230,24 +230,6 @@ impl fmt::Display for LocationConstraint { } } -/// Used in [`RelayConstraints`] to limit relay selection based on protocol. -#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] -pub enum TunnelProtocol { - #[serde(rename = "wireguard")] - Wireguard, - #[serde(rename = "openvpn")] - OpenVpn, -} - -impl fmt::Display for TunnelProtocol { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - match self { - TunnelProtocol::Wireguard => write!(f, "WireGuard"), - TunnelProtocol::OpenVpn => write!(f, "OpenVPN"), - } - } -} - /// Deprecated. Contains protocol-specific constraints for relay selection. #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] pub enum TunnelConstraints { @@ -421,7 +403,7 @@ impl RelaySettingsUpdate { endpoint.endpoint().protocol == TransportProtocol::Tcp } RelaySettingsUpdate::Normal(update) => { - if let Some(Constraint::Only(TunnelProtocol::Wireguard)) = &update.tunnel_protocol { + if let Some(Constraint::Only(TunnelType::Wireguard)) = &update.tunnel_protocol { false } else if let Some(constraints) = &update.openvpn_constraints { if let Constraint::Only(TransportProtocol::Udp) = &constraints.protocol { @@ -445,7 +427,7 @@ impl RelaySettingsUpdate { pub struct RelayConstraintsUpdate { pub location: Option<Constraint<LocationConstraint>>, #[cfg_attr(target_os = "android", jnix(default))] - pub tunnel_protocol: Option<Constraint<TunnelProtocol>>, + pub tunnel_protocol: Option<Constraint<TunnelType>>, #[cfg_attr(target_os = "android", jnix(default))] pub wireguard_constraints: Option<WireguardConstraints>, #[cfg_attr(target_os = "android", jnix(default))] diff --git a/mullvad-types/src/settings/migrations/v1.rs b/mullvad-types/src/settings/migrations/v1.rs index 268af6c048..22aa4d29a0 100644 --- a/mullvad-types/src/settings/migrations/v1.rs +++ b/mullvad-types/src/settings/migrations/v1.rs @@ -3,13 +3,13 @@ use crate::{ custom_tunnel::CustomTunnelEndpoint, relay_constraints::{ BridgeConstraints, BridgeSettings, BridgeState, Constraint, LocationConstraint, - OpenVpnConstraints, RelaySettings as NewRelaySettings, TunnelProtocol, - WireguardConstraints, + OpenVpnConstraints, RelaySettings as NewRelaySettings, WireguardConstraints, }, settings::TunnelOptions, }; use serde::{Deserialize, Serialize}; use std::io::Read; +use talpid_types::net::TunnelType; /// Mullvad daemon settings. @@ -95,7 +95,7 @@ fn migrate_relay_settings(relay_settings: RelaySettings) -> NewRelaySettings { } Constraint::Only(TunnelConstraints::Wireguard(constraints)) => { new_constraints.wireguard_constraints = constraints; - new_constraints.tunnel_protocol = Constraint::Only(TunnelProtocol::Wireguard); + new_constraints.tunnel_protocol = Constraint::Only(TunnelType::Wireguard); } }; crate::relay_constraints::RelaySettings::Normal(new_constraints) |
