Merge pull request #8 from sachaaaaa/send_as_bytes

Send outgoing messages as bytes
pull/10/head
sachaaaaa 7 years ago committed by GitHub
commit 583fb2e6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,22 +29,19 @@ function initialize({ url }) {
}; };
log.info(options.type, options.url); log.info(options.type, options.url);
const body = JSON.stringify({
pub_key,
message: data,
ttl,
});
const fetchOptions = { const fetchOptions = {
method: options.type, method: options.type,
body, body: data,
headers: { 'X-Loki-Messenger-Agent': 'OWD' }, headers: {
'X-Loki-ttl': ttl,
'X-Loki-recipient': pub_key,
'Content-Length': data.byteLength,
},
timeout: options.timeout, timeout: options.timeout,
}; };
fetchOptions.headers['Content-Type'] = 'application/json; charset=utf-8';
let response; let response;
try { try {
response = await fetch(options.url, fetchOptions); response = await fetch(options.url, fetchOptions);

@ -150,11 +150,11 @@ OutgoingMessage.prototype = {
return promise; return promise;
}, },
async transmitMessage(number, jsonData, timestamp) { async transmitMessage(number, data, timestamp) {
const pubKey = number; const pubKey = number;
const ttl = 2 * 24 * 60 * 60; const ttl = 2 * 24 * 60 * 60;
try { 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; return response;
} }
catch (e) { catch (e) {
@ -251,13 +251,16 @@ OutgoingMessage.prototype = {
); );
} }
ciphers[address.getDeviceId()] = sessionCipher; 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, type: ciphertext.type,
address: address, address: address,
destinationDeviceId: address.getDeviceId(), destinationDeviceId: address.getDeviceId(),
destinationRegistrationId: ciphertext.registrationId, destinationRegistrationId: ciphertext.registrationId,
// TODO: simplify the binary -> string -> binary here content: ciphertext.body,
content: dcodeIO.ByteBuffer.wrap(ciphertext.body,'binary').toArrayBuffer(),
})); }));
}) })
) )
@ -266,7 +269,8 @@ OutgoingMessage.prototype = {
outgoingObjects.forEach(outgoingObject => { outgoingObjects.forEach(outgoingObject => {
promises.push(this.wrapInWebsocketMessage(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); await this.transmitMessage(number, socketMessages, this.timestamp);
this.successfulNumbers[this.successfulNumbers.length] = number; this.successfulNumbers[this.successfulNumbers.length] = number;
this.numberCompleted(); this.numberCompleted();

@ -15,10 +15,18 @@ class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
#self.send_header() #self.send_header()
self.end_headers() self.end_headers()
message = self.rfile.read(int(self.headers.get('Content-Length'))).decode('UTF-8') #message = self.rfile.read(int(self.headers.get('Content-Length'))).decode('UTF-8')
length = self.headers.get('Content-Length')
print(message) 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 # Send message back to client
#message = "ok" #message = "ok"
# Write content as utf-8 data # Write content as utf-8 data

@ -73,7 +73,7 @@
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"moment": "^2.21.0", "moment": "^2.21.0",
"mustache": "^2.3.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-gyp": "^3.8.0",
"node-sass": "^4.9.3", "node-sass": "^4.9.3",
"os-locale": "^2.1.0", "os-locale": "^2.1.0",

@ -5680,13 +5680,17 @@ node-dir@^0.1.10:
dependencies: dependencies:
minimatch "^3.0.2" 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" version "1.7.3"
resolved "https://github.com/scottnonnenberg-signal/node-fetch.git#3e5f51e08c647ee5f20c43b15cf2d352d61c36b4" resolved "https://github.com/scottnonnenberg-signal/node-fetch.git#3e5f51e08c647ee5f20c43b15cf2d352d61c36b4"
dependencies: dependencies:
encoding "^0.1.11" encoding "^0.1.11"
is-stream "^1.0.1" 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: node-forge@0.7.1:
version "0.7.1" version "0.7.1"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.1.tgz#9da611ea08982f4b94206b3beb4cc9665f20c300" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.1.tgz#9da611ea08982f4b94206b3beb4cc9665f20c300"

Loading…
Cancel
Save