New websocket protocol

This commit is contained in:
lilia 2014-11-14 17:48:57 -08:00
parent 9364cee578
commit 7f04439b37
6 changed files with 87 additions and 24 deletions

View file

@ -356,19 +356,27 @@ window.textsecure.api = function () {
//TODO: wrap onmessage so that we reconnect on missing pong //TODO: wrap onmessage so that we reconnect on missing pong
socket.onmessage = function(response) { socket.onmessage = function(response) {
try { var blob = response.data;
var message = JSON.parse(response.data); var reader = new FileReader();
} catch (e) { reader.addEventListener("loadend", function() {
console.log('Error parsing server JSON message: ' + response); // reader.result contains the contents of blob as a typed array
return; try {
} var message = textsecure.protobuf.WebSocketMessage.decode(reader.result);
console.log(message);
if (message.type === textsecure.protobuf.WebSocketMessage.Type.REQUEST ) {
socketWrapper.onmessage(message.request);
}
else {
throw "Got invalid message from server: " + message;
}
if ((message.type === undefined && message.id !== undefined) || message.type === 4) { } catch (e) {
socketWrapper.onmessage(message); console.log('Error parsing server JSON message: ' + response);
} return;
else { }
console.log("Got invalid message from server: " + message); });
}
reader.readAsArrayBuffer(blob);
resetKeepAliveTimer(); resetKeepAliveTimer();
}; };

View file

@ -24,13 +24,12 @@
} else { } else {
if (textsecure.registration.isDone()) { if (textsecure.registration.isDone()) {
textsecure.subscribeToPush(function(message) { textsecure.subscribeToPush(function(message) {
Whisper.Threads.addIncomingMessage(message).then(function() { Whisper.Threads.addIncomingMessage(message);
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice + console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
': "' + getString(message.message.body) + '"'); ': "' + getString(message.message.body) + '"');
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1; var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
textsecure.storage.putUnencrypted("unreadCount", newUnreadCount); textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
extension.navigator.setBadgeText(newUnreadCount); extension.navigator.setBadgeText(newUnreadCount);
});
}); });
} }
} }

View file

@ -241,12 +241,19 @@ window.textsecure.subscribeToPush = function(message_callback) {
var socket = textsecure.api.getMessageWebsocket(); var socket = textsecure.api.getMessageWebsocket();
socket.onmessage = function(message) { socket.onmessage = function(message) {
textsecure.protocol.decryptWebsocketMessage(message.message).then(function(plaintext) { textsecure.protocol.decryptWebsocketMessage(message.body).then(function(plaintext) {
var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext); var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext);
// After this point, a) decoding errors are not the server's fault, and // After this point, a) decoding errors are not the server's fault, and
// b) we should handle them gracefully and tell the user they received an invalid message // b) we should handle them gracefully and tell the user they received an invalid message
console.log("Successfully decoded message with id: " + message.id); console.log("Successfully decoded message with id: " + message.id);
socket.send(JSON.stringify({type: 1, id: message.id}));
socket.send(
new textsecure.protobuf.WebSocketMessage({
response: { id: message.id, message: 'OK', status: 200 },
type: textsecure.protobuf.WebSocketMessage.Type.RESPONSE
}).encode().toArrayBuffer()
);
return textsecure.protocol.handleIncomingPushMessageProto(proto).then(function(decrypted) { return textsecure.protocol.handleIncomingPushMessageProto(proto).then(function(decrypted) {
// Delivery receipt // Delivery receipt
if (decrypted === null) if (decrypted === null)

View file

@ -38,7 +38,7 @@
}, },
sendMessage: function(message, attachments) { sendMessage: function(message, attachments) {
encodeAttachments(attachments).then(function(base64_attachments) { return encodeAttachments(attachments).then(function(base64_attachments) {
var timestamp = Date.now(); var timestamp = Date.now();
this.messages().add({ type: 'outgoing', this.messages().add({ type: 'outgoing',
body: message, body: message,
@ -155,7 +155,7 @@
addIncomingMessage: function(decrypted) { addIncomingMessage: function(decrypted) {
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted); var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
thread.receiveMessage(decrypted); return thread.receiveMessage(decrypted);
} }
}))(); }))();
})(); })();

View file

@ -6,6 +6,7 @@
var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto'); var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto');
var protocolMessages = loadProtoBufs('WhisperTextProtocol.proto'); var protocolMessages = loadProtoBufs('WhisperTextProtocol.proto');
var subProtocolMessages = loadProtoBufs('SubProtocol.proto');
var deviceMessages = loadProtoBufs('DeviceMessages.proto'); var deviceMessages = loadProtoBufs('DeviceMessages.proto');
window.textsecure = window.textsecure || {}; window.textsecure = window.textsecure || {};
@ -16,6 +17,9 @@
PreKeyWhisperMessage : protocolMessages.PreKeyWhisperMessage, PreKeyWhisperMessage : protocolMessages.PreKeyWhisperMessage,
DeviceInit : deviceMessages.DeviceInit, DeviceInit : deviceMessages.DeviceInit,
IdentityKey : deviceMessages.IdentityKey, IdentityKey : deviceMessages.IdentityKey,
DeviceControl : deviceMessages.DeviceControl DeviceControl : deviceMessages.DeviceControl,
WebSocketResponseMessage : subProtocolMessages.WebSocketResponseMessage,
WebSocketRequestMessage : subProtocolMessages.WebSocketRequestMessage,
WebSocketMessage : subProtocolMessages.WebSocketMessage
}; };
})(); })();

45
protos/SubProtocol.proto Normal file
View file

@ -0,0 +1,45 @@
/**
* Copyright (C) 2014 Open WhisperSystems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package textsecure;
option java_package = "org.whispersystems.websocket.messages.protobuf";
message WebSocketRequestMessage {
optional string verb = 1;
optional string path = 2;
optional bytes body = 3;
optional uint64 id = 4;
}
message WebSocketResponseMessage {
optional uint64 id = 1;
optional uint32 status = 2;
optional string message = 3;
optional bytes body = 4;
}
message WebSocketMessage {
enum Type {
UNKNOWN = 0;
REQUEST = 1;
RESPONSE = 2;
}
optional Type type = 1;
optional WebSocketRequestMessage request = 2;
optional WebSocketResponseMessage response = 3;
}