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
|
// @flow
import JsonRpcWs, { InvalidReply } from './jsonrpc-ws-ipc';
import {
object,
maybe,
string,
number,
boolean,
enumeration,
arrayOf,
oneOf,
} from 'validated/schema';
import { validate } from 'validated/object';
import type { Node as SchemaNode } from 'validated/schema';
export type AccountData = { expiry: string };
export type AccountToken = string;
export type Ip = string;
export type Location = {
ip: Ip,
country: string,
city: ?string,
latitude: number,
longitude: number,
mullvad_exit_ip: boolean,
};
const LocationSchema = object({
ip: string,
country: string,
city: maybe(string),
latitude: number,
longitude: number,
mullvad_exit_ip: boolean,
});
export type SecurityState = 'secured' | 'unsecured';
export type BackendState = {
state: SecurityState,
target_state: SecurityState,
};
export type RelayProtocol = 'tcp' | 'udp';
export type RelayLocation = {| city: [string, string] |} | {| country: string |};
type OpenVpnParameters = {
port: 'any' | { only: number },
protocol: 'any' | { only: RelayProtocol },
};
type TunnelOptions<TOpenVpnParameters> = {
openvpn: TOpenVpnParameters,
};
type RelaySettingsNormal<TTunnelOptions> = {
location:
| 'any'
| {
only: RelayLocation,
},
tunnel:
| 'any'
| {
only: TTunnelOptions,
},
};
// types describing the structure of RelaySettings
export type RelaySettingsCustom = {
host: string,
tunnel: {
openvpn: {
port: number,
protocol: RelayProtocol,
},
},
};
export type RelaySettings =
| {|
normal: RelaySettingsNormal<TunnelOptions<OpenVpnParameters>>,
|}
| {|
custom_tunnel_endpoint: RelaySettingsCustom,
|};
// types describing the partial update of RelaySettings
export type RelaySettingsNormalUpdate = $Shape<
RelaySettingsNormal<TunnelOptions<$Shape<OpenVpnParameters>>>,
>;
export type RelaySettingsUpdate =
| {|
normal: RelaySettingsNormalUpdate,
|}
| {|
custom_tunnel_endpoint: RelaySettingsCustom,
|};
const constraint = <T>(constraintValue: SchemaNode<T>) =>
oneOf(
string,
object({
only: constraintValue,
}),
);
const RelaySettingsSchema = oneOf(
object({
normal: object({
location: constraint(
oneOf(
object({
city: arrayOf(string),
}),
object({
country: string,
}),
),
),
tunnel: constraint(
object({
openvpn: object({
port: constraint(number),
protocol: constraint(enumeration('udp', 'tcp')),
}),
}),
),
}),
}),
object({
custom_tunnel_endpoint: object({
host: string,
tunnel: object({
openvpn: object({
port: number,
protocol: enumeration('udp', 'tcp'),
}),
}),
}),
}),
);
export type RelayList = {
countries: Array<RelayListCountry>,
};
export type RelayListCountry = {
name: string,
code: string,
cities: Array<RelayListCity>,
};
export type RelayListCity = {
name: string,
code: string,
latitude: number,
longitude: number,
has_active_relays: boolean,
};
const RelayListSchema = object({
countries: arrayOf(
object({
name: string,
code: string,
cities: arrayOf(
object({
name: string,
code: string,
latitude: number,
longitude: number,
has_active_relays: boolean,
}),
),
}),
),
});
export interface IpcFacade {
setConnectionString(string): void;
getAccountData(AccountToken): Promise<AccountData>;
getRelayLocations(): Promise<RelayList>;
getAccount(): Promise<?AccountToken>;
setAccount(accountToken: ?AccountToken): Promise<void>;
updateRelaySettings(RelaySettingsUpdate): Promise<void>;
getRelaySettings(): Promise<RelaySettings>;
setAllowLan(boolean): Promise<void>;
getAllowLan(): Promise<boolean>;
connect(): Promise<void>;
disconnect(): Promise<void>;
shutdown(): Promise<void>;
getLocation(): Promise<Location>;
getState(): Promise<BackendState>;
registerStateListener((BackendState) => void): void;
setCloseConnectionHandler(() => void): void;
authenticate(sharedSecret: string): Promise<void>;
getAccountHistory(): Promise<Array<AccountToken>>;
removeAccountFromHistory(accountToken: AccountToken): Promise<void>;
}
export class RealIpc implements IpcFacade {
_ipc: JsonRpcWs;
constructor(connectionString: string) {
this._ipc = new JsonRpcWs(connectionString);
}
setConnectionString(str: string) {
this._ipc.setConnectionString(str);
}
getAccountData(accountToken: AccountToken): Promise<AccountData> {
// send the IPC with 30s timeout since the backend will wait
// for a HTTP request before replying
return this._ipc.send('get_account_data', accountToken, 30000).then((raw) => {
if (typeof raw === 'object' && raw && raw.expiry) {
return raw;
} else {
throw new InvalidReply(raw, 'Expected an object with expiry');
}
});
}
async getRelayLocations(): Promise<RelayList> {
const raw = await this._ipc.send('get_relay_locations');
try {
const validated: any = validate(RelayListSchema, raw);
return (validated: RelayList);
} catch (e) {
throw new InvalidReply(raw, e);
}
}
getAccount(): Promise<?AccountToken> {
return this._ipc.send('get_account').then((raw) => {
if (raw === undefined || raw === null || typeof raw === 'string') {
return raw;
} else {
throw new InvalidReply(raw);
}
});
}
setAccount(accountToken: ?AccountToken): Promise<void> {
return this._ipc.send('set_account', accountToken).then(this._ignoreResponse);
}
_ignoreResponse(_response: mixed): void {
return;
}
updateRelaySettings(relaySettings: RelaySettingsUpdate): Promise<void> {
return this._ipc.send('update_relay_settings', [relaySettings]).then(this._ignoreResponse);
}
getRelaySettings(): Promise<RelaySettings> {
return this._ipc.send('get_relay_settings').then((raw) => {
try {
const validated: any = validate(RelaySettingsSchema, raw);
return (validated: RelaySettings);
} catch (e) {
throw new InvalidReply(raw, e);
}
});
}
setAllowLan(allowLan: boolean): Promise<void> {
return this._ipc.send('set_allow_lan', [allowLan]).then(this._ignoreResponse);
}
async getAllowLan(): Promise<boolean> {
const raw = await this._ipc.send('get_allow_lan');
if (typeof raw === 'boolean') {
return raw;
} else {
throw new InvalidReply(raw, 'Expected a boolean');
}
}
connect(): Promise<void> {
return this._ipc.send('connect').then(this._ignoreResponse);
}
disconnect(): Promise<void> {
return this._ipc.send('disconnect').then(this._ignoreResponse);
}
shutdown(): Promise<void> {
return this._ipc.send('shutdown').then(this._ignoreResponse);
}
getLocation(): Promise<Location> {
// send the IPC with 30s timeout since the backend will wait
// for a HTTP request before replying
return this._ipc.send('get_current_location', [], 30000).then((raw) => {
try {
const validated: any = validate(LocationSchema, raw);
return (validated: Location);
} catch (e) {
throw new InvalidReply(raw, e);
}
});
}
getState(): Promise<BackendState> {
return this._ipc.send('get_state').then((raw) => {
return this._parseBackendState(raw);
});
}
_parseBackendState(raw: mixed): BackendState {
if (raw && raw.state && raw.target_state) {
const uncheckedRaw: any = raw;
const states: Array<SecurityState> = ['secured', 'unsecured'];
const correctState = states.includes(uncheckedRaw.state);
const correctTargetState = states.includes(uncheckedRaw.target_state);
if (!correctState || !correctTargetState) {
throw new InvalidReply(raw);
}
return (uncheckedRaw: BackendState);
} else {
throw new InvalidReply(raw);
}
}
registerStateListener(listener: (BackendState) => void) {
this._ipc.on('new_state', (rawEvent) => {
const parsedEvent: BackendState = this._parseBackendState(rawEvent);
listener(parsedEvent);
});
}
setCloseConnectionHandler(handler: () => void) {
this._ipc.setCloseConnectionHandler(handler);
}
authenticate(sharedSecret: string): Promise<void> {
return this._ipc.send('auth', sharedSecret).then(this._ignoreResponse);
}
getAccountHistory(): Promise<Array<AccountToken>> {
return this._ipc.send('get_account_history').then((raw) => {
if (Array.isArray(raw) && raw.every((i) => typeof i === 'string')) {
const checked: any = raw;
return (checked: Array<AccountToken>);
} else {
throw new InvalidReply(raw, 'Expected an array of strings');
}
});
}
removeAccountFromHistory(accountToken: AccountToken): Promise<void> {
return this._ipc.send('remove_account_from_history', accountToken).then(this._ignoreResponse);
}
}
|