diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index cc642732d..e75341e82 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -29,22 +29,19 @@ function initialize({ url }) { }; log.info(options.type, options.url); - - const body = JSON.stringify({ - pub_key, - message: data, - ttl, - }); + const fetchOptions = { method: options.type, - body, - headers: { 'X-Loki-Messenger-Agent': 'OWD' }, + body: data, + headers: { + 'X-Loki-ttl': ttl, + 'X-Loki-recipient': pub_key, + 'Content-Length': data.byteLength, + }, timeout: options.timeout, }; - fetchOptions.headers['Content-Type'] = 'application/json; charset=utf-8'; - let response; try { response = await fetch(options.url, fetchOptions); diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 723e8c7bd..fb57efd5b 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -150,11 +150,11 @@ OutgoingMessage.prototype = { return promise; }, - async transmitMessage(number, jsonData, timestamp) { + async transmitMessage(number, data, timestamp) { const pubKey = number; const ttl = 2 * 24 * 60 * 60; try { - const [response, status] = await this.lokiserver.sendMessage(pubKey, JSON.stringify(jsonData), ttl); + const [response, status] = await this.lokiserver.sendMessage(pubKey, data, ttl); return response; } catch (e) { @@ -251,13 +251,16 @@ OutgoingMessage.prototype = { ); } ciphers[address.getDeviceId()] = sessionCipher; - return sessionCipher.encrypt(plaintext).then(ciphertext => ({ + return sessionCipher.encrypt(plaintext).then(ciphertext => { + if (! this.fallBackEncryption) + ciphertext.body = new Uint8Array(dcodeIO.ByteBuffer.wrap(ciphertext.body,'binary').toArrayBuffer()); + return ciphertext; + }).then(ciphertext => ({ type: ciphertext.type, address: address, destinationDeviceId: address.getDeviceId(), destinationRegistrationId: ciphertext.registrationId, - // TODO: simplify the binary -> string -> binary here - content: dcodeIO.ByteBuffer.wrap(ciphertext.body,'binary').toArrayBuffer(), + content: ciphertext.body, })); }) ) @@ -266,7 +269,8 @@ OutgoingMessage.prototype = { outgoingObjects.forEach(outgoingObject => { promises.push(this.wrapInWebsocketMessage(outgoingObject)); }); - const socketMessages = await Promise.all(promises); + // TODO: handle multiple devices/messages per transmit + const socketMessages = await promises[0]; await this.transmitMessage(number, socketMessages, this.timestamp); this.successfulNumbers[this.successfulNumbers.length] = number; this.numberCompleted(); diff --git a/mockup_servers/http_server.py b/mockup_servers/http_server.py index cd3dfc5aa..dd47bf598 100644 --- a/mockup_servers/http_server.py +++ b/mockup_servers/http_server.py @@ -15,10 +15,18 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler): #self.send_header() self.end_headers() - message = self.rfile.read(int(self.headers.get('Content-Length'))).decode('UTF-8') - - print(message) - + #message = self.rfile.read(int(self.headers.get('Content-Length'))).decode('UTF-8') + length = self.headers.get('Content-Length') + for (k,v) in self.headers.items(): + print(k + ':' + v) + if length: + print ('length: ' + self.headers.get('Content-Length')) + message = self.rfile.read(int(length)) + + array = [] + for k in message: + array += [k] + print(array) # Send message back to client #message = "ok" # Write content as utf-8 data diff --git a/package.json b/package.json index 7b6395905..4d9720e8b 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "mkdirp": "^0.5.1", "moment": "^2.21.0", "mustache": "^2.3.0", - "node-fetch": "https://github.com/scottnonnenberg-signal/node-fetch.git#3e5f51e08c647ee5f20c43b15cf2d352d61c36b4", + "node-fetch": "^2.2.0", "node-gyp": "^3.8.0", "node-sass": "^4.9.3", "os-locale": "^2.1.0", diff --git a/yarn.lock b/yarn.lock index 1ea722401..8d368faca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5680,13 +5680,17 @@ node-dir@^0.1.10: dependencies: minimatch "^3.0.2" -node-fetch@^1.0.1, "node-fetch@https://github.com/scottnonnenberg-signal/node-fetch.git#3e5f51e08c647ee5f20c43b15cf2d352d61c36b4": +node-fetch@^1.0.1: version "1.7.3" resolved "https://github.com/scottnonnenberg-signal/node-fetch.git#3e5f51e08c647ee5f20c43b15cf2d352d61c36b4" dependencies: encoding "^0.1.11" is-stream "^1.0.1" +node-fetch@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5" + node-forge@0.7.1: version "0.7.1" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.1.tgz#9da611ea08982f4b94206b3beb4cc9665f20c300"