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
|
// @flow
import { ipcRenderer } from 'electron';
import { bindActionCreators } from 'redux';
import { push as pushHistory } from 'react-router-redux';
import {
RemoteError as JsonRpcTransportRemoteError,
TimeOutError as JsonRpcTransportTimeOutError,
} from './jsonrpc-transport';
import accountActions from '../redux/account/actions';
import connectionActions from '../redux/connection/actions';
import settingsActions from '../redux/settings/actions';
import { log } from '../lib/platform';
import type { RpcCredentials as OriginalRpcCredentials } from './rpc-address-file';
import type {
DaemonRpcProtocol,
ConnectionObserver as DaemonConnectionObserver,
} from './daemon-rpc';
import type { ReduxStore } from '../redux/store';
import type { AccountToken, BackendState, RelaySettingsUpdate } from './daemon-rpc';
import type { ConnectionState } from '../redux/connection/reducers';
export class NoCreditError extends Error {
constructor() {
super("Account doesn't have enough credit available for connection");
}
get userFriendlyTitle(): string {
return 'Out of time';
}
get userFriendlyMessage(): string {
return 'Buy more time, so you can continue using the internet securely';
}
}
export class NoInternetError extends Error {
constructor() {
super('Internet connectivity is currently unavailable');
}
get userFriendlyTitle(): string {
return 'Offline';
}
get userFriendlyMessage(): string {
return 'Your internet connection will be secured when you get back online';
}
}
export class NoDaemonError extends Error {
constructor() {
super('Could not connect to Mullvad daemon');
}
}
export class InvalidAccountError extends Error {
constructor() {
super('Invalid account number');
}
}
export class NoAccountError extends Error {
constructor() {
super('No account was set');
}
}
export class CommunicationError extends Error {
constructor() {
super('api.mullvad.net is blocked, please check your firewall');
}
}
export class UnknownError extends Error {
constructor(cause: string) {
super(`An unknown error occurred, ${cause}`);
}
get userFriendlyTitle(): string {
return 'Something went wrong';
}
}
export class CredentialsRequestError extends Error {
_reason: Error;
constructor(reason: Error) {
super('Failed to request the RPC credentials');
this._reason = reason;
}
get reason(): Error {
return this._reason;
}
}
export type RpcCredentials = OriginalRpcCredentials;
export interface RpcCredentialsProvider {
request(): Promise<RpcCredentials>;
}
/**
* Backend implementation
*/
export class Backend {
_daemonRpc: DaemonRpcProtocol;
_credentialsProvider: RpcCredentialsProvider;
_reconnectBackoff = new ReconnectionBackoff();
_credentials: ?RpcCredentials;
_openConnectionObserver: ?DaemonConnectionObserver;
_closeConnectionObserver: ?DaemonConnectionObserver;
_reduxActions: *;
constructor(
store: ReduxStore,
rpc: DaemonRpcProtocol,
credentialsProvider: RpcCredentialsProvider,
) {
this._daemonRpc = rpc;
this._credentialsProvider = credentialsProvider;
this._reduxActions = {
account: bindActionCreators(accountActions, store.dispatch),
connection: bindActionCreators(connectionActions, store.dispatch),
settings: bindActionCreators(settingsActions, store.dispatch),
history: bindActionCreators({ push: pushHistory }, store.dispatch),
};
this._openConnectionObserver = rpc.addOpenConnectionObserver(() => {
this._onOpenConnection();
});
this._closeConnectionObserver = rpc.addCloseConnectionObserver((error) => {
this._onCloseConnection(error);
});
this._setupReachability();
}
dispose() {
const openConnectionObserver = this._openConnectionObserver;
const closeConnectionObserver = this._closeConnectionObserver;
if (openConnectionObserver) {
openConnectionObserver.unsubscribe();
this._openConnectionObserver = null;
}
if (closeConnectionObserver) {
closeConnectionObserver.unsubscribe();
this._closeConnectionObserver = null;
}
}
connect() {
this._connectToDaemon();
}
disconnect() {
this._daemonRpc.disconnect();
}
async login(accountToken: AccountToken) {
const actions = this._reduxActions;
actions.account.startLogin(accountToken);
log.debug('Attempting to login');
try {
const accountData = await this._daemonRpc.getAccountData(accountToken);
await this._daemonRpc.setAccount(accountToken);
actions.account.loginSuccessful(accountData.expiry);
// Redirect the user after some time to allow for
// the 'Login Successful' screen to be visible
setTimeout(() => {
actions.history.push('/connect');
log.debug('Autoconnecting...');
this.connectTunnel();
}, 1000);
} catch (error) {
log.error('Failed to log in,', error.message);
actions.account.loginFailed(this._rpcErrorToBackendError(error));
}
}
async autologin() {
const actions = this._reduxActions;
actions.account.startLogin();
log.debug('Attempting to log in automatically');
try {
const accountToken = await this._daemonRpc.getAccount();
if (!accountToken) {
throw new NoAccountError();
}
log.debug(`The backend had an account number stored: ${accountToken}`);
actions.account.startLogin(accountToken);
const accountData = await this._daemonRpc.getAccountData(accountToken);
log.debug('The stored account number still exists:', accountData);
actions.account.loginSuccessful(accountData.expiry);
actions.history.push('/connect');
} catch (e) {
log.warn('Unable to autologin,', e.message);
actions.account.autoLoginFailed();
actions.history.push('/');
throw e;
}
}
async logout() {
const actions = this._reduxActions;
try {
await Promise.all([
this.disconnectTunnel(),
this._daemonRpc.setAccount(null),
this.fetchAccountHistory(),
]);
actions.account.loggedOut();
actions.history.push('/');
} catch (e) {
log.info('Failed to logout: ', e.message);
}
}
async connectTunnel() {
const actions = this._reduxActions;
try {
const currentState = await this._daemonRpc.getState();
if (currentState.state === 'secured') {
log.debug('Refusing to connect as connection is already secured');
actions.connection.connected();
} else {
actions.connection.connecting();
await this._daemonRpc.connectTunnel();
}
} catch (error) {
actions.connection.disconnected();
throw error;
}
}
disconnectTunnel() {
return this._daemonRpc.disconnectTunnel();
}
updateRelaySettings(relaySettings: RelaySettingsUpdate) {
return this._daemonRpc.updateRelaySettings(relaySettings);
}
async fetchRelaySettings() {
const actions = this._reduxActions;
const relaySettings = await this._daemonRpc.getRelaySettings();
log.debug('Got relay settings from backend', JSON.stringify(relaySettings));
if (relaySettings.normal) {
const payload = {};
const normal = relaySettings.normal;
const tunnel = normal.tunnel;
const location = normal.location;
if (location === 'any') {
payload.location = 'any';
} else {
payload.location = location.only;
}
if (tunnel === 'any') {
payload.port = 'any';
payload.protocol = 'any';
} else {
const { port, protocol } = tunnel.only.openvpn;
payload.port = port === 'any' ? port : port.only;
payload.protocol = protocol === 'any' ? protocol : protocol.only;
}
actions.settings.updateRelay({
normal: payload,
});
} else if (relaySettings.custom_tunnel_endpoint) {
const custom_tunnel_endpoint = relaySettings.custom_tunnel_endpoint;
const {
host,
tunnel: {
openvpn: { port, protocol },
},
} = custom_tunnel_endpoint;
actions.settings.updateRelay({
custom_tunnel_endpoint: {
host,
port,
protocol,
},
});
}
}
async updateAccountExpiry() {
const actions = this._reduxActions;
try {
const accountToken = await this._daemonRpc.getAccount();
if (!accountToken) {
throw new NoAccountError();
}
const accountData = await this._daemonRpc.getAccountData(accountToken);
actions.account.updateAccountExpiry(accountData.expiry);
} catch (e) {
log.error(`Failed to update account expiry: ${e.message}`);
}
}
async removeAccountFromHistory(accountToken: AccountToken): Promise<void> {
await this._daemonRpc.removeAccountFromHistory(accountToken);
await this.fetchAccountHistory();
}
async fetchAccountHistory(): Promise<void> {
const actions = this._reduxActions;
const accountHistory = await this._daemonRpc.getAccountHistory();
actions.account.updateAccountHistory(accountHistory);
}
async fetchRelayLocations() {
const actions = this._reduxActions;
const locations = await this._daemonRpc.getRelayLocations();
log.info('Got relay locations');
const storedLocations = locations.countries.map((country) => ({
name: country.name,
code: country.code,
hasActiveRelays: country.cities.some((city) => city.has_active_relays),
cities: country.cities.map((city) => ({
name: city.name,
code: city.code,
latitude: city.latitude,
longitude: city.longitude,
hasActiveRelays: city.has_active_relays,
})),
}));
actions.settings.updateRelayLocations(storedLocations);
}
async fetchLocation() {
const actions = this._reduxActions;
const location = await this._daemonRpc.getLocation();
log.info('Got location from daemon');
const locationUpdate = {
ip: location.ip,
country: location.country,
city: location.city,
latitude: location.latitude,
longitude: location.longitude,
mullvadExitIp: location.mullvad_exit_ip,
};
actions.connection.newLocation(locationUpdate);
}
async setAllowLan(allowLan: boolean) {
const actions = this._reduxActions;
await this._daemonRpc.setAllowLan(allowLan);
actions.settings.updateAllowLan(allowLan);
}
async fetchAllowLan() {
const actions = this._reduxActions;
const allowLan = await this._daemonRpc.getAllowLan();
actions.settings.updateAllowLan(allowLan);
}
async fetchSecurityState() {
const securityState = await this._daemonRpc.getState();
const connectionState = this._securityStateToConnectionState(securityState);
this._updateConnectionState(connectionState);
}
async _requestCredentials(): Promise<RpcCredentials> {
try {
return await this._credentialsProvider.request();
} catch (providerError) {
throw new CredentialsRequestError(providerError);
}
}
async _connectToDaemon(): Promise<void> {
let credentials;
try {
credentials = await this._requestCredentials();
} catch (error) {
log.error(`Cannot request the RPC credentials: ${error.message}`);
return;
}
this._credentials = credentials;
this._daemonRpc.connect(credentials.connectionString);
}
async _onOpenConnection() {
this._reconnectBackoff.reset();
// authenticate once connected
const credentials = this._credentials;
try {
if (!credentials) {
throw new Error('Credentials cannot be unset after connection is established.');
}
await this._authenticate(credentials.sharedSecret);
} catch (error) {
log.error(`Cannot authenticate: ${error.message}`);
}
// autologin
try {
await this.autologin();
} catch (error) {
if (error instanceof NoAccountError) {
log.debug('No previously configured account set, showing window');
ipcRenderer.send('show-window');
} else {
log.error(`Failed to autologin: ${error.message}`);
}
}
// make sure to re-subscribe to state notifications when connection is re-established.
try {
await this._subscribeStateListener();
} catch (error) {
log.error(`Cannot subscribe for RPC notifications: ${error.message}`);
}
// fetch initial state
try {
await this._fetchInitialState();
} catch (error) {
log.error(`Cannot fetch initial state: ${error.message}`);
}
// auto connect the tunnel
try {
await this.connectTunnel();
} catch (error) {
log.error(`Cannot autoconnect the tunnel: ${error.message}`);
}
}
async _onCloseConnection(error: ?Error) {
if (error) {
log.debug(`Lost connection to daemon: ${error.message}`);
const recover = async () => {
try {
await this.connect();
} catch (error) {
log.error(`Failed to reconnect: ${error.message}`);
}
};
this._reconnectBackoff.attempt(() => {
recover();
});
}
}
/**
* Start reachability monitoring for online/offline detection
* This is currently done via HTML5 APIs but will be replaced later
* with proper backend integration.
*/
_setupReachability() {
const actions = this._reduxActions;
window.addEventListener('online', () => {
actions.connection.online();
});
window.addEventListener('offline', () => {
actions.connection.offline();
});
if (navigator.onLine) {
actions.connection.online();
} else {
actions.connection.offline();
}
}
async _subscribeStateListener() {
await this._daemonRpc.subscribeStateListener((newState, error) => {
if (error) {
log.error(`Received an error when processing the incoming state change: ${error.message}`);
}
if (newState) {
const connectionState = this._securityStateToConnectionState(newState);
log.debug(
`Got new state from backend {state: ${newState.state}, target_state: ${
newState.target_state
}}, translated to '${connectionState}'`,
);
this._updateConnectionState(connectionState);
this._refreshStateOnChange();
}
});
}
async _fetchInitialState() {
return Promise.all([
this.fetchSecurityState(),
this.fetchRelaySettings(),
this.fetchRelayLocations(),
this.fetchAllowLan(),
this.fetchLocation(),
this.fetchAccountHistory(),
]);
}
async _refreshStateOnChange() {
try {
await this.fetchLocation();
} catch (error) {
log.error(`Failed to fetch the location: ${error.message}`);
}
}
_securityStateToConnectionState(backendState: BackendState): ConnectionState {
if (backendState.state === 'unsecured' && backendState.target_state === 'secured') {
return 'connecting';
} else if (backendState.state === 'secured' && backendState.target_state === 'secured') {
return 'connected';
} else if (backendState.target_state === 'unsecured') {
return 'disconnected';
}
throw new Error('Unsupported state/target state combination: ' + JSON.stringify(backendState));
}
_updateConnectionState(connectionState: ConnectionState) {
const actions = this._reduxActions;
switch (connectionState) {
case 'connecting':
actions.connection.connecting();
break;
case 'connected':
actions.connection.connected();
break;
case 'disconnected':
actions.connection.disconnected();
break;
}
}
_rpcErrorToBackendError(e) {
if (e instanceof JsonRpcTransportRemoteError) {
switch (e.code) {
case -200: // Account doesn't exist
return new InvalidAccountError();
case -32603: // Internal error
// We treat all internal backend errors as the user cannot reach
// api.mullvad.net. This is not always true of course, but it is
// true so often that we choose to disregard the other edge cases
// for now.
return new CommunicationError();
}
} else if (e instanceof JsonRpcTransportTimeOutError) {
return new CommunicationError();
} else if (e instanceof NoDaemonError) {
return e;
}
return new UnknownError(e.message);
}
async _authenticate(sharedSecret: string) {
try {
await this._daemonRpc.authenticate(sharedSecret);
log.info('Authenticated with backend');
} catch (e) {
log.error(`Failed to authenticate with backend: ${e.message}`);
throw e;
}
}
}
/*
* Used to calculate the time to wait before reconnecting
* the websocket.
*
* It uses a linear backoff function that goes from 500ms
* to 3000ms
*/
class ReconnectionBackoff {
_attempt: number;
constructor() {
this._attempt = 0;
}
attempt(handler: () => void) {
setTimeout(handler, this._getIncreasedBackoff());
}
reset() {
this._attempt = 0;
}
_getIncreasedBackoff() {
if (this._attempt < 6) {
this._attempt++;
}
return this._attempt * 500;
}
}
|