From 86132a38a8cda365c2be50a85eb0fd77357e3732 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 2 Feb 2016 13:24:29 -0800 Subject: [PATCH] Log envelopes and session end events Help debug bad session errors by logging some envelope info about the message we are about to decrypt. With this, if there is a decryption error (e.g., bad mac or no session) it is clear from the logs what number and device message sent the bad message. Also log when we send and receive end session messages and when we close sessions for certain devices. // FREEBIE --- js/libtextsecure.js | 13 +++++++++++-- libtextsecure/message_receiver.js | 11 +++++++++-- libtextsecure/sendmessage.js | 2 ++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 016e508d..402ab25b 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -36902,6 +36902,7 @@ MessageReceiver.prototype.extend({ this.pending = this.pending.then(handleEnvelope, handleEnvelope); }, handleEnvelope: function(envelope) { + console.log('envelope from', envelope.source + '.' + envelope.sourceDevice, envelope.timestamp.toNumber()); if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) { return this.onDeliveryReceipt(envelope); } else if (envelope.content) { @@ -36957,9 +36958,12 @@ MessageReceiver.prototype.extend({ }.bind(this)); }, handleDataMessage: function(envelope, message, close_session) { - console.log('data message from', envelope.source + '.' + envelope.sourceDevice, envelope.timestamp.toNumber()); + var encodedNumber = envelope.source + '.' + envelope.sourceDevice; + console.log('data message from', encodedNumber, envelope.timestamp.toNumber()); if ((message.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION) == textsecure.protobuf.DataMessage.Flags.END_SESSION ) { + console.log('got end session'); + console.log('closing session for device', encodedNumber); close_session(); } return this.processDecrypted(message, envelope.source).then(function(message) { @@ -37096,8 +37100,11 @@ MessageReceiver.prototype.extend({ if ((finalMessage.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION) == textsecure.protobuf.DataMessage.Flags.END_SESSION && - finalMessage.sync !== null) + finalMessage.sync !== null) { + console.log('got end session'); res[1](); + console.log('session closed for device', from); + } return this.processDecrypted(finalMessage); }.bind(this)); @@ -37545,12 +37552,14 @@ MessageSender.prototype = { }, closeSession: function(number, timestamp) { + console.log('sending end session'); var proto = new textsecure.protobuf.DataMessage(); proto.body = "TERMINATE"; proto.flags = textsecure.protobuf.DataMessage.Flags.END_SESSION; return this.sendIndividualProto(number, proto, timestamp).then(function(res) { return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { return Promise.all(devices.map(function(device) { + console.log('closing session for', device.encodedNumber); return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber); })).then(function() { return res; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index a00c4b6a..7240452d 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -85,6 +85,7 @@ MessageReceiver.prototype.extend({ this.pending = this.pending.then(handleEnvelope, handleEnvelope); }, handleEnvelope: function(envelope) { + console.log('envelope from', envelope.source + '.' + envelope.sourceDevice, envelope.timestamp.toNumber()); if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) { return this.onDeliveryReceipt(envelope); } else if (envelope.content) { @@ -140,9 +141,12 @@ MessageReceiver.prototype.extend({ }.bind(this)); }, handleDataMessage: function(envelope, message, close_session) { - console.log('data message from', envelope.source + '.' + envelope.sourceDevice, envelope.timestamp.toNumber()); + var encodedNumber = envelope.source + '.' + envelope.sourceDevice; + console.log('data message from', encodedNumber, envelope.timestamp.toNumber()); if ((message.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION) == textsecure.protobuf.DataMessage.Flags.END_SESSION ) { + console.log('got end session'); + console.log('closing session for device', encodedNumber); close_session(); } return this.processDecrypted(message, envelope.source).then(function(message) { @@ -279,8 +283,11 @@ MessageReceiver.prototype.extend({ if ((finalMessage.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION) == textsecure.protobuf.DataMessage.Flags.END_SESSION && - finalMessage.sync !== null) + finalMessage.sync !== null) { + console.log('got end session'); res[1](); + console.log('session closed for device', from); + } return this.processDecrypted(finalMessage); }.bind(this)); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index eaee8a40..f617b098 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -153,12 +153,14 @@ MessageSender.prototype = { }, closeSession: function(number, timestamp) { + console.log('sending end session'); var proto = new textsecure.protobuf.DataMessage(); proto.body = "TERMINATE"; proto.flags = textsecure.protobuf.DataMessage.Flags.END_SESSION; return this.sendIndividualProto(number, proto, timestamp).then(function(res) { return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { return Promise.all(devices.map(function(device) { + console.log('closing session for', device.encodedNumber); return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber); })).then(function() { return res;