2015-10-20 23:30:39 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
|
|
|
*/
|
2015-11-17 20:00:41 +00:00
|
|
|
function OutgoingMessage(server, timestamp, numbers, message, callback) {
|
|
|
|
this.server = server;
|
2015-10-20 23:30:39 +00:00
|
|
|
this.timestamp = timestamp;
|
|
|
|
this.numbers = numbers;
|
2015-11-17 20:00:41 +00:00
|
|
|
this.message = message; // DataMessage or ContentMessage proto
|
2015-10-20 23:30:39 +00:00
|
|
|
this.callback = callback;
|
2015-11-17 20:00:41 +00:00
|
|
|
this.legacy = (message instanceof textsecure.protobuf.DataMessage);
|
2015-10-20 23:30:39 +00:00
|
|
|
|
|
|
|
this.numbersCompleted = 0;
|
|
|
|
this.errors = [];
|
|
|
|
this.successfulNumbers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
OutgoingMessage.prototype = {
|
|
|
|
constructor: OutgoingMessage,
|
|
|
|
numberCompleted: function() {
|
|
|
|
this.numbersCompleted++;
|
|
|
|
if (this.numbersCompleted >= this.numbers.length) {
|
2015-11-20 00:50:14 +00:00
|
|
|
this.callback({successfulNumbers: this.successfulNumbers, errors: this.errors});
|
2015-10-20 23:30:39 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
registerError: function(number, reason, error) {
|
|
|
|
if (!error || error.name === 'HTTPError') {
|
|
|
|
error = new textsecure.OutgoingMessageError(number, this.message.toArrayBuffer(), this.timestamp, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
error.number = number;
|
|
|
|
error.reason = reason;
|
|
|
|
this.errors[this.errors.length] = error;
|
|
|
|
this.numberCompleted();
|
|
|
|
},
|
|
|
|
reloadDevicesAndSend: function(number, recurse) {
|
|
|
|
return function() {
|
2016-05-04 06:02:35 +00:00
|
|
|
return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) {
|
|
|
|
if (deviceIds.length == 0) {
|
2015-10-20 23:30:39 +00:00
|
|
|
return this.registerError(number, "Got empty device list when loading device keys", null);
|
2015-11-17 20:00:41 +00:00
|
|
|
}
|
2016-05-04 06:02:35 +00:00
|
|
|
return this.doSendMessage(number, deviceIds, recurse);
|
2015-10-20 23:30:39 +00:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
getKeysForNumber: function(number, updateDevices) {
|
|
|
|
var handleResult = function(response) {
|
|
|
|
return Promise.all(response.devices.map(function(device) {
|
2016-04-27 22:21:44 +00:00
|
|
|
device.identityKey = response.identityKey;
|
|
|
|
if (updateDevices === undefined || updateDevices.indexOf(device.deviceId) > -1) {
|
2016-05-14 00:10:48 +00:00
|
|
|
var address = new libsignal.SignalProtocolAddress(number, device.deviceId);
|
2016-05-01 21:46:16 +00:00
|
|
|
var builder = new libsignal.SessionBuilder(textsecure.storage.protocol, address);
|
|
|
|
return builder.processPreKey(device).catch(function(error) {
|
2015-10-20 23:30:39 +00:00
|
|
|
if (error.message === "Identity key changed") {
|
2016-04-27 22:21:44 +00:00
|
|
|
error = new textsecure.OutgoingIdentityKeyError(
|
|
|
|
number, this.message.toArrayBuffer(),
|
|
|
|
this.timestamp, device.identityKey);
|
2015-10-20 23:30:39 +00:00
|
|
|
this.registerError(number, "Identity key changed", error);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}.bind(this));
|
2016-04-27 22:21:44 +00:00
|
|
|
}
|
2015-10-20 23:30:39 +00:00
|
|
|
}.bind(this)));
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
if (updateDevices === undefined) {
|
|
|
|
return this.server.getKeysForNumber(number).then(handleResult);
|
|
|
|
} else {
|
2015-11-02 19:32:23 +00:00
|
|
|
var promise = Promise.resolve();
|
2015-11-04 00:13:19 +00:00
|
|
|
updateDevices.forEach(function(device) {
|
2015-11-02 19:32:23 +00:00
|
|
|
promise = promise.then(function() {
|
2015-11-04 00:13:19 +00:00
|
|
|
return this.server.getKeysForNumber(number, device).then(handleResult);
|
2015-11-02 19:32:23 +00:00
|
|
|
}.bind(this));
|
2015-11-04 00:13:19 +00:00
|
|
|
}.bind(this));
|
2015-10-20 23:30:39 +00:00
|
|
|
|
2015-11-02 19:32:23 +00:00
|
|
|
return promise;
|
2015-10-20 23:30:39 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-17 20:00:41 +00:00
|
|
|
transmitMessage: function(number, jsonData, timestamp) {
|
|
|
|
return this.server.sendMessages(number, jsonData, timestamp).catch(function(e) {
|
|
|
|
if (e.name === 'HTTPError' && (e.code !== 409 && e.code !== 410)) {
|
|
|
|
// 409 and 410 should bubble and be handled by doSendMessage
|
|
|
|
// all other network errors can be retried later.
|
2015-11-29 19:25:27 +00:00
|
|
|
throw new textsecure.SendMessageNetworkError(number, jsonData, e, timestamp);
|
2015-11-17 20:00:41 +00:00
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-05-04 06:02:35 +00:00
|
|
|
doSendMessage: function(number, deviceIds, recurse) {
|
2016-05-01 22:02:55 +00:00
|
|
|
var ciphers = {};
|
|
|
|
var plaintext = this.message.toArrayBuffer();
|
2016-05-04 06:02:35 +00:00
|
|
|
return Promise.all(deviceIds.map(function(deviceId) {
|
|
|
|
var address = new libsignal.SignalProtocolAddress(number, deviceId);
|
2016-05-01 22:02:55 +00:00
|
|
|
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);
|
|
|
|
ciphers[address.getDeviceId()] = sessionCipher;
|
2016-05-04 06:02:35 +00:00
|
|
|
return this.encryptToDevice(address, plaintext, sessionCipher);
|
2016-05-01 22:02:55 +00:00
|
|
|
}.bind(this))).then(function(jsonData) {
|
2015-11-17 20:00:41 +00:00
|
|
|
return this.transmitMessage(number, jsonData, this.timestamp).then(function() {
|
2015-10-20 23:30:39 +00:00
|
|
|
this.successfulNumbers[this.successfulNumbers.length] = number;
|
|
|
|
this.numberCompleted();
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this)).catch(function(error) {
|
|
|
|
if (error instanceof Error && error.name == "HTTPError" && (error.code == 410 || error.code == 409)) {
|
|
|
|
if (!recurse)
|
|
|
|
return this.registerError(number, "Hit retry limit attempting to reload device list", error);
|
|
|
|
|
|
|
|
var p;
|
|
|
|
if (error.code == 409) {
|
2016-05-04 06:31:29 +00:00
|
|
|
p = this.removeDeviceIdsForNumber(number, error.response.extraDevices);
|
2015-10-20 23:30:39 +00:00
|
|
|
} else {
|
|
|
|
p = Promise.all(error.response.staleDevices.map(function(deviceId) {
|
2016-05-01 22:02:55 +00:00
|
|
|
return ciphers[deviceId].closeOpenSessionForDevice();
|
2015-10-20 23:30:39 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.then(function() {
|
|
|
|
var resetDevices = ((error.code == 410) ? error.response.staleDevices : error.response.missingDevices);
|
|
|
|
return this.getKeysForNumber(number, resetDevices)
|
|
|
|
.then(this.reloadDevicesAndSend(number, (error.code == 409)))
|
|
|
|
.catch(function(error) {
|
|
|
|
this.registerError(number, "Failed to reload device keys", error);
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
this.registerError(number, "Failed to create or send message", error);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2016-05-04 06:02:35 +00:00
|
|
|
encryptToDevice: function(address, plaintext, sessionCipher) {
|
2016-05-01 22:02:55 +00:00
|
|
|
return Promise.all([
|
|
|
|
sessionCipher.encrypt(plaintext),
|
|
|
|
sessionCipher.getRemoteRegistrationId()
|
|
|
|
]).then(function(result) {
|
2016-05-04 06:02:35 +00:00
|
|
|
return this.toJSON(address, result[0], result[1]);
|
2016-05-01 22:02:55 +00:00
|
|
|
}.bind(this));
|
2015-11-17 20:00:41 +00:00
|
|
|
},
|
|
|
|
|
2016-05-04 06:02:35 +00:00
|
|
|
toJSON: function(address, encryptedMsg, registrationId) {
|
2015-11-17 20:00:41 +00:00
|
|
|
var json = {
|
|
|
|
type: encryptedMsg.type,
|
2016-05-04 06:02:35 +00:00
|
|
|
destinationDeviceId: address.getDeviceId(),
|
2016-05-01 21:15:51 +00:00
|
|
|
destinationRegistrationId: registrationId
|
2015-11-17 20:00:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var content = btoa(encryptedMsg.body);
|
|
|
|
if (this.legacy) {
|
|
|
|
json.body = content;
|
|
|
|
} else {
|
|
|
|
json.content = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
2016-05-04 06:31:29 +00:00
|
|
|
getStaleDeviceIdsForNumber: function(number) {
|
|
|
|
return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) {
|
|
|
|
if (deviceIds.length === 0) {
|
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
var updateDevices = [];
|
|
|
|
return Promise.all(deviceIds.map(function(deviceId) {
|
|
|
|
var address = new libsignal.SignalProtocolAddress(number, deviceId);
|
|
|
|
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);
|
|
|
|
return sessionCipher.hasOpenSession().then(function(hasSession) {
|
|
|
|
if (!hasSession) {
|
|
|
|
updateDevices.push(deviceId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})).then(function() {
|
|
|
|
return updateDevices;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeDeviceIdsForNumber: function(number, deviceIdsToRemove) {
|
|
|
|
var promise = Promise.resolve();
|
|
|
|
for (var j in deviceIdsToRemove) {
|
|
|
|
promise = promise.then(function() {
|
|
|
|
var encodedNumber = number + "." + deviceIdsToRemove[j];
|
|
|
|
return textsecure.storage.protocol.removeSession(encodedNumber);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
2015-10-20 23:30:39 +00:00
|
|
|
sendToNumber: function(number) {
|
2016-05-04 06:31:29 +00:00
|
|
|
return this.getStaleDeviceIdsForNumber(number).then(function(updateDevices) {
|
2015-11-04 00:13:19 +00:00
|
|
|
return this.getKeysForNumber(number, updateDevices)
|
|
|
|
.then(this.reloadDevicesAndSend(number, true))
|
|
|
|
.catch(function(error) {
|
|
|
|
this.registerError(number, "Failed to retreive new device keys for number " + number, error);
|
2015-10-20 23:30:39 +00:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
};
|