Skip to content

Instantly share code, notes, and snippets.

@brunoaduarte
Forked from PurpShell/monitor.js
Last active July 15, 2024 17:37
Show Gist options
  • Save brunoaduarte/c24d83b9398d194a0d73c832c9e148c2 to your computer and use it in GitHub Desktop.
Save brunoaduarte/c24d83b9398d194a0d73c832c9e148c2 to your computer and use it in GitHub Desktop.
Monitor Socket messages on 2.3000x and above
if (!window.decodeBackStanza) {
window.decodeBackStanza = require("WAWap").decodeStanza;
window.encodeBackStanza = require("WAWap").encodeStanza;
}
function byteArrayToHex(byteArray) {
return Array.from(byteArray).map(byte => byte.toString(16).padStart(2, '0')).join('');
}
function isAscii(byteArray) {
return byteArray.every(byte => byte >= 32 && byte <= 126);
}
function byteArrayToString(byteArray) {
return String.fromCharCode.apply(null, byteArray);
}
function convertToXML(tag, attrs, content) {
const attributes = Object.keys(attrs).map(key => `${key}="${attrs[key]}"`).join(' ');
let contentString = '';
if (Array.isArray(content)) {
contentString = content.map(item => {
if (typeof item === 'object' && item !== null) {
if (item instanceof Uint8Array) {
if (isAscii(item)) {
return byteArrayToString(item);
}
return byteArrayToHex(item);
}
return convertToXML(item.tag, item.attrs || {}, item.content || []);
}
return item;
}).join('');
} else if (content instanceof Uint8Array) {
if (isAscii(content)) {
contentString = byteArrayToString(content);
} else {
contentString = byteArrayToHex(content);
}
} else if (content) {
contentString = content;
}
if (attributes) {
return contentString ? `<${tag} ${attributes}>${contentString}</${tag}>` : `<${tag} ${attributes}/>`;
} else {
return contentString ? `<${tag}>${contentString}</${tag}>` : `<${tag}/>`;
}
}
require("WAWap").decodeStanza = async (e, t) => {
const result = await window.decodeBackStanza(e, t);
const xmlString = convertToXML(result.tag, result.attrs || {}, result.content || []);
console.log('[XML] <- ', xmlString);
return result;
}
require("WAWap").encodeStanza = (...args) => {
const result = window.encodeBackStanza(...args);
const xmlString = convertToXML(args[0].tag, args[0].attrs || {}, args[0].content || []);
console.log('[XML] -> ', xmlString);
return result;
}
@brunoaduarte
Copy link
Author

Print the data in a readable XML format.

@PurpShell
Copy link

Print the data in a readable XML format.

you can simply .toString() it my friend, WAWap nodes have a custom toString

@brunoaduarte
Copy link
Author

brunoaduarte commented Jul 15, 2024

Print the data in a readable XML format.

you can simply .toString() it my friend, WAWap nodes have a custom toString

Perfect @PurpShell . In my case I had to customize it because I need to print the raw keys and encoded data byte arrays in hex string format.

Instead of

<message id="xxxx" to="xxxxxxx@s.whatsapp.net" type="text"><enc v="2" type="msg"><!-- 162 bytes --></enc></message>

I need for example

<message id="xxxx" to="xxxxxxx@s.whatsapp.net" type="text"><enc v="2" type="msg">330a2105be8fe7c2126da10a5afa809e83fde092053d179eeb3d95ec1e90da9f93307d0f100318002270121eef62475570d818dd5c5f82422e7a9553850b570d0cd1e3100bb6d7746942f8a52e531b359da6ff29aee0778db0502fe04bd1ea2af9ae755ac0305e31ba433dab11c0753079099073bfe84b751f3e08169481625167b2b674d2e4a6b1a2893e7af6ccf1da8d4c7a29f19c516d0c916ca6809a50b9b6c7</enc></message>

But in cases where encoded data analysis is not needed, you are correct, it's much simpler.

if (!window.decodeBackStanza) {
  window.decodeBackStanza = require("WAWap").decodeStanza;
  window.encodeBackStanza = require("WAWap").encodeStanza;
}

require("WAWap").decodeStanza = async (e, t) => {
  const result = await window.decodeBackStanza(e, t);
  console.log('[XML] <- ', result.toString());
  return result;
}

require("WAWap").encodeStanza = (...args) => {
  const result = window.encodeBackStanza(...args);
  console.log('[XML] -> ', args[0].toString());
  return result;
}

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment