Skip to content

Commit bb0d119

Browse files
committed
Remove dependency on relay-selector in mullvad-api
1 parent 16bc4b5 commit bb0d119

File tree

8 files changed

+52
-68
lines changed

8 files changed

+52
-68
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mullvad-api/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ mullvad-fs = { path = "../mullvad-fs" }
5252
mullvad-types = { path = "../mullvad-types" }
5353
talpid-types = { path = "../talpid-types" }
5454
talpid-time = { path = "../talpid-time" }
55-
mullvad-relay-selector = { path = "../mullvad-relay-selector" }
5655

5756
shadowsocks = { workspace = true, features = ["stream-cipher"] }
5857

mullvad-api/src/access_mode.rs

+27-33
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#[cfg(feature = "api-override")]
88
use crate::ApiEndpoint;
99
use crate::{
10-
proxy::{AllowedClientsProvider, ApiConnectionMode, ConnectionModeProvider},
10+
proxy::{ApiConnectionMode, ConnectionModeProvider},
1111
AddressCache,
1212
};
1313
use async_trait::async_trait;
@@ -16,8 +16,8 @@ use futures::{
1616
StreamExt,
1717
};
1818
use mullvad_types::access_method::{AccessMethod, AccessMethodSetting, Id, Settings};
19-
use std::{marker::PhantomData, net::SocketAddr, path::PathBuf};
20-
use talpid_types::net::{AllowedEndpoint, Endpoint, TransportProtocol};
19+
use std::{net::SocketAddr, path::PathBuf};
20+
use talpid_types::net::{AllowedClients, AllowedEndpoint, Endpoint, TransportProtocol};
2121

