Skip to content

Commit

Permalink
Add smarter bridge location constraint selection
Browse files Browse the repository at this point in the history
Add the same location constraint logic as `relay set location` to the
bridge location constraint for the `bridge set location` command. This
implies that the relay selection for both work in the same way.
  • Loading branch information
MarkusPettersson98 committed Jun 21, 2023
1 parent 4f7a239 commit 130eced
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
35 changes: 32 additions & 3 deletions mullvad-cli/src/cmds/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use mullvad_types::{
use std::net::{IpAddr, SocketAddr};
use talpid_types::net::openvpn::{self, SHADOWSOCKS_CIPHERS};

use super::relay::find_relay_by_hostname;
use super::relay_constraints::LocationArgs;

#[derive(Subcommand, Debug)]
Expand All @@ -29,8 +30,27 @@ pub enum SetCommands {
/// Specify whether to use a bridge
State { policy: BridgeState },

/// Set country or city to select relays from. Use the 'list'
/// command to show available alternatives.
/// Set country or city to select relays from.
/// Use the 'mullvad bridge list' command to show available alternatives.
#[command(
override_usage = "mullvad bridge set location <COUNTRY> [CITY] [HOSTNAME] | <HOSTNAME>
Select bridge using a country:
\tmullvad bridge set location se
Select bridge using a country and city:
\tmullvad bridge set location se got
Select bridge using a country, city and hostname:
\tmullvad bridge set location se got se-got-br-001
Select bridge using only its hostname:
\tmullvad bridge set location se-got-br-001"
)]
Location(LocationArgs),

/// Set hosting provider(s) to select relays from. The 'list'
Expand Down Expand Up @@ -138,7 +158,16 @@ impl Bridge {
Ok(())
}
SetCommands::Location(location) => {
Self::update_bridge_settings(Some(Constraint::from(location)), None, None).await
let mut rpc = MullvadProxyClient::new().await?;
let countries = rpc.get_relay_locations().await?.countries;
let location_constraint =
if let Some(relay) = find_relay_by_hostname(&countries, &location.country) {
Constraint::Only(relay)
} else {
Constraint::from(location)
};

Self::update_bridge_settings(Some(location_constraint), None, None).await
}
SetCommands::Ownership { ownership } => {
Self::update_bridge_settings(None, None, Some(ownership)).await
Expand Down
2 changes: 1 addition & 1 deletion mullvad-cli/src/cmds/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ fn parse_transport_port(

/// Lookup a relay among a list of [`RelayListCountry`]s by hostname.
/// The matching is exact, bar capitalization.
fn find_relay_by_hostname(
pub fn find_relay_by_hostname(
countries: &[RelayListCountry],
hostname: &str,
) -> Option<LocationConstraint> {
Expand Down

0 comments on commit 130eced

Please sign in to comment.