Skip to content

Commit da01b47

Browse files
committed
Remove Option from Relay::location
1 parent 760acda commit da01b47

File tree

13 files changed

+82
-88
lines changed

13 files changed

+82
-88
lines changed

mullvad-api/src/relay_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn into_mullvad_relay(
159159
provider: relay.provider,
160160
weight: relay.weight,
161161
endpoint_data,
162-
location: Some(location),
162+
location,
163163
}
164164
}
165165

mullvad-cli/src/cmds/relay.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use itertools::Itertools;
44
use mullvad_management_interface::MullvadProxyClient;
55
use mullvad_types::{
66
constraints::{Constraint, Match},
7-
location::{CountryCode, Location},
7+
location::CountryCode,
88
relay_constraints::{
99
GeographicLocationConstraint, LocationConstraint, LocationConstraintFormatter,
1010
OpenVpnConstraints, Ownership, Provider, Providers, RelayConstraints, RelayOverride,
@@ -921,15 +921,11 @@ fn parse_transport_port(
921921

922922
fn relay_to_geographical_constraint(
923923
relay: mullvad_types::relay_list::Relay,
924-
) -> Option<GeographicLocationConstraint> {
925-
relay.location.map(
926-
|Location {
927-
country_code,
928-
city_code,
929-
..
930-
}| {
931-
GeographicLocationConstraint::Hostname(country_code, city_code, relay.hostname)
932-
},
924+
) -> GeographicLocationConstraint {
925+
GeographicLocationConstraint::Hostname(
926+
relay.location.country_code,
927+
relay.location.city_code,
928+
relay.hostname,
933929
)
934930
}
935931

@@ -952,10 +948,9 @@ pub async fn resolve_location_constraint(
952948
.find(|relay| relay.hostname.to_lowercase() == location_constraint_args.country)
953949
{
954950
if relay_filter(&matching_relay) {
955-
Ok(Constraint::Only(
956-
relay_to_geographical_constraint(matching_relay)
957-
.context("Selected relay did not contain a valid location")?,
958-
))
951+
Ok(Constraint::Only(relay_to_geographical_constraint(
952+
matching_relay,
953+
)))
959954
} else {
960955
bail!(
961956
"The relay `{}` is not valid for this operation",

mullvad-daemon/src/tunnel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ impl ParametersGenerator {
111111
hostname = exit.hostname.clone();
112112
obfuscator_hostname = take_hostname(obfuscator);
113113
bridge_hostname = None;
114-
location = exit.location.as_ref().cloned().unwrap();
114+
location = exit.location.clone();
115115
}
116116
#[cfg(not(target_os = "android"))]
117117
LastSelectedRelays::OpenVpn { relay, bridge } => {
118118
hostname = relay.hostname.clone();
119119
bridge_hostname = take_hostname(bridge);
120120
entry_hostname = None;
121121
obfuscator_hostname = None;
122-
location = relay.location.as_ref().cloned().unwrap();
122+
location = relay.location.clone();
123123
}
124124
};
125125

mullvad-management-interface/src/types/conversions/relay_list.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ impl From<mullvad_types::relay_list::Relay> for proto::Relay {
127127
)),
128128
_ => None,
129129
},
130-
location: relay.location.map(|location| proto::Location {
131-
country: location.country,
132-
country_code: location.country_code,
133-
city: location.city,
134-
city_code: location.city_code,
135-
latitude: location.latitude,
136-
longitude: location.longitude,
130+
location: Some(proto::Location {
131+
country: relay.location.country,
132+
country_code: relay.location.country_code,
133+
city: relay.location.city,
134+
city_code: relay.location.city_code,
135+
latitude: relay.location.latitude,
136+
longitude: relay.location.longitude,
137137
}),
138138
}
139139
}
@@ -269,14 +269,18 @@ impl TryFrom<proto::Relay> for mullvad_types::relay_list::Relay {
269269
provider: relay.provider,
270270
weight: relay.weight,
271271
endpoint_data,
272-
location: relay.location.map(|location| MullvadLocation {
273-
country: location.country,
274-
country_code: location.country_code,
275-
city: location.city,
276-
city_code: location.city_code,
277-
latitude: location.latitude,
278-
longitude: location.longitude,
279-
}),
272+
location: relay
273+
.location
274+
.map(|location| MullvadLocation {
275+
country: location.country,
276+
country_code: location.country_code,
277+
city: location.city,
278+
city_code: location.city_code,
279+
latitude: location.latitude,
280+
longitude: location.longitude,
281+
})
282+
.ok_or("missing relay location")
283+
.map_err(FromProtobufTypeError::InvalidArgument)?,
280284
})
281285
}
282286
}

mullvad-relay-selector/src/relay_selector/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,9 @@ impl RelaySelector {
954954
Err(Error::NoBridge)
955955
}
956956
TransportProtocol::Tcp => {
957-
let location = relay.location.as_ref().ok_or(Error::NoRelay)?;
958957
Self::get_bridge_for(
959958
bridge_query,
960-
location,
959+
&relay.location,
961960
// FIXME: This is temporary while talpid-core only supports TCP proxies
962961
TransportProtocol::Tcp,
963962
parsed_relays,
@@ -1037,7 +1036,7 @@ impl RelaySelector {
10371036
let matching_bridges: Vec<RelayWithDistance> = relays
10381037
.into_iter()
10391038
.map(|relay| RelayWithDistance {
1040-
distance: relay.location.as_ref().unwrap().distance_from(&location),
1039+
distance: relay.location.distance_from(&location),
10411040
relay,
10421041
})
10431042
.sorted_unstable_by_key(|relay| relay.distance as usize)
@@ -1075,7 +1074,7 @@ impl RelaySelector {
10751074
let matching_locations: Vec<Location> =
10761075
filter_matching_relay_list(query, parsed_relays.relays(), custom_lists)
10771076
.into_iter()
1078-
.filter_map(|relay| relay.location)
1077+
.map(|relay| relay.location)
10791078
.unique_by(|location| location.city.clone())
10801079
.collect();
10811080

mullvad-relay-selector/src/relay_selector/parsed_relays.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ impl ParsedRelays {
168168
for city in &mut country.cities {
169169
for relay in &mut city.relays {
170170
// Append location data
171-
relay.location = Some(Location {
171+
relay.location = Location {
172172
country: country.name.clone(),
173173
country_code: country.code.clone(),
174174
city: city.name.clone(),
175175
city_code: city.code.clone(),
176176
latitude: city.latitude,
177177
longitude: city.longitude,
178-
});
178+
};
179179

180180
// Append overrides
181181
if let Some(overrides) = remaining_overrides.remove(&relay.hostname) {

mullvad-relay-selector/tests/relay_selector.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use mullvad_relay_selector::{
1818
use mullvad_types::{
1919
constraints::Constraint,
2020
endpoint::MullvadEndpoint,
21+
location::Location,
2122
relay_constraints::{
2223
BridgeConstraints, BridgeState, GeographicLocationConstraint, Ownership, Providers,
2324
TransportPort,
@@ -29,6 +30,15 @@ use mullvad_types::{
2930
},
3031
};
3132

33+
static DUMMY_LOCATION: Lazy<Location> = Lazy::new(|| Location {
34+
country: "Sweden".to_string(),
35+
country_code: "se".to_string(),
36+
city: "Gothenburg".to_string(),
37+
city_code: "got".to_string(),
38+
latitude: 57.71,
39+
longitude: 11.97,
40+
});
41+
3242
static RELAYS: Lazy<RelayList> = Lazy::new(|| RelayList {
3343
etag: None,
3444
countries: vec![RelayListCountry {
@@ -56,7 +66,7 @@ static RELAYS: Lazy<RelayList> = Lazy::new(|| RelayList {
5666
.unwrap(),
5767
daita: false,
5868
}),
59-
location: None,
69+
location: DUMMY_LOCATION.clone(),
6070
},
6171
Relay {
6272
hostname: "se10-wireguard".to_string(),
@@ -74,7 +84,7 @@ static RELAYS: Lazy<RelayList> = Lazy::new(|| RelayList {
7484
.unwrap(),
7585
daita: false,
7686
}),
77-
location: None,
87+
location: DUMMY_LOCATION.clone(),
7888
},
7989
Relay {
8090
hostname: "se-got-001".to_string(),
@@ -86,7 +96,7 @@ static RELAYS: Lazy<RelayList> = Lazy::new(|| RelayList {
8696
provider: "provider2".to_string(),
8797
weight: 1,
8898
endpoint_data: RelayEndpointData::Openvpn,
89-
location: None,
99+
location: DUMMY_LOCATION.clone(),
90100
},
91101
Relay {
92102
hostname: "se-got-002".to_string(),
@@ -98,7 +108,7 @@ static RELAYS: Lazy<RelayList> = Lazy::new(|| RelayList {
98108
provider: "provider0".to_string(),
99109
weight: 1,
100110
endpoint_data: RelayEndpointData::Openvpn,
101-
location: None,
111+
location: DUMMY_LOCATION.clone(),
102112
},
103113
Relay {
104114
hostname: "se-got-br-001".to_string(),
@@ -110,7 +120,7 @@ static RELAYS: Lazy<RelayList> = Lazy::new(|| RelayList {
110120
provider: "provider3".to_string(),
111121
weight: 1,
112122
endpoint_data: RelayEndpointData::Bridge,
113-
location: None,
123+
location: DUMMY_LOCATION.clone(),
114124
},
115125
],
116126
}],
@@ -438,7 +448,7 @@ fn test_wireguard_entry() {
438448
.unwrap(),
439449
daita: false,
440450
}),
441-
location: None,
451+
location: DUMMY_LOCATION.clone(),
442452
},
443453
Relay {
444454
hostname: "se10-wireguard".to_string(),
@@ -456,7 +466,7 @@ fn test_wireguard_entry() {
456466
.unwrap(),
457467
daita: false,
458468
}),
459-
location: None,
469+
location: DUMMY_LOCATION.clone(),
460470
},
461471
],
462472
}],
@@ -956,7 +966,7 @@ fn test_include_in_country() {
956966
.unwrap(),
957967
daita: false,
958968
}),
959-
location: None,
969+
location: DUMMY_LOCATION.clone(),
960970
},
961971
Relay {
962972
hostname: "se10-wireguard".to_string(),
@@ -974,7 +984,7 @@ fn test_include_in_country() {
974984
.unwrap(),
975985
daita: false,
976986
}),
977-
location: None,
987+
location: DUMMY_LOCATION.clone(),
978988
},
979989
],
980990
}],
@@ -1174,7 +1184,7 @@ fn test_daita() {
11741184
.unwrap(),
11751185
daita: false,
11761186
}),
1177-
location: None,
1187+
location: DUMMY_LOCATION.clone(),
11781188
},
11791189
Relay {
11801190
hostname: "se10-wireguard".to_string(),
@@ -1192,7 +1202,7 @@ fn test_daita() {
11921202
.unwrap(),
11931203
daita: true,
11941204
}),
1195-
location: None,
1205+
location: DUMMY_LOCATION.clone(),
11961206
},
11971207
],
11981208
}],

