From 7f04439b37309e32c90abb4574a162343fb57f2e Mon Sep 17 00:00:00 2001 From: lilia Date: Fri, 14 Nov 2014 17:48:57 -0800 Subject: [PATCH] New websocket protocol --- js/api.js | 32 +++++++++++++++++----------- js/background.js | 13 ++++++------ js/helpers.js | 11 ++++++++-- js/models/threads.js | 4 ++-- js/protobufs.js | 6 +++++- protos/SubProtocol.proto | 45 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 protos/SubProtocol.proto diff --git a/js/api.js b/js/api.js index 2f68ca566ff8..ecf1463916dc 100644 --- a/js/api.js +++ b/js/api.js @@ -356,19 +356,27 @@ window.textsecure.api = function () { //TODO: wrap onmessage so that we reconnect on missing pong socket.onmessage = function(response) { - try { - var message = JSON.parse(response.data); - } catch (e) { - console.log('Error parsing server JSON message: ' + response); - return; - } + var blob = response.data; + var reader = new FileReader(); + reader.addEventListener("loadend", function() { + // reader.result contains the contents of blob as a typed array + 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) { - socketWrapper.onmessage(message); - } - else { - console.log("Got invalid message from server: " + message); - } + } catch (e) { + console.log('Error parsing server JSON message: ' + response); + return; + } + }); + + reader.readAsArrayBuffer(blob); resetKeepAliveTimer(); }; diff --git a/js/background.js b/js/background.js index 566ff9caea7e..1595971acc89 100644 --- a/js/background.js +++ b/js/background.js @@ -24,13 +24,12 @@ } else { if (textsecure.registration.isDone()) { textsecure.subscribeToPush(function(message) { - Whisper.Threads.addIncomingMessage(message).then(function() { - console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice + - ': "' + getString(message.message.body) + '"'); - var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1; - textsecure.storage.putUnencrypted("unreadCount", newUnreadCount); - extension.navigator.setBadgeText(newUnreadCount); - }); + Whisper.Threads.addIncomingMessage(message); + console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice + + ': "' + getString(message.message.body) + '"'); + var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1; + textsecure.storage.putUnencrypted("unreadCount", newUnreadCount); + extension.navigator.setBadgeText(newUnreadCount); }); } } diff --git a/js/helpers.js b/js/helpers.js index 8f5eed2a87d9..95375979ed0b 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -241,12 +241,19 @@ window.textsecure.subscribeToPush = function(message_callback) { var socket = textsecure.api.getMessageWebsocket(); 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); // 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 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) { // Delivery receipt if (decrypted === null) diff --git a/js/models/threads.js b/js/models/threads.js index 6396702383f8..255bb37c0a04 100644 --- a/js/models/threads.js +++ b/js/models/threads.js @@ -38,7 +38,7 @@ }, sendMessage: function(message, attachments) { - encodeAttachments(attachments).then(function(base64_attachments) { + return encodeAttachments(attachments).then(function(base64_attachments) { var timestamp = Date.now(); this.messages().add({ type: 'outgoing', body: message, @@ -155,7 +155,7 @@ addIncomingMessage: function(decrypted) { var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted); - thread.receiveMessage(decrypted); + return thread.receiveMessage(decrypted); } }))(); })(); diff --git a/js/protobufs.js b/js/protobufs.js index 173e42089417..ec620b976c0b 100644 --- a/js/protobufs.js +++ b/js/protobufs.js @@ -6,6 +6,7 @@ var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto'); var protocolMessages = loadProtoBufs('WhisperTextProtocol.proto'); + var subProtocolMessages = loadProtoBufs('SubProtocol.proto'); var deviceMessages = loadProtoBufs('DeviceMessages.proto'); window.textsecure = window.textsecure || {}; @@ -16,6 +17,9 @@ PreKeyWhisperMessage : protocolMessages.PreKeyWhisperMessage, DeviceInit : deviceMessages.DeviceInit, IdentityKey : deviceMessages.IdentityKey, - DeviceControl : deviceMessages.DeviceControl + DeviceControl : deviceMessages.DeviceControl, + WebSocketResponseMessage : subProtocolMessages.WebSocketResponseMessage, + WebSocketRequestMessage : subProtocolMessages.WebSocketRequestMessage, + WebSocketMessage : subProtocolMessages.WebSocketMessage }; })(); diff --git a/protos/SubProtocol.proto b/protos/SubProtocol.proto new file mode 100644 index 000000000000..0fc5ce75d969 --- /dev/null +++ b/protos/SubProtocol.proto @@ -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 . + */ +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; +} \ No newline at end of file