Skip to content

Commit

Permalink
Improved outgoing node handling flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer8007 committed Jan 21, 2024
1 parent a0a8a07 commit b2c92d5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
11 changes: 2 additions & 9 deletions core/interception.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,8 @@ wsHook.before = function (originalData, url)
var counter = decryptedFrameInfo.counter;

var realNode = await nodeReaderWriter.decodeStanza(decryptedFrameOriginal, gzipInflate);

var isAllowed = NodeHandler.isSentNodeAllowed(realNode);
var manipulatedNode = deepClone(realNode);
if (!isAllowed)
{
manipulatedNode.tag = "blocked_node";
}

manipulatedNode = await NodeHandler.onSentNode(manipulatedNode);

var [isAllowed, manipulatedNode] = await NodeHandler.interceptOutgoingNode(realNode);
decryptedFrames[i] = {node: manipulatedNode, counter: counter};

if (WAdebugMode || WAPassthroughWithDebug)
Expand Down
99 changes: 57 additions & 42 deletions core/node_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,63 +83,78 @@ NodeHandler.isSentNodeAllowed = function (node)
return true;
}

NodeHandler.onSentNode = async function (node)
NodeHandler.interceptOutgoingNode = async function (node)
{
try
var isAllowed = NodeHandler.isSentNodeAllowed(node);
if (!isAllowed)
{
//
// Check for message nodes
//
if (node.tag == "message")
{
// manipulating a message node
var manipulatedNode = deepClone(node);
manipulatedNode.tag = "blocked_node";
return [isAllowed, manipulatedNode];
}

var childNodes = node.content;
for (var i = 0; i < childNodes.length; i++)
{
var childNode = childNodes[i];
//
// Check for message nodes
//
if (node.tag == "message")
{
// manipulating a message node
node = await NodeHandler.onOutgoingMessageNode(node);
}

if (childNode.tag == "enc")
{
childNodes[i] = await this.onSentMessageNode(childNode, node.attrs["to"].toString());
}
return [true, node];
}

NodeHandler.onOutgoingMessageNode = async function (messageNode)
{
var childNodes = messageNode.content;
for (var i = 0; i < childNodes.length; i++)
{
var childNode = childNodes[i];

// list of devices to which a copy of the message is sent
if (childNode.tag == "participants")
try
{
if (childNode.tag == "enc")
{
childNodes[i] = await this.onSentEncNode(childNode, messageNode.attrs["to"].toString());
}

// list of devices to which a copy of the message is sent
// say, in a message to a group, will contain copies of the message for each group participant.
// in a private chat, will contain copies for the recipient and for the sender's real device
if (childNode.tag == "participants")
{
var participants = childNode.content;
for (var j = 0; j < participants.length; j++)
{
var participants = childNode.content;
for (var j = 0; j < participants.length; j++)
var participant = participants[j];
if (participant.tag != "to") continue;

var encNode = participant.content[0];
if (encNode.tag == "enc")
{
var participant = participants[j];
if (participant.tag != "to") continue;

var messageNode = participant.content[0];
if (messageNode.tag == "enc")
{
var toJID = participant.attrs["jid"] ? participant.attrs["jid"]: participant.attrs["from"];
var toJID = participant.attrs["jid"] ? participant.attrs["jid"]: participant.attrs["from"];

participant = await this.onSentMessageNode(participant, toJID.toString());
participants[j] = participant;
}
encNode = await this.onSentEncNode(encNode, toJID.toString());
participant.content[0] = encNode;
participants[j] = participant;
}
}
}

}

}
catch (exception)
{
console.error("WhatsIncognito: Allowing WA packet due to exception:");
console.error(exception);
console.error(exception.stack);
return node;
catch (exception)
{
console.error("WhatsIncognito: Allowing WA packet due to exception:");
console.error(exception);
console.error(exception.stack);
return [true, messageNode]
}
}

return node;
return messageNode;
}

NodeHandler.onSentMessageNode = async function (messageNode, remoteJid)
NodeHandler.onSentEncNode = async function (encNode, remoteJid)
{
if (remoteJid && isChatBlocked(remoteJid) && autoReceiptOnReplay)
{
Expand All @@ -159,7 +174,7 @@ NodeHandler.onSentMessageNode = async function (messageNode, remoteJid)
// ...
var putBreakpointHere = 1;

return messageNode;
return encNode;
}

NodeHandler.onReceivedNode = async function (node)
Expand Down
6 changes: 3 additions & 3 deletions old/legacy_single_device.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ WACrypto.encryptWithWebCrypto = function(nodeBuffer, isMultiDevice = false, isIn
}
}

NodeHandler.onSentNode = async function (node, isMultiDevice)
NodeHandler.interceptOutgoingNode = async function (node, isMultiDevice)
{
try
{
Expand All @@ -177,7 +177,7 @@ NodeHandler.onSentNode = async function (node, isMultiDevice)
var child = participants[j];
if (child.tag == "message")
{
var messageNode = await this.onSentMessageNode(child, isMultiDevice);
var messageNode = await this.onSentEncNode(child, isMultiDevice);
participants[j] = messageNode;
}
}
Expand All @@ -196,7 +196,7 @@ NodeHandler.onSentNode = async function (node, isMultiDevice)
return node;
}

NodeHandler.onSentMessageNode = async function (messageNode, remoteJid, isMultiDevice)
NodeHandler.onSentEncNode = async function (messageNode, remoteJid, isMultiDevice)
{
if (!isMultiDevice)
{
Expand Down

0 comments on commit b2c92d5

Please sign in to comment.