Skip to content

Commit 850d9f7

Browse files
committed
Replace refs to config with just custom list
The entire config was passed around just for the custom lists.
1 parent 244a9c7 commit 850d9f7

File tree

1 file changed

+38
-38
lines changed
  • mullvad-relay-selector/src/relay_selector

1 file changed

+38
-38
lines changed

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

+38-38
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ impl RelaySelector {
472472
let near_location = match specialized_config {
473473
SpecializedSelectorConfig::Normal(config) => {
474474
let user_preferences = RelayQuery::from(config.clone());
475-
Self::get_relay_midpoint(&user_preferences, parsed_relays, &config)
475+
Self::get_relay_midpoint(&user_preferences, parsed_relays, config.custom_lists)
476476
}
477477
SpecializedSelectorConfig::Custom(_) => None,
478478
};
@@ -508,9 +508,9 @@ impl RelaySelector {
508508
SpecializedSelectorConfig::Custom(custom_config) => {
509509
Ok(GetRelay::Custom(custom_config.clone()))
510510
}
511-
SpecializedSelectorConfig::Normal(pure_config) => {
511+
SpecializedSelectorConfig::Normal(normal_config) => {
512512
let parsed_relays = &self.parsed_relays.lock().unwrap();
513-
Self::get_relay_inner(&query, parsed_relays, &pure_config)
513+
Self::get_relay_inner(&query, parsed_relays, normal_config.custom_lists)
514514
}
515515
}
516516
}
@@ -555,7 +555,7 @@ impl RelaySelector {
555555
parsed_relays,
556556
)
557557
.ok_or(Error::NoRelay)?;
558-
Self::get_relay_inner(&query, parsed_relays, &normal_config)
558+
Self::get_relay_inner(&query, parsed_relays, normal_config.custom_lists)
559559
}
560560
}
561561
}
@@ -585,7 +585,7 @@ impl RelaySelector {
585585
// settings
586586
.filter(|query| runtime_params.compatible(query))
587587
.filter_map(|query| query.clone().intersection(user_query.clone()))
588-
.filter(|query| Self::get_relay_inner(query, parsed_relays, user_config).is_ok())
588+
.filter(|query| Self::get_relay_inner(query, parsed_relays, user_config.custom_lists).is_ok())
589589
.cycle() // If the above filters remove all relays, cycle will also return an empty iterator
590590
.nth(retry_attempt)
591591
}
@@ -610,22 +610,24 @@ impl RelaySelector {
610610
fn get_relay_inner(
611611
query: &RelayQuery,
612612
parsed_relays: &ParsedRelays,
613-
config: &NormalSelectorConfig<'_>,
613+
custom_lists: &CustomListsSettings,
614614
) -> Result<GetRelay, Error> {
615615
match query.tunnel_protocol {
616616
Constraint::Only(TunnelType::Wireguard) => {
617-
Self::get_wireguard_relay(query, config, parsed_relays)
617+
Self::get_wireguard_relay(query, custom_lists, parsed_relays)
618618
}
619619
Constraint::Only(TunnelType::OpenVpn) => {
620-
Self::get_openvpn_relay(query, config, parsed_relays)
620+
Self::get_openvpn_relay(query, custom_lists, parsed_relays)
621621
}
622622
Constraint::Any => {
623623
// Try Wireguard, then OpenVPN, then fail
624624
for tunnel_type in [TunnelType::Wireguard, TunnelType::OpenVpn] {
625625
let mut new_query = query.clone();
626626
new_query.tunnel_protocol = Constraint::Only(tunnel_type);
627627
// If a suitable relay is found, short-circuit and return it
628-
if let Ok(relay) = Self::get_relay_inner(&new_query, parsed_relays, config) {
628+
if let Ok(relay) =
629+
Self::get_relay_inner(&new_query, parsed_relays, custom_lists)
630+
{
629631
return Ok(relay);
630632
}
631633
}
@@ -638,14 +640,14 @@ impl RelaySelector {
638640
fn get_relay_inner(
639641
mut query: RelayQuery,
640642
parsed_relays: &ParsedRelays,
641-
config: &NormalSelectorConfig<'_>,
643+
custom_lists: &CustomListsSettings,
642644
) -> Result<GetRelay, Error> {
643645
// FIXME: A bit of defensive programming - calling `get_wiregurad_relay` with a query that
644646
// doesn't specify Wireguard as the desired tunnel type is not valid and will lead
645647
// to unwanted behavior. This should be seen as a workaround, and it would be nicer
646648
// to lift this invariant to be checked by the type system instead.
647649
query.tunnel_protocol = Constraint::Only(TunnelType::Wireguard);
648-
Self::get_wireguard_relay(query, config, parsed_relays)
650+
Self::get_wireguard_relay(query, custom_lists, parsed_relays)
649651
}
650652

651653
/// Derive a valid Wireguard relay configuration from `query`.
@@ -663,17 +665,17 @@ impl RelaySelector {
663665
/// [`MullvadEndpoint`]: mullvad_types::endpoint::MullvadEndpoint
664666
fn get_wireguard_relay(
665667
query: &RelayQuery,
666-
config: &NormalSelectorConfig<'_>,
668+
custom_lists: &CustomListsSettings,
667669
parsed_relays: &ParsedRelays,
668670
) -> Result<GetRelay, Error> {
669671
assert_eq!(
670672
query.tunnel_protocol,
671673
Constraint::Only(TunnelType::Wireguard)
672674
);
673675
let inner = if !query.wireguard_constraints.multihop() {
674-
Self::get_wireguard_singlehop_config(query, config, parsed_relays)?
676+
Self::get_wireguard_singlehop_config(query, custom_lists, parsed_relays)?
675677
} else {
676-
Self::get_wireguard_multihop_config(query, config, parsed_relays)?
678+
Self::get_wireguard_multihop_config(query, custom_lists, parsed_relays)?
677679
};
678680
let endpoint = Self::get_wireguard_endpoint(query, parsed_relays, &inner)?;
679681
let obfuscator =
@@ -693,11 +695,10 @@ impl RelaySelector {
693695
/// * `Ok(WireguardInner::Singlehop)` otherwise
694696
fn get_wireguard_singlehop_config(
695697
query: &RelayQuery,
696-
config: &NormalSelectorConfig<'_>,
698+
custom_lists: &CustomListsSettings,
697699
parsed_relays: &ParsedRelays,
698700
) -> Result<WireguardConfig, Error> {
699-
let candidates =
700-
filter_matching_relay_list(query, parsed_relays.relays(), config.custom_lists);
701+
let candidates = filter_matching_relay_list(query, parsed_relays.relays(), custom_lists);
701702
helpers::pick_random_relay(&candidates)
702703
.cloned()
703704
.map(WireguardConfig::singlehop)
@@ -713,7 +714,7 @@ impl RelaySelector {
713714
/// * `Ok(WireguardInner::Multihop)` otherwise
714715
fn get_wireguard_multihop_config(
715716
query: &RelayQuery,
716-
config: &NormalSelectorConfig<'_>,
717+
custom_lists: &CustomListsSettings,
717718
parsed_relays: &ParsedRelays,
718719
) -> Result<WireguardConfig, Error> {
719720
// Here, we modify the original query just a bit.
@@ -728,16 +729,10 @@ impl RelaySelector {
728729
let mut exit_relay_query = query.clone();
729730
// DAITA should only be enabled for the entry relay
730731
exit_relay_query.wireguard_constraints.daita = Constraint::Only(false);
731-
let exit_candidates = filter_matching_relay_list(
732-
&exit_relay_query,
733-
parsed_relays.relays(),
734-
config.custom_lists,
735-
);
736-
let entry_candidates = filter_matching_relay_list(
737-
&entry_relay_query,
738-
parsed_relays.relays(),
739-
config.custom_lists,
740-
);
732+
let exit_candidates =
733+
filter_matching_relay_list(&exit_relay_query, parsed_relays.relays(), custom_lists);
734+
let entry_candidates =
735+
filter_matching_relay_list(&entry_relay_query, parsed_relays.relays(), custom_lists);
741736

742737
fn pick_random_excluding<'a>(list: &'a [Relay], exclude: &'a Relay) -> Option<&'a Relay> {
743738
list.iter()
@@ -826,15 +821,20 @@ impl RelaySelector {
826821
#[cfg(not(target_os = "android"))]
827822
fn get_openvpn_relay(
828823
query: &RelayQuery,
829-
config: &NormalSelectorConfig<'_>,
824+
custom_lists: &CustomListsSettings,
830825
parsed_relays: &ParsedRelays,
831826
) -> Result<GetRelay, Error> {
832827
assert_eq!(query.tunnel_protocol, Constraint::Only(TunnelType::OpenVpn));
833828
let exit =
834-
Self::choose_openvpn_relay(query, config, parsed_relays).ok_or(Error::NoRelay)?;
829+
Self::choose_openvpn_relay(query, custom_lists, parsed_relays).ok_or(Error::NoRelay)?;
835830
let endpoint = Self::get_openvpn_endpoint(query, &exit, parsed_relays)?;
836-
let bridge =
837-
Self::get_openvpn_bridge(query, &exit, &endpoint.protocol, parsed_relays, config)?;
831+
let bridge = Self::get_openvpn_bridge(
832+
query,
833+
&exit,
834+
&endpoint.protocol,
835+
parsed_relays,
836+
custom_lists,
837+
)?;
838838

839839
// FIXME: This assert would be better to encode at the type level.
840840
assert!(matches!(exit.endpoint_data, RelayEndpointData::Openvpn));
@@ -887,13 +887,13 @@ impl RelaySelector {
887887
relay: &Relay,
888888
protocol: &TransportProtocol,
889889
parsed_relays: &ParsedRelays,
890-
config: &NormalSelectorConfig<'_>,
890+
custom_lists: &CustomListsSettings,
891891
) -> Result<Option<SelectedBridge>, Error> {
892892
if !BridgeQuery::should_use_bridge(&query.openvpn_constraints.bridge_settings) {
893893
Ok(None)
894894
} else {
895895
let bridge_query = &query.openvpn_constraints.bridge_settings.clone().unwrap();
896-
let custom_lists = &config.custom_lists;
896+
let custom_lists = &custom_lists;
897897
match protocol {
898898
TransportProtocol::Udp => {
899899
log::error!("Can not use OpenVPN bridges over UDP");
@@ -1011,15 +1011,15 @@ impl RelaySelector {
10111011
fn get_relay_midpoint(
10121012
query: &RelayQuery,
10131013
parsed_relays: &ParsedRelays,
1014-
config: &NormalSelectorConfig<'_>,
1014+
custom_lists: &CustomListsSettings,
10151015
) -> Option<Coordinates> {
10161016
use std::ops::Not;
10171017
if query.location.is_any() {
10181018
return None;
10191019
}
10201020

10211021
let matching_locations: Vec<Location> =
1022-
filter_matching_relay_list(query, parsed_relays.relays(), config.custom_lists)
1022+
filter_matching_relay_list(query, parsed_relays.relays(), custom_lists)
10231023
.into_iter()
10241024
.filter_map(|relay| relay.location)
10251025
.unique_by(|location| location.city.clone())
@@ -1037,12 +1037,12 @@ impl RelaySelector {
10371037
#[cfg(not(target_os = "android"))]
10381038
fn choose_openvpn_relay(
10391039
query: &RelayQuery,
1040-
config: &NormalSelectorConfig<'_>,
1040+
custom_lists: &CustomListsSettings,
10411041
parsed_relays: &ParsedRelays,
10421042
) -> Option<Relay> {
10431043
// Filter among all valid relays
10441044
let relays = parsed_relays.relays();
1045-
let candidates = filter_matching_relay_list(query, relays, config.custom_lists);
1045+
let candidates = filter_matching_relay_list(query, relays, custom_lists);
10461046
// Pick one of the valid relays.
10471047
helpers::pick_random_relay(&candidates).cloned()
10481048
}

0 commit comments

Comments
 (0)