-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathdaemon_interface.rs
70 lines (58 loc) · 2.1 KB
/
daemon_interface.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
use futures::{channel::oneshot, executor::block_on};
use mullvad_daemon::{device, DaemonCommand, DaemonCommandSender};
use mullvad_types::{
account::{AccountData, AccountToken, VoucherSubmission},
custom_list::CustomList,
device::{Device, DeviceState},
relay_constraints::{ObfuscationSettings, RelaySettings},
relay_list::RelayList,
settings::{DnsOptions, Settings},
states::{TargetState, TunnelState},
version::AppVersionInfo,
wireguard,
wireguard::QuantumResistantState,
};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Can't send command to daemon because it is not running")]
NoDaemon(#[source] mullvad_daemon::Error),
#[error("No response received from daemon")]
NoResponse,
#[error("Attempt to use daemon command sender before it was configured")]
NoSender,
#[error("Error performing RPC with the remote API")]
Api(#[source] mullvad_api::rest::Error),
#[error("Failed to update settings")]
UpdateSettings,
#[error("Daemon returned an error")]
Other(#[source] mullvad_daemon::Error),
}
impl From<mullvad_daemon::Error> for Error {
fn from(error: mullvad_daemon::Error) -> Error {
match error {
mullvad_daemon::Error::RestError(error) => Error::Api(error),
mullvad_daemon::Error::LoginError(device::Error::OtherRestError(error)) => {
Error::Api(error)
}
mullvad_daemon::Error::ListDevicesError(device::Error::OtherRestError(error)) => {
Error::Api(error)
}
error => Error::Other(error),
}
}
}
type Result<T> = std::result::Result<T, Error>;
pub struct DaemonInterface {
command_sender: DaemonCommandSender,
}
impl DaemonInterface {
pub fn new(command_sender: DaemonCommandSender) -> Self {
DaemonInterface { command_sender }
}
pub fn shutdown(&self) -> Result<()> {
self.command_sender.shutdown().map_err(Error::NoDaemon)
}
fn send_command(&self, command: DaemonCommand) -> Result<()> {
self.command_sender.send(command).map_err(Error::NoDaemon)
}
}