Skip to content

Commit

Permalink
Refactor Shadowsocks bridge detailing function
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Apr 8, 2024
1 parent cb08efb commit 74b4998
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
26 changes: 25 additions & 1 deletion mullvad-relay-selector/src/relay_selector/detailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ use mullvad_types::{
endpoint::MullvadWireguardEndpoint,
relay_constraints::TransportPort,
relay_list::{
OpenVpnEndpoint, OpenVpnEndpointData, Relay, RelayEndpointData, WireguardEndpointData,
BridgeEndpointData, OpenVpnEndpoint, OpenVpnEndpointData, Relay, RelayEndpointData,
WireguardEndpointData,
},
};
use talpid_types::net::{
all_of_the_internet,
proxy::CustomProxy,
wireguard::{PeerConfig, PublicKey},
Endpoint, IpVersion, TransportProtocol,
};
Expand Down Expand Up @@ -293,3 +295,25 @@ fn compatible_openvpn_port_combo(
},
}
}

/// Picks a random bridge from a relay.
pub fn bridge_endpoint(data: &BridgeEndpointData, relay: &Relay) -> Option<CustomProxy> {
use rand::seq::SliceRandom;
if relay.endpoint_data != RelayEndpointData::Bridge {
return None;
}
data.shadowsocks
.choose(&mut rand::thread_rng())
.inspect(|shadowsocks_endpoint| {
log::info!(
"Selected Shadowsocks bridge {} at {}:{}/{}",
relay.hostname,
relay.ipv4_addr_in,
shadowsocks_endpoint.port,
shadowsocks_endpoint.protocol
);
})
.map(|shadowsocks_endpoint| {
shadowsocks_endpoint.to_proxy_settings(relay.ipv4_addr_in.into())
})
}
29 changes: 3 additions & 26 deletions mullvad-relay-selector/src/relay_selector/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
use std::net::SocketAddr;

use mullvad_types::{
constraints::Constraint,
endpoint::MullvadWireguardEndpoint,
relay_constraints::Udp2TcpObfuscationSettings,
relay_list::{BridgeEndpointData, Relay, RelayEndpointData},
constraints::Constraint, endpoint::MullvadWireguardEndpoint,
relay_constraints::Udp2TcpObfuscationSettings, relay_list::Relay,
};
use rand::{seq::SliceRandom, thread_rng, Rng};
use talpid_types::net::{obfuscation::ObfuscatorConfig, proxy::CustomProxy};
use talpid_types::net::obfuscation::ObfuscatorConfig;

use crate::SelectedObfuscator;

Expand Down Expand Up @@ -59,27 +57,6 @@ pub fn pick_random_relay_weighted<RelayType>(
}
}

/// Picks a random bridge from a relay.
pub fn pick_random_bridge(data: &BridgeEndpointData, relay: &Relay) -> Option<CustomProxy> {
if relay.endpoint_data != RelayEndpointData::Bridge {
return None;
}
data.shadowsocks
.choose(&mut rand::thread_rng())
.inspect(|shadowsocks_endpoint| {
log::info!(
"Selected Shadowsocks bridge {} at {}:{}/{}",
relay.hostname,
relay.ipv4_addr_in,
shadowsocks_endpoint.port,
shadowsocks_endpoint.protocol
);
})
.map(|shadowsocks_endpoint| {
shadowsocks_endpoint.to_proxy_settings(relay.ipv4_addr_in.into())
})
}

pub fn get_udp2tcp_obfuscator(
obfuscation_settings_constraint: &Constraint<Udp2TcpObfuscationSettings>,
udp2tcp_ports: &[u16],
Expand Down
2 changes: 1 addition & 1 deletion mullvad-relay-selector/src/relay_selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ impl RelaySelector {
.cloned()
.ok_or(Error::NoRelay),
}?;
let endpoint = helpers::pick_random_bridge(bridge_data, &bridge).ok_or(Error::NoBridge)?;
let endpoint = detailer::bridge_endpoint(bridge_data, &bridge).ok_or(Error::NoBridge)?;
Ok((endpoint, bridge))
}

Expand Down

0 comments on commit 74b4998

Please sign in to comment.