1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
|
use super::{Error, TestContext, WAIT_FOR_TUNNEL_STATE_TIMEOUT, config::TEST_CONFIG};
use crate::{
mullvad_daemon::RpcClientProvider,
network_monitor::{
self, MonitorOptions, MonitorUnexpectedlyStopped, PacketMonitor, start_packet_monitor,
},
tests::{
account::{clear_devices, new_device_client},
helpers,
},
};
use anyhow::{Context, anyhow, bail, ensure};
use futures::StreamExt;
use mullvad_management_interface::{MullvadProxyClient, client::DaemonEvent};
use mullvad_relay_selector::{
GetRelay, RelaySelector, SelectorConfig, WireguardConfig,
query::{OpenVpnRelayQuery, RelayQuery, WireguardRelayQuery},
};
use mullvad_types::{
constraints::Constraint,
custom_list::CustomList,
relay_constraints::{
GeographicLocationConstraint, LocationConstraint, RelayConstraints, RelaySettings,
},
relay_list::Relay,
states::TunnelState,
};
use pcap::Direction;
use pnet_packet::ip::IpNextHeaderProtocols;
use std::{
collections::HashMap,
net::{IpAddr, Ipv4Addr, SocketAddr},
path::Path,
time::{Duration, Instant},
};
use talpid_types::net::wireguard::{PeerConfig, PrivateKey, TunnelConfig};
use test_rpc::{AmIMullvad, ServiceClient, SpawnOpts, meta::Os, package::Package};
use tokio::time::sleep;
pub const THROTTLE_RETRY_DELAY: Duration = Duration::from_secs(120);
const CHECKER_FILENAME_WINDOWS: &str = "connection-checker.exe";
const CHECKER_FILENAME_UNIX: &str = "connection-checker";
const AM_I_MULLVAD_TIMEOUT_S: u64 = 10;
const LEAK_TIMEOUT_S: u64 = 1;
/// Timeout of [ConnCheckerHandle::check_connection].
const CONN_CHECKER_TIMEOUT: Duration = Duration::from_secs(
AM_I_MULLVAD_TIMEOUT_S // https://am.i.mullvad.net timeout
+ LEAK_TIMEOUT_S // leak-tcp timeout
+ LEAK_TIMEOUT_S // leak-icmp timeout
+ 1, // plus some extra grace time
);
#[macro_export]
macro_rules! assert_tunnel_state {
($mullvad_client:expr, $pattern:pat) => {{
let state = $mullvad_client.get_tunnel_state().await?;
assert!(matches!(state, $pattern), "state: {:?}", state);
}};
}
/// Install the app cleanly, failing if the installer doesn't succeed
/// or if the VPN service is not running afterwards.
pub async fn install_app(
rpc: &ServiceClient,
app_filename: &str,
rpc_provider: &RpcClientProvider,
) -> anyhow::Result<MullvadProxyClient> {
// install package
log::info!("Installing app '{}'", app_filename);
rpc.install_app(get_package_desc(app_filename)).await?;
// Set the log level to trace
rpc.set_daemon_log_level(test_rpc::mullvad_daemon::Verbosity::Trace)
.await
.context("Failed to set log level")?;
replace_openvpn_certificate(rpc)
.await
.context("Replace OpenVPN certs")?;
// Override env vars
rpc.set_daemon_environment(get_app_env().await?)
.await
.context("Failed to set daemon environment")?;
// Wait for the relay list to be updated
let mut mullvad_client = rpc_provider.new_client().await;
helpers::ensure_updated_relay_list(&mut mullvad_client)
.await
.context("Failed to update relay list")?;
Ok(mullvad_client)
}
/// Replace the OpenVPN CA certificate which is currently used by the installed Mullvad App.
/// This needs to be invoked after reach (re)installation to use the custom OpenVPN certificate.
async fn replace_openvpn_certificate(rpc: &ServiceClient) -> Result<(), Error> {
const DEST_CERT_FILENAME: &str = "ca.crt";
let dest_dir = match TEST_CONFIG.os {
Os::Windows => "C:\\Program Files\\Mullvad VPN\\resources",
Os::Linux => "/opt/Mullvad VPN/resources",
Os::Macos => "/Applications/Mullvad VPN.app/Contents/Resources",
};
let dest = Path::new(dest_dir)
.join(DEST_CERT_FILENAME)
.as_os_str()
.to_string_lossy()
.into_owned();
rpc.write_file(dest, TEST_CONFIG.openvpn_certificate.to_vec())
.await
.map_err(Error::Rpc)
}
pub fn get_package_desc(name: &str) -> Package {
Package {
path: Path::new(&TEST_CONFIG.artifacts_dir).join(name),
}
}
/// Reboot the guest virtual machine.
///
/// # macOS
/// The tunnel must be reconfigured after the virtual machine is up,
/// or macOS refuses to assign an IP. The reasons for this are poorly understood.
pub async fn reboot(rpc: &mut ServiceClient) -> Result<(), Error> {
rpc.reboot().await?;
#[cfg(target_os = "macos")]
crate::vm::network::macos::configure_tunnel()
.await
.context("Failed to recreate custom wg tun: {error}")?;
Ok(())
}
#[derive(Debug, Default)]
pub struct ProbeResult {
tcp: usize,
udp: usize,
icmp: usize,
}
impl ProbeResult {
pub fn all(&self) -> bool {
self.tcp > 0 && self.udp > 0 && self.icmp > 0
}
pub fn none(&self) -> bool {
!self.any()
}
pub fn any(&self) -> bool {
self.tcp > 0 || self.udp > 0 || self.icmp > 0
}
}
/// Return whether the guest exit IP is a Mullvad relay
pub async fn using_mullvad_exit(rpc: &ServiceClient) -> bool {
log::info!("Test whether exit IP is a mullvad relay");
geoip_lookup_with_retries(rpc)
.await
.unwrap()
.mullvad_exit_ip
}
/// Get VPN tunnel interface name
pub async fn get_tunnel_interface(client: &mut MullvadProxyClient) -> anyhow::Result<String> {
match client.get_tunnel_state().await? {
TunnelState::Connecting { endpoint, .. } | TunnelState::Connected { endpoint, .. } => {
let Some(tunnel_interface) = endpoint.tunnel_interface else {
bail!("Unknown tunnel interface");
};
Ok(tunnel_interface)
}
_ => bail!("Tunnel is not up"),
}
}
/// Sends a number of probes and returns the number of observed packets (UDP, TCP, or ICMP)
pub async fn send_guest_probes(
rpc: ServiceClient,
interface: String,
destination: SocketAddr,
) -> ProbeResult {
const MONITOR_DURATION: Duration = Duration::from_secs(8);
let pktmon = start_packet_monitor(
move |packet| packet.destination.ip() == destination.ip(),
MonitorOptions {
direction: Some(network_monitor::Direction::In),
timeout: Some(MONITOR_DURATION),
..Default::default()
},
)
.await;
let send_handle = tokio::spawn(send_guest_probes_without_monitor(
rpc,
Some(interface),
destination,
));
let monitor_result = pktmon.wait().await.unwrap();
send_handle.abort();
let _ = send_handle.await;
let mut result = ProbeResult::default();
for pkt in monitor_result.packets {
match pkt.protocol {
IpNextHeaderProtocols::Tcp => {
result.tcp = result.tcp.saturating_add(1);
}
IpNextHeaderProtocols::Udp => {
result.udp = result.udp.saturating_add(1);
}
IpNextHeaderProtocols::Icmp => {
result.icmp = result.icmp.saturating_add(1);
}
_ => (),
}
}
result
}
/// Send one probe per transport protocol to `destination` without running a packet monitor
pub async fn send_guest_probes_without_monitor(
rpc: ServiceClient,
interface: Option<String>,
destination: SocketAddr,
) {
let bind_addr = if let Some(ref interface) = interface {
SocketAddr::new(
rpc.get_interface_ip(interface.clone())
.await
.expect("failed to obtain interface IP"),
0,
)
} else {
"0.0.0.0:0".parse().unwrap()
};
let tcp_rpc = rpc.clone();
let tcp_interface = interface.clone();
let tcp_send = async move {
tcp_rpc
.send_tcp(tcp_interface, bind_addr, destination)
.await
};
let udp_rpc = rpc.clone();
let udp_interface = interface.clone();
let udp_send = async move {
udp_rpc
.send_udp(udp_interface, bind_addr, destination)
.await
};
let icmp = async move { ping_with_timeout(&rpc, destination.ip(), interface).await };
let _ = tokio::join!(tcp_send, udp_send, icmp);
}
pub async fn ping_with_timeout(
rpc: &ServiceClient,
dest: IpAddr,
interface: Option<String>,
) -> Result<(), Error> {
const DEFAULT_PING_SIZE: usize = 64;
rpc.send_ping(dest, interface, DEFAULT_PING_SIZE)
.await
.map_err(Error::Rpc)
}
pub async fn ping_sized_with_timeout(
rpc: &ServiceClient,
dest: IpAddr,
interface: Option<String>,
size: usize,
) -> Result<(), Error> {
rpc.send_ping(dest, interface, size)
.await
.map_err(Error::Rpc)
}
/// Return the first address that `host` resolves to
pub async fn resolve_hostname_with_retries(
host: impl tokio::net::ToSocketAddrs,
) -> std::io::Result<SocketAddr> {
const MAX_ATTEMPTS: usize = 10;
const RETRY_DELAY: Duration = Duration::from_secs(5);
let mut last_error = None;
let mut attempt = 0;
loop {
if attempt >= MAX_ATTEMPTS {
break Err(last_error.unwrap_or(std::io::Error::other("lookup timed out")));
}
attempt += 1;
match tokio::net::lookup_host(&host).await {
Ok(mut addrs) => {
if let Some(addr) = addrs.next() {
// done
break Ok(addr);
}
}
Err(err) => last_error = Some(err),
}
tokio::time::sleep(RETRY_DELAY).await;
}
}
/// Get the mac address (if any) of a network interface (on the test-manager machine).
#[cfg(target_os = "linux")] // not used on macos
pub fn get_interface_mac(interface: &str) -> anyhow::Result<Option<[u8; 6]>> {
let addrs = nix::ifaddrs::getifaddrs().map_err(|error| {
log::error!("Failed to obtain interfaces: {}", error);
test_rpc::Error::Syscall
})?;
let mut interface_exists = false;
let mac_addr = addrs
.filter(|addr| addr.interface_name == interface)
.find_map(|addr| {
// sadly, the only way of distinguishing between "iface doesn't exist" and
// "iface has no mac addr" is to check if the interface appears anywhere in the list.
interface_exists = true;
let addr = addr.address.as_ref()?;
let link_addr = addr.as_link_addr()?;
let mac_addr = link_addr.addr()?;
Some(mac_addr)
});
if interface_exists {
Ok(mac_addr)
} else {
bail!("Interface not found: {interface:?}")
}
}
/// Get the index of a network interface (on the test-manager machine).
#[cfg(target_os = "linux")] // not used on macos
pub fn get_interface_index(interface: &str) -> anyhow::Result<std::ffi::c_uint> {
use nix::errno::Errno;
use std::ffi::CString;
let interface = CString::new(interface).context(anyhow!(
"Failed to turn interface name {interface:?} into cstr"
))?;
match unsafe { libc::if_nametoindex(interface.as_ptr()) } {
0 => {
let err = Errno::last();
Err(err).context("Failed to get interface index")
}
i => Ok(i),
}
}
/// Log in and retry if it fails due to throttling
pub async fn login_with_retries(
mullvad_client: &mut MullvadProxyClient,
) -> Result<(), mullvad_management_interface::Error> {
log::debug!("Logging in/generating device");
loop {
match mullvad_client
.login_account(TEST_CONFIG.account_number.clone())
.await
{
Err(mullvad_management_interface::Error::Rpc(status))
if status.message().to_uppercase().contains("THROTTLED") =>
{
// Work around throttling errors by sleeping
log::debug!(
"Login failed due to throttling. Sleeping for {} seconds",
THROTTLE_RETRY_DELAY.as_secs()
);
tokio::time::sleep(THROTTLE_RETRY_DELAY).await;
}
Err(err) => break Err(err),
Ok(_) => break Ok(()),
}
}
}
/// Ensure that the test runner is logged in to an account.
///
/// This will first check whether we are logged in. If not, it will also try to login
/// on your behalf. If this function returns without any errors, we are logged in to a valid
/// account.
pub async fn ensure_logged_in(mullvad_client: &mut MullvadProxyClient) -> anyhow::Result<()> {
if !matches!(
mullvad_client.update_device().await,
Err(mullvad_management_interface::Error::DeviceNotFound)
) && mullvad_client.get_device().await?.is_logged_in()
{
return Ok(());
}
log::info!("Current device not logged in. Clearing devices and logging in.");
// We are apparently not logged in already.. Try to log in.
clear_devices(
&new_device_client()
.await
.context("Failed to create device client")?,
)
.await
.context("failed to clear devices")?;
login_with_retries(mullvad_client)
.await
.context("Failed to log in")?;
Ok(())
}
/// Try to connect to a Mullvad Tunnel.
///
/// # Returns
/// - `Result::Ok(new_state)` if the daemon successfully connected to a tunnel
/// - `Result::Err` if:
/// - The daemon failed to even begin connecting. Then [`Error::Rpc`] is returned.
/// - The daemon started to connect but ended up in the [`TunnelState::Error`] state. Then
/// [`Error::UnexpectedErrorState`] is returned
pub async fn connect_and_wait(
mullvad_client: &mut MullvadProxyClient,
) -> Result<TunnelState, Error> {
log::info!("Connecting");
let initial_time = Instant::now();
mullvad_client.connect_tunnel().await?;
let new_state = wait_for_tunnel_state(mullvad_client.clone(), |state| {
matches!(
state,
TunnelState::Connected { .. } | TunnelState::Error(..)
)
})
.await?;
if let TunnelState::Error(error_state) = new_state {
return Err(Error::UnexpectedErrorState(error_state));
}
log::info!(
"Connected after {} seconds",
initial_time.elapsed().as_secs()
);
Ok(new_state)
}
pub async fn disconnect_and_wait(mullvad_client: &mut MullvadProxyClient) -> Result<(), Error> {
log::trace!("Disconnecting");
mullvad_client.disconnect_tunnel().await?;
wait_for_tunnel_state(mullvad_client.clone(), |state| {
matches!(state, TunnelState::Disconnected { .. })
})
.await?;
log::trace!("Disconnected");
Ok(())
}
pub async fn wait_for_tunnel_state(
mut rpc: MullvadProxyClient,
accept_state_fn: impl Fn(&mullvad_types::states::TunnelState) -> bool,
) -> Result<mullvad_types::states::TunnelState, Error> {
let events = rpc
.events_listen()
.await
.map_err(|status| Error::Daemon(format!("Failed to get event stream: {status}")))?;
let state = rpc
.get_tunnel_state()
.await
.map_err(|error| Error::Daemon(format!("Failed to get tunnel state: {error:?}")))?;
if accept_state_fn(&state) {
return Ok(state);
}
find_next_tunnel_state(events, accept_state_fn).await
}
pub async fn find_next_tunnel_state(
stream: impl futures::Stream<Item = Result<DaemonEvent, mullvad_management_interface::Error>>
+ Unpin,
accept_state_fn: impl Fn(&mullvad_types::states::TunnelState) -> bool,
) -> Result<mullvad_types::states::TunnelState, Error> {
tokio::time::timeout(
WAIT_FOR_TUNNEL_STATE_TIMEOUT,
find_daemon_event(stream, |daemon_event| match daemon_event {
DaemonEvent::TunnelState(state) if accept_state_fn(&state) => Some(state),
_ => None,
}),
)
.await
.map_err(|_error| Error::Daemon(String::from("Tunnel event listener timed out")))?
}
pub async fn find_daemon_event<Accept, AcceptedEvent>(
mut event_stream: impl futures::Stream<
Item = Result<DaemonEvent, mullvad_management_interface::Error>,
> + Unpin,
accept_event: Accept,
) -> Result<AcceptedEvent, Error>
where
Accept: Fn(DaemonEvent) -> Option<AcceptedEvent>,
{
loop {
match event_stream.next().await {
Some(Ok(daemon_event)) => match accept_event(daemon_event) {
Some(accepted_event) => break Ok(accepted_event),
None => continue,
},
Some(Err(status)) => {
break Err(Error::Daemon(format!("Failed to get next event: {status}")));
}
None => break Err(Error::Daemon(String::from("Lost daemon event stream"))),
}
}
}
/// Set environment variables specified by `env` and restart the Mullvad daemon.
/// Returns a new [rpc client][`MullvadProxyClient`], since the old client *probably*
/// can't communicate with the new daemon.
///
/// # Note
/// This is just a thin wrapper around [`ServiceClient::set_daemon_environment`] which also
/// invalidates the old [`MullvadProxyClient`].
pub async fn restart_daemon_with<K, V, Env>(
rpc: &ServiceClient,
test_context: &TestContext,
_: MullvadProxyClient, // Just consume the old proxy client
env: Env,
) -> Result<MullvadProxyClient, Error>
where
Env: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: Into<String>,
{
rpc.set_daemon_environment(env).await?;
// Need to create a new `mullvad_client` here after the restart
// otherwise we *probably* can't communicate with the daemon.
Ok(test_context.rpc_provider.new_client().await)
}
pub async fn geoip_lookup_with_retries(rpc: &ServiceClient) -> Result<AmIMullvad, Error> {
const MAX_ATTEMPTS: usize = 5;
const BEFORE_RETRY_DELAY: Duration = Duration::from_secs(2);
let mut attempt = 0;
loop {
let result = rpc
.geoip_lookup(TEST_CONFIG.mullvad_host.clone())
.await
.map_err(Error::GeoipLookup);
attempt += 1;
if result.is_ok() || attempt >= MAX_ATTEMPTS {
return result;
}
tokio::time::sleep(BEFORE_RETRY_DELAY).await;
}
}
pub struct AbortOnDrop<T>(Option<tokio::task::JoinHandle<T>>);
impl<T> AbortOnDrop<T> {
pub fn new(inner: tokio::task::JoinHandle<T>) -> AbortOnDrop<T> {
AbortOnDrop(Some(inner))
}
}
impl<T> Drop for AbortOnDrop<T> {
fn drop(&mut self) {
if let Some(task) = self.0.take() {
task.abort();
}
}
}
/// Applies the given query to the daemon location selection. The query will be intersected with
/// the current location settings, so that the default location custom list is still used.
pub async fn apply_settings_from_relay_query(
mullvad_client: &mut MullvadProxyClient,
query: RelayQuery,
) -> anyhow::Result<()> {
// To prevent overwriting default custom list location constraint, we make an intersection with
// a query containing only the current location constraint
let intersected_relay_query = intersect_with_current_location(mullvad_client, query.clone())
.await
.with_context(|| {
format!("Failed to join query with current daemon settings. Query: {query:#?}")
})?;
let (constraints, bridge_state, bridge_settings, obfuscation) =
intersected_relay_query.into_settings();
mullvad_client
.set_relay_settings(constraints.into())
.await
.context("Failed to set daemon settings")?;
mullvad_client
.set_bridge_state(bridge_state)
.await
.context("Failed to set bridge state")?;
mullvad_client
.set_bridge_settings(bridge_settings)
.await
.context("Failed to set bridge settings")?;
mullvad_client
.set_obfuscation_settings(obfuscation)
.await
.context("Failed to set obfuscation settings")?;
Ok(())
}
pub async fn set_custom_endpoint(
mullvad_client: &mut MullvadProxyClient,
custom_endpoint: mullvad_types::CustomTunnelEndpoint,
) -> Result<(), Error> {
mullvad_client
.set_relay_settings(RelaySettings::CustomTunnelEndpoint(custom_endpoint))
.await
.map_err(|error| Error::Daemon(format!("Failed to set relay settings: {error}")))
}
pub async fn update_relay_constraints(
mullvad_client: &mut MullvadProxyClient,
fn_mut: impl FnOnce(&mut RelayConstraints),
) -> anyhow::Result<()> {
let settings = mullvad_client
.get_settings()
.await
.context("Failed to get setting from daemon")?;
let RelaySettings::Normal(mut relay_constraints) = settings.relay_settings else {
bail!("Mutating custom endpoint not supported");
};
fn_mut(&mut relay_constraints);
mullvad_client
.set_relay_settings(RelaySettings::Normal(relay_constraints))
.await
.context("Failed to set relay settings")?;
Ok(())
}
/// Wait for the relay list to be updated, to make sure we have the overridden one.
/// Time out after a while.
pub async fn ensure_updated_relay_list(
mullvad_client: &mut MullvadProxyClient,
) -> Result<(), mullvad_management_interface::Error> {
let mut events = mullvad_client.events_listen().await?;
mullvad_client.update_relay_locations().await?;
let _ = tokio::time::timeout(std::time::Duration::from_secs(3), async move {
while let Some(Ok(event)) = events.next().await {
if matches!(event, DaemonEvent::RelayList(_)) {
log::debug!("Received new relay list");
break;
}
}
})
.await;
Ok(())
}
pub fn unreachable_wireguard_tunnel() -> talpid_types::net::wireguard::ConnectionConfig {
talpid_types::net::wireguard::ConnectionConfig {
tunnel: TunnelConfig {
private_key: PrivateKey::new_from_random(),
addresses: vec![IpAddr::V4(Ipv4Addr::new(10, 64, 10, 1))],
},
peer: PeerConfig {
public_key: PrivateKey::new_from_random().public_key(),
allowed_ips: vec![
"0.0.0.0/0".parse().expect("Failed to parse ipv6 network"),
"::0/0".parse().expect("Failed to parse ipv6 network"),
],
endpoint: "1.3.3.7:1234".parse().unwrap(),
psk: None,
constant_packet_size: false,
},
exit_peer: None,
ipv4_gateway: Ipv4Addr::new(10, 64, 10, 1),
ipv6_gateway: None,
#[cfg(target_os = "linux")]
fwmark: None,
}
}
/// Return the current `MULLVAD_API_HOST` et al.
///
/// # Note
/// This is independent of the running daemon's environment.
/// It is solely dependant on the current value of [`TEST_CONFIG`].
pub async fn get_app_env() -> anyhow::Result<HashMap<String, String>> {
use mullvad_api_constants::env;
let api_host = format!("api.{}", TEST_CONFIG.mullvad_host);
let api_host_with_port = format!("{api_host}:443");
let api_addr = resolve_hostname_with_retries(api_host_with_port)
.await
.context("failed to resolve API host")?;
Ok(HashMap::from_iter(vec![
(env::API_HOST_VAR.to_string(), api_host),
(env::API_ADDR_VAR.to_string(), api_addr.to_string()),
]))
}
/// Constrain the daemon to only select the relay compatible with `query` and the current relay
/// settings when establishing all future tunnels (until relay settings are updated, see [`set_relay_settings`]).
/// Returns the selected [`Relay`] for future reference.
pub async fn constrain_to_relay(
mullvad_client: &mut MullvadProxyClient,
query: RelayQuery,
) -> anyhow::Result<Relay> {
let intersect_query = intersect_with_current_location(mullvad_client, query).await?;
let (exit, relay_constraints) =
get_single_relay_location_contraint(mullvad_client, intersect_query).await?;
update_relay_constraints(mullvad_client, |current_constraints| {
*current_constraints = relay_constraints
})
.await
.unwrap();
Ok(exit)
}
/// Intersects the given query with the current location constraints, to prevent accidentally
/// overwriting the default location custom list
async fn intersect_with_current_location(
mullvad_client: &mut MullvadProxyClient,
query: RelayQuery,
) -> anyhow::Result<RelayQuery> {
let settings = mullvad_client
.get_settings()
.await
.context("Failed to get settings")?;
let RelaySettings::Normal(constraint) = settings.relay_settings else {
unimplemented!("Setting location for a custom endpoint is not supported");
};
// Construct a relay query preserving only the information about the current location
let current_location_query = RelayQuery::new(
constraint.location,
Constraint::Any,
Constraint::Any,
query.tunnel_protocol(),
WireguardRelayQuery {
entry_location: constraint.wireguard_constraints.entry_location,
..Default::default()
},
OpenVpnRelayQuery {
bridge_settings: mullvad_relay_selector::query::BridgeQuery::Normal(
settings.bridge_settings.normal,
),
..Default::default()
},
)?;
use mullvad_types::Intersection;
let intersect_query = query
.intersection(current_location_query)
.context("Relay query incompatible with default settings")?;
Ok(intersect_query)
}
/// Get a query representing the current daemon settings
async fn get_query_from_current_settings(
mullvad_client: &mut MullvadProxyClient,
) -> anyhow::Result<RelayQuery> {
let settings = mullvad_client
.get_settings()
.await
.context("Failed to get settings")?;
RelayQuery::try_from(settings).context("Failed to convert settings to relay query")
}
pub async fn get_all_pickable_relays(
mullvad_client: &mut MullvadProxyClient,
) -> anyhow::Result<Vec<Relay>> {
let settings = mullvad_client.get_settings().await?;
let relay_list = mullvad_client.get_relay_locations().await?;
let relays = mullvad_relay_selector::filter_matching_relay_list(
&helpers::get_query_from_current_settings(mullvad_client).await?,
&relay_list,
&settings.custom_lists,
);
Ok(relays)
}
/// Selects a relay compatible with the given query and relay list from the client, and returns a
/// location constraint for only that relay, along with the relay itself.
///
/// # Note
/// This function does not handle bridges and multihop configurations (currently). There is no
/// particular reason for this other than it not being needed at the time, so feel free to extend
/// this function :).
async fn get_single_relay_location_contraint(
mullvad_client: &mut MullvadProxyClient,
query: RelayQuery,
) -> anyhow::Result<(Relay, RelayConstraints)> {
/// Convert the result of invoking the relay selector to a relay constraint.
fn convert_to_relay_constraints(
query: RelayQuery,
selected_relay: GetRelay,
) -> anyhow::Result<(Relay, RelayConstraints)> {
match selected_relay {
GetRelay::Wireguard {
inner: WireguardConfig::Singlehop { exit },
..
}
| GetRelay::OpenVpn { exit, .. } => {
let location = into_constraint(&exit);
let (mut relay_constraints, ..) = query.into_settings();
relay_constraints.location = location;
Ok((exit, relay_constraints))
}
unsupported => bail!("Can not constrain to a {unsupported:?}"),
}
}
let settings = mullvad_client.get_settings().await?;
let relay_list = mullvad_client.get_relay_locations().await?;
let relay_selector = get_daemon_relay_selector(&settings, relay_list);
let relay = relay_selector.get_relay_by_query(query.clone())?;
convert_to_relay_constraints(query, relay)
}
/// Get a mirror of the relay selector used by the daemon.
///
/// This can be used to query the relay selector without triggering a tunnel state change in the
/// daemon.
pub fn get_daemon_relay_selector(
settings: &mullvad_types::settings::Settings,
relay_list: mullvad_types::relay_list::RelayList,
) -> RelaySelector {
RelaySelector::from_list(SelectorConfig::from_settings(settings), relay_list)
}
/// Convenience function for constructing a constraint from a given [`Relay`].
///
/// # Panics
///
/// The relay must have a location set.
pub fn into_constraint(relay: &Relay) -> Constraint<LocationConstraint> {
let constraint = GeographicLocationConstraint::hostname(
relay.location.country_code.clone(),
relay.location.city_code.clone(),
&relay.hostname,
);
Constraint::Only(LocationConstraint::Location(constraint))
}
/// Ping monitoring made easy!
///
/// Continuously ping some destination while monitoring to detect diverging
/// packets.
///
/// To customize [`Pinger`] before the pinging and network monitoring starts,
/// see [`PingerBuilder`]. Call [`start`](Pinger::start) to start pinging, and
/// [`stop`](Pinger::stop) to get the leak test results.
#[allow(dead_code)]
pub struct Pinger {
// These values can be configured with [`PingerBuilder`].
destination: SocketAddr,
interval: tokio::time::Interval,
// Run-time specific values
pub guest_ip: IpAddr,
ping_task: AbortOnDrop<tokio::task::JoinHandle<()>>,
monitor: PacketMonitor,
}
impl Pinger {
/// Create a [`Pinger`] with a default configuration.
///
/// See [`PingerBuilder`] for details.
pub async fn start(rpc: &test_rpc::ServiceClient) -> Pinger {
let defaults = PingerBuilder::default();
Self::start_with(defaults, rpc).await
}
/// Create a [`Pinger`] using the configuration of `builder`.
///
/// See [`PingerBuilder`] for details on how to configure a [`Pinger`]
/// before starting it.
pub async fn start_with(builder: PingerBuilder, rpc: &test_rpc::ServiceClient) -> Pinger {
// Get the associated IP address of the test runner on the default, non-tunnel interface.
let guest_ip = obtain_guest_ip(rpc).await;
log::debug!("Guest IP: {guest_ip}");
// Start a network monitor
log::debug!("Monitoring outgoing traffic");
let monitor = start_packet_monitor(
move |packet| {
// NOTE: Many packets will likely be observed for API traffic. Rather than filtering
// all of those specifically, simply fail if our probes are
// observed.
packet.source.ip() == guest_ip
&& packet.destination.ip() == builder.destination.ip()
},
MonitorOptions::default(),
)
.await;
// Start pinging
//
// Create some network activity for the network monitor to sniff.
let ping_rpc = rpc.clone();
let mut interval = tokio::time::interval(builder.interval.period());
#[allow(clippy::async_yields_async)]
let ping_task = AbortOnDrop::new(tokio::spawn(async move {
loop {
send_guest_probes_without_monitor(ping_rpc.clone(), None, builder.destination)
.await;
interval.tick().await;
}
}));
Pinger {
destination: builder.destination,
interval: builder.interval,
guest_ip,
ping_task,
monitor,
}
}
/// Stop pinging and extract the result of the network monitor.
pub async fn stop(self) -> Result<network_monitor::MonitorResult, MonitorUnexpectedlyStopped> {
// Abort the inner probe sender, which is accomplished by dropping the
// join handle to the running task.
drop(self.ping_task);
self.monitor.into_result().await
}
/// Return the time period determining the cadence of pings that are sent.
pub fn period(&self) -> tokio::time::Duration {
self.interval.period()
}
}
/// Returns the [`IpAddr`] of the default non-tunnel interface.
async fn obtain_guest_ip(rpc: &ServiceClient) -> IpAddr {
let guest_iface = rpc
.get_default_interface()
.await
.expect("failed to obtain default interface");
rpc.get_interface_ip(guest_iface)
.await
.expect("failed to obtain non-tun IP")
}
/// Configure a [`Pinger`] before starting it.
pub struct PingerBuilder {
destination: SocketAddr,
interval: tokio::time::Interval,
}
#[allow(dead_code)]
impl PingerBuilder {
/// Create a default [`PingerBuilder`].
///
/// This is probably good enough for checking network traffic leaks when the
/// test-runner is supposed to be blocked from sending or receiving *any*
/// packets outside of localhost.
pub fn default() -> PingerBuilder {
PingerBuilder {
destination: "1.1.1.1:1337".parse().unwrap(),
interval: tokio::time::interval(Duration::from_secs(1)),
}
}
/// Set the target to ping.
pub fn destination(mut self, destination: SocketAddr) -> Self {
self.destination = destination;
self
}
/// How often a ping should be sent.
pub fn interval(mut self, period: Duration) -> Self {
self.interval = tokio::time::interval(period);
self
}
}
/// This helper spawns a separate process which checks if we are connected to Mullvad, and tries to
/// leak traffic outside the tunnel by sending TCP, UDP, and ICMP packets to [LEAK_DESTINATION].
pub struct ConnChecker {
rpc: ServiceClient,
mullvad_client: MullvadProxyClient,
leak_destination: SocketAddr,
/// Path to the process binary.
executable_path: String,
/// Whether the process should be split when spawned. Needed on Linux.
split: bool,
/// Some arbitrary payload
payload: Option<String>,
}
pub struct ConnCheckerHandle<'a> {
checker: &'a mut ConnChecker,
/// ID of the spawned process.
pid: u32,
}
pub struct ConnectionStatus {
/// True if <https://am.i.mullvad.net/> reported we are connected.
am_i_mullvad: anyhow::Result<bool>,
/// True if we sniffed TCP packets going outside the tunnel.
leaked_tcp: bool,
/// True if we sniffed UDP packets going outside the tunnel.
leaked_udp: bool,
/// True if we sniffed ICMP packets going outside the tunnel.
leaked_icmp: bool,
}
impl ConnChecker {
pub fn new(
rpc: ServiceClient,
mullvad_client: MullvadProxyClient,
leak_destination: impl Into<SocketAddr>,
) -> Self {
let artifacts_dir = &TEST_CONFIG.artifacts_dir;
let executable_path = match TEST_CONFIG.os {
Os::Linux | Os::Macos => format!("{artifacts_dir}/{CHECKER_FILENAME_UNIX}"),
Os::Windows => format!("{artifacts_dir}\\{CHECKER_FILENAME_WINDOWS}"),
};
Self {
rpc,
mullvad_client,
leak_destination: leak_destination.into(),
split: false,
executable_path,
payload: None,
}
}
/// Set a custom magic payload that the connection checker binary should use when leak-testing.
pub fn payload(&mut self, payload: impl Into<String>) {
self.payload = Some(payload.into())
}
/// Spawn the connection checker process and return a handle to it.
///
/// Dropping the handle will stop the process.
/// **NOTE**: The handle must be dropped from a tokio runtime context.
pub async fn spawn(&mut self) -> anyhow::Result<ConnCheckerHandle<'_>> {
log::debug!("spawning connection checker");
let opts = {
let ipvx = match self.leak_destination {
SocketAddr::V4(..) => "ipv4",
SocketAddr::V6(..) => "ipv6",
};
let mut args = [
"--interactive",
"--timeout",
&AM_I_MULLVAD_TIMEOUT_S.to_string(),
// try to leak traffic to LEAK_DESTINATION
"--leak",
&self.leak_destination.to_string(),
"--leak-timeout",
&LEAK_TIMEOUT_S.to_string(),
"--leak-tcp",
"--leak-udp",
"--leak-icmp",
"--url",
&format!("https://{ipvx}.am.i.{}/json", TEST_CONFIG.mullvad_host),
]
.map(String::from)
.to_vec();
if let Some(payload) = &self.payload {
args.push("--payload".to_string());
args.push(payload.clone());
};
SpawnOpts {
attach_stdin: true,
attach_stdout: true,
args,
..SpawnOpts::new(&self.executable_path)
}
};
let pid = self.rpc.spawn(opts).await?;
if self.split && TEST_CONFIG.os == Os::Linux {
self.mullvad_client
.add_split_tunnel_process(pid as i32)
.await?;
}
// TODO: The ST process monitor is a bit racy on macOS, such that processes aren't
// immediately recognized. This is a workaround until fixed.
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Ok(ConnCheckerHandle { pid, checker: self })
}
/// Enable split tunneling for the connection checker.
pub async fn split(&mut self) -> anyhow::Result<()> {
log::debug!("enable split tunnel");
self.split = true;
match TEST_CONFIG.os {
Os::Linux => { /* linux programs can't be split until they are spawned */ }
Os::Macos | Os::Windows => {
self.mullvad_client
.add_split_tunnel_app(&self.executable_path)
.await?;
self.mullvad_client.set_split_tunnel_state(true).await?;
}
}
Ok(())
}
/// Disable split tunneling for the connection checker.
pub async fn unsplit(&mut self) -> anyhow::Result<()> {
log::debug!("disable split tunnel");
self.split = false;
match TEST_CONFIG.os {
Os::Linux => {}
Os::Macos | Os::Windows => {
self.mullvad_client.set_split_tunnel_state(false).await?;
self.mullvad_client
.remove_split_tunnel_app(&self.executable_path)
.await?;
}
}
Ok(())
}
}
impl ConnCheckerHandle<'_> {
pub async fn split(&mut self) -> anyhow::Result<()> {
if TEST_CONFIG.os == Os::Linux {
self.checker
.mullvad_client
.add_split_tunnel_process(self.pid as i32)
.await?;
}
self.checker.split().await
}
pub async fn unsplit(&mut self) -> anyhow::Result<()> {
if TEST_CONFIG.os == Os::Linux {
self.checker
.mullvad_client
.remove_split_tunnel_process(self.pid as i32)
.await?;
}
self.checker.unsplit().await
}
/// Assert that traffic is blocked and that no packets are leaked.
pub async fn assert_blocked(&mut self) -> anyhow::Result<()> {
log::info!("checking that connection is blocked");
async {
let status = self.check_connection().await?;
ensure!(status.am_i_mullvad.is_err());
ensure!(!status.leaked_tcp);
ensure!(!status.leaked_udp);
ensure!(!status.leaked_icmp);
Ok(())
}
.await
.with_context(|| {
anyhow!(
"assert_secure failed (leak_destination={})",
self.checker.leak_destination,
)
})
}
/// Assert that traffic is flowing through the Mullvad tunnel and that no packets are leaked.
pub async fn assert_secure(&mut self) -> anyhow::Result<()> {
log::info!("checking that connection is secure");
async {
let status = self.check_connection().await?;
ensure!(status.am_i_mullvad?);
ensure!(!status.leaked_tcp);
ensure!(!status.leaked_udp);
ensure!(!status.leaked_icmp);
Ok(())
}
.await
.with_context(|| {
anyhow!(
"assert_secure failed (leak_destination={})",
self.checker.leak_destination,
)
})
}
/// Assert that traffic is NOT flowing through the Mullvad tunnel and that packets ARE leaked.
pub async fn assert_insecure(&mut self) -> anyhow::Result<()> {
log::info!("checking that connection is not secure");
async {
let status = self.check_connection().await?;
ensure!(!status.am_i_mullvad?);
ensure!(status.leaked_tcp);
ensure!(status.leaked_udp);
ensure!(status.leaked_icmp);
Ok(())
}
.await
.with_context(|| {
anyhow!(
"assert_secure failed (leak_destination={})",
self.checker.leak_destination,
)
})
}
pub async fn check_connection(&mut self) -> anyhow::Result<ConnectionStatus> {
// Monitor all packets going to LEAK_DESTINATION during the check.
let leak_destination = self.checker.leak_destination;
let monitor = start_packet_monitor(
move |packet| packet.destination.ip() == leak_destination.ip(),
MonitorOptions {
direction: Some(Direction::In),
..MonitorOptions::default()
},
)
.await;
// Write a newline to the connection checker to prompt it to perform the check.
self.checker
.rpc
.write_child_stdin(self.pid, "Say the line, Bart!\r\n".into())
.await?;
// The checker responds when the check is complete.
let line = self.read_stdout_line().await?;
let monitor_result = monitor
.into_result()
.await
.map_err(|_e| anyhow!("Packet monitor unexpectedly stopped"))?;
let leak_destination = self.checker.leak_destination;
Ok(ConnectionStatus {
am_i_mullvad: parse_am_i_mullvad(line),
leaked_tcp: (monitor_result.packets.iter())
.any(|pkt| pkt.protocol == IpNextHeaderProtocols::Tcp),
leaked_udp: (monitor_result.packets.iter())
.any(|pkt| pkt.protocol == IpNextHeaderProtocols::Udp),
leaked_icmp: (monitor_result.packets.iter()).any(|pkt| match leak_destination {
SocketAddr::V4(..) => pkt.protocol == IpNextHeaderProtocols::Icmp,
SocketAddr::V6(..) => pkt.protocol == IpNextHeaderProtocols::Icmpv6,
}),
})
}
/// Try to a single line of output from the spawned process
async fn read_stdout_line(&mut self) -> anyhow::Result<String> {
// Add a timeout to avoid waiting forever.
tokio::time::timeout(CONN_CHECKER_TIMEOUT, async {
let mut line = String::new();
// tarpc doesn't support streams, so we poll the checker process in a loop instead
loop {
let Some(output) = self.checker.rpc.read_child_stdout(self.pid).await? else {
bail!("got EOF from connection checker process");
};
if output.is_empty() {
sleep(Duration::from_millis(500)).await;
continue;
}
line.push_str(&output);
if line.contains('\n') {
log::info!("output from child process: {output:?}");
return Ok(line);
}
}
})
.await
.with_context(|| "Timeout reading stdout from connection checker")?
}
}
impl Drop for ConnCheckerHandle<'_> {
fn drop(&mut self) {
let rpc = self.checker.rpc.clone();
let pid = self.pid;
let Ok(runtime_handle) = tokio::runtime::Handle::try_current() else {
log::error!("ConnCheckerHandle dropped outside of a tokio runtime.");
return;
};
runtime_handle.spawn(async move {
// Make sure child process is stopped when this handle is dropped.
// Closing stdin does the trick.
let _ = rpc.close_child_stdin(pid).await;
});
}
}
/// Parse output from connection-checker. Returns true if connected to Mullvad.
fn parse_am_i_mullvad(result: String) -> anyhow::Result<bool> {
Ok(if result.contains("You are connected") {
true
} else if result.contains("You are not connected") {
false
} else {
bail!("Unexpected output from connection-checker: {result:?}")
})
}
/// Set the location to the given [`LocationConstraint`]. The same location constraint will be set
/// for the multihop entry and OpenVPN bridge location as well.
pub async fn set_location(
mullvad_client: &mut MullvadProxyClient,
location: impl Into<LocationConstraint>,
) -> anyhow::Result<()> {
let location_constraint: LocationConstraint = location.into();
let mut settings = mullvad_client
.get_settings()
.await
.map_err(|error| Error::Daemon(format!("Failed to set relay settings: {error}")))?;
settings.bridge_settings.normal.location = Constraint::Only(location_constraint.clone());
mullvad_client
.set_bridge_settings(settings.bridge_settings)
.await?;
let RelaySettings::Normal(mut constraint) = settings.relay_settings else {
unimplemented!("Setting location for a custom endpoint is not supported");
};
constraint.location = Constraint::Only(location_constraint.clone());
constraint.wireguard_constraints.entry_location = Constraint::Only(location_constraint);
mullvad_client
.set_relay_settings(RelaySettings::Normal(constraint))
.await?;
Ok(())
}
/// Dig out a custom list from the daemon settings based on the custom list's name.
/// There should be an rpc for this.
pub async fn find_custom_list(
rpc: &mut MullvadProxyClient,
name: &str,
) -> anyhow::Result<CustomList> {
rpc.get_settings()
.await?
.custom_lists
.into_iter()
.find(|list| list.name == name)
.ok_or(anyhow!("List '{name}' not found"))
}
|