mullvad-types/src/relay_constraints.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,18 @@ impl GeographicLocationConstraint {
203203
impl Match<Relay> for GeographicLocationConstraint {
204204
fn matches(&self, relay: &Relay) -> bool {
205205
match self {
206-
GeographicLocationConstraint::Country(ref country) => relay
207-
.location
208-
.as_ref()
209-
.map_or(false, |loc| loc.country_code == *country),
206+
GeographicLocationConstraint::Country(ref country) => {
207+
relay.location.country_code == *country
208+
}
210209
GeographicLocationConstraint::City(ref country, ref city) => {
211-
relay.location.as_ref().map_or(false, |loc| {
212-
loc.country_code == *country && loc.city_code == *city
213-
})
210+
let loc = &relay.location;
211+
loc.country_code == *country && loc.city_code == *city
214212
}
215213
GeographicLocationConstraint::Hostname(ref country, ref city, ref hostname) => {
216-
relay.location.as_ref().map_or(false, |loc| {
217-
loc.country_code == *country
218-
&& loc.city_code == *city
219-
&& relay.hostname == *hostname
220-
})
214+
let loc = &relay.location;
215+
loc.country_code == *country
216+
&& loc.city_code == *city
217+
&& relay.hostname == *hostname
221218
}
222219
}
223220
}

mullvad-types/src/relay_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub struct Relay {
8383
pub provider: String,
8484
pub weight: u64,
8585
pub endpoint_data: RelayEndpointData,
86-
pub location: Option<Location>,
86+
pub location: Location,
8787
}
8888

8989
impl PartialEq for Relay {

test/test-manager/src/tests/dns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ async fn connect_local_wg_relay(mullvad_client: &mut MullvadProxyClient) -> Resu
642642
.set_quantum_resistant_tunnel(QuantumResistantState::Off)
643643
.await?;
644644
mullvad_client
645-
.set_daita_settings(DaitaSettings { enabled: false })
645+
.set_daita_settings(DaitaSettings::default())
646646
.await?;
647647

648648
let peer_addr: SocketAddr = SocketAddr::new(

test/test-manager/src/tests/helpers.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use mullvad_relay_selector::{
1010
};
1111
use mullvad_types::{
1212
constraints::Constraint,
13-
location::Location,
1413
relay_constraints::{
1514
BridgeSettings, GeographicLocationConstraint, LocationConstraint, RelayConstraints,
1615
RelaySettings,
@@ -618,7 +617,7 @@ pub async fn constrain_to_relay(
618617
..
619618
}
620619
| GetRelay::OpenVpn { exit, .. } => {
621-
let location = into_constraint(&exit)?;
620+
let location = into_constraint(&exit);
622621
let relay_constraints = RelayConstraints {
623622
location,
624623
..Default::default()
@@ -646,22 +645,14 @@ pub async fn constrain_to_relay(
646645
/// # Panics
647646
///
648647
/// The relay must have a location set.
649-
pub fn into_constraint(relay: &Relay) -> anyhow::Result<Constraint<LocationConstraint>> {
650-
relay
651-
.location
652-
.as_ref()
653-
.map(
654-
|Location {
655-
country_code,
656-
city_code,
657-
..
658-
}| {
659-
GeographicLocationConstraint::hostname(country_code, city_code, &relay.hostname)
660-
},
661-
)
662-
.map(LocationConstraint::Location)
663-
.map(Constraint::Only)
664-
.ok_or(anyhow!("relay is missing location"))
648+
pub fn into_constraint(relay: &Relay) -> Constraint<LocationConstraint> {
649+
let constraint = GeographicLocationConstraint::hostname(
650+
relay.location.country_code.clone(),
651+
relay.location.city_code.clone(),
652+
&relay.hostname,
653+
);
654+
655+
Constraint::Only(LocationConstraint::Location(constraint))
665656
}
666657

667658
/// Ping monitoring made easy!

test/test-manager/src/tests/relay_ip_overrides.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,7 @@ async fn pick_a_relay(
298298

299299
let relay_ip = relay.ipv4_addr_in;
300300
let hostname = relay.hostname.clone();
301-
let city = relay
302-
.location
303-
.as_ref()
304-
.ok_or(anyhow!("Got Relay with an unknown location"))?
305-
.city_code
306-
.clone();
301+
let city = relay.location.city_code.clone();
307302

308303
log::info!("selected {hostname} ({relay_ip})");
309304
let location = GeographicLocationConstraint::Hostname(country, city, hostname.clone()).into();

0 commit comments

Comments
 (0)