diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-03-27 17:28:50 +0800 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-04-07 13:40:46 +0800 |
| commit | 7599c0b1009d1c188307f0c170a05fae80ababa7 (patch) | |
| tree | 3fd5541e58f3a3eb6ae71452b27fae0aa505d446 /app | |
| parent | fbd72425977346cf9eb32ba394436f061f22d698 (diff) | |
| download | mullvadvpn-7599c0b1009d1c188307f0c170a05fae80ababa7.tar.xz mullvadvpn-7599c0b1009d1c188307f0c170a05fae80ababa7.zip | |
Rust backend
Diffstat (limited to 'app')
| -rw-r--r-- | app/lib/backend.js | 48 | ||||
| -rw-r--r-- | app/lib/ipc.js | 120 | ||||
| -rw-r--r-- | app/main.js | 10 |
3 files changed, 51 insertions, 127 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index 2797c9ac46..3ebb75e813 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -114,25 +114,25 @@ class BackendError extends Error { /** * Backend implementation - * + * * @class Backend */ export default class Backend extends EventEmitter { /** * BackendError type - * + * * @static - * + * * @memberOf Backend */ static Error = BackendError; /** * Backend error enum - * + * * @static - * + * * @memberOf Backend */ static ErrorType = new Enum({ @@ -143,7 +143,7 @@ export default class Backend extends EventEmitter { /** * Event type enum - * + * * @type {EventType} * @extends {Enum} * @property {string} connect @@ -157,10 +157,10 @@ export default class Backend extends EventEmitter { * @property {string} updatedReachability */ static EventType = new Enum('connect', 'connecting', 'disconnect', 'login', 'logging', 'logout', 'updatedIp', 'updatedLocation', 'updatedReachability'); - + /** * Creates an instance of Backend. - * + * * @memberOf Backend */ constructor(ipc) { @@ -181,15 +181,15 @@ export default class Backend extends EventEmitter { /** * Tells whether account has credits - * + * * @type {bool} * @readonly - * + * * @memberOf Backend */ - get hasCredits() { - return this._paidUntil !== null && - moment(this._paidUntil).isAfter(moment()); + get hasCredits() { + return this._paidUntil !== null && + moment(this._paidUntil).isAfter(moment()); } sync() { @@ -222,10 +222,10 @@ export default class Backend extends EventEmitter { /** * Get server info by key * 'fastest' or 'nearest' can be used as well - * - * @param {string} key + * + * @param {string} key * @returns {ServerInfo} - * + * * @memberOf Backend */ serverInfo(key) { @@ -238,9 +238,9 @@ export default class Backend extends EventEmitter { /** * Get fastest server info - * + * * @returns {ServerInfo} - * + * * @memberOf Backend */ fastestServer() { @@ -255,9 +255,9 @@ export default class Backend extends EventEmitter { /** * Get nearest server info - * + * * @returns {ServerInfo} - * + * * @memberOf Backend */ nearestServer() { @@ -275,8 +275,8 @@ export default class Backend extends EventEmitter { * * @emits Backend.EventType.logging * @emits Backend.EventType.login - * @param {string} account - * + * @param {string} account + * * @memberOf Backend */ login(account) { @@ -328,7 +328,7 @@ export default class Backend extends EventEmitter { * @emits Backend.EventType.connect * * @param {string} addr IP address or domain name - * + * * @memberOf Backend */ connect(addr) { @@ -353,7 +353,7 @@ export default class Backend extends EventEmitter { /** * Disconnect from VPN server - * + * * @emits Backend.EventType.disconnect * @memberOf Backend */ diff --git a/app/lib/ipc.js b/app/lib/ipc.js index a1f31c4a9a..0be6341cf6 100644 --- a/app/lib/ipc.js +++ b/app/lib/ipc.js @@ -1,17 +1,6 @@ -import moment from 'moment'; +import jsonrpc from 'jsonrpc-lite'; +import uuid from 'uuid'; -const mockUnsecureGeoIp = { - ip: '192.168.1.2', - latlong: [60,61], - country: 'sweden', - city: 'bollebygd', -}; -const mockSecureGeoIp = { - ip: '1.2.3.4', - latlong: [1,2], - country: 'Narnia', - city: 'LE CITY', -}; export default class Ipc { constructor(connectionString) { @@ -23,97 +12,32 @@ export default class Ipc { } send(action, data) { - return this.mockSend(action, data) - .catch(e => { - console.error('IPC call failed', action, data, e); - throw e; - }); - } - - mockSend(action, data) { - const actions = { - login: this.mockLogin, - logout: this.mockLogout, - connect: ::this.mockConnect, - cancelConnection: this.mockCancelConnection, - disconnect: ::this.mockDisconnect, - getConnectionInfo: () => { - return new Promise((resolve) => { - resolve({ - ip: this._mockIsConnected ? mockSecureGeoIp.ip : mockUnsecureGeoIp.ip, - }); - }); - }, - getLocation: () => { - return new Promise((resolve) => { - const data = this._mockIsConnected ? mockSecureGeoIp : mockUnsecureGeoIp; - resolve({ - latlong: data.latlong, - country: data.country, - city: data.city, - }); - }); - }, - }; - - const actionCb = actions[action]; - if (actionCb) { - return actionCb(action, data); - } else { - console.log('UNKNOWN IPC ACTION', action); - return new Promise((resolve, reject) => { - reject('Unknown IPC action ' + action); - }); - } - } + const id = uuid.v4(); + const jsonrpcMessage = jsonrpc.request(id, action, data); - mockLogin(action, data) { - return new Promise((resolve, reject) => { - let res = {account: data.accountNumber}; - - if(data.accountNumber.startsWith('1111')) { // accounts starting with 1111 expire in one month - res.paidUntil = moment().startOf('day').add(15, 'days').toISOString(); - } else if(data.accountNumber.startsWith('2222')) { // expired in 2013 - res.paidUntil = moment('2013-01-01').toISOString(); - } else if(data.accountNumber.startsWith('3333')) { // expire in 2038 - res.paidUntil = moment('2038-01-01').toISOString(); - } else { - reject(new Error('Invalid account number.')); - return; + return fetch(this.connectionString, { + method: 'POST', + headers: new Headers({ + 'Content-Type': 'application/json', + }), + body: JSON.stringify(jsonrpcMessage), + }).then((res) => { + if (!res.ok) { + throw {msg: 'HTTP request failed', data: res}; } - resolve(res); - }); - } + return res.json(); + }).then(json => { - mockLogout() { - return new Promise(function(resolve) { - resolve(); - }); - } - - mockConnect(action, data) { - return new Promise((resolve, reject) => { - // Prototype: Swedish servers will throw error during connect - if(/se\d+\.mullvad\.net/.test(data.address)) { - reject(new Error('Server is unreachable')); - } else { - this._mockIsConnected = true; - resolve(); + const c = jsonrpc.parseObject(json); + if (c.type === 'error') { + throw c.payload.error.message; } - }); - } - - mockCancelConnection() { - return new Promise(function(resolve) { - resolve(); - }); - } + return c.payload.result; - mockDisconnect() { - return new Promise((resolve) => { - this._mockIsConnected = false; - resolve(); + }).catch(e => { + console.error('IPC call failed', action, data, e); + throw e; }); } } diff --git a/app/main.js b/app/main.js index c86606b39c..9dfc6598b0 100644 --- a/app/main.js +++ b/app/main.js @@ -33,7 +33,7 @@ const installDevTools = async () => { const createWindow = () => { const contentHeight = 568; let options = { - width: 320, + width: 320, height: contentHeight, resizable: false, maximizable: false, @@ -136,7 +136,7 @@ const showWindow = () => { const { x, y } = getWindowPosition(); window.setPosition(x, y, false); } - + window.show(); window.focus(); }; @@ -158,7 +158,7 @@ const createTray = () => { tray = new Tray(nativeImage.createEmpty()); tray.setHighlightMode('never'); tray.on('click', toggleWindow); - + // setup NSEvent monitor to fix inconsistent window.blur // see https://github.com/electron/electron/issues/8689 const { NSEventMonitor, NSEventMask } = require('nseventmonitor'); @@ -168,7 +168,7 @@ const createTray = () => { // add IPC handler to change tray icon from renderer ipcMain.on('changeTrayIcon', (_, type) => trayIconManager.iconType = type); - + // setup event handlers window.on('show', () => macEventMonitor.start(eventMask, () => window.hide())); window.on('hide', () => macEventMonitor.stop()); @@ -176,7 +176,7 @@ const createTray = () => { window.on('blur', () => !window.isDevToolsOpened() && window.hide()); }; -app.on('window-all-closed', () => { +app.on('window-all-closed', () => { app.quit(); }); |
