-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index-2x.js
488 lines (437 loc) · 16 KB
/
index-2x.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*!
* Copyright 2024 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Based on https://github.com/WhiskeySockets/Baileys/tree/master/WAProto
*/
const request = require('request-promise-native');
const acorn = require('acorn');
const walk = require('acorn-walk');
const fs = require('fs/promises');
let whatsAppVersion = 'latest';
const addPrefix = (lines, prefix) => lines.map((line) => prefix + line);
const extractAllExpressions = (node) => {
const expressions = [node];
const exp = node.expression;
if (exp) {
expressions.push(exp);
}
if (node.expression?.expressions?.length) {
for (const exp of node.expression?.expressions) {
expressions.push(...extractAllExpressions(exp));
}
}
return expressions;
};
async function findAppModules() {
const ua = {
headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0',
'Sec-Fetch-Dest': 'script',
'Sec-Fetch-Mode': 'no-cors',
'Sec-Fetch-Site': 'same-origin',
Referer: 'https://web.whatsapp.com/',
Accept: '*/*',
'Accept-Language': 'Accept-Language: en-US,en;q=0.5',
},
};
const baseURL = 'https://web.whatsapp.com';
const serviceworker = await request.get(`${baseURL}/serviceworker.js`, ua);
const versions = [
...serviceworker.matchAll(/assets-manifest-([\d\.]+).json/g),
].map((r) => r[1]);
const version = versions[0];
let bootstrapQRURL = '';
if (version) {
const asset = await request.get(
`${baseURL}/assets-manifest-${version}.json`,
ua
);
const hashFiles = JSON.parse(asset);
const files = Object.keys(hashFiles);
const app = files.find((f) => /^app\./.test(f));
bootstrapQRURL = `${baseURL}/${app}`;
} else {
const index = await request.get(baseURL, ua);
const bootstrapQRID = index.match(/src="\/app.([0-9a-z]{10,}).js"/)[1];
bootstrapQRURL = baseURL + '/app.' + bootstrapQRID + '.js';
}
console.error('Found source JS URL:', bootstrapQRURL);
const qrData = await request.get(bootstrapQRURL, ua);
const waVersion = qrData.match(
/(?:appVersion:|VERSION_STR=)"(\d\.\d+\.\d+)"/
)[1];
console.log('Current version:', waVersion);
whatsAppVersion = waVersion;
// This one list of types is so long that it's split into two JavaScript declarations.
// The module finder below can't handle it, so just patch it manually here.
const patchedQrData = qrData.replace(
't.ActionLinkSpec=void 0,t.TemplateButtonSpec',
't.ActionLinkSpec=t.TemplateButtonSpec'
);
//const patchedQrData = qrData.replace("Spec=void 0,t.", "Spec=t.")
const qrModules =
acorn.parse(patchedQrData).body[0].expression.arguments[0].elements[1]
.properties;
const result = qrModules.filter((m) => {
const hasProto = !!m.value.body.body.find((b) => {
const expressions = extractAllExpressions(b);
return expressions?.find(
(e) => e?.left?.property?.name === 'internalSpec'
);
});
if (hasProto) {
return true;
}
});
return result;
}
(async () => {
const unspecName = (name) =>
name.endsWith('Spec') ? name.slice(0, -4) : name;
const unnestName = (name) => name.split('$').slice(-1)[0];
const getNesting = (name) => name.split('$').slice(0, -1).join('$');
const makeRenameFunc = () => (name) => {
name = unspecName(name);
return name; // .replaceAll('$', '__')
// return renames[name] ?? unnestName(name)
};
// The constructor IDs that can be used for enum types
// const enumConstructorIDs = [76672, 54302]
const modules = await findAppModules();
// Sort modules so that whatsapp module id changes don't change the order in the output protobuf schema
// const modules = []
// for (const mod of wantedModules) {
// modules.push(unsortedModules.find(node => node.key.value === mod))
// }
// find aliases of cross references between the wanted modules
const modulesInfo = {};
const moduleIndentationMap = {};
modules.forEach(({ key, value }) => {
const requiringParam = value.params[2].name;
modulesInfo[key.value] = { crossRefs: [] };
walk.simple(value, {
VariableDeclarator(node) {
if (
node.init &&
node.init.type === 'CallExpression' &&
node.init.callee.name === requiringParam &&
node.init.arguments.length === 1
) {
modulesInfo[key.value].crossRefs.push({
alias: node.id.name,
module: node.init.arguments[0].value,
});
}
},
});
});
// find all identifiers and, for enums, their array of values
for (const mod of modules) {
const modInfo = modulesInfo[mod.key.value];
const rename = makeRenameFunc(mod.key.value);
// all identifiers will be initialized to "void 0" (i.e. "undefined") at the start, so capture them here
walk.ancestor(mod, {
UnaryExpression(node, anc) {
if (!modInfo.identifiers && node.operator === 'void') {
const assignments = [];
let i = 1;
anc.reverse();
while (anc[i].type === 'AssignmentExpression') {
assignments.push(anc[i++].left);
}
const makeBlankIdent = (a) => {
const key = rename(a.property.name);
const indentation = getNesting(key);
const value = { name: key };
moduleIndentationMap[key] = moduleIndentationMap[key] || {};
moduleIndentationMap[key].indentation = indentation;
if (indentation.length) {
moduleIndentationMap[indentation] =
moduleIndentationMap[indentation] || {};
moduleIndentationMap[indentation].members =
moduleIndentationMap[indentation].members || new Set();
moduleIndentationMap[indentation].members.add(key);
}
return [key, value];
};
modInfo.identifiers = Object.fromEntries(
assignments.map(makeBlankIdent).reverse()
);
}
},
});
const enumAliases = {};
// enums are defined directly, and both enums and messages get a one-letter alias
walk.simple(mod, {
VariableDeclarator(node) {
if (
node.init?.type === 'CallExpression' &&
// && enumConstructorIDs.includes(node.init.callee?.arguments?.[0]?.value)
!!node.init.arguments.length &&
node.init.arguments[0].type === 'ObjectExpression' &&
node.init.arguments[0].properties.length
) {
const values = node.init.arguments[0].properties.map((p) => ({
name: p.key.name,
id: p.value.value,
}));
enumAliases[node.id.name] = values;
}
},
AssignmentExpression(node) {
if (
node.left.type === 'MemberExpression' &&
modInfo.identifiers[rename(node.left.property.name)]
) {
const ident = modInfo.identifiers[rename(node.left.property.name)];
ident.alias = node.right.name;
// enumAliases[ident.alias] = enumAliases[ident.alias] || []
ident.enumValues = enumAliases[ident.alias];
console.log(ident.enumValues);
}
},
});
}
// find the contents for all protobuf messages
for (const mod of modules) {
const modInfo = modulesInfo[mod.key.value];
const rename = makeRenameFunc(mod.key.value);
// message specifications are stored in a "internalSpec" attribute of the respective identifier alias
walk.simple(mod, {
AssignmentExpression(node) {
if (
node.left.type === 'MemberExpression' &&
node.left.property.name === 'internalSpec' &&
node.right.type === 'ObjectExpression'
) {
const targetIdent = Object.values(modInfo.identifiers).find(
(v) => v.alias === node.left.object.name
);
if (!targetIdent) {
console.warn(
`found message specification for unknown identifier alias: ${node.left.object.name}`
);
return;
}
// partition spec properties by normal members and constraints (like "__oneofs__") which will be processed afterwards
const constraints = [];
let members = [];
for (const p of node.right.properties) {
p.key.name = p.key.type === 'Identifier' ? p.key.name : p.key.value;
const arr =
p.key.name.substr(0, 2) === '__' ? constraints : members;
arr.push(p);
}
members = members.map(({ key: { name }, value: { elements } }) => {
let type;
const flags = [];
const unwrapBinaryOr = (n) =>
n.type === 'BinaryExpression' && n.operator === '|'
? [].concat(unwrapBinaryOr(n.left), unwrapBinaryOr(n.right))
: [n];
// find type and flags
unwrapBinaryOr(elements[1]).forEach((m) => {
if (
m.type === 'MemberExpression' &&
m.object.type === 'MemberExpression'
) {
if (m.object.property.name === 'TYPES') {
type = m.property.name.toLowerCase();
} else if (m.object.property.name === 'FLAGS') {
flags.push(m.property.name.toLowerCase());
}
}
});
// determine cross reference name from alias if this member has type "message" or "enum"
if (type === 'message' || type === 'enum') {
const currLoc = ` from member '${name}' of message '${targetIdent.name}'`;
if (elements[2].type === 'Identifier') {
type = Object.values(modInfo.identifiers).find(
(v) => v.alias === elements[2].name
)?.name;
if (!type) {
console.warn(
`unable to find reference of alias '${elements[2].name}'` +
currLoc
);
}
} else if (elements[2].type === 'MemberExpression') {
const crossRef = modInfo.crossRefs.find(
(r) => r.alias === elements[2].object.name
);
if (
crossRef &&
modulesInfo[crossRef.module].identifiers[
rename(elements[2].property.name)
]
) {
type = rename(elements[2].property.name);
} else {
console.warn(
`unable to find reference of alias to other module '${elements[2].object.name}' or to message ${elements[2].property.name} of this module` +
currLoc
);
}
}
}
return { name, id: elements[0].value, type, flags };
});
// resolve constraints for members
constraints.forEach((c) => {
if (
c.key.name === '__oneofs__' &&
c.value.type === 'ObjectExpression'
) {
const newOneOfs = c.value.properties.map((p) => ({
name: p.key.name,
type: '__oneof__',
members: p.value.elements.map((e) => {
const idx = members.findIndex((m) => m.name === e.value);
const member = members[idx];
members.splice(idx, 1);
return member;
}),
}));
members.push(...newOneOfs);
}
});
targetIdent.members = members;
}
},
});
}
const decodedProtoMap = {};
const spaceIndent = ' '.repeat(4);
for (const mod of modules) {
const modInfo = modulesInfo[mod.key.value];
const identifiers = Object.values(modInfo.identifiers);
// enum stringifying function
const stringifyEnum = (ident, overrideName = null) =>
[].concat(
[`enum ${overrideName || ident.displayName || ident.name} {`],
addPrefix(
ident.enumValues.map((v) => `${v.name} = ${v.id};`),
spaceIndent
),
['}']
);
// message specification member stringifying function
const stringifyMessageSpecMember = (
info,
completeFlags,
parentName = undefined
) => {
if (info.type === '__oneof__') {
return [].concat(
[`oneof ${info.name} {`],
addPrefix(
[].concat(
...info.members.map((m) => stringifyMessageSpecMember(m, false))
),
spaceIndent
),
['}']
);
} else {
if (info.flags.includes('packed')) {
info.flags.splice(info.flags.indexOf('packed'));
info.packed = ' [packed=true]';
}
if (completeFlags && info.flags.length === 0) {
info.flags.push('optional');
}
const ret = [];
const indentation = moduleIndentationMap[info.type]?.indentation;
let typeName = unnestName(info.type);
if (indentation !== parentName && indentation) {
typeName = `${indentation.replaceAll('$', '.')}.${typeName}`;
}
// if(info.enumValues) {
// // typeName = unnestName(info.type)
// ret = stringifyEnum(info, typeName)
// }
ret.push(
`${
info.flags.join(' ') + (info.flags.length === 0 ? '' : ' ')
}${typeName} ${info.name} = ${info.id}${info.packed || ''};`
);
return ret;
}
};
// message specification stringifying function
const stringifyMessageSpec = (ident) => {
const members = moduleIndentationMap[ident.name]?.members;
const result = [];
result.push(
`message ${ident.displayName || ident.name} {`,
...addPrefix(
[].concat(
...ident.members.map((m) =>
stringifyMessageSpecMember(m, true, ident.name)
)
),
spaceIndent
)
);
if (members?.size) {
const sortedMembers = Array.from(members).sort();
for (const memberName of sortedMembers) {
let entity = modInfo.identifiers[memberName];
if (entity) {
const displayName = entity.name.slice(ident.name.length + 1);
entity = { ...entity, displayName };
result.push(...addPrefix(getEntity(entity), spaceIndent));
} else {
console.log('missing nested entity ', memberName);
}
}
}
result.push('}');
result.push('');
return result;
};
const getEntity = (v) => {
let result;
if (v.members) {
result = stringifyMessageSpec(v);
} else if (v.enumValues?.length) {
result = stringifyEnum(v);
} else {
result = ['// Unknown entity ' + v.name];
}
return result;
};
const stringifyEntity = (v) => {
return {
content: getEntity(v).join('\n'),
name: v.name,
};
};
for (const value of identifiers) {
const { name, content } = stringifyEntity(value);
if (!moduleIndentationMap[name]?.indentation?.length) {
decodedProtoMap[name] = content;
}
}
}
const decodedProto = Object.keys(decodedProtoMap).sort();
const sortedStr = decodedProto.map((d) => decodedProtoMap[d]).join('\n');
const decodedProtoStr = `syntax = "proto2";\npackage waproto;\n\n/// WhatsApp Version: ${whatsAppVersion}\n\n${sortedStr}`;
const destinationPath = 'WAProto.proto';
await fs.writeFile(destinationPath, decodedProtoStr);
console.log(`Extracted protobuf schema to "${destinationPath}"`);
})();