7
7
#[ cfg( feature = "api-override" ) ]
8
8
use crate :: ApiEndpoint ;
9
9
use crate :: {
10
- proxy:: { AllowedClientsProvider , ApiConnectionMode , ConnectionModeProvider , ProxyConfig } ,
10
+ proxy:: { AllowedClientsProvider , ApiConnectionMode , ConnectionModeProvider } ,
11
11
AddressCache ,
12
12
} ;
13
+ use async_trait:: async_trait;
13
14
use futures:: {
14
15
channel:: { mpsc, oneshot} ,
15
16
StreamExt ,
16
17
} ;
17
- use mullvad_encrypted_dns_proxy:: state:: EncryptedDnsProxyState ;
18
- use mullvad_relay_selector:: RelaySelector ;
19
- use mullvad_types:: access_method:: {
20
- AccessMethod , AccessMethodSetting , BuiltInAccessMethod , Id , Settings ,
21
- } ;
18
+ use mullvad_types:: access_method:: { AccessMethod , AccessMethodSetting , Id , Settings } ;
22
19
use std:: { marker:: PhantomData , net:: SocketAddr , path:: PathBuf } ;
23
- use talpid_types:: net:: { proxy :: CustomProxy , AllowedEndpoint , Endpoint , TransportProtocol } ;
20
+ use talpid_types:: net:: { AllowedEndpoint , Endpoint , TransportProtocol } ;
24
21
25
22
pub enum Message {
26
23
Get ( ResponseTx < ResolvedConnectionMode > ) ,
@@ -250,10 +247,7 @@ pub struct AccessModeSelector<P> {
250
247
api_endpoint : ApiEndpoint ,
251
248
cmd_rx : mpsc:: UnboundedReceiver < Message > ,
252
249
cache_dir : PathBuf ,
253
- /// Used for selecting a Bridge when the `Mullvad Bridges` access method is used.
254
- relay_selector : RelaySelector ,
255
- /// Used for selecting a config for the 'Encrypted DNS proxy' access method.
256
- encrypted_dns_proxy_cache : EncryptedDnsProxyState ,
250
+ bridge_dns_proxy_provider : Box < dyn BridgeAndDNSProxy > ,
257
251
access_method_settings : Settings ,
258
252
address_cache : AddressCache ,
259
253
access_method_event_sender : mpsc:: UnboundedSender < ( AccessMethodEvent , oneshot:: Sender < ( ) > ) > ,
@@ -270,7 +264,7 @@ where
270
264
{
271
265
pub async fn spawn (
272
266
cache_dir : PathBuf ,
273
- relay_selector : RelaySelector ,
267
+ mut bridge_dns_proxy_provider : Box < dyn BridgeAndDNSProxy > ,
274
268
#[ cfg_attr( not( feature = "api-override" ) , allow( unused_mut) ) ]
275
269
mut access_method_settings : Settings ,
276
270
#[ cfg( feature = "api-override" ) ] api_endpoint : ApiEndpoint ,
@@ -287,16 +281,12 @@ where
287
281
}
288
282
}
289
283
290
- // Initialize the Encrypted DNS cache
291
- let mut encrypted_dns_proxy_cache = EncryptedDnsProxyState :: default ( ) ;
292
-
293
284
// Always start looking from the position of `Direct`.
294
285
let ( index, next) = Self :: find_next_active ( 0 , & access_method_settings) ;
295
286
let initial_connection_mode = Self :: resolve_inner_with_default (
296
287
& next,
297
- & relay_selector,
298
- & mut encrypted_dns_proxy_cache,
299
288
& address_cache,
289
+ & mut * bridge_dns_proxy_provider,
300
290
)
301
291
. await ;
302
292
@@ -309,8 +299,7 @@ where
309
299
api_endpoint,
310
300
cmd_rx,
311
301
cache_dir,
312
- relay_selector,
313
- encrypted_dns_proxy_cache,
302
+ bridge_dns_proxy_provider,
314
303
access_method_settings,
315
304
address_cache,
316
305
access_method_event_sender,
@@ -537,22 +526,20 @@ where
537
526
) -> Option < ResolvedConnectionMode > {
538
527
Self :: resolve_inner (
539
528
& access_method,
540
- & self . relay_selector ,
541
- & mut self . encrypted_dns_proxy_cache ,
542
529
& self . address_cache ,
530
+ & mut * self . bridge_dns_proxy_provider ,
543
531
)
544
532
. await
545
533
}
546
534
547
535
async fn resolve_inner (
548
536
access_method : & AccessMethodSetting ,
549
- relay_selector : & RelaySelector ,
550
- encrypted_dns_proxy_cache : & mut EncryptedDnsProxyState ,
551
537
address_cache : & AddressCache ,
538
+ bridge_dns_proxy_provider : & mut dyn BridgeAndDNSProxy ,
552
539
) -> Option < ResolvedConnectionMode > {
553
- let connection_mode =
554
- Self :: resolve_connection_mode ( access_method, relay_selector , encrypted_dns_proxy_cache )
555
- . await ?;
540
+ let connection_mode = bridge_dns_proxy_provider
541
+ . match_access_method ( access_method)
542
+ . await ?;
556
543
let endpoint =
557
544
resolve_allowed_endpoint :: < P > ( & connection_mode, address_cache. get_address ( ) . await ) ;
558
545
Some ( ResolvedConnectionMode {
@@ -570,27 +557,18 @@ where
570
557
) -> ResolvedConnectionMode {
571
558
Self :: resolve_inner_with_default (
572
559
& access_method,
573
- & self . relay_selector ,
574
- & mut self . encrypted_dns_proxy_cache ,
575
560
& self . address_cache ,
561
+ & mut * self . bridge_dns_proxy_provider ,
576
562
)
577
563
. await
578
564
}
579
565
580
566
async fn resolve_inner_with_default (
581
567
access_method : & AccessMethodSetting ,
582
- relay_selector : & RelaySelector ,
583
- encrypted_dns_proxy_cache : & mut EncryptedDnsProxyState ,
584
568
address_cache : & AddressCache ,
569
+ bridge_dns_proxy_provider : & mut dyn BridgeAndDNSProxy ,
585
570
) -> ResolvedConnectionMode {
586
- match Self :: resolve_inner (
587
- access_method,
588
- relay_selector,
589
- encrypted_dns_proxy_cache,
590
- address_cache,
591
- )
592
- . await
593
- {
571
+ match Self :: resolve_inner ( access_method, address_cache, bridge_dns_proxy_provider) . await {
594
572
Some ( resolved) => resolved,
595
573
None => {
596
574
log:: trace!( "Defaulting to direct API connection" ) ;
@@ -606,42 +584,14 @@ where
606
584
}
607
585
}
608
586
}
587
+ }
609
588
610
- async fn resolve_connection_mode (
589
+ #[ async_trait]
590
+ pub trait BridgeAndDNSProxy : Send + Sync {
591
+ async fn match_access_method (
592
+ & mut self ,
611
593
access_method : & AccessMethodSetting ,
612
- relay_selector : & RelaySelector ,
613
- encrypted_dns_proxy_cache : & mut EncryptedDnsProxyState ,
614
- ) -> Option < ApiConnectionMode > {
615
- let connection_mode = {
616
- match & access_method. access_method {
617
- AccessMethod :: BuiltIn ( BuiltInAccessMethod :: Direct ) => ApiConnectionMode :: Direct ,
618
- AccessMethod :: BuiltIn ( BuiltInAccessMethod :: Bridge ) => {
619
- let Some ( bridge) = relay_selector. get_bridge_forced ( ) else {
620
- log:: warn!( "Could not select a Mullvad bridge" ) ;
621
- log:: debug!( "The relay list might be empty" ) ;
622
- return None ;
623
- } ;
624
- let proxy = CustomProxy :: Shadowsocks ( bridge) ;
625
- ApiConnectionMode :: Proxied ( ProxyConfig :: from ( proxy) )
626
- }
627
- AccessMethod :: BuiltIn ( BuiltInAccessMethod :: EncryptedDnsProxy ) => {
628
- if let Err ( error) = encrypted_dns_proxy_cache. fetch_configs ( "frakta.eu" ) . await {
629
- log:: warn!( "Failed to fetch new Encrypted DNS Proxy configurations" ) ;
630
- log:: debug!( "{error:#?}" ) ;
631
- }
632
- let Some ( edp) = encrypted_dns_proxy_cache. next_configuration ( ) else {
633
- log:: warn!( "Could not select next Encrypted DNS proxy config" ) ;
634
- return None ;
635
- } ;
636
- ApiConnectionMode :: Proxied ( ProxyConfig :: from ( edp) )
637
- }
638
- AccessMethod :: Custom ( config) => {
639
- ApiConnectionMode :: Proxied ( ProxyConfig :: from ( config. clone ( ) ) )
640
- }
641
- }
642
- } ;
643
- Some ( connection_mode)
644
- }
594
+ ) -> Option < ApiConnectionMode > ;
645
595
}
646
596
647
597
pub fn resolve_allowed_endpoint < P > (
0 commit comments