-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathclient.rs
720 lines (639 loc) · 21.7 KB
/
client.rs
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
//! Client that returns and takes mullvad types as arguments instead of prost-generated types
use crate::types;
use futures::{Stream, StreamExt};
use mullvad_types::{
access_method::{self, AccessMethod, AccessMethodSetting},
account::{AccountData, AccountToken, VoucherSubmission},
custom_list::{CustomList, Id},
device::{Device, DeviceEvent, DeviceId, DeviceState, RemoveDeviceEvent},
relay_constraints::{
BridgeSettings, BridgeState, ObfuscationSettings, RelayOverride, RelaySettings,
},
relay_list::RelayList,
settings::{DnsOptions, Settings},
states::TunnelState,
version::AppVersionInfo,
wireguard::{PublicKey, QuantumResistantState, RotationInterval},
};
use std::path::Path;
use std::str::FromStr;
#[cfg(target_os = "windows")]
use talpid_types::split_tunnel::ExcludedProcess;
use tonic::{Code, Status};
type Error = super::Error;
pub type Result<T> = std::result::Result<T, super::Error>;
#[derive(Debug, Clone)]
pub struct MullvadProxyClient(crate::ManagementServiceClient);
pub enum DaemonEvent {
TunnelState(TunnelState),
Settings(Settings),
RelayList(RelayList),
AppVersionInfo(AppVersionInfo),
Device(DeviceEvent),
RemoveDevice(RemoveDeviceEvent),
NewAccessMethod(AccessMethodSetting),
}
impl TryFrom<types::daemon_event::Event> for DaemonEvent {
type Error = Error;
fn try_from(value: types::daemon_event::Event) -> Result<Self> {
match value {
types::daemon_event::Event::TunnelState(state) => TunnelState::try_from(state)
.map(DaemonEvent::TunnelState)
.map_err(Error::InvalidResponse),
types::daemon_event::Event::Settings(settings) => Settings::try_from(settings)
.map(DaemonEvent::Settings)
.map_err(Error::InvalidResponse),
types::daemon_event::Event::RelayList(list) => RelayList::try_from(list)
.map(DaemonEvent::RelayList)
.map_err(Error::InvalidResponse),
types::daemon_event::Event::VersionInfo(info) => {
Ok(DaemonEvent::AppVersionInfo(AppVersionInfo::from(info)))
}
types::daemon_event::Event::Device(event) => DeviceEvent::try_from(event)
.map(DaemonEvent::Device)
.map_err(Error::InvalidResponse),
types::daemon_event::Event::RemoveDevice(event) => RemoveDeviceEvent::try_from(event)
.map(DaemonEvent::RemoveDevice)
.map_err(Error::InvalidResponse),
types::daemon_event::Event::NewAccessMethod(event) => {
AccessMethodSetting::try_from(event)
.map(DaemonEvent::NewAccessMethod)
.map_err(Error::InvalidResponse)
}
}
}
}
impl MullvadProxyClient {
pub async fn new() -> Result<Self> {
#[allow(deprecated)]
super::new_rpc_client().await.map(Self)
}
pub fn from_rpc_client(client: crate::ManagementServiceClient) -> Self {
Self(client)
}
pub async fn connect_tunnel(&mut self) -> Result<bool> {
Ok(self
.0
.connect_tunnel(())
.await
.map_err(Error::Rpc)?
.into_inner())
}
pub async fn disconnect_tunnel(&mut self) -> Result<bool> {
Ok(self
.0
.disconnect_tunnel(())
.await
.map_err(Error::Rpc)?
.into_inner())
}
pub async fn reconnect_tunnel(&mut self) -> Result<bool> {
Ok(self
.0
.reconnect_tunnel(())
.await
.map_err(Error::Rpc)?
.into_inner())
}
pub async fn get_tunnel_state(&mut self) -> Result<TunnelState> {
let state = self
.0
.get_tunnel_state(())
.await
.map_err(Error::Rpc)?
.into_inner();
TunnelState::try_from(state).map_err(Error::InvalidResponse)
}
pub async fn events_listen(&mut self) -> Result<impl Stream<Item = Result<DaemonEvent>>> {
let listener = self
.0
.events_listen(())
.await
.map_err(Error::Rpc)?
.into_inner();
Ok(listener.map(|item| {
let event = item
.map_err(Error::Rpc)?
.event
.ok_or(Error::MissingDaemonEvent)?;
DaemonEvent::try_from(event)
}))
}
pub async fn prepare_restart(&mut self) -> Result<()> {
self.0.prepare_restart(()).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn factory_reset(&mut self) -> Result<()> {
self.0.factory_reset(()).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn get_current_version(&mut self) -> Result<String> {
Ok(self
.0
.get_current_version(())
.await
.map_err(Error::Rpc)?
.into_inner())
}
pub async fn get_version_info(&mut self) -> Result<AppVersionInfo> {
let version_info = self
.0
.get_version_info(())
.await
.map_err(Error::Rpc)?
.into_inner();
Ok(AppVersionInfo::from(version_info))
}
pub async fn get_relay_locations(&mut self) -> Result<RelayList> {
let list = self
.0
.get_relay_locations(())
.await
.map_err(Error::Rpc)?
.into_inner();
mullvad_types::relay_list::RelayList::try_from(list).map_err(Error::InvalidResponse)
}
pub async fn get_api_access_methods(&mut self) -> Result<Vec<AccessMethodSetting>> {
let access_method_settings = self
.0
.get_settings(())
.await
.map_err(Error::Rpc)?
.into_inner()
.api_access_methods
.ok_or(Error::ApiAccessMethodSettingsNotFound)
.and_then(|access_method_settings| {
access_method::Settings::try_from(access_method_settings)
.map_err(Error::InvalidResponse)
})?;
Ok(access_method_settings.iter().cloned().collect())
}
pub async fn get_api_access_method(
&mut self,
id: &access_method::Id,
) -> Result<AccessMethodSetting> {
self.get_api_access_methods()
.await?
.into_iter()
.find(|api_access_method| api_access_method.get_id() == *id)
.ok_or(Error::ApiAccessMethodNotFound)
}
pub async fn get_current_api_access_method(&mut self) -> Result<AccessMethodSetting> {
self.0
.get_current_api_access_method(())
.await
.map_err(Error::Rpc)
.map(tonic::Response::into_inner)
.and_then(|access_method| {
AccessMethodSetting::try_from(access_method).map_err(Error::InvalidResponse)
})
}
pub async fn test_api_access_method(&mut self, id: access_method::Id) -> Result<bool> {
let result = self
.0
.test_api_access_method_by_id(types::Uuid::from(id))
.await
.map_err(Error::Rpc)?;
Ok(result.into_inner())
}
pub async fn test_custom_api_access_method(
&mut self,
config: talpid_types::net::proxy::CustomProxy,
) -> Result<bool> {
let result = self
.0
.test_custom_api_access_method(types::CustomProxy::from(config))
.await
.map_err(Error::Rpc)?;
Ok(result.into_inner())
}
pub async fn update_relay_locations(&mut self) -> Result<()> {
self.0
.update_relay_locations(())
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_relay_settings(&mut self, update: RelaySettings) -> Result<()> {
let update = types::RelaySettings::from(update);
self.0
.set_relay_settings(update)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_bridge_settings(&mut self, settings: BridgeSettings) -> Result<()> {
let settings = types::BridgeSettings::from(settings);
self.0
.set_bridge_settings(settings)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_bridge_state(&mut self, state: BridgeState) -> Result<()> {
let state = types::BridgeState::from(state);
self.0.set_bridge_state(state).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_obfuscation_settings(&mut self, settings: ObfuscationSettings) -> Result<()> {
let settings = types::ObfuscationSettings::from(&settings);
self.0
.set_obfuscation_settings(settings)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn get_settings(&mut self) -> Result<Settings> {
let settings = self
.0
.get_settings(())
.await
.map_err(Error::Rpc)?
.into_inner();
Settings::try_from(settings).map_err(Error::InvalidResponse)
}
pub async fn set_allow_lan(&mut self, state: bool) -> Result<()> {
self.0.set_allow_lan(state).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_show_beta_releases(&mut self, state: bool) -> Result<()> {
self.0
.set_show_beta_releases(state)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_block_when_disconnected(&mut self, state: bool) -> Result<()> {
self.0
.set_block_when_disconnected(state)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_auto_connect(&mut self, state: bool) -> Result<()> {
self.0.set_auto_connect(state).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_openvpn_mssfix(&mut self, mssfix: Option<u16>) -> Result<()> {
self.0
.set_openvpn_mssfix(mssfix.map(u32::from).unwrap_or(0))
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_wireguard_mtu(&mut self, mtu: Option<u16>) -> Result<()> {
self.0
.set_wireguard_mtu(mtu.map(u32::from).unwrap_or(0))
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_enable_ipv6(&mut self, state: bool) -> Result<()> {
self.0.set_enable_ipv6(state).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_quantum_resistant_tunnel(
&mut self,
state: QuantumResistantState,
) -> Result<()> {
let state = types::QuantumResistantState::from(state);
self.0
.set_quantum_resistant_tunnel(state)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_dns_options(&mut self, options: DnsOptions) -> Result<()> {
let options = types::DnsOptions::from(&options);
self.0.set_dns_options(options).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_relay_override(&mut self, relay_override: RelayOverride) -> Result<()> {
let r#override = types::RelayOverride::from(relay_override);
self.0
.set_relay_override(r#override)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn clear_all_relay_overrides(&mut self) -> Result<()> {
self.0
.clear_all_relay_overrides(())
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn create_new_account(&mut self) -> Result<AccountToken> {
Ok(self
.0
.create_new_account(())
.await
.map_err(map_device_error)?
.into_inner())
}
pub async fn login_account(&mut self, account: AccountToken) -> Result<()> {
self.0
.login_account(account)
.await
.map_err(map_device_error)?;
Ok(())
}
pub async fn logout_account(&mut self) -> Result<()> {
self.0.logout_account(()).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn get_account_data(&mut self, account: AccountToken) -> Result<AccountData> {
let data = self
.0
.get_account_data(account)
.await
.map_err(Error::Rpc)?
.into_inner();
AccountData::try_from(data).map_err(Error::InvalidResponse)
}
pub async fn get_account_history(&mut self) -> Result<Option<AccountToken>> {
let history = self
.0
.get_account_history(())
.await
.map_err(Error::Rpc)?
.into_inner();
Ok(history.token)
}
pub async fn clear_account_history(&mut self) -> Result<()> {
self.0.clear_account_history(()).await.map_err(Error::Rpc)?;
Ok(())
}
// get_www_auth_token
pub async fn submit_voucher(&mut self, voucher: String) -> Result<VoucherSubmission> {
let result = self
.0
.submit_voucher(voucher)
.await
.map_err(|error| match error.code() {
Code::NotFound => Error::InvalidVoucher,
Code::ResourceExhausted => Error::UsedVoucher,
_other => Error::Rpc(error),
})?
.into_inner();
VoucherSubmission::try_from(result).map_err(Error::InvalidResponse)
}
pub async fn get_device(&mut self) -> Result<DeviceState> {
let state = self
.0
.get_device(())
.await
.map_err(map_device_error)?
.into_inner();
DeviceState::try_from(state).map_err(Error::InvalidResponse)
}
pub async fn update_device(&mut self) -> Result<()> {
self.0.update_device(()).await.map_err(map_device_error)?;
Ok(())
}
pub async fn list_devices(&mut self, account: AccountToken) -> Result<Vec<Device>> {
let list = self
.0
.list_devices(account)
.await
.map_err(map_device_error)?
.into_inner();
list.devices
.into_iter()
.map(|d| Device::try_from(d).map_err(Error::InvalidResponse))
.collect::<Result<_>>()
}
pub async fn remove_device(
&mut self,
account: AccountToken,
device_id: DeviceId,
) -> Result<()> {
self.0
.remove_device(types::DeviceRemoval {
account_token: account,
device_id,
})
.await
.map_err(map_device_error)?;
Ok(())
}
pub async fn set_wireguard_rotation_interval(
&mut self,
interval: RotationInterval,
) -> Result<()> {
let duration = types::Duration::try_from(*interval.as_duration())
.map_err(|_| Error::DurationTooLarge)?;
self.0
.set_wireguard_rotation_interval(duration)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn reset_wireguard_rotation_interval(&mut self) -> Result<()> {
self.0
.reset_wireguard_rotation_interval(())
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn rotate_wireguard_key(&mut self) -> Result<()> {
self.0.rotate_wireguard_key(()).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn get_wireguard_key(&mut self) -> Result<PublicKey> {
let key = self
.0
.get_wireguard_key(())
.await
.map_err(Error::Rpc)?
.into_inner();
PublicKey::try_from(key).map_err(Error::InvalidResponse)
}
pub async fn create_custom_list(&mut self, name: String) -> Result<Id> {
let id = self
.0
.create_custom_list(name)
.await
.map_err(map_custom_list_error)?
.into_inner();
Id::from_str(&id).map_err(|_| Error::CustomListListNotFound)
}
pub async fn delete_custom_list(&mut self, id: String) -> Result<()> {
self.0
.delete_custom_list(id)
.await
.map_err(map_custom_list_error)?;
Ok(())
}
pub async fn update_custom_list(&mut self, custom_list: CustomList) -> Result<()> {
self.0
.update_custom_list(types::CustomList::from(custom_list))
.await
.map_err(map_custom_list_error)?;
Ok(())
}
pub async fn add_access_method(
&mut self,
name: String,
enabled: bool,
access_method: AccessMethod,
) -> Result<()> {
let request = types::NewAccessMethodSetting {
name,
enabled,
access_method: Some(types::AccessMethod::from(access_method)),
};
self.0
.add_api_access_method(request)
.await
.map_err(Error::Rpc)
.map(drop)
}
pub async fn remove_access_method(
&mut self,
api_access_method: access_method::Id,
) -> Result<()> {
self.0
.remove_api_access_method(types::Uuid::from(api_access_method))
.await
.map_err(Error::Rpc)
.map(drop)
}
pub async fn update_access_method(
&mut self,
access_method_update: AccessMethodSetting,
) -> Result<()> {
self.0
.update_api_access_method(types::AccessMethodSetting::from(access_method_update))
.await
.map_err(Error::Rpc)
.map(drop)
}
/// Set the [`AccessMethod`] which [`ApiConnectionModeProvider`] should
/// pick.
///
/// - `access_method`: If `Some(access_method)`, [`ApiConnectionModeProvider`] will skip ahead
/// and return `access_method` when asked for a new access method. If `None`,
/// [`ApiConnectionModeProvider`] will pick the next access method "randomly"
///
/// [`ApiConnectionModeProvider`]: mullvad_daemon::api::ApiConnectionModeProvider
pub async fn set_access_method(&mut self, api_access_method: access_method::Id) -> Result<()> {
self.0
.set_api_access_method(types::Uuid::from(api_access_method))
.await
.map_err(Error::Rpc)
.map(drop)
}
pub async fn get_split_tunnel_processes(&mut self) -> Result<Vec<i32>> {
use futures::TryStreamExt;
let procs = self
.0
.get_split_tunnel_processes(())
.await
.map_err(Error::Rpc)?
.into_inner();
procs.try_collect().await.map_err(Error::Rpc)
}
pub async fn add_split_tunnel_process(&mut self, pid: i32) -> Result<()> {
self.0
.add_split_tunnel_process(pid)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn remove_split_tunnel_process(&mut self, pid: i32) -> Result<()> {
self.0
.remove_split_tunnel_process(pid)
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn clear_split_tunnel_processes(&mut self) -> Result<()> {
self.0
.clear_split_tunnel_processes(())
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn add_split_tunnel_app<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let path = path.as_ref().to_str().ok_or(Error::PathMustBeUtf8)?;
self.0
.add_split_tunnel_app(path.to_owned())
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn remove_split_tunnel_app<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let path = path.as_ref().to_str().ok_or(Error::PathMustBeUtf8)?;
self.0
.remove_split_tunnel_app(path.to_owned())
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn clear_split_tunnel_apps(&mut self) -> Result<()> {
self.0
.clear_split_tunnel_apps(())
.await
.map_err(Error::Rpc)?;
Ok(())
}
pub async fn set_split_tunnel_state(&mut self, state: bool) -> Result<()> {
self.0
.set_split_tunnel_state(state)
.await
.map_err(Error::Rpc)?;
Ok(())
}
#[cfg(target_os = "windows")]
pub async fn get_excluded_processes(&mut self) -> Result<Vec<ExcludedProcess>> {
let procs = self
.0
.get_excluded_processes(())
.await
.map_err(Error::Rpc)?
.into_inner();
Ok(procs
.processes
.into_iter()
.map(ExcludedProcess::from)
.collect::<Vec<_>>())
}
// check_volumes
pub async fn apply_json_settings(&mut self, blob: String) -> Result<()> {
self.0.apply_json_settings(blob).await.map_err(Error::Rpc)?;
Ok(())
}
pub async fn export_json_settings(&mut self) -> Result<String> {
let blob = self.0.export_json_settings(()).await.map_err(Error::Rpc)?;
Ok(blob.into_inner())
}
}
fn map_device_error(status: Status) -> Error {
match status.code() {
Code::ResourceExhausted => Error::TooManyDevices,
Code::Unauthenticated => Error::InvalidAccount,
Code::AlreadyExists => Error::AlreadyLoggedIn,
Code::NotFound => Error::DeviceNotFound,
_other => Error::Rpc(status),
}
}
fn map_custom_list_error(status: Status) -> Error {
match status.code() {
Code::NotFound => {
if status.details() == crate::CUSTOM_LIST_LIST_NOT_FOUND_DETAILS {
Error::CustomListListNotFound
} else {
Error::Rpc(status)
}
}
Code::AlreadyExists => {
if status.details() == crate::CUSTOM_LIST_LIST_EXISTS_DETAILS {
Error::CustomListExists
} else {
Error::Rpc(status)
}
}
_other => Error::Rpc(status),
}
}