2015-04-29 20:56:40 +02:00
|
|
|
/* vim: ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
;(function () {
|
|
|
|
'use strict';
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
|
2015-07-24 00:41:17 +02:00
|
|
|
function MessageReceiver(url, eventTarget) {
|
2015-04-29 20:56:40 +02:00
|
|
|
if (eventTarget instanceof EventTarget) {
|
|
|
|
this.target = eventTarget;
|
|
|
|
} else {
|
|
|
|
throw new TypeError('MessageReceiver expected an EventTarget');
|
|
|
|
}
|
2015-07-24 00:41:17 +02:00
|
|
|
this.connect(url);
|
2015-04-29 20:56:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageReceiver.prototype = {
|
|
|
|
constructor: MessageReceiver,
|
2015-07-24 00:41:17 +02:00
|
|
|
connect: function(url) {
|
2015-06-20 00:32:25 +02:00
|
|
|
var eventTarget = this.target;
|
2015-04-29 20:56:40 +02:00
|
|
|
// initialize the socket and start listening for messages
|
2015-07-24 00:41:17 +02:00
|
|
|
this.socket = TextSecureServer.getMessageWebsocket(url);
|
2015-06-20 00:32:25 +02:00
|
|
|
this.socket.onclose = function(e) {
|
|
|
|
if (e.code === 1006) {
|
|
|
|
// possible 403. Make an request to confirm
|
|
|
|
TextSecureServer.getDevices(textsecure.storage.user.getNumber()).catch(function(e) {
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:error');
|
2015-07-10 21:24:12 +02:00
|
|
|
ev.error = e;
|
|
|
|
eventTarget.dispatchEvent(ev);
|
2015-06-20 00:32:25 +02:00
|
|
|
});
|
|
|
|
}
|
2015-07-28 22:05:55 +02:00
|
|
|
};
|
2015-04-29 20:56:40 +02:00
|
|
|
|
2015-07-28 23:58:55 +02:00
|
|
|
this.wsr = new WebSocketResource(this.socket, {
|
|
|
|
handleRequest: this.handleRequest.bind(this),
|
2015-07-28 22:05:55 +02:00
|
|
|
keepalive: { path: '/v1/keepalive', disconnect: true }
|
|
|
|
});
|
2015-07-16 22:16:50 +02:00
|
|
|
|
|
|
|
},
|
2015-06-01 23:08:21 +02:00
|
|
|
handleRequest: function(request) {
|
2015-07-28 22:05:55 +02:00
|
|
|
this.wsr.resetKeepAliveTimer();
|
2015-06-01 23:08:21 +02:00
|
|
|
// TODO: handle different types of requests. for now we only expect
|
|
|
|
// PUT /messages <encrypted IncomingPushMessageSignal>
|
|
|
|
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
|
|
|
|
var envelope = textsecure.protobuf.Envelope.decode(plaintext);
|
|
|
|
// After this point, decoding errors are not the server's
|
|
|
|
// fault, and we should handle them gracefully and tell the
|
|
|
|
// user they received an invalid message
|
|
|
|
request.respond(200, 'OK');
|
2015-04-29 20:56:40 +02:00
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) {
|
|
|
|
this.onDeliveryReceipt(envelope);
|
|
|
|
} else if (envelope.content) {
|
|
|
|
this.handleContentMessage(envelope);
|
|
|
|
} else if (envelope.legacyMessage) {
|
|
|
|
this.handleLegacyMessage(envelope);
|
|
|
|
} else {
|
|
|
|
throw new Error('Received message with no content and no legacyMessage');
|
|
|
|
}
|
2015-04-29 20:56:40 +02:00
|
|
|
|
2015-06-01 23:08:21 +02:00
|
|
|
}.bind(this)).catch(function(e) {
|
|
|
|
request.respond(500, 'Bad encrypted websocket message');
|
|
|
|
console.log("Error handling incoming message:", e);
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:error');
|
2015-06-01 23:08:21 +02:00
|
|
|
ev.error = e;
|
|
|
|
this.target.dispatchEvent(ev);
|
|
|
|
}.bind(this));
|
2015-04-29 20:56:40 +02:00
|
|
|
},
|
|
|
|
getStatus: function() {
|
|
|
|
if (this.socket) {
|
|
|
|
return this.socket.getStatus();
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
},
|
|
|
|
onDeliveryReceipt: function (envelope) {
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:receipt');
|
2015-06-01 23:08:21 +02:00
|
|
|
ev.proto = envelope;
|
|
|
|
this.target.dispatchEvent(ev);
|
|
|
|
},
|
|
|
|
decrypt: function(envelope, ciphertext) {
|
|
|
|
return textsecure.protocol_wrapper.decrypt(
|
|
|
|
envelope.source,
|
|
|
|
envelope.sourceDevice,
|
|
|
|
envelope.type,
|
|
|
|
ciphertext
|
|
|
|
).catch(function(error) {
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:error');
|
2015-06-01 23:08:21 +02:00
|
|
|
ev.error = error;
|
|
|
|
ev.proto = envelope;
|
|
|
|
this.target.dispatchEvent(ev);
|
2015-07-06 21:45:29 +02:00
|
|
|
throw error; // reject this promise
|
2015-06-01 23:08:21 +02:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleSentMessage: function(destination, timestamp, message) {
|
|
|
|
var source = textsecure.storage.user.getNumber();
|
|
|
|
return processDecrypted(message, source).then(function(message) {
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:sent');
|
2015-06-01 23:08:21 +02:00
|
|
|
ev.data = {
|
|
|
|
destination : destination,
|
|
|
|
timestamp : timestamp.toNumber(),
|
|
|
|
message : message
|
|
|
|
};
|
|
|
|
this.target.dispatchEvent(ev);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleDataMessage: function(envelope, message, close_session) {
|
|
|
|
if ((message.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION)
|
|
|
|
== textsecure.protobuf.DataMessage.Flags.END_SESSION ) {
|
|
|
|
close_session();
|
|
|
|
}
|
|
|
|
return processDecrypted(message, envelope.source).then(function(message) {
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:message');
|
2015-06-01 23:08:21 +02:00
|
|
|
ev.data = {
|
|
|
|
source : envelope.source,
|
|
|
|
timestamp : envelope.timestamp.toNumber(),
|
|
|
|
message : message
|
|
|
|
};
|
|
|
|
this.target.dispatchEvent(ev);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleLegacyMessage: function (envelope) {
|
|
|
|
return this.decrypt(envelope, envelope.legacyMessage).then(function(result) {
|
|
|
|
var plaintext = result[0]; // array buffer
|
|
|
|
var close_session = result[1]; // function
|
|
|
|
var message = textsecure.protobuf.DataMessage.decode(plaintext);
|
|
|
|
return this.handleDataMessage(envelope, message, close_session);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleContentMessage: function (envelope) {
|
|
|
|
return this.decrypt(envelope, envelope.content).then(function(result) {
|
|
|
|
var plaintext = result[0]; // array buffer
|
|
|
|
var close_session = result[1]; // function
|
|
|
|
var content = textsecure.protobuf.Content.decode(plaintext);
|
|
|
|
if (content.syncMessage) {
|
|
|
|
return this.handleSyncMessage(envelope, content.syncMessage);
|
|
|
|
} else if (content.dataMessage) {
|
|
|
|
return this.handleDataMessage(envelope, content.dataMessage, close_session);
|
|
|
|
} else {
|
|
|
|
throw new Error('Got Content message with no dataMessage and no syncMessage');
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleSyncMessage: function(envelope, syncMessage) {
|
|
|
|
if (envelope.source !== textsecure.storage.user.getNumber()) {
|
|
|
|
throw new Error('Received sync message from another number');
|
|
|
|
}
|
|
|
|
if (envelope.sourceDevice == textsecure.storage.user.getDeviceId()) {
|
|
|
|
throw new Error('Received sync message from our own device');
|
|
|
|
}
|
|
|
|
if (syncMessage.sent) {
|
|
|
|
var sentMessage = syncMessage.sent;
|
|
|
|
return this.handleSentMessage(
|
|
|
|
sentMessage.destination,
|
|
|
|
sentMessage.timestamp,
|
|
|
|
sentMessage.message
|
|
|
|
);
|
|
|
|
} else if (syncMessage.contacts) {
|
|
|
|
this.handleContacts(syncMessage.contacts);
|
2015-06-22 23:45:42 +02:00
|
|
|
} else if (syncMessage.groups) {
|
|
|
|
this.handleGroups(syncMessage.groups);
|
2015-06-01 23:08:21 +02:00
|
|
|
} else {
|
2015-06-22 23:45:42 +02:00
|
|
|
throw new Error('Got SyncMessage with no sent, contacts, or groups');
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
handleContacts: function(contacts) {
|
|
|
|
var eventTarget = this.target;
|
|
|
|
var attachmentPointer = contacts.blob;
|
|
|
|
return handleAttachment(attachmentPointer).then(function() {
|
|
|
|
var contactBuffer = new ContactBuffer(attachmentPointer.data);
|
2015-06-22 23:45:42 +02:00
|
|
|
var contactDetails = contactBuffer.next();
|
|
|
|
while (contactDetails !== undefined) {
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:contact');
|
2015-06-22 23:45:42 +02:00
|
|
|
ev.contactDetails = contactDetails;
|
2015-06-01 23:08:21 +02:00
|
|
|
eventTarget.dispatchEvent(ev);
|
2015-06-22 23:45:42 +02:00
|
|
|
contactDetails = contactBuffer.next();
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
2015-08-06 21:30:51 +02:00
|
|
|
eventTarget.dispatchEvent(new Event('textsecure:contactsync'));
|
2015-06-01 23:08:21 +02:00
|
|
|
});
|
|
|
|
},
|
2015-06-22 23:45:42 +02:00
|
|
|
handleGroups: function(groups) {
|
|
|
|
var eventTarget = this.target;
|
|
|
|
var attachmentPointer = groups.blob;
|
|
|
|
return handleAttachment(attachmentPointer).then(function() {
|
|
|
|
var groupBuffer = new GroupBuffer(attachmentPointer.data);
|
|
|
|
var groupDetails = groupBuffer.next();
|
|
|
|
while (groupDetails !== undefined) {
|
|
|
|
(function(groupDetails) {
|
|
|
|
groupDetails.id = getString(groupDetails.id);
|
|
|
|
textsecure.storage.groups.getGroup(groupDetails.id).
|
|
|
|
then(function(existingGroup) {
|
|
|
|
if (existingGroup === undefined) {
|
|
|
|
return textsecure.storage.groups.createNewGroup(
|
|
|
|
groupDetails.members, groupDetails.id
|
|
|
|
);
|
|
|
|
} else {
|
2015-06-23 02:55:15 +02:00
|
|
|
return textsecure.storage.groups.updateNumbers(
|
|
|
|
groupDetails.id, groupDetails.members
|
|
|
|
);
|
2015-06-22 23:45:42 +02:00
|
|
|
}
|
|
|
|
}).then(function() {
|
2015-07-10 23:29:46 +02:00
|
|
|
var ev = new Event('textsecure:group');
|
2015-06-22 23:45:42 +02:00
|
|
|
ev.groupDetails = groupDetails;
|
|
|
|
eventTarget.dispatchEvent(ev);
|
|
|
|
});
|
|
|
|
})(groupDetails);
|
|
|
|
groupDetails = groupBuffer.next();
|
|
|
|
}
|
|
|
|
});
|
2015-04-29 20:56:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-24 00:41:17 +02:00
|
|
|
textsecure.MessageReceiver = function (url, eventTarget) {
|
|
|
|
var messageReceiver = new MessageReceiver(url, eventTarget);
|
2015-06-17 20:30:29 +02:00
|
|
|
this.getStatus = function() {
|
|
|
|
return messageReceiver.getStatus();
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 20:56:40 +02:00
|
|
|
|
2015-06-17 20:30:29 +02:00
|
|
|
textsecure.MessageReceiver.prototype = {
|
|
|
|
constructor: textsecure.MessageReceiver
|
|
|
|
};
|
2015-06-01 23:08:21 +02:00
|
|
|
|
2015-04-29 20:56:40 +02:00
|
|
|
}());
|