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
|
use futures::{channel::oneshot, executor::block_on};
use mullvad_daemon::{DaemonCommand, DaemonCommandSender};
use mullvad_types::{
account::{AccountData, AccountToken, VoucherSubmission},
location::GeoIpLocation,
relay_constraints::RelaySettingsUpdate,
relay_list::RelayList,
settings::{DnsOptions, Settings},
states::{TargetState, TunnelState},
version::AppVersionInfo,
wireguard::{self, KeygenEvent},
};
#[derive(Debug, err_derive::Error)]
#[error(no_from)]
pub enum Error {
#[error(display = "Can't send command to daemon because it is not running")]
NoDaemon(#[error(source)] mullvad_daemon::Error),
#[error(display = "No response received from daemon")]
NoResponse,
#[error(display = "Attempt to use daemon command sender before it was configured")]
NoSender,
#[error(display = "Error performing RPC with the remote API")]
RpcError(#[error(source)] mullvad_rpc::rest::Error),
#[error(display = "Failed to update settings")]
SettingsError,
#[error(display = "Daemon returned an error")]
OtherError(#[error(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::RpcError(error),
error => Error::OtherError(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 connect(&self) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SetTargetState(tx, TargetState::Secured))?;
block_on(rx).map(|_| ()).map_err(|_| Error::NoResponse)
}
pub fn create_new_account(&self) -> Result<String> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::CreateNewAccount(tx))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::from)
}
pub fn disconnect(&self) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SetTargetState(tx, TargetState::Unsecured))?;
block_on(rx).map(|_| ()).map_err(|_| Error::NoResponse)
}
pub fn generate_wireguard_key(&self) -> Result<KeygenEvent> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GenerateWireguardKey(tx))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::from)
}
pub fn get_account_data(&self, account_token: String) -> Result<AccountData> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetAccountData(tx, account_token))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::RpcError)
}
pub fn get_account_history(&self) -> Result<Option<AccountToken>> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetAccountHistory(tx))?;
block_on(rx).map_err(|_| Error::NoResponse)
}
pub fn get_www_auth_token(&self) -> Result<String> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetWwwAuthToken(tx))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::from)
}
pub fn get_current_location(&self) -> Result<Option<GeoIpLocation>> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetCurrentLocation(tx))?;
Ok(block_on(rx).map_err(|_| Error::NoResponse)?)
}
pub fn get_current_version(&self) -> Result<String> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetCurrentVersion(tx))?;
Ok(block_on(rx).map_err(|_| Error::NoResponse)?)
}
pub fn get_relay_locations(&self) -> Result<RelayList> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetRelayLocations(tx))?;
Ok(block_on(rx).map_err(|_| Error::NoResponse)?)
}
pub fn get_settings(&self) -> Result<Settings> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetSettings(tx))?;
Ok(block_on(rx).map_err(|_| Error::NoResponse)?)
}
pub fn get_state(&self) -> Result<TunnelState> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetState(tx))?;
Ok(block_on(rx).map_err(|_| Error::NoResponse)?)
}
pub fn get_version_info(&self) -> Result<AppVersionInfo> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetVersionInfo(tx))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.ok_or(Error::NoResponse)
}
pub fn reconnect(&self) -> Result<()> {
let (tx, _) = oneshot::channel();
self.send_command(DaemonCommand::Reconnect(tx))?;
Ok(())
}
pub fn clear_account_history(&self) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::ClearAccountHistory(tx))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::from)
}
pub fn get_wireguard_key(&self) -> Result<Option<wireguard::PublicKey>> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::GetWireguardKey(tx))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::from)
}
pub fn verify_wireguard_key(&self) -> Result<bool> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::VerifyWireguardKey(tx))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::from)
}
pub fn set_account(&self, account_token: Option<String>) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SetAccount(tx, account_token))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(|_| Error::SettingsError)
}
pub fn set_allow_lan(&self, allow_lan: bool) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SetAllowLan(tx, allow_lan))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(|_| Error::SettingsError)
}
pub fn set_auto_connect(&self, auto_connect: bool) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SetAutoConnect(tx, auto_connect))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(|_| Error::SettingsError)
}
pub fn set_dns_options(&self, dns_options: DnsOptions) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SetDnsOptions(tx, dns_options))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(|_| Error::SettingsError)
}
pub fn set_wireguard_mtu(&self, wireguard_mtu: Option<u16>) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SetWireguardMtu(tx, wireguard_mtu))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(|_| Error::SettingsError)
}
pub fn shutdown(&self) -> Result<()> {
self.send_command(DaemonCommand::Shutdown)
}
pub fn submit_voucher(&self, voucher: String) -> Result<VoucherSubmission> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::SubmitVoucher(tx, voucher))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(Error::from)
}
pub fn update_relay_settings(&self, update: RelaySettingsUpdate) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.send_command(DaemonCommand::UpdateRelaySettings(tx, update))?;
block_on(rx)
.map_err(|_| Error::NoResponse)?
.map_err(|_| Error::SettingsError)
}
fn send_command(&self, command: DaemonCommand) -> Result<()> {
self.command_sender.send(command).map_err(Error::NoDaemon)
}
}
|