2014-05-14 20:58:12 +02:00
|
|
|
/* vim: ts=4:sw=4
|
|
|
|
*
|
2014-05-04 08:34:13 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-04-04 08:33:02 +02:00
|
|
|
/* START CRAP TO BE DELETED */
|
2014-01-12 08:32:13 +01:00
|
|
|
//TODO: Stolen from MDN (copyright...)
|
|
|
|
function b64ToUint6 (nChr) {
|
|
|
|
|
|
|
|
return nChr > 64 && nChr < 91 ?
|
|
|
|
nChr - 65
|
|
|
|
: nChr > 96 && nChr < 123 ?
|
|
|
|
nChr - 71
|
|
|
|
: nChr > 47 && nChr < 58 ?
|
|
|
|
nChr + 4
|
|
|
|
: nChr === 43 ?
|
|
|
|
62
|
|
|
|
: nChr === 47 ?
|
|
|
|
63
|
|
|
|
:
|
|
|
|
0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function base64DecToArr (sBase64, nBlocksSize) {
|
|
|
|
var
|
|
|
|
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
|
2014-01-22 07:23:41 +01:00
|
|
|
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2;
|
|
|
|
var aBBytes = new ArrayBuffer(nOutLen);
|
|
|
|
var taBytes = new Uint8Array(aBBytes);
|
2014-01-12 08:32:13 +01:00
|
|
|
|
|
|
|
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
|
|
|
|
nMod4 = nInIdx & 3;
|
|
|
|
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
|
|
|
|
if (nMod4 === 3 || nInLen - nInIdx === 1) {
|
|
|
|
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
|
|
|
|
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
|
|
|
|
}
|
|
|
|
nUint24 = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 07:23:41 +01:00
|
|
|
return aBBytes;
|
2014-01-12 08:32:13 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 02:31:15 +01:00
|
|
|
/* Base64 string to array encoding */
|
|
|
|
|
|
|
|
function uint6ToB64 (nUint6) {
|
|
|
|
|
|
|
|
return nUint6 < 26 ?
|
|
|
|
nUint6 + 65
|
|
|
|
: nUint6 < 52 ?
|
|
|
|
nUint6 + 71
|
|
|
|
: nUint6 < 62 ?
|
|
|
|
nUint6 - 4
|
|
|
|
: nUint6 === 62 ?
|
|
|
|
43
|
|
|
|
: nUint6 === 63 ?
|
|
|
|
47
|
|
|
|
:
|
|
|
|
65;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function base64EncArr (aBytes) {
|
|
|
|
|
|
|
|
var nMod3, sB64Enc = "";
|
|
|
|
|
|
|
|
for (var nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
|
|
|
|
nMod3 = nIdx % 3;
|
|
|
|
//if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
|
|
|
|
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
|
|
|
|
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
|
|
|
|
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
|
|
|
|
nUint24 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sB64Enc.replace(/A(?=A$|$)/g, "=");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:33:02 +02:00
|
|
|
/* END CRAP TO BE DELETED */
|
|
|
|
|
2014-01-12 08:32:13 +01:00
|
|
|
/*********************************
|
|
|
|
*** Type conversion utilities ***
|
|
|
|
*********************************/
|
2014-04-04 08:33:02 +02:00
|
|
|
function intToArrayBuffer(nInt) {
|
2014-05-14 08:08:01 +02:00
|
|
|
var res = new ArrayBuffer(16);
|
2014-05-06 05:42:48 +02:00
|
|
|
var thing = new Uint8Array(res);
|
|
|
|
thing[0] = (nInt >> 24) & 0xff;
|
|
|
|
thing[1] = (nInt >> 16) & 0xff;
|
|
|
|
thing[2] = (nInt >> 8 ) & 0xff;
|
|
|
|
thing[3] = (nInt >> 0 ) & 0xff;
|
|
|
|
return res;
|
2014-04-04 08:33:02 +02:00
|
|
|
}
|
|
|
|
|
2014-01-15 08:46:05 +01:00
|
|
|
// Strings/arrays
|
2014-03-06 22:44:59 +01:00
|
|
|
//TODO: Throw all this shit in favor of consistent types
|
2014-01-12 15:07:13 +01:00
|
|
|
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
|
2014-01-22 07:23:41 +01:00
|
|
|
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
2014-03-06 19:18:11 +01:00
|
|
|
var StaticUint8ArrayProto = new Uint8Array().__proto__;
|
|
|
|
var StaticWordArrayProto = CryptoJS.lib.WordArray.create('').__proto__;
|
2014-01-12 08:32:13 +01:00
|
|
|
function getString(thing) {
|
2014-03-06 19:18:11 +01:00
|
|
|
if (thing === Object(thing)) {
|
|
|
|
if (thing.__proto__ == StaticUint8ArrayProto)
|
|
|
|
return String.fromCharCode.apply(null, thing);
|
|
|
|
if (thing.__proto__ == StaticArrayBufferProto)
|
|
|
|
return getString(new Uint8Array(thing));
|
|
|
|
if (thing.__proto__ == StaticByteBufferProto)
|
|
|
|
return thing.toString("binary");
|
|
|
|
if (thing.__proto__ == StaticWordArrayProto)
|
|
|
|
return thing.toString(CryptoJS.enc.Latin1);
|
|
|
|
}
|
2014-01-12 08:32:13 +01:00
|
|
|
return thing;
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:31:15 +01:00
|
|
|
function getStringable(thing) {
|
2014-03-10 23:47:37 +01:00
|
|
|
return (typeof thing == "string" || typeof thing == "number" || typeof thing == "boolean" ||
|
2014-03-06 19:18:11 +01:00
|
|
|
(thing === Object(thing) &&
|
|
|
|
(thing.__proto__ == StaticArrayBufferProto ||
|
|
|
|
thing.__proto__ == StaticUint8ArrayProto ||
|
|
|
|
thing.__proto__ == StaticByteBufferProto ||
|
|
|
|
thing.__proto__ == StaticWordArrayProto)));
|
2014-03-05 02:31:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function toArrayBuffer(thing) {
|
2014-03-06 19:18:11 +01:00
|
|
|
//TODO: Optimize this for specific cases
|
2014-03-05 02:31:15 +01:00
|
|
|
if (thing === undefined)
|
|
|
|
return undefined;
|
2014-03-06 22:44:59 +01:00
|
|
|
if (thing === Object(thing) && thing.__proto__ == StaticArrayBufferProto)
|
|
|
|
return thing;
|
2014-03-10 01:32:00 +01:00
|
|
|
|
|
|
|
if (thing instanceof Array) {
|
|
|
|
// Assuming Uint16Array from curve25519
|
|
|
|
var res = new ArrayBuffer(thing.length * 2);
|
|
|
|
var uint = new Uint16Array(res);
|
|
|
|
for (var i = 0; i < thing.length; i++)
|
|
|
|
uint[i] = thing[i];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:31:15 +01:00
|
|
|
if (!getStringable(thing))
|
2014-05-03 05:16:36 +02:00
|
|
|
throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer");
|
2014-03-05 02:31:15 +01:00
|
|
|
var str = getString(thing);
|
|
|
|
var res = new ArrayBuffer(str.length);
|
|
|
|
var uint = new Uint8Array(res);
|
|
|
|
for (var i = 0; i < str.length; i++)
|
|
|
|
uint[i] = str.charCodeAt(i);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureStringed(thing) {
|
|
|
|
if (getStringable(thing))
|
|
|
|
return getString(thing);
|
|
|
|
else if (thing instanceof Array) {
|
|
|
|
var res = [];
|
|
|
|
for (var i = 0; i < thing.length; i++)
|
2014-03-20 08:20:54 +01:00
|
|
|
res[i] = ensureStringed(thing[i]);
|
2014-03-05 02:31:15 +01:00
|
|
|
return res;
|
|
|
|
} else if (thing === Object(thing)) {
|
|
|
|
var res = {};
|
|
|
|
for (key in thing)
|
|
|
|
res[key] = ensureStringed(thing[key]);
|
|
|
|
return res;
|
|
|
|
}
|
2014-05-03 05:16:36 +02:00
|
|
|
throw new Error("unsure of how to jsonify object of type " + typeof thing);
|
2014-03-05 02:31:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function jsonThing(thing) {
|
|
|
|
return JSON.stringify(ensureStringed(thing));
|
|
|
|
}
|
|
|
|
|
2014-01-22 07:23:41 +01:00
|
|
|
function base64ToArrayBuffer(string) {
|
2014-01-12 08:32:13 +01:00
|
|
|
return base64DecToArr(string);
|
|
|
|
}
|
|
|
|
|
2014-01-15 08:46:05 +01:00
|
|
|
// Protobuf decodingA
|
|
|
|
//TODO: throw on missing fields everywhere
|
2014-01-22 04:28:35 +01:00
|
|
|
var IncomingPushMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/IncomingPushMessageSignal.proto").build("textsecure.IncomingPushMessageSignal");
|
2014-01-15 08:46:05 +01:00
|
|
|
function decodeIncomingPushMessageProtobuf(string) {
|
|
|
|
return IncomingPushMessageProtobuf.decode(btoa(string));
|
|
|
|
}
|
|
|
|
|
2014-01-22 04:28:35 +01:00
|
|
|
var PushMessageContentProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/IncomingPushMessageSignal.proto").build("textsecure.PushMessageContent");
|
2014-01-17 07:08:33 +01:00
|
|
|
function decodePushMessageContentProtobuf(string) {
|
2014-01-15 08:46:05 +01:00
|
|
|
return PushMessageContentProtobuf.decode(btoa(string));
|
|
|
|
}
|
|
|
|
|
2014-01-22 04:28:35 +01:00
|
|
|
var WhisperMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/WhisperTextProtocol.proto").build("textsecure.WhisperMessage");
|
2014-01-15 08:46:05 +01:00
|
|
|
function decodeWhisperMessageProtobuf(string) {
|
|
|
|
return WhisperMessageProtobuf.decode(btoa(string));
|
|
|
|
}
|
|
|
|
|
2014-01-22 04:28:35 +01:00
|
|
|
var PreKeyWhisperMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/WhisperTextProtocol.proto").build("textsecure.PreKeyWhisperMessage");
|
2014-01-15 08:46:05 +01:00
|
|
|
function decodePreKeyWhisperMessageProtobuf(string) {
|
|
|
|
return PreKeyWhisperMessageProtobuf.decode(btoa(string));
|
|
|
|
}
|
|
|
|
|
2014-01-22 04:28:35 +01:00
|
|
|
var KeyExchangeMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/WhisperTextProtocol.proto").build("textsecure.KeyExchangeMessage");
|
2014-01-15 08:46:05 +01:00
|
|
|
function decodeKeyExchangeMessageProtobuf(string) {
|
|
|
|
return KeyExchangeMessageProtobuf.decode(btoa(string));
|
2014-01-12 08:32:13 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 08:46:05 +01:00
|
|
|
// Number formatting
|
2014-01-12 08:32:13 +01:00
|
|
|
function getNumberFromString(string) {
|
|
|
|
return string.split(".")[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEncodedNumber(number) {
|
|
|
|
var split = number.split(".");
|
|
|
|
if (split.length > 1) {
|
|
|
|
if (split[1] == 1)
|
|
|
|
return split[0];
|
|
|
|
else
|
|
|
|
return number;
|
|
|
|
} else
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
2014-03-25 20:27:19 +01:00
|
|
|
function verifyNumber(string) {
|
|
|
|
//TODO: fancy country-code guessing and number verification
|
|
|
|
return getEncodedNumber(string.trim());
|
|
|
|
}
|
|
|
|
|
2014-01-12 08:32:13 +01:00
|
|
|
function getDeviceId(encodedNumber) {
|
|
|
|
var split = encodedNumber.split(".");
|
|
|
|
if (split.length > 1)
|
|
|
|
return split[1];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-01-15 08:46:05 +01:00
|
|
|
// Other
|
|
|
|
|
2014-01-12 15:42:05 +01:00
|
|
|
function timestampToHumanReadable(timestamp) {
|
|
|
|
var date = new Date();
|
|
|
|
date.setTime(timestamp*1000);
|
|
|
|
return date.toUTCString();
|
|
|
|
}
|
|
|
|
|
2014-01-15 08:46:05 +01:00
|
|
|
function objectContainsKeys(object) {
|
|
|
|
var count = 0;
|
|
|
|
for (key in object) {
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return count != 0;
|
|
|
|
}
|
|
|
|
|
2014-01-10 08:48:05 +01:00
|
|
|
/************************************************
|
|
|
|
*** Utilities to store data in local storage ***
|
|
|
|
************************************************/
|
2014-01-17 07:08:33 +01:00
|
|
|
var storage = {};
|
|
|
|
|
2014-01-10 08:48:05 +01:00
|
|
|
storage.putEncrypted = function(key, value) {
|
|
|
|
//TODO
|
2014-01-12 15:07:13 +01:00
|
|
|
if (value === undefined)
|
2014-05-03 05:16:36 +02:00
|
|
|
throw new Error("Tried to store undefined");
|
2014-03-05 02:31:15 +01:00
|
|
|
localStorage.setItem("e" + key, jsonThing(value));
|
2014-01-10 08:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.getEncrypted = function(key, defaultValue) {
|
|
|
|
//TODO
|
|
|
|
var value = localStorage.getItem("e" + key);
|
|
|
|
if (value === null)
|
|
|
|
return defaultValue;
|
2014-01-12 09:49:46 +01:00
|
|
|
return JSON.parse(value);
|
2014-01-10 08:48:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 08:46:05 +01:00
|
|
|
storage.removeEncrypted = function(key) {
|
2014-05-04 08:19:43 +02:00
|
|
|
localStorage.removeItem("e" + key);
|
2014-01-15 08:46:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-10 08:48:05 +01:00
|
|
|
storage.putUnencrypted = function(key, value) {
|
2014-01-12 15:07:13 +01:00
|
|
|
if (value === undefined)
|
2014-05-03 05:16:36 +02:00
|
|
|
throw new Error("Tried to store undefined");
|
2014-03-05 02:31:15 +01:00
|
|
|
localStorage.setItem("u" + key, jsonThing(value));
|
2014-01-10 08:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.getUnencrypted = function(key, defaultValue) {
|
|
|
|
var value = localStorage.getItem("u" + key);
|
|
|
|
if (value === null)
|
|
|
|
return defaultValue;
|
2014-01-12 09:49:46 +01:00
|
|
|
return JSON.parse(value);
|
2014-01-10 08:48:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 08:46:05 +01:00
|
|
|
storage.removeUnencrypted = function(key) {
|
2014-05-04 08:19:43 +02:00
|
|
|
localStorage.removeItem("u" + key);
|
2014-01-15 08:46:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 15:07:13 +01:00
|
|
|
function registrationDone() {
|
|
|
|
storage.putUnencrypted("registration_done", "");
|
2014-01-17 07:08:33 +01:00
|
|
|
//TODO: Fix dirty hack:
|
|
|
|
chrome.runtime.reload();
|
2014-01-12 15:07:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isRegistrationDone() {
|
|
|
|
return storage.getUnencrypted("registration_done") !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageMap() {
|
|
|
|
return storage.getEncrypted("messageMap", {});
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:05:09 +01:00
|
|
|
function storeMessage(messageObject) {
|
2014-01-12 15:07:13 +01:00
|
|
|
var messageMap = getMessageMap();
|
2014-03-26 20:05:09 +01:00
|
|
|
var conversation = messageMap[messageObject.pushMessage.source]; //TODO: Also support Group message IDs here
|
2014-01-12 15:07:13 +01:00
|
|
|
if (conversation === undefined) {
|
2014-05-04 23:07:25 +02:00
|
|
|
conversation = [];
|
2014-03-26 20:05:09 +01:00
|
|
|
messageMap[messageObject.pushMessage.source] = conversation;
|
2014-01-12 15:07:13 +01:00
|
|
|
}
|
2014-01-12 15:07:30 +01:00
|
|
|
|
2014-03-26 20:05:09 +01:00
|
|
|
conversation[conversation.length] = { message: getString(messageObject.message.body),
|
|
|
|
sender: messageObject.pushMessage.source,
|
|
|
|
timestamp: messageObject.pushMessage.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() };
|
2014-01-12 15:07:13 +01:00
|
|
|
storage.putEncrypted("messageMap", messageMap);
|
|
|
|
chrome.runtime.sendMessage(conversation[conversation.length - 1]);
|
|
|
|
}
|
|
|
|
|
2014-01-13 02:52:58 +01:00
|
|
|
function getDeviceObject(encodedNumber) {
|
|
|
|
return storage.getEncrypted("deviceObject" + getEncodedNumber(encodedNumber));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDeviceIdListFromNumber(number) {
|
|
|
|
return storage.getEncrypted("deviceIdList" + getNumberFromString(number), []);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addDeviceIdForNumber(number, deviceId) {
|
|
|
|
var deviceIdList = getDeviceIdListFromNumber(getNumberFromString(number));
|
|
|
|
for (var i = 0; i < deviceIdList.length; i++) {
|
|
|
|
if (deviceIdList[i] == deviceId)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
deviceIdList[deviceIdList.length] = deviceId;
|
|
|
|
storage.putEncrypted("deviceIdList" + getNumberFromString(number), deviceIdList);
|
|
|
|
}
|
|
|
|
|
|
|
|
// throws "Identity key mismatch"
|
|
|
|
function saveDeviceObject(deviceObject) {
|
|
|
|
var existing = getDeviceObject(deviceObject.encodedNumber);
|
|
|
|
if (existing === undefined)
|
|
|
|
existing = {encodedNumber: getEncodedNumber(deviceObject.encodedNumber)};
|
|
|
|
for (key in deviceObject) {
|
|
|
|
if (key == "encodedNumber")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (key == "identityKey" && deviceObject.identityKey != deviceObject.identityKey)
|
2014-05-03 05:16:36 +02:00
|
|
|
throw new Error("Identity key mismatch");
|
2014-01-13 02:52:58 +01:00
|
|
|
|
|
|
|
existing[key] = deviceObject[key];
|
|
|
|
}
|
|
|
|
storage.putEncrypted("deviceObject" + getEncodedNumber(deviceObject.encodedNumber), existing);
|
|
|
|
addDeviceIdForNumber(deviceObject.encodedNumber, getDeviceId(deviceObject.encodedNumber));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDeviceObjectListFromNumber(number) {
|
|
|
|
var deviceObjectList = [];
|
|
|
|
var deviceIdList = getDeviceIdListFromNumber(number);
|
|
|
|
for (var i = 0; i < deviceIdList.length; i++)
|
|
|
|
deviceObjectList[deviceObjectList.length] = getDeviceObject(getNumberFromString(number) + "." + deviceIdList[i]);
|
|
|
|
return deviceObjectList;
|
|
|
|
}
|
|
|
|
|
2014-01-22 07:23:41 +01:00
|
|
|
/**********************
|
|
|
|
*** NaCL Interface ***
|
|
|
|
**********************/
|
2014-05-09 08:00:49 +02:00
|
|
|
var USE_NACL = false;
|
|
|
|
|
2014-01-22 07:23:41 +01:00
|
|
|
var onLoadCallbacks = [];
|
|
|
|
var naclLoaded = 0;
|
|
|
|
function registerOnLoadFunction(func) {
|
2014-03-10 01:32:00 +01:00
|
|
|
if (naclLoaded || !USE_NACL) {
|
2014-01-22 07:23:41 +01:00
|
|
|
func();
|
2014-03-10 01:32:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-22 07:23:41 +01:00
|
|
|
onLoadCallbacks[onLoadCallbacks.length] = func;
|
|
|
|
}
|
|
|
|
|
|
|
|
var naclMessageNextId = 0;
|
|
|
|
var naclMessageIdCallbackMap = {};
|
|
|
|
function moduleDidLoad() {
|
|
|
|
common.hideModule();
|
|
|
|
naclLoaded = 1;
|
|
|
|
for (var i = 0; i < onLoadCallbacks.length; i++)
|
|
|
|
onLoadCallbacks[i]();
|
|
|
|
onLoadCallbacks = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMessage(message) {
|
|
|
|
naclMessageIdCallbackMap[message.data.call_id](message.data);
|
|
|
|
}
|
|
|
|
|
2014-05-09 08:00:49 +02:00
|
|
|
function postNaclMessage(message) {
|
2014-03-10 01:32:00 +01:00
|
|
|
if (!USE_NACL)
|
2014-05-03 05:16:36 +02:00
|
|
|
throw new Error("Attempted to make NaCL call with !USE_NACL?");
|
2014-03-10 01:32:00 +01:00
|
|
|
|
2014-05-09 08:00:49 +02:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
naclMessageIdCallbackMap[naclMessageNextId] = resolve;
|
|
|
|
message.call_id = naclMessageNextId++;
|
2014-01-15 08:46:05 +01:00
|
|
|
|
2014-05-09 08:00:49 +02:00
|
|
|
common.naclModule.postMessage(message);
|
|
|
|
});
|
|
|
|
}
|
2014-01-10 08:48:05 +01:00
|
|
|
|
2014-01-12 09:49:46 +01:00
|
|
|
// message_callback(decoded_protobuf) (use decodeMessage(proto))
|
2014-05-04 23:07:25 +02:00
|
|
|
var subscribeToPushMessageSemaphore = 0;
|
2014-01-12 08:32:13 +01:00
|
|
|
function subscribeToPush(message_callback) {
|
2014-05-04 23:07:25 +02:00
|
|
|
subscribeToPushMessageSemaphore++;
|
2014-03-26 20:05:09 +01:00
|
|
|
if (subscribeToPushMessageSemaphore <= 0)
|
|
|
|
return;
|
|
|
|
|
2014-01-12 08:32:13 +01:00
|
|
|
var user = storage.getUnencrypted("number_id");
|
|
|
|
var password = storage.getEncrypted("password");
|
2014-03-25 20:27:19 +01:00
|
|
|
var URL = URL_BASE.replace(/^http:/g, "ws:").replace(/^https:/g, "wss:") + URL_CALLS['push'] + "/?user=%2B" + getString(user).substring(1) + "&password=" + getString(password);
|
|
|
|
var socket = new WebSocket(URL);
|
|
|
|
|
2014-04-04 10:47:04 +02:00
|
|
|
var pingInterval;
|
|
|
|
|
2014-03-25 20:27:19 +01:00
|
|
|
//TODO: GUI
|
|
|
|
socket.onerror = function(socketEvent) {
|
|
|
|
console.log('Server is down :(');
|
2014-04-04 10:47:04 +02:00
|
|
|
clearInterval(pingInterval);
|
2014-05-04 23:07:25 +02:00
|
|
|
subscribeToPushMessageSemaphore--;
|
2014-04-04 10:47:04 +02:00
|
|
|
setTimeout(function() { subscribeToPush(message_callback); }, 60000);
|
2014-03-25 20:27:19 +01:00
|
|
|
};
|
|
|
|
socket.onclose = function(socketEvent) {
|
|
|
|
console.log('Server closed :(');
|
2014-04-04 10:47:04 +02:00
|
|
|
clearInterval(pingInterval);
|
2014-05-04 23:07:25 +02:00
|
|
|
subscribeToPushMessageSemaphore--;
|
2014-04-04 10:47:04 +02:00
|
|
|
setTimeout(function() { subscribeToPush(message_callback); }, 60000);
|
2014-03-25 20:27:19 +01:00
|
|
|
};
|
|
|
|
socket.onopen = function(socketEvent) {
|
|
|
|
console.log('Connected to server!');
|
2014-04-04 10:47:04 +02:00
|
|
|
pingInterval = setInterval(function() { console.log("Sending server ping message."); socket.send(JSON.stringify({type: 2})); }, 30000);
|
2014-03-25 20:27:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onmessage = function(response) {
|
|
|
|
try {
|
2014-03-26 20:05:09 +01:00
|
|
|
var message = JSON.parse(response.data);
|
2014-03-25 20:27:19 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log('Error parsing server JSON message: ' + response.responseBody.split("|")[1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-04 10:47:04 +02:00
|
|
|
if (message.type == 3) {
|
|
|
|
console.log("Got pong message");
|
|
|
|
} else if (message.type === undefined && message.id !== undefined) {
|
2014-04-04 08:33:02 +02:00
|
|
|
crypto.decryptWebsocketMessage(message.message).then(function(plaintext) {
|
2014-05-13 21:15:45 +02:00
|
|
|
var proto = decodeIncomingPushMessageProtobuf(getString(plaintext));
|
2014-04-04 10:47:04 +02:00
|
|
|
// 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
|
2014-04-04 11:50:26 +02:00
|
|
|
console.log("Successfully decoded message with id: " + message.id);
|
2014-04-04 10:47:04 +02:00
|
|
|
socket.send(JSON.stringify({type: 1, id: message.id}));
|
2014-05-14 08:08:01 +02:00
|
|
|
return crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
2014-05-04 23:07:25 +02:00
|
|
|
storeMessage(decrypted);
|
2014-04-04 10:47:04 +02:00
|
|
|
message_callback(decrypted);
|
2014-05-14 08:08:01 +02:00
|
|
|
})
|
2014-04-04 08:33:02 +02:00
|
|
|
}).catch(function(e) {
|
2014-05-14 08:08:01 +02:00
|
|
|
console.log("Error handling incoming message: ");
|
|
|
|
console.log(e);
|
2014-04-04 08:33:02 +02:00
|
|
|
});
|
2014-03-25 20:27:19 +01:00
|
|
|
}
|
|
|
|
};
|
2014-01-11 08:58:58 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 08:32:13 +01:00
|
|
|
// success_callback(identity_key), error_callback(error_msg)
|
2014-05-13 21:15:45 +02:00
|
|
|
function getKeysForNumber(number) {
|
|
|
|
return API.getKeysForNumber(number).then(function(response) {
|
|
|
|
for (var i = 0; i < response.length; i++) {
|
|
|
|
saveDeviceObject({
|
|
|
|
encodedNumber: number + "." + response[i].deviceId,
|
|
|
|
identityKey: response[i].identityKey,
|
|
|
|
publicKey: response[i].publicKey,
|
|
|
|
preKeyId: response[i].keyId,
|
|
|
|
registrationId: response[i].registrationId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return response[0].identityKey;
|
|
|
|
});
|
2014-01-10 08:48:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-12 08:32:13 +01:00
|
|
|
// success_callback(server success/failure map), error_callback(error_msg)
|
2014-03-09 01:15:36 +01:00
|
|
|
// message == PushMessageContentProto (NOT STRING)
|
2014-03-25 20:27:19 +01:00
|
|
|
function sendMessageToDevices(number, deviceObjectList, message, success_callback, error_callback) {
|
2014-01-12 08:32:13 +01:00
|
|
|
var jsonData = [];
|
2014-03-25 20:27:19 +01:00
|
|
|
var relay = undefined;
|
2014-05-13 10:40:29 +02:00
|
|
|
var promises = [];
|
2014-03-25 20:27:19 +01:00
|
|
|
|
2014-05-13 10:40:29 +02:00
|
|
|
var addEncryptionFor = function(i) {
|
|
|
|
return crypto.encryptMessageFor(deviceObjectList[i], message).then(function(encryptedMsg) {
|
2014-03-25 20:27:19 +01:00
|
|
|
jsonData[i] = {
|
|
|
|
type: encryptedMsg.type,
|
|
|
|
destination: deviceObjectList[i].encodedNumber,
|
2014-05-02 17:59:01 +02:00
|
|
|
destinationRegistrationId: deviceObjectList[i].registrationId,
|
2014-03-25 20:27:19 +01:00
|
|
|
body: encryptedMsg.body,
|
|
|
|
timestamp: new Date().getTime()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (deviceObjectList[i].relay !== undefined) {
|
|
|
|
jsonData[i].relay = deviceObjectList[i].relay;
|
|
|
|
if (relay === undefined)
|
|
|
|
relay = jsonData[i].relay;
|
2014-05-13 10:40:29 +02:00
|
|
|
else if (relay != jsonData[i].relay)
|
|
|
|
throw new Error("Mismatched relays for number " + number);
|
2014-01-13 02:52:58 +01:00
|
|
|
} else {
|
2014-03-25 20:27:19 +01:00
|
|
|
if (relay === undefined)
|
|
|
|
relay = "";
|
2014-05-13 10:40:29 +02:00
|
|
|
else if (relay != "")
|
|
|
|
throw new Error("Mismatched relays for number " + number);
|
2014-01-13 02:52:58 +01:00
|
|
|
}
|
2014-03-25 20:27:19 +01:00
|
|
|
});
|
|
|
|
}
|
2014-05-13 10:40:29 +02:00
|
|
|
for (var i = 0; i < deviceObjectList.length; i++)
|
|
|
|
promises[i] = addEncryptionFor(i);
|
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
return API.sendMessages(number, jsonData);
|
|
|
|
});
|
2014-01-10 08:48:05 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 20:27:19 +01:00
|
|
|
// callback(success/failure map, see code)
|
|
|
|
// message == PushMessageContentProto (NOT STRING)
|
|
|
|
function sendMessageToNumbers(numbers, message, callback) {
|
|
|
|
var numbersCompleted = 0;
|
|
|
|
var errors = [];
|
|
|
|
var successfulNumbers = [];
|
|
|
|
|
|
|
|
var numberCompleted = function() {
|
|
|
|
numbersCompleted++;
|
|
|
|
if (numbersCompleted >= numbers.length)
|
|
|
|
callback({success: successfulNumbers, failure: errors});
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:15:45 +02:00
|
|
|
var registerError = function(number, message, error) {
|
|
|
|
errors[errors.length] = { number: number, reason: message, error: error };
|
2014-03-25 20:27:19 +01:00
|
|
|
numberCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
var doSendMessage = function(number, devicesForNumber, message) {
|
2014-05-13 10:40:29 +02:00
|
|
|
return sendMessageToDevices(number, devicesForNumber, message).then(function(result) {
|
2014-03-25 20:27:19 +01:00
|
|
|
successfulNumbers[successfulNumbers.length] = number;
|
|
|
|
numberCompleted();
|
2014-05-13 10:40:29 +02:00
|
|
|
}).catch(function(error) {
|
|
|
|
if (error instanceof Error && error.name == "HTTPError" && (error.message == 410 || error.message == 409)) {
|
|
|
|
//TODO: Re-request keys for number here
|
|
|
|
}
|
2014-05-13 21:15:45 +02:00
|
|
|
registerError(number, "Failed to create or send message", error);
|
2014-03-25 20:27:19 +01:00
|
|
|
});
|
|
|
|
}
|
2014-01-13 02:52:58 +01:00
|
|
|
|
2014-01-12 08:32:13 +01:00
|
|
|
for (var i = 0; i < numbers.length; i++) {
|
2014-03-26 20:05:09 +01:00
|
|
|
var number = numbers[i];
|
|
|
|
var devicesForNumber = getDeviceObjectListFromNumber(number);
|
2014-01-13 02:52:58 +01:00
|
|
|
|
|
|
|
if (devicesForNumber.length == 0) {
|
2014-05-13 21:15:45 +02:00
|
|
|
getKeysForNumber(number).then(function(identity_key) {
|
|
|
|
devicesForNumber = getDeviceObjectListFromNumber(number);
|
|
|
|
if (devicesForNumber.length == 0)
|
|
|
|
registerError(number, "Failed to retreive new device keys for number " + number, null);
|
|
|
|
else
|
|
|
|
doSendMessage(number, devicesForNumber, message);
|
|
|
|
}).catch(function(error) {
|
|
|
|
registerError(number, "Failed to retreive new device keys for number " + number, error);
|
|
|
|
});
|
2014-03-25 20:27:19 +01:00
|
|
|
} else
|
2014-03-26 20:05:09 +01:00
|
|
|
doSendMessage(number, devicesForNumber, message);
|
2014-01-12 08:32:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestIdentityPrivKeyFromMasterDevice(number, identityKey) {
|
2014-01-12 09:49:46 +01:00
|
|
|
sendMessageToDevices([getDeviceObject(getNumberFromString(number)) + ".1"],
|
|
|
|
{message: "Identity Key request"}, function() {}, function() {});//TODO
|
2014-01-10 08:48:05 +01:00
|
|
|
}
|
2014-01-22 07:23:41 +01:00
|
|
|
|