From 3aa135fdb4d2f36c2759005b003591f1a9478889 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Wed, 14 Nov 2018 13:09:33 +1100 Subject: [PATCH 1/2] Grabbing last hash from server response. only requesting messages after the lasthash. Keeping track of lasthash for array of nodes (currently hardcoded for one) --- js/modules/loki_message_api.js | 37 +++++++++++++++++++++++++--------- libloki/proof-of-work.js | 2 +- package.json | 1 + preload.js | 2 +- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 0caaf8130..af835e3cd 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -36,15 +36,20 @@ function getPoWNonce(timestamp, ttl, pubKey, data) { class LokiServer { - constructor({ url }) { - if (!is.string(url)) { - throw new Error('WebAPI.initialize: Invalid server url'); - } - this.url = url; + constructor({ urls }) { + this.nodes = []; + urls.forEach(url => { + if (!is.string(url)) { + throw new Error('WebAPI.initialize: Invalid server url'); + } + this.nodes.push({ url }); + }); } async sendMessage(pubKey, data, ttl) { const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64'); + // Hardcoded to use a single node/server for now + const currentNode = this.nodes[0]; const timestamp = Math.floor(Date.now() / 1000); // Nonce is returned as a base64 string to include in header @@ -58,7 +63,7 @@ class LokiServer { } const options = { - url: `${this.url}/store`, + url: `${currentNode.url}/store`, type: 'POST', responseType: undefined, timeout: undefined, @@ -107,8 +112,11 @@ class LokiServer { } async retrieveMessages(pubKey) { + // Hardcoded to use a single node/server for now + const currentNode = this.nodes[0]; + const options = { - url: `${this.url}/retrieve`, + url: `${currentNode.url}/retrieve`, type: 'GET', responseType: 'json', timeout: undefined, @@ -116,11 +124,17 @@ class LokiServer { log.info(options.type, options.url); + const headers = { + 'X-Loki-recipient': pubKey, + }; + + if (currentNode.lastHash) { + headers['X-Loki-last-hash'] = currentNode.lastHash; + } + const fetchOptions = { method: options.type, - headers: { - 'X-Loki-recipient': pubKey, - }, + headers, timeout: options.timeout, }; @@ -146,6 +160,9 @@ class LokiServer { if (response.status >= 0 && response.status < 400) { log.info(options.type, options.url, response.status, 'Success'); + if (result.lastHash) { + currentNode.lastHash = result.lastHash; + } return result; } log.error(options.type, options.url, response.status, 'Error'); diff --git a/libloki/proof-of-work.js b/libloki/proof-of-work.js index e9bdafb1f..3adab32b2 100644 --- a/libloki/proof-of-work.js +++ b/libloki/proof-of-work.js @@ -1,6 +1,6 @@ const hash = require('js-sha512'); const bb = require('bytebuffer'); -const BigInteger = require('jsbn').BigInteger; +const { BigInteger } = require('jsbn'); const NONCE_LEN = 8; // Modify this value for difficulty scaling diff --git a/package.json b/package.json index de82011e0..67c188093 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "intl-tel-input": "^12.1.15", "jquery": "^3.3.1", "js-sha512": "^0.8.0", + "jsbn": "^1.1.0", "linkify-it": "^2.0.3", "lodash": "^4.17.4", "mkdirp": "^0.5.1", diff --git a/preload.js b/preload.js index d2383891f..abf376e3e 100644 --- a/preload.js +++ b/preload.js @@ -204,7 +204,7 @@ window.WebAPI = initializeWebAPI({ const { LokiServer } = require('./js/modules/loki_message_api'); window.LokiAPI = new LokiServer({ - url: config.serverUrl, + urls: [config.serverUrl], }); window.mnemonic = require('./libloki/mnemonic'); From 9731c9e086f8e116d7d54de6349a3f8d7e30eeba Mon Sep 17 00:00:00 2001 From: Beaudan Date: Wed, 14 Nov 2018 13:20:18 +1100 Subject: [PATCH 2/2] Comment out some signal server based code causing errors. Return before trying to unpad message because they aren't being padded in the first place --- libtextsecure/account_manager.js | 30 +++++++++++++++--------------- libtextsecure/message_receiver.js | 6 ++++-- libtextsecure/outgoing_message.js | 6 +++--- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index abf394788..cd046a444 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -23,7 +23,7 @@ const ARCHIVE_AGE = 7 * 24 * 60 * 60 * 1000; function AccountManager(username, password) { - this.server = window.WebAPI.connect({ username, password }); + // this.server = window.WebAPI.connect({ username, password }); this.pending = Promise.resolve(); } @@ -44,10 +44,10 @@ AccountManager.prototype.extend({ constructor: AccountManager, requestVoiceVerification(number) { - return this.server.requestVerificationVoice(number); + // return this.server.requestVerificationVoice(number); }, requestSMSVerification(number) { - return this.server.requestVerificationSMS(number); + // return this.server.requestVerificationSMS(number); }, registerSingleDevice(mnemonic, mnemonicLanguage) { const createAccount = this.createAccount.bind(this); @@ -140,18 +140,18 @@ throw new Error('account_manager: registerSecondDevice has not been implemented!'); }, refreshPreKeys() { - const generateKeys = this.generateKeys.bind(this, 0); - const registerKeys = this.server.registerKeys.bind(this.server); - - return this.queueTask(() => - this.server.getMyKeys().then(preKeyCount => { - window.log.info(`prekey count ${preKeyCount}`); - if (preKeyCount < 10) { - return generateKeys().then(registerKeys); - } - return null; - }) - ); + // const generateKeys = this.generateKeys.bind(this, 0); + // const registerKeys = this.server.registerKeys.bind(this.server); + + // return this.queueTask(() => + // this.server.getMyKeys().then(preKeyCount => { + // window.log.info(`prekey count ${preKeyCount}`); + // if (preKeyCount < 10) { + // return generateKeys().then(registerKeys); + // } + // return null; + // }) + // ); }, rotateSignedPreKey() { return this.queueTask(() => { diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index e70ba299c..bb0c74b41 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -807,6 +807,7 @@ MessageReceiver.prototype.extend({ }, async decryptPreKeyWhisperMessage(ciphertext, sessionCipher, address) { const padded = await sessionCipher.decryptPreKeyWhisperMessage(ciphertext); + return padded; try { return this.unpad(padded); @@ -1032,8 +1033,9 @@ MessageReceiver.prototype.extend({ } else if (content.receiptMessage) { return this.handleReceiptMessage(envelope, content.receiptMessage); } - this.removeFromCache(envelope); - throw new Error('Unsupported content message'); + if (!content.preKeyBundleMessage) { + throw new Error('Unsupported content message'); + } }, handleCallMessage(envelope) { window.log.info('call message from', this.getEnvelopeId(envelope)); diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index c1fb2b615..52b5129c8 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -134,9 +134,9 @@ OutgoingMessage.prototype = { }) ); // TODO: check if still applicable - if (updateDevices === undefined) { - return this.server.getKeysForNumber(number, '*').then(handleResult); - } + // if (updateDevices === undefined) { + // return this.server.getKeysForNumber(number, '*').then(handleResult); + // } let promise = Promise.resolve(true); updateDevices.forEach(device => { promise = promise.then(() =>