summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarkus Pettersson <markus.pettersson@mullvad.net>2024-02-16 16:24:33 +0100
committerMarkus Pettersson <markus.pettersson@mullvad.net>2024-04-04 16:48:24 +0200
commitb9e3ec49991bf5afa8291aa4a5b318c2feaf8262 (patch)
tree92a9223a470aeaedac98cd93762deab4efceece5
parente45f61d0f538f20d594106e0ec9ccecb40678a3b (diff)
downloadmullvadvpn-b9e3ec49991bf5afa8291aa4a5b318c2feaf8262.tar.xz
mullvadvpn-b9e3ec49991bf5afa8291aa4a5b318c2feaf8262.zip
Format docstrings
-rw-r--r--mullvad-relay-selector/src/relay_selector/detailer.rs7
-rw-r--r--mullvad-relay-selector/src/relay_selector/matcher.rs8
-rw-r--r--mullvad-relay-selector/src/relay_selector/mod.rs76
-rw-r--r--mullvad-relay-selector/src/relay_selector/query.rs19
-rw-r--r--mullvad-relay-selector/tests/relay_selector.rs43
5 files changed, 88 insertions, 65 deletions
diff --git a/mullvad-relay-selector/src/relay_selector/detailer.rs b/mullvad-relay-selector/src/relay_selector/detailer.rs
index 374d92da09..8fa36f5434 100644
--- a/mullvad-relay-selector/src/relay_selector/detailer.rs
+++ b/mullvad-relay-selector/src/relay_selector/detailer.rs
@@ -48,7 +48,8 @@ pub enum Error {
/// Constructs a [`MullvadWireguardEndpoint`] with details for how to connect to a Wireguard relay.
///
/// # Returns
-/// - A configured endpoint for Wireguard relay, encapsulating either a single-hop or multi-hop connection.
+/// - A configured endpoint for Wireguard relay, encapsulating either a single-hop or multi-hop
+/// connection.
/// - Returns [`Option::None`] if the desired port is not in a valid port range (see
/// [`WireguardRelayQuery::port`]) or relay addresses cannot be resolved.
pub fn wireguard_endpoint(
@@ -198,8 +199,8 @@ const fn get_public_key(relay: &Relay) -> Result<&PublicKey, Error> {
/// - `port_ranges`: A slice of tuples, each representing a range of valid port numbers.
///
/// # Returns
-/// - `Option<u16>`: A randomly selected port number within the given ranges, or `None` if
-/// the input is empty or the total number of available ports is zero.
+/// - `Option<u16>`: A randomly selected port number within the given ranges, or `None` if the input
+/// is empty or the total number of available ports is zero.
fn select_random_port(port_ranges: &[(u16, u16)]) -> Result<u16, Error> {
use rand::Rng;
let get_port_amount = |range: &(u16, u16)| -> u64 { (1 + range.1 - range.0) as u64 };
diff --git a/mullvad-relay-selector/src/relay_selector/matcher.rs b/mullvad-relay-selector/src/relay_selector/matcher.rs
index f06e04224f..2987d8965a 100644
--- a/mullvad-relay-selector/src/relay_selector/matcher.rs
+++ b/mullvad-relay-selector/src/relay_selector/matcher.rs
@@ -35,10 +35,10 @@ pub fn filter_matching_relay_list<'a, R: Iterator<Item = &'a Relay> + Clone>(
.filter(|relay| filter_on_providers(&query.providers, relay));
// The last filtering to be done is on the `include_in_country` attribute found on each
- // relay. When the location constraint is based on country, a relay which has `include_in_country`
- // set to true should always be prioritized over relays which has this flag set to false.
- // We should only consider relays with `include_in_country` set to false if there are no
- // other candidates left.
+ // relay. When the location constraint is based on country, a relay which has
+ // `include_in_country` set to true should always be prioritized over relays which has this
+ // flag set to false. We should only consider relays with `include_in_country` set to false
+ // if there are no other candidates left.
match &locations {
Constraint::Any => shortlist.cloned().collect(),
Constraint::Only(locations) => {
diff --git a/mullvad-relay-selector/src/relay_selector/mod.rs b/mullvad-relay-selector/src/relay_selector/mod.rs
index 424ca6385c..1cb9dca523 100644
--- a/mullvad-relay-selector/src/relay_selector/mod.rs
+++ b/mullvad-relay-selector/src/relay_selector/mod.rs
@@ -45,9 +45,9 @@ use self::{
query::{BridgeQuery, Intersection, OpenVpnRelayQuery, RelayQuery, WireguardRelayQuery},
};
-/// [`RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector should prioritize on
-/// successive connection attempts. Note that these will *never* override user preferences.
-/// See [the documentation on `RelayQuery`][RelayQuery] for further details.
+/// [`RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector should
+/// prioritize on successive connection attempts. Note that these will *never* override user
+/// preferences. See [the documentation on `RelayQuery`][RelayQuery] for further details.
///
/// This list should be kept in sync with the expected behavior defined in `docs/relay-selector.md`
pub static RETRY_ORDER: Lazy<Vec<RelayQuery>> = Lazy::new(|| {
@@ -144,14 +144,16 @@ impl Default for RuntimeParameters {
/// This enum exists to separate the two types of [`SelectorConfig`] that exists.
///
-/// The first one is a "regular" config, where [`SelectorConfig::relay_settings`] is [`RelaySettings::Normal`].
-/// This is the most common variant, and there exists a mapping from this variant to [`RelayQueryBuilder`].
-/// Being able to implement `From<NormalSelectorConfig> for RelayQueryBuilder` was the main
-/// motivator for introducing these seemingly useless derivates of [`SelectorConfig`].
+/// The first one is a "regular" config, where [`SelectorConfig::relay_settings`] is
+/// [`RelaySettings::Normal`]. This is the most common variant, and there exists a mapping from this
+/// variant to [`RelayQueryBuilder`]. Being able to implement `From<NormalSelectorConfig> for
+/// RelayQueryBuilder` was the main motivator for introducing these seemingly useless derivates of
+/// [`SelectorConfig`].
///
-/// The second one is a custom config, where [`SelectorConfig::relay_settings`] is [`RelaySettings::Custom`].
-/// For this variant, the endpoint where the client should connect to is already specified inside of the variant,
-/// so in practice the relay selector becomes superfluous. Also, there exists no mapping to [`RelayQueryBuilder`].
+/// The second one is a custom config, where [`SelectorConfig::relay_settings`] is
+/// [`RelaySettings::Custom`]. For this variant, the endpoint where the client should connect to is
+/// already specified inside of the variant, so in practice the relay selector becomes superfluous.
+/// Also, there exists no mapping to [`RelayQueryBuilder`].
#[derive(Debug, Clone)]
enum SpecializedSelectorConfig<'a> {
// This variant implements `From<NormalSelectorConfig> for RelayQuery`
@@ -500,9 +502,9 @@ impl RelaySelector {
self.get_relay_with_custom_params(retry_attempt, &RETRY_ORDER, runtime_params)
}
- /// Peek at which [`TunnelType`] that would be returned for a certain connection attempt for a given
- /// [`SelectorConfig`]. Returns [`Option::None`] if the given config would return a custom
- /// tunnel endpoint.
+ /// Peek at which [`TunnelType`] that would be returned for a certain connection attempt for a
+ /// given [`SelectorConfig`]. Returns [`Option::None`] if the given config would return a
+ /// custom tunnel endpoint.
///
/// # Note
/// This function is only really useful for testing-purposes. It is exposed to ease testing of
@@ -556,15 +558,16 @@ impl RelaySelector {
}
}
- /// This function defines the merge between a set of pre-defined queries and `user_preferences` for the given
- /// `retry_attempt`.
+ /// This function defines the merge between a set of pre-defined queries and `user_preferences`
+ /// for the given `retry_attempt`.
///
- /// This algorithm will loop back to the start of `retry_order` if `retry_attempt < retry_order.len()`.
- /// If `user_preferences` is not compatible with any of the pre-defined queries in `retry_order`, `user_preferences`
- /// is returned.
+ /// This algorithm will loop back to the start of `retry_order` if `retry_attempt <
+ /// retry_order.len()`. If `user_preferences` is not compatible with any of the pre-defined
+ /// queries in `retry_order`, `user_preferences` is returned.
///
/// Runtime parameters may affect which of the default queries that are considered. For example,
- /// queries which rely on IPv6 will not be considered if working IPv6 is not available at runtime.
+ /// queries which rely on IPv6 will not be considered if working IPv6 is not available at
+ /// runtime.
fn pick_and_merge_query(
retry_attempt: usize,
retry_order: &[RelayQuery],
@@ -583,15 +586,19 @@ impl RelaySelector {
.unwrap_or(user_preferences)
}
- /// "Execute" the given query, yielding a final set of relays and/or bridges which the VPN traffic shall be routed through.
+ /// "Execute" the given query, yielding a final set of relays and/or bridges which the VPN
+ /// traffic shall be routed through.
///
/// # Parameters
- /// - `query`: Constraints that filter the available relays, such as geographic location or tunnel protocol.
- /// - `config`: Configuration settings that influence relay selection, including bridge state and custom lists.
+ /// - `query`: Constraints that filter the available relays, such as geographic location or
+ /// tunnel protocol.
+ /// - `config`: Configuration settings that influence relay selection, including bridge state
+ /// and custom lists.
/// - `parsed_relays`: The complete set of parsed relays available for selection.
///
/// # Returns
- /// * A randomly selected relay that meets the specified constraints (and a random bridge/entry relay if applicable).
+ /// * A randomly selected relay that meets the specified constraints (and a random bridge/entry
+ /// relay if applicable).
/// See [`GetRelay`] for more details.
/// * An `Err` if no suitable relay is found
/// * An `Err` if no suitable bridge is found
@@ -629,10 +636,10 @@ impl RelaySelector {
parsed_relays: &ParsedRelays,
config: &NormalSelectorConfig<'_>,
) -> Result<GetRelay, Error> {
- // FIXME: A bit of defensive programming - calling `get_wiregurad_relay` with a query that doesn't
- // specify Wireguard as the desired tunnel type is not valid and will lead to unwanted
- // behavior. This should be seen as a workaround, and it would be nicer to lift this
- // invariant to be checked by the type system instead.
+ // FIXME: A bit of defensive programming - calling `get_wiregurad_relay` with a query that
+ // doesn't specify Wireguard as the desired tunnel type is not valid and will lead
+ // to unwanted behavior. This should be seen as a workaround, and it would be nicer
+ // to lift this invariant to be checked by the type system instead.
query.tunnel_protocol = Constraint::Only(TunnelType::Wireguard);
Self::get_wireguard_relay(query, config, parsed_relays)
}
@@ -712,7 +719,8 @@ impl RelaySelector {
let mut entry_relay_query = query.clone();
entry_relay_query.location = query.wireguard_constraints.entry_location.clone();
// After we have our two queries (one for the exit relay & one for the entry relay),
- // we can query for all exit & entry candidates! All candidates are needed for the next step.
+ // we can query for all exit & entry candidates! All candidates are needed for the next
+ // step.
let exit_candidates =
filter_matching_relay_list(query, parsed_relays.relays(), config.custom_lists);
let entry_candidates = filter_matching_relay_list(
@@ -847,17 +855,20 @@ impl RelaySelector {
})
}
- /// Selects a suitable bridge based on the specified settings, relay information, and transport protocol.
+ /// Selects a suitable bridge based on the specified settings, relay information, and transport
+ /// protocol.
///
/// # Parameters
/// - `query`: The filter criteria for selecting a bridge.
/// - `relay`: Information about the current relay, including its location.
/// - `protocol`: The transport protocol (TCP or UDP) in use.
/// - `parsed_relays`: A structured representation of all available relays.
- /// - `custom_lists`: User-defined or application-specific settings that may influence bridge selection.
+ /// - `custom_lists`: User-defined or application-specific settings that may influence bridge
+ /// selection.
///
/// # Returns
- /// * On success, returns an `Option` containing the selected bridge, if one is found. Returns `None` if no suitable bridge meets the criteria or bridges should not be used.
+ /// * On success, returns an `Option` containing the selected bridge, if one is found. Returns
+ /// `None` if no suitable bridge meets the criteria or bridges should not be used.
/// * `Error::NoBridge` if attempting to use OpenVPN bridges over UDP, as this is unsupported.
/// * `Error::NoRelay` if `relay` does not have a location set.
#[cfg(not(target_os = "android"))]
@@ -1010,7 +1021,8 @@ impl RelaySelector {
}
/// # Returns
- /// A randomly selected relay that meets the specified constraints, or `None` if no suitable relay is found.
+ /// A randomly selected relay that meets the specified constraints, or `None` if no suitable
+ /// relay is found.
#[cfg(not(target_os = "android"))]
fn choose_openvpn_relay(
query: &RelayQuery,
diff --git a/mullvad-relay-selector/src/relay_selector/query.rs b/mullvad-relay-selector/src/relay_selector/query.rs
index d5b17b2303..0536379dbc 100644
--- a/mullvad-relay-selector/src/relay_selector/query.rs
+++ b/mullvad-relay-selector/src/relay_selector/query.rs
@@ -1,18 +1,20 @@
//! This module provides a flexible way to specify 'queries' for relays.
//!
//! A query is a set of constraints that the [`crate::RelaySelector`] will use when filtering out
-//! potential relays that the daemon should connect to. It supports filtering relays by geographic location,
-//! provider, ownership, and tunnel protocol, along with protocol-specific settings for WireGuard and OpenVPN.
+//! potential relays that the daemon should connect to. It supports filtering relays by geographic
+//! location, provider, ownership, and tunnel protocol, along with protocol-specific settings for
+//! WireGuard and OpenVPN.
//!
//! The main components of this module include:
//!
-//! - [`RelayQuery`]: The core struct for specifying a query to select relay servers. It
-//! aggregates constraints on location, providers, ownership, tunnel protocol, and
-//! protocol-specific constraints for WireGuard and OpenVPN.
+//! - [`RelayQuery`]: The core struct for specifying a query to select relay servers. It aggregates
+//! constraints on location, providers, ownership, tunnel protocol, and protocol-specific
+//! constraints for WireGuard and OpenVPN.
//! - [`WireguardRelayQuery`] and [`OpenVpnRelayQuery`]: Structs that define protocol-specific
//! constraints for selecting WireGuard and OpenVPN relays, respectively.
-//! - [`Intersection`]: A trait implemented by the different query types that support intersection logic,
-//! which allows for combining two queries into a single query that represents the common constraints of both.
+//! - [`Intersection`]: A trait implemented by the different query types that support intersection
+//! logic, which allows for combining two queries into a single query that represents the common
+//! constraints of both.
//! - [Builder patterns][builder]: The module also provides builder patterns for creating instances
//! of `RelayQuery`, `WireguardRelayQuery`, and `OpenVpnRelayQuery` with a fluent API.
//!
@@ -218,7 +220,8 @@ impl From<RelayQuery> for RelayConstraints {
}
}
-/// A query for a relay with Wireguard-specific properties, such as `multihop` and [wireguard obfuscation][`SelectedObfuscation`].
+/// A query for a relay with Wireguard-specific properties, such as `multihop` and [wireguard
+/// obfuscation][`SelectedObfuscation`].
///
/// This struct may look a lot like [`WireguardConstraints`], and that is the point!
/// This struct is meant to be that type in the "universe of relay queries". The difference
diff --git a/mullvad-relay-selector/tests/relay_selector.rs b/mullvad-relay-selector/tests/relay_selector.rs
index 99a5491fad..ed3546c62e 100644
--- a/mullvad-relay-selector/tests/relay_selector.rs
+++ b/mullvad-relay-selector/tests/relay_selector.rs
@@ -201,10 +201,11 @@ fn default_relay_selector() -> RelaySelector {
}
/// This is not an actual test. Rather, it serves as a reminder that if [`RETRY_ORDER`] is modified,
-/// the programmer should be made aware to update all external documents which rely on the retry order
-/// to be correct.
+/// the programmer should be made aware to update all external documents which rely on the retry
+/// order to be correct.
///
-/// When all necessary changes have been made, feel free to update this test to mirror the new [`RETRY_ORDER`].
+/// When all necessary changes have been made, feel free to update this test to mirror the new
+/// [`RETRY_ORDER`].
#[test]
fn assert_retry_order() {
use talpid_types::net::{IpVersion, TransportProtocol};
@@ -341,8 +342,8 @@ fn prefer_wireguard_when_auto() {
}
}
-/// If a Wireguard relay is only specified by it's hostname (and not tunnel type), the relay selector should
-/// still return a relay of the correct tunnel type (Wireguard).
+/// If a Wireguard relay is only specified by it's hostname (and not tunnel type), the relay
+/// selector should still return a relay of the correct tunnel type (Wireguard).
#[test]
fn test_prefer_wireguard_if_location_supports_it() {
let relay_selector = default_relay_selector();
@@ -361,8 +362,8 @@ fn test_prefer_wireguard_if_location_supports_it() {
}
}
-/// If an OpenVPN relay is only specified by it's hostname (and not tunnel type), the relay selector should
-/// still return a relay of the correct tunnel type (OpenVPN).
+/// If an OpenVPN relay is only specified by it's hostname (and not tunnel type), the relay selector
+/// should still return a relay of the correct tunnel type (OpenVPN).
#[test]
fn test_prefer_openvpn_if_location_supports_it() {
let relay_selector = default_relay_selector();
@@ -381,9 +382,10 @@ fn test_prefer_openvpn_if_location_supports_it() {
}
}
-/// Assert that the relay selector does *not* return a multihop configuration where the exit and entry relay are
-/// the same, even if the constraints would allow for it. Also verify that the relay selector is smart enough to
-/// pick either the entry or exit relay first depending on which one ends up yielding a valid configuration.
+/// Assert that the relay selector does *not* return a multihop configuration where the exit and
+/// entry relay are the same, even if the constraints would allow for it. Also verify that the relay
+/// selector is smart enough to pick either the entry or exit relay first depending on which one
+/// ends up yielding a valid configuration.
#[test]
fn test_wireguard_entry() {
// Define a relay list containing exactly two Wireguard relays in Gothenburg.
@@ -555,7 +557,8 @@ fn test_wireguard_entry_hostname_collision() {
/// Test that the relay selector:
/// * returns an OpenVPN relay given a constraint of a valid transport protocol + port combo
-/// * does *not* return an OpenVPN relay given a constraint of an *invalid* transport protocol + port combo
+/// * does *not* return an OpenVPN relay given a constraint of an *invalid* transport protocol +
+/// port combo
#[test]
fn test_openvpn_constraints() {
let relay_selector = default_relay_selector();
@@ -644,7 +647,8 @@ fn test_openvpn_constraints() {
}
}
-/// Construct a query for multihop configuration and assert that the relay selector picks an accompanying entry relay.
+/// Construct a query for multihop configuration and assert that the relay selector picks an
+/// accompanying entry relay.
#[test]
fn test_selecting_wireguard_location_will_consider_multihop() {
let relay_selector = default_relay_selector();
@@ -678,8 +682,9 @@ fn test_selecting_any_relay_will_consider_multihop() {
}
}
-/// Construct a query for a Wireguard configuration where UDP2TCP obfuscation is selected and multihop is explicitly
-/// turned off. Assert that the relay selector always return an obfuscator configuration.
+/// Construct a query for a Wireguard configuration where UDP2TCP obfuscation is selected and
+/// multihop is explicitly turned off. Assert that the relay selector always return an obfuscator
+/// configuration.
#[test]
fn test_selecting_wireguard_endpoint_with_udp2tcp_obfuscation() {
let relay_selector = default_relay_selector();
@@ -704,12 +709,14 @@ fn test_selecting_wireguard_endpoint_with_udp2tcp_obfuscation() {
}
}
-/// Construct a query for a Wireguard configuration where UDP2TCP obfuscation is set to "Auto" and multihop is
-/// explicitly turned off. Assert that the relay selector does *not* return an obfuscator config.
+/// Construct a query for a Wireguard configuration where UDP2TCP obfuscation is set to "Auto" and
+/// multihop is explicitly turned off. Assert that the relay selector does *not* return an
+/// obfuscator config.
///
/// # Note
-/// This is a highly specific test which details how the relay selector should behave at the time of writing this test.
-/// The cost (in latency primarily) of using obfuscation is deemed to be too high to enable it as an auto-configuration.
+/// This is a highly specific test which details how the relay selector should behave at the time of
+/// writing this test. The cost (in latency primarily) of using obfuscation is deemed to be too high
+/// to enable it as an auto-configuration.
#[test]
fn test_selecting_wireguard_endpoint_with_auto_obfuscation() {
let relay_selector = default_relay_selector();