Merge pull request #8 from sachaaaaa/send_as_bytes

Send outgoing messages as bytes
This commit is contained in:
sachaaaaa 2018-10-11 14:13:43 +11:00 committed by GitHub
commit 583fb2e6c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 22 deletions

View file

@ -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);

View file

@ -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();

View file

@ -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

View file

@ -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",

View file

@ -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"