summaryrefslogtreecommitdiffhomepage
path: root/app/lib/ipc.js
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-03-27 17:28:50 +0800
committerErik Larkö <erik@mullvad.net>2017-04-07 13:40:46 +0800
commit7599c0b1009d1c188307f0c170a05fae80ababa7 (patch)
tree3fd5541e58f3a3eb6ae71452b27fae0aa505d446 /app/lib/ipc.js
parentfbd72425977346cf9eb32ba394436f061f22d698 (diff)
downloadmullvadvpn-7599c0b1009d1c188307f0c170a05fae80ababa7.tar.xz
mullvadvpn-7599c0b1009d1c188307f0c170a05fae80ababa7.zip
Rust backend
Diffstat (limited to 'app/lib/ipc.js')
-rw-r--r--app/lib/ipc.js120
1 files changed, 22 insertions, 98 deletions
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;
});
}
}