2222
pub enum Message {
2323
Get(ResponseTx<ResolvedConnectionMode>),
@@ -242,29 +242,25 @@ impl ConnectionModeProvider for AccessModeConnectionModeProvider {
242242
/// [`ApiConnectionMode::Direct`]) via a bridge ([`ApiConnectionMode::Proxied`])
243243
/// or via any supported custom proxy protocol
244244
/// ([`talpid_types::net::proxy::CustomProxy`]).
245-
pub struct AccessModeSelector<P> {
245+
pub struct AccessModeSelector<B: AccessMethodResolver> {
246246
#[cfg(feature = "api-override")]
247247
api_endpoint: ApiEndpoint,
248248
cmd_rx: mpsc::UnboundedReceiver<Message>,
249249
cache_dir: PathBuf,
250-
bridge_dns_proxy_provider: Box<dyn BridgeAndDNSProxy>,
250+
bridge_dns_proxy_provider: B,
251251
access_method_settings: Settings,
252252
address_cache: AddressCache,
253253
access_method_event_sender: mpsc::UnboundedSender<(AccessMethodEvent, oneshot::Sender<()>)>,
254254
connection_mode_provider_sender: mpsc::UnboundedSender<ApiConnectionMode>,
255255
current: ResolvedConnectionMode,
256256
/// `index` is used to keep track of the [`AccessMethodSetting`] to use.
257257
index: usize,
258-
provider: PhantomData<P>,
259258
}
260259

261-
impl<P> AccessModeSelector<P>
262-
where
263-
P: AllowedClientsProvider + 'static,
264-
{
260+
impl<B: AccessMethodResolver + 'static> AccessModeSelector<B> {
265261
pub async fn spawn(
266262
cache_dir: PathBuf,
267-
mut bridge_dns_proxy_provider: Box<dyn BridgeAndDNSProxy>,
263+
mut bridge_dns_proxy_provider: B,
268264
#[cfg_attr(not(feature = "api-override"), allow(unused_mut))]
269265
mut access_method_settings: Settings,
270266
#[cfg(feature = "api-override")] api_endpoint: ApiEndpoint,
@@ -283,18 +279,15 @@ where
283279

284280
// Always start looking from the position of `Direct`.
285281
let (index, next) = Self::find_next_active(0, &access_method_settings);
286-
let initial_connection_mode = Self::resolve_inner_with_default(
287-
&next,
288-
&address_cache,
289-
&mut *bridge_dns_proxy_provider,
290-
)
291-
.await;
282+
let initial_connection_mode =
283+
Self::resolve_inner_with_default(&next, &address_cache, &mut bridge_dns_proxy_provider)
284+
.await;
292285

293286
let (change_tx, change_rx) = mpsc::unbounded();
294287

295288
let api_connection_mode = initial_connection_mode.connection_mode.clone();
296289

297-
let selector: AccessModeSelector<P> = AccessModeSelector {
290+
let selector = AccessModeSelector {
298291
#[cfg(feature = "api-override")]
299292
api_endpoint,
300293
cmd_rx,
@@ -306,7 +299,6 @@ where
306299
connection_mode_provider_sender: change_tx,
307300
current: initial_connection_mode,
308301
index,
309-
provider: PhantomData,
310302
};
311303

312304
tokio::spawn(selector.into_future());
@@ -527,25 +519,25 @@ where
527519
Self::resolve_inner(
528520
&access_method,
529521
&self.address_cache,
530-
&mut *self.bridge_dns_proxy_provider,
522+
&mut self.bridge_dns_proxy_provider,
531523
)
532524
.await
533525
}
534526

535527
async fn resolve_inner(
536-
access_method: &AccessMethodSetting,
528+
method_setting: &AccessMethodSetting,
537529
address_cache: &AddressCache,
538-
bridge_dns_proxy_provider: &mut dyn BridgeAndDNSProxy,
530+
bridge_dns_proxy_provider: &mut B,
539531
) -> Option<ResolvedConnectionMode> {
540532
let connection_mode = bridge_dns_proxy_provider
541-
.match_access_method(access_method)
533+
.resolve_access_method_setting(method_setting)
542534
.await?;
543535
let endpoint =
544-
resolve_allowed_endpoint::<P>(&connection_mode, address_cache.get_address().await);
536+
resolve_allowed_endpoint::<B>(&connection_mode, address_cache.get_address().await);
545537
Some(ResolvedConnectionMode {
546538
connection_mode,
547539
endpoint,
548-
setting: access_method.clone(),
540+
setting: method_setting.clone(),
549541
})
550542
}
551543

@@ -558,21 +550,21 @@ where
558550
Self::resolve_inner_with_default(
559551
&access_method,
560552
&self.address_cache,
561-
&mut *self.bridge_dns_proxy_provider,
553+
&mut self.bridge_dns_proxy_provider,
562554
)
563555
.await
564556
}
565557

566558
async fn resolve_inner_with_default(
567559
access_method: &AccessMethodSetting,
568560
address_cache: &AddressCache,
569-
bridge_dns_proxy_provider: &mut dyn BridgeAndDNSProxy,
561+
bridge_dns_proxy_provider: &mut B,
570562
) -> ResolvedConnectionMode {
571563
match Self::resolve_inner(access_method, address_cache, bridge_dns_proxy_provider).await {
572564
Some(resolved) => resolved,
573565
None => {
574566
log::trace!("Defaulting to direct API connection");
575-
let endpoint = resolve_allowed_endpoint::<P>(
567+
let endpoint = resolve_allowed_endpoint::<B>(
576568
&ApiConnectionMode::Direct,
577569
address_cache.get_address().await,
578570
);
@@ -587,24 +579,26 @@ where
587579
}
588580

589581
#[async_trait]
590-
pub trait BridgeAndDNSProxy: Send + Sync {
591-
async fn match_access_method(
582+
pub trait AccessMethodResolver: Send + Sync {
583+
async fn resolve_access_method_setting(
592584
&mut self,
593585
access_method: &AccessMethodSetting,
594586
) -> Option<ApiConnectionMode>;
587+
588+
fn allowed_clients(connection_mode: &ApiConnectionMode) -> AllowedClients;
595589
}
596590

597-
pub fn resolve_allowed_endpoint<P>(
591+
pub fn resolve_allowed_endpoint<B>(
598592
connection_mode: &ApiConnectionMode,
599593
fallback: SocketAddr,
600594
) -> AllowedEndpoint
601595
where
602-
P: AllowedClientsProvider,
596+
B: AccessMethodResolver,
603597
{
604598
let endpoint = match connection_mode.get_endpoint() {
605599
Some(endpoint) => endpoint,
606600
None => Endpoint::from_socket_address(fallback, TransportProtocol::Tcp),
607601
};
608-
let clients = P::allowed_clients(connection_mode);
602+
let clients = B::allowed_clients(connection_mode);
609603
AllowedEndpoint { endpoint, clients }
610604
}

mullvad-api/src/proxy.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
task::{self, Poll},
99
};
1010
use talpid_types::{
11-
net::{proxy, AllowedClients, Endpoint, TransportProtocol},
11+
net::{proxy, Endpoint, TransportProtocol},
1212
ErrorExt,
1313
};
1414
use tokio::{
@@ -53,10 +53,6 @@ impl ConnectionModeProvider for StaticConnectionModeProvider {
5353
}
5454
}
5555

56-
pub trait AllowedClientsProvider: Send + Sync {
57-
fn allowed_clients(connection_mode: &ApiConnectionMode) -> AllowedClients;
58-
}
59-
6056
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
6157
pub enum ApiConnectionMode {
6258
/// Connect directly to the target.

mullvad-daemon/src/api.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::DaemonCommand;
44
use crate::DaemonEventSender;
55
use futures::{channel::mpsc, StreamExt};
66
use mullvad_api::{
7-
access_mode::BridgeAndDNSProxy,
7+
access_mode::AccessMethodResolver,
88
availability::ApiAvailability,
9-
proxy::{AllowedClientsProvider, ApiConnectionMode, ProxyConfig},
9+
proxy::{ApiConnectionMode, ProxyConfig},
1010
};
1111
use mullvad_encrypted_dns_proxy::state::EncryptedDnsProxyState;
1212
use mullvad_management_interface::async_trait;
@@ -16,12 +16,12 @@ use mullvad_types::access_method::{AccessMethod, AccessMethodSetting, BuiltInAcc
1616
use talpid_core::mpsc::Sender;
1717
use talpid_types::net::{proxy::CustomProxy, AllowedClients, Connectivity};
1818

19-
pub struct BridgeAndDNSProxyProvider {
19+
pub struct DaemonAccessMethodResolver {
2020
relay_selector: RelaySelector,
2121
encrypted_dns_proxy_cache: EncryptedDnsProxyState,
2222
}
2323

24-
impl BridgeAndDNSProxyProvider {
24+
impl DaemonAccessMethodResolver {
2525
pub fn new(
2626
relay_selector: RelaySelector,
2727
encrypted_dns_proxy_cache: EncryptedDnsProxyState,
@@ -34,8 +34,8 @@ impl BridgeAndDNSProxyProvider {
3434
}
3535

3636
#[async_trait]
37-
impl BridgeAndDNSProxy for BridgeAndDNSProxyProvider {
38-
async fn match_access_method(
37+
impl AccessMethodResolver for DaemonAccessMethodResolver {
38+
async fn resolve_access_method_setting(
3939
&mut self,
4040
access_method: &AccessMethodSetting,
4141
) -> Option<ApiConnectionMode> {
@@ -73,12 +73,7 @@ impl BridgeAndDNSProxy for BridgeAndDNSProxyProvider {
7373
};
7474
Some(connection_mode)
7575
}
76-
}
77-
78-
#[derive(Clone, Copy)]
79-
pub struct AllowedClientsSelector {}
8076

81-
impl AllowedClientsProvider for AllowedClientsSelector {
8277
#[cfg(unix)]
8378
fn allowed_clients(connection_mode: &ApiConnectionMode) -> AllowedClients {
8479
match connection_mode {

mullvad-daemon/src/lib.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub mod version;
3131
mod version_check;
3232

3333
use crate::target_state::PersistentTargetState;
34-
use api::{AllowedClientsSelector, BridgeAndDNSProxyProvider};
34+
use api::DaemonAccessMethodResolver;
3535
use device::{AccountEvent, PrivateAccountAndDevice, PrivateDeviceEvent};
3636
use futures::{
3737
channel::{mpsc, oneshot},
@@ -41,7 +41,10 @@ use futures::{
4141
use geoip::GeoIpHandler;
4242
use leak_checker::{LeakChecker, LeakInfo};
4343
use management_interface::ManagementInterfaceServer;
44-
use mullvad_api::{access_mode::AccessMethodEvent, proxy::AllowedClientsProvider, ApiEndpoint};
44+
use mullvad_api::{
45+
access_mode::{AccessMethodEvent, AccessMethodResolver},
46+
ApiEndpoint,
47+
};
4548
use mullvad_encrypted_dns_proxy::state::EncryptedDnsProxyState;
4649
use mullvad_relay_selector::{RelaySelector, SelectorConfig};
4750
#[cfg(target_os = "android")]
@@ -709,12 +712,12 @@ impl Daemon {
709712

710713
let encrypted_dns_proxy_cache = EncryptedDnsProxyState::default();
711714
let bridge_dns_proxy_provider =
712-
BridgeAndDNSProxyProvider::new(relay_selector.clone(), encrypted_dns_proxy_cache);
715+
DaemonAccessMethodResolver::new(relay_selector.clone(), encrypted_dns_proxy_cache);
713716

714717
let (access_mode_handler, access_mode_provider) =
715-
mullvad_api::access_mode::AccessModeSelector::<AllowedClientsSelector>::spawn(
718+
mullvad_api::access_mode::AccessModeSelector::spawn(
716719
config.cache_dir.clone(),
717-
Box::new(bridge_dns_proxy_provider),
720+
bridge_dns_proxy_provider,
718721
settings.api_access_methods.clone(),
719722
#[cfg(feature = "api-override")]
720723
config.endpoint.clone(),
@@ -2855,7 +2858,7 @@ impl Daemon {
28552858
let api_proxy = self.create_limited_api_proxy(connection_mode.clone());
28562859
let proxy_endpoint = AllowedEndpoint {
28572860
endpoint: proxy.get_remote_endpoint().endpoint,
2858-
clients: AllowedClientsSelector::allowed_clients(&connection_mode),
2861+
clients: DaemonAccessMethodResolver::allowed_clients(&connection_mode),
28592862
};
28602863

28612864
let daemon_event_sender = self.tx.to_specialized_sender();

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ use talpid_types::{
5353
ErrorExt,
5454
};
5555

56-
/// [`WIREGUARD_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector
57-
/// should should prioritize on successive connection attempts. Note that these will *never*
58-
/// override user preferences. See [the documentation on `RelayQuery`][RelayQuery] for further
59-
/// details.
56+
/// [`WIREGUARD_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector should
57+
/// prioritize on successive connection attempts. Note that these will *never* override user
58+
/// preferences. See [the documentation on `RelayQuery`][RelayQuery] for further details.
6059
///
6160
/// This list should be kept in sync with the expected behavior defined in `docs/relay-selector.md`
6261
pub static WIREGUARD_RETRY_ORDER: LazyLock<Vec<RelayQuery>> = LazyLock::new(|| {
@@ -82,8 +81,8 @@ pub static WIREGUARD_RETRY_ORDER: LazyLock<Vec<RelayQuery>> = LazyLock::new(|| {
8281
]
8382
});
8483

85-
/// [`OPENVPN_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector
86-
/// should prioritize on successive connection attempts. Note that these will *never* override user
84+
/// [`OPENVPN_RETRY_ORDER`] defines an ordered set of relay parameters which the relay selector should
85+
/// prioritize on successive connection attempts. Note that these will *never* override user
8786
/// preferences. See [the documentation on `RelayQuery`][RelayQuery] for further details.
8887
///
8988
/// This list should be kept in sync with the expected behavior defined in `docs/relay-selector.md`
@@ -694,10 +693,10 @@ impl RelaySelector {
694693
parsed_relays: &RelayList,
695694
custom_lists: &CustomListsSettings,
696695
) -> Result<GetRelay, Error> {
697-
// FIXME: A bit of defensive programming - calling `get_wireguard_relay_inner` with a query
698-
// that doesn't specify Wireguard as the desired tunnel type is not valid and will
699-
// lead to unwanted behavior. This should be seen as a workaround, and it would be
700-
// nicer to lift this invariant to be checked by the type system instead.
696+
// FIXME: A bit of defensive programming - calling `get_wireguard_relay_inner` with a query that
697+
// doesn't specify Wireguard as the desired tunnel type is not valid and will lead
698+
// to unwanted behavior. This should be seen as a workaround, and it would be nicer
699+
// to lift this invariant to be checked by the type system instead.
701700
let mut query = query.clone();
702701
query.set_tunnel_protocol(TunnelType::Wireguard)?;
703702
Self::get_wireguard_relay_inner(&query, custom_lists, parsed_relays)

test/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)