whalibmob 5.1.21 → 5.5.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -83
- package/cli.js +2 -2
- package/index.js +4 -2
- package/lib/Client.js +19 -0
- package/lib/DeviceConfig.js +51 -36
- package/lib/DeviceManager.js +414 -33
- package/lib/MediaService.js +1 -1
- package/lib/Registration.js +102 -97
- package/lib/Store.js +2 -2
- package/lib/constants.js +116 -101
- package/lib/messages/MessageSender.js +51 -5
- package/lib/noise.js +5 -8
- package/lib/signal/SignalStore.js +20 -2
- package/package.json +1 -1
package/lib/DeviceManager.js
CHANGED
|
@@ -18,6 +18,28 @@ function makeDeviceJid(user, device, server) {
|
|
|
18
18
|
return `${user}:${device}@${server}`;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
// Convert a JID string like "user@server" or "user:device@server" into the
|
|
22
|
+
// BinaryNode JID object format so _writeJid() emits proper binary JID_PAIR /
|
|
23
|
+
// AD_JID bytes instead of raw UTF-8. The server requires binary JID encoding
|
|
24
|
+
// for routing attributes — raw UTF-8 strings are silently ignored for @lid JIDs.
|
|
25
|
+
function jidStrToObj(jidStr) {
|
|
26
|
+
const str = typeof jidStr === 'string' ? jidStr : String(jidStr);
|
|
27
|
+
const at = str.indexOf('@');
|
|
28
|
+
const raw = at >= 0 ? str.slice(0, at) : str;
|
|
29
|
+
const server = at >= 0 ? str.slice(at + 1) : 's.whatsapp.net';
|
|
30
|
+
const colon = raw.indexOf(':');
|
|
31
|
+
const user = colon >= 0 ? raw.slice(0, colon) : raw;
|
|
32
|
+
const device = colon >= 0 ? (parseInt(raw.slice(colon + 1), 10) || 0) : 0;
|
|
33
|
+
const self = str;
|
|
34
|
+
// AD_JID binary format requires both agent AND device fields — use only for
|
|
35
|
+
// multi-device @s.whatsapp.net JIDs where device > 0.
|
|
36
|
+
if (server === 's.whatsapp.net' && device > 0) {
|
|
37
|
+
return { user, agent: 0, device, server, toString() { return self; } };
|
|
38
|
+
}
|
|
39
|
+
// JID_PAIR: used for @lid, @g.us, and primary (device-0) @s.whatsapp.net
|
|
40
|
+
return { user, server, toString() { return self; } };
|
|
41
|
+
}
|
|
42
|
+
|
|
21
43
|
function phoneFromJid(jid) {
|
|
22
44
|
const { user } = stripUser(jid);
|
|
23
45
|
return user;
|
|
@@ -119,30 +141,70 @@ class DeviceManager {
|
|
|
119
141
|
|
|
120
142
|
// ─── Fetch pre-key bundles for a list of JIDs via encrypt IQ ───────────────
|
|
121
143
|
// Only called for JIDs where no Signal session exists yet.
|
|
122
|
-
// Returns Map<
|
|
144
|
+
// Returns Map<string_jid, bundle> — keys are always normalised strings.
|
|
123
145
|
async fetchBundles(jids) {
|
|
124
146
|
if (!jids || jids.length === 0) return new Map();
|
|
125
147
|
|
|
126
|
-
|
|
148
|
+
// Convert a string JID like "user@server" or "user:device@server" into
|
|
149
|
+
// a proper JID object so the binary encoder uses JID_PAIR / AD_JID format.
|
|
150
|
+
// Raw UTF-8 strings are NOT recognised by the WA server for @lid JIDs — the
|
|
151
|
+
// server silently ignores unknown JIDs and returns an empty bundle list.
|
|
152
|
+
const toJidObj = jidStr => {
|
|
153
|
+
const str = typeof jidStr === 'string' ? jidStr : String(jidStr);
|
|
154
|
+
const at = str.indexOf('@');
|
|
155
|
+
const raw = at >= 0 ? str.slice(0, at) : str;
|
|
156
|
+
const server = at >= 0 ? str.slice(at + 1) : 's.whatsapp.net';
|
|
157
|
+
const colon = raw.indexOf(':');
|
|
158
|
+
const user = colon >= 0 ? raw.slice(0, colon) : raw;
|
|
159
|
+
const device = colon >= 0 ? (parseInt(raw.slice(colon + 1), 10) || 0) : 0;
|
|
160
|
+
const self = str;
|
|
161
|
+
// AD_JID binary format is for @s.whatsapp.net with device > 0.
|
|
162
|
+
// JID_PAIR binary format is used for @lid and for device-0 @s.whatsapp.net.
|
|
163
|
+
if (server === 's.whatsapp.net' && device > 0) {
|
|
164
|
+
// AD_JID: agent=0, device=N
|
|
165
|
+
return { user, agent: 0, device, server, toString() { return self; } };
|
|
166
|
+
}
|
|
167
|
+
// JID_PAIR: user@server — server may be 'lid' or 's.whatsapp.net'
|
|
168
|
+
return { user, server, toString() { return self; } };
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const userNodes = jids.map(jid => new BinaryNode('user', { jid: toJidObj(jid) }, null));
|
|
127
172
|
const iqId = this._client._genMsgId();
|
|
128
173
|
const iqNode = new BinaryNode('iq',
|
|
129
174
|
{ id: iqId, xmlns: 'encrypt', type: 'get', to: 's.whatsapp.net' },
|
|
130
175
|
[new BinaryNode('key', {}, userNodes)]
|
|
131
176
|
);
|
|
132
177
|
|
|
178
|
+
process.stderr.write('[DBG] FETCH_BUNDLES jids=[' + jids.join(',') + ']\n');
|
|
179
|
+
|
|
133
180
|
const response = await this._client._sendIq(iqNode);
|
|
134
181
|
const bundles = new Map();
|
|
135
|
-
if (!response)
|
|
182
|
+
if (!response) {
|
|
183
|
+
process.stderr.write('[DBG] FETCH_BUNDLES_NULL\n');
|
|
184
|
+
return bundles;
|
|
185
|
+
}
|
|
136
186
|
|
|
137
187
|
const listNode = findChild(response, 'list');
|
|
138
188
|
const children = listNode ? listNode.content : (response.content || []);
|
|
189
|
+
|
|
190
|
+
process.stderr.write('[DBG] FETCH_BUNDLES_RESP childCount=' +
|
|
191
|
+
(Array.isArray(children) ? children.length : 0) + '\n');
|
|
192
|
+
|
|
139
193
|
for (const userNode of (Array.isArray(children) ? children : [])) {
|
|
140
194
|
if (!userNode || userNode.description !== 'user') continue;
|
|
141
|
-
const
|
|
142
|
-
if (!
|
|
195
|
+
const jidRaw = userNode.attrs && userNode.attrs.jid;
|
|
196
|
+
if (!jidRaw) continue;
|
|
197
|
+
// Always normalise to a string key so Map lookups are consistent
|
|
198
|
+
const jidStr = String(jidRaw);
|
|
199
|
+
process.stderr.write('[DBG] FETCH_BUNDLES_USER jid=' + jidStr +
|
|
200
|
+
' childTags=' + (Array.isArray(userNode.content)
|
|
201
|
+
? userNode.content.map(c => c && c.description).filter(Boolean).join(',')
|
|
202
|
+
: '') + '\n');
|
|
143
203
|
const bundle = parseBundleFromUserNode(userNode);
|
|
144
|
-
if (bundle) bundles.set(
|
|
204
|
+
if (bundle) bundles.set(jidStr, bundle);
|
|
205
|
+
else process.stderr.write('[DBG] FETCH_BUNDLES_NO_BUNDLE jid=' + jidStr + '\n');
|
|
145
206
|
}
|
|
207
|
+
process.stderr.write('[DBG] FETCH_BUNDLES_DONE count=' + bundles.size + '\n');
|
|
146
208
|
return bundles;
|
|
147
209
|
}
|
|
148
210
|
|
|
@@ -198,54 +260,158 @@ class DeviceManager {
|
|
|
198
260
|
return jids.length > 0 ? jids : phones.map(p => makeDeviceJid(p, 0));
|
|
199
261
|
}
|
|
200
262
|
|
|
201
|
-
//
|
|
263
|
+
// ─── FIXED usync IQ — corrects 4 bugs in the original implementation ────────
|
|
264
|
+
//
|
|
265
|
+
// Bug 1 — Wrong <list> node format:
|
|
266
|
+
// Was: <user><contact>+phone</contact></user>
|
|
267
|
+
// Fix: <user jid="phone@s.whatsapp.net"/> (jid ATTRIBUTE, not contact child)
|
|
268
|
+
//
|
|
269
|
+
// Bug 2 — Wrong <query><devices> node:
|
|
270
|
+
// Was: <devices version="2"><device jid="phone@s.whatsapp.net"/></devices>
|
|
271
|
+
// Fix: <devices version="2"/> (NO device children in query)
|
|
272
|
+
//
|
|
273
|
+
// Bug 3 — Missing <side_list/> node:
|
|
274
|
+
// Was: (absent)
|
|
275
|
+
// Fix: <side_list/> (required for LID device discovery)
|
|
276
|
+
//
|
|
277
|
+
// Bug 4 — Wrong response parser:
|
|
278
|
+
// Was: deep walk looking for <device jid="..."> attrs
|
|
279
|
+
// Fix: navigate usync → list → user[jid=...] → devices → device-list → device[id=N]
|
|
280
|
+
// Server returns <device id="0"/> NOT <device jid="..."/>
|
|
281
|
+
// The phone/LID identifier is on the PARENT <user jid="..."> node, not on device.
|
|
282
|
+
//
|
|
202
283
|
async _doUsyncIq(phones) {
|
|
203
284
|
const iqId = this._client._genMsgId();
|
|
204
285
|
const sid = this._client._genMsgId();
|
|
205
286
|
|
|
287
|
+
// FIX Bug 1: jid attribute on <user>, not a <contact> child node
|
|
288
|
+
// FIX Bug 2: <devices version="2"/> with NO children
|
|
206
289
|
const listChildren = phones.map(p =>
|
|
207
|
-
new BinaryNode('user', {},
|
|
208
|
-
[new BinaryNode('contact', {}, Buffer.from('+' + p))]
|
|
209
|
-
)
|
|
210
|
-
);
|
|
211
|
-
const deviceChildren = phones.map(p =>
|
|
212
|
-
new BinaryNode('device', { jid: makeDeviceJid(p, 0) }, null)
|
|
290
|
+
new BinaryNode('user', { jid: `${p}@s.whatsapp.net` }, null)
|
|
213
291
|
);
|
|
214
292
|
|
|
215
293
|
const iqNode = new BinaryNode('iq',
|
|
216
294
|
{ id: iqId, to: 's.whatsapp.net', type: 'get', xmlns: 'usync' },
|
|
217
295
|
[new BinaryNode('usync',
|
|
218
|
-
{ sid, mode: 'query', last: 'true', index: '0', context: '
|
|
296
|
+
{ sid, mode: 'query', last: 'true', index: '0', context: 'interactive' },
|
|
219
297
|
[
|
|
220
298
|
new BinaryNode('query', {},
|
|
221
|
-
[new BinaryNode('devices', { version: '2' },
|
|
299
|
+
[new BinaryNode('devices', { version: '2' }, null)] // FIX Bug 2: null, not deviceChildren
|
|
222
300
|
),
|
|
223
|
-
new BinaryNode('list', {}, listChildren)
|
|
301
|
+
new BinaryNode('list', {}, listChildren),
|
|
302
|
+
new BinaryNode('side_list', {}, null) // FIX Bug 3: required node
|
|
224
303
|
]
|
|
225
304
|
)]
|
|
226
305
|
);
|
|
227
306
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
307
|
+
process.stderr.write('[DBG] USYNC_IQ phones=[' + phones.join(',') + ']\n');
|
|
308
|
+
|
|
309
|
+
const response = await this._client._sendIq(iqNode).catch(err => {
|
|
310
|
+
process.stderr.write('[DBG] USYNC_ERR ' + (err && err.message) + '\n');
|
|
311
|
+
return null;
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// FIX Bug 4: Correct response structure is:
|
|
315
|
+
// <usync>
|
|
316
|
+
// <list>
|
|
317
|
+
// <user jid="phone@s.whatsapp.net" [lid="lid_user@lid"]>
|
|
318
|
+
// <devices>
|
|
319
|
+
// <device-list>
|
|
320
|
+
// <device id="0"/>
|
|
321
|
+
// <device id="2"/>
|
|
322
|
+
// </device-list>
|
|
323
|
+
// </devices>
|
|
324
|
+
// </user>
|
|
325
|
+
// </list>
|
|
326
|
+
// </usync>
|
|
327
|
+
if (response) {
|
|
328
|
+
const usyncNode = findChild(response, 'usync');
|
|
329
|
+
const listNode = usyncNode
|
|
330
|
+
? findChild(usyncNode, 'list')
|
|
331
|
+
: findChild(response, 'list');
|
|
332
|
+
|
|
333
|
+
if (listNode && Array.isArray(listNode.content)) {
|
|
334
|
+
for (const userNode of listNode.content) {
|
|
335
|
+
if (!userNode || userNode.description !== 'user') continue;
|
|
336
|
+
|
|
337
|
+
const userJid = userNode.attrs && (userNode.attrs.jid || userNode.attrs.value);
|
|
338
|
+
if (!userJid) continue;
|
|
339
|
+
|
|
340
|
+
const isLidUser = String(userJid).endsWith('@lid');
|
|
341
|
+
const { user: rawUser } = stripUser(String(userJid));
|
|
342
|
+
|
|
343
|
+
// Determine phone-based cache key (always a phone number, never a LID)
|
|
344
|
+
let cachePhone;
|
|
345
|
+
if (isLidUser) {
|
|
346
|
+
// Modern account: server returned LID JID in the user node
|
|
347
|
+
const pn = this._client._lidToPn && this._client._lidToPn.get(rawUser);
|
|
348
|
+
if (pn) {
|
|
349
|
+
cachePhone = pn;
|
|
350
|
+
} else if (phones.length === 1) {
|
|
351
|
+
// Single phone queried — this LID must belong to it
|
|
352
|
+
cachePhone = phones[0];
|
|
353
|
+
if (this._client._lidToPn) this._client._lidToPn.set(rawUser, phones[0]);
|
|
354
|
+
if (this._client._pnToLid) this._client._pnToLid.set(phones[0], rawUser);
|
|
355
|
+
} else {
|
|
356
|
+
cachePhone = rawUser; // last resort: use LID user as key
|
|
357
|
+
}
|
|
358
|
+
process.stderr.write('[DBG] USYNC_LID_USER userJid=' + userJid + ' → cachePhone=' + cachePhone + '\n');
|
|
359
|
+
} else {
|
|
360
|
+
// Normal account: user JID is phone@s.whatsapp.net
|
|
361
|
+
cachePhone = rawUser;
|
|
362
|
+
|
|
363
|
+
// Also extract LID from user node's `lid` attribute if the server provided it
|
|
364
|
+
const lidAttr = userNode.attrs && userNode.attrs.lid;
|
|
365
|
+
if (lidAttr) {
|
|
366
|
+
const lidUser = String(lidAttr).split('@')[0].split(':')[0];
|
|
367
|
+
if (this._client._lidToPn) this._client._lidToPn.set(lidUser, cachePhone);
|
|
368
|
+
if (this._client._pnToLid) this._client._pnToLid.set(cachePhone, lidUser);
|
|
369
|
+
process.stderr.write('[DBG] USYNC_LID_MAP phone=' + cachePhone + ' ↔ lid=' + lidUser + '\n');
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Navigate user → devices → device-list → device[id=N]
|
|
374
|
+
const devicesNode = findChild(userNode, 'devices');
|
|
375
|
+
const deviceListNode = devicesNode ? findChild(devicesNode, 'device-list') : null;
|
|
376
|
+
|
|
377
|
+
if (!this._deviceCache.has(cachePhone)) {
|
|
378
|
+
this._deviceCache.set(cachePhone, new Set());
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (deviceListNode && Array.isArray(deviceListNode.content)) {
|
|
382
|
+
for (const devNode of deviceListNode.content) {
|
|
383
|
+
if (!devNode || devNode.description !== 'device') continue;
|
|
384
|
+
// FIX: server uses `id` attribute (e.g. id="0"), NOT `jid` attribute
|
|
385
|
+
const devId = devNode.attrs && devNode.attrs.id != null
|
|
386
|
+
? parseInt(String(devNode.attrs.id), 10)
|
|
387
|
+
: 0;
|
|
388
|
+
this._deviceCache.get(cachePhone).add(devId);
|
|
389
|
+
}
|
|
390
|
+
process.stderr.write('[DBG] USYNC_DEVICES phone=' + cachePhone +
|
|
391
|
+
' ids=[' + [...this._deviceCache.get(cachePhone)].join(',') + ']\n');
|
|
392
|
+
} else {
|
|
393
|
+
// No device-list in response — fall back to device 0
|
|
394
|
+
this._deviceCache.get(cachePhone).add(0);
|
|
395
|
+
process.stderr.write('[DBG] USYNC_NO_DEVLIST phone=' + cachePhone + ' → fallback id=0\n');
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
} else {
|
|
399
|
+
process.stderr.write('[DBG] USYNC_RESP_NO_LIST\n');
|
|
240
400
|
}
|
|
241
|
-
|
|
401
|
+
} else {
|
|
402
|
+
process.stderr.write('[DBG] USYNC_NULL_RESP\n');
|
|
403
|
+
// Device usync timed out — fall back to a contact usync (query/contact).
|
|
404
|
+
// This DOES receive a server response and resolves the phone → LID mapping.
|
|
405
|
+
// Once _pnToLid is populated here, _sendDMMessage (after its await) will
|
|
406
|
+
// use the LID JID for routing instead of the stale phone JID.
|
|
407
|
+
await this._doContactUsync(phones).catch(() => {});
|
|
242
408
|
}
|
|
243
|
-
if (response) walk.call(this, response);
|
|
244
409
|
|
|
245
|
-
// For
|
|
410
|
+
// For phones with no usync response at all, cache device 0 so we don't re-query
|
|
246
411
|
for (const p of phones) {
|
|
247
412
|
if (!this._deviceCache.has(p)) {
|
|
248
413
|
this._deviceCache.set(p, new Set([0]));
|
|
414
|
+
process.stderr.write('[DBG] USYNC_FALLBACK phone=' + p + '\n');
|
|
249
415
|
}
|
|
250
416
|
}
|
|
251
417
|
}
|
|
@@ -297,6 +463,217 @@ class DeviceManager {
|
|
|
297
463
|
return readyJids;
|
|
298
464
|
}
|
|
299
465
|
|
|
466
|
+
// ─── Ensure sessions for a LID-addressed recipient ────────────────────────
|
|
467
|
+
//
|
|
468
|
+
// For accounts migrated to WhatsApp LID, devices are registered under the
|
|
469
|
+
// LID identity (e.g. 139471160877194@lid), NOT the phone number.
|
|
470
|
+
// This method:
|
|
471
|
+
// 1. Queries usync for device list by LID JID
|
|
472
|
+
// 2. Establishes Signal sessions for LID device JIDs (e.g. 139471160877194:0@lid)
|
|
473
|
+
// 3. Returns the list of ready LID device JIDs for participant fanout
|
|
474
|
+
//
|
|
475
|
+
async bulkEnsureSessionsForLid(lidUser, signalProto, allowPkmsg = true, skipUsync = false) {
|
|
476
|
+
const lidJid = `${lidUser}@lid`;
|
|
477
|
+
const cacheKey = `lid:${lidUser}`;
|
|
478
|
+
|
|
479
|
+
// skipUsync=true: caller already knows the LID (from _pnToLid) — skip the
|
|
480
|
+
// usync round-trip and assume primary device 0. Saves up to 15 s.
|
|
481
|
+
if (skipUsync) {
|
|
482
|
+
if (!this._deviceCache.has(cacheKey)) {
|
|
483
|
+
this._deviceCache.set(cacheKey, new Set([0]));
|
|
484
|
+
process.stderr.write('[DBG] LID_SKIP_USYNC lidUser=' + lidUser + ' assume device=0\n');
|
|
485
|
+
}
|
|
486
|
+
} else if (!this._deviceCache.has(cacheKey)) {
|
|
487
|
+
await this._doUsyncIqByJid(lidJid, cacheKey, lidUser);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const deviceIds = this._deviceCache.get(cacheKey) || new Set([0]);
|
|
491
|
+
const deviceJids = [...deviceIds].map(d => makeDeviceJid(lidUser, d, 'lid'));
|
|
492
|
+
|
|
493
|
+
process.stderr.write('[DBG] LID_DEVICES lidUser=' + lidUser +
|
|
494
|
+
' deviceJids=[' + deviceJids.join(',') + ']\n');
|
|
495
|
+
|
|
496
|
+
// Split: existing sessions (ready) vs new (need bundle fetch)
|
|
497
|
+
const readyJids = [];
|
|
498
|
+
const fetchJids = [];
|
|
499
|
+
for (const jid of deviceJids) {
|
|
500
|
+
if (signalProto.store.hasSession(this._jidToAddr(jid))) {
|
|
501
|
+
readyJids.push(jid);
|
|
502
|
+
} else {
|
|
503
|
+
fetchJids.push(jid);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (allowPkmsg && fetchJids.length > 0) {
|
|
508
|
+
const bundles = await this.fetchBundles(fetchJids);
|
|
509
|
+
for (const [jid, bundle] of bundles) {
|
|
510
|
+
try {
|
|
511
|
+
await signalProto.buildSessionFromBundle(jid, bundle);
|
|
512
|
+
readyJids.push(jid);
|
|
513
|
+
process.stderr.write('[DBG] LID_SESSION_BUILT jid=' + jid + '\n');
|
|
514
|
+
} catch (e) {
|
|
515
|
+
process.stderr.write('[DBG] LID_SESSION_ERR jid=' + jid + ' err=' + e.message + '\n');
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Fallback: use primary LID device 0 directly (no session yet, will use pkmsg)
|
|
521
|
+
if (readyJids.length === 0) {
|
|
522
|
+
readyJids.push(makeDeviceJid(lidUser, 0, 'lid'));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return readyJids;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// ─── Contact usync fallback ────────────────────────────────────────────────
|
|
529
|
+
// When query/devices usync times out (server silently ignores it for LID
|
|
530
|
+
// accounts), we fall back to query/contact which uses the older
|
|
531
|
+
// <user><contact>+phone</contact></user> format and DOES get a response.
|
|
532
|
+
// The response includes the actual JID (LID or PN) for each phone.
|
|
533
|
+
// Calling this populates _pnToLid / _lidToPn in the Client, which is used
|
|
534
|
+
// by _sendDMMessage (checked AFTER the await of _usyncGetDevices).
|
|
535
|
+
async _doContactUsync(phones) {
|
|
536
|
+
const iqId = this._client._genMsgId();
|
|
537
|
+
const sid = this._client._genMsgId();
|
|
538
|
+
|
|
539
|
+
const userNodes = phones.map(p =>
|
|
540
|
+
new BinaryNode('user', {}, [
|
|
541
|
+
new BinaryNode('contact', {}, Buffer.from('+' + p, 'utf8'))
|
|
542
|
+
])
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
const iqNode = new BinaryNode('iq',
|
|
546
|
+
{ id: iqId, to: 's.whatsapp.net', type: 'get', xmlns: 'usync' },
|
|
547
|
+
[new BinaryNode('usync',
|
|
548
|
+
{ sid, mode: 'query', last: 'true', index: '0', context: 'interactive' },
|
|
549
|
+
[
|
|
550
|
+
new BinaryNode('query', {}, [new BinaryNode('contact', {}, null)]),
|
|
551
|
+
new BinaryNode('list', {}, userNodes),
|
|
552
|
+
new BinaryNode('side_list', {}, null)
|
|
553
|
+
]
|
|
554
|
+
)]
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
process.stderr.write('[DBG] CONTACT_USYNC phones=[' + phones.join(',') + ']\n');
|
|
558
|
+
|
|
559
|
+
const response = await this._client._sendIq(iqNode).catch(err => {
|
|
560
|
+
process.stderr.write('[DBG] CONTACT_USYNC_ERR ' + (err && err.message) + '\n');
|
|
561
|
+
return null;
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
if (!response) {
|
|
565
|
+
process.stderr.write('[DBG] CONTACT_USYNC_NULL\n');
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
const usyncNode = findChild(response, 'usync');
|
|
570
|
+
const listNode = usyncNode ? findChild(usyncNode, 'list') : findChild(response, 'list');
|
|
571
|
+
|
|
572
|
+
if (!listNode || !Array.isArray(listNode.content)) {
|
|
573
|
+
process.stderr.write('[DBG] CONTACT_USYNC_NO_LIST\n');
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
for (let i = 0; i < listNode.content.length; i++) {
|
|
578
|
+
const userNode = listNode.content[i];
|
|
579
|
+
if (!userNode || userNode.description !== 'user') continue;
|
|
580
|
+
|
|
581
|
+
const actualJid = userNode.attrs && (userNode.attrs.jid || userNode.attrs.value);
|
|
582
|
+
if (!actualJid) continue;
|
|
583
|
+
|
|
584
|
+
const actualJidStr = String(actualJid);
|
|
585
|
+
const isLid = actualJidStr.endsWith('@lid');
|
|
586
|
+
|
|
587
|
+
if (isLid) {
|
|
588
|
+
const { user: lidUser } = stripUser(actualJidStr);
|
|
589
|
+
// Determine the corresponding phone. For single-phone queries it's
|
|
590
|
+
// unambiguous; for multi-phone queries match by index.
|
|
591
|
+
const phone = phones.length === 1 ? phones[0] : phones[i];
|
|
592
|
+
if (phone) {
|
|
593
|
+
process.stderr.write('[DBG] CONTACT_USYNC_LID phone=' + phone + ' → lid=' + lidUser + '\n');
|
|
594
|
+
if (this._client._pnToLid) this._client._pnToLid.set(phone, lidUser);
|
|
595
|
+
if (this._client._lidToPn) this._client._lidToPn.set(lidUser, phone);
|
|
596
|
+
// Persist the mapping so future sessions don't need to re-query
|
|
597
|
+
const store = this._client._signal && this._client._signal.store;
|
|
598
|
+
if (store && store.setLidMapping) store.setLidMapping(phone, lidUser);
|
|
599
|
+
}
|
|
600
|
+
} else {
|
|
601
|
+
// PN account — note it (still on phone-based routing, no LID needed)
|
|
602
|
+
const { user: pnUser } = stripUser(actualJidStr);
|
|
603
|
+
const phone = phones.length === 1 ? phones[0] : phones[i];
|
|
604
|
+
process.stderr.write('[DBG] CONTACT_USYNC_PN phone=' + phone + ' jid=' + actualJidStr + '\n');
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// ─── usync IQ for a specific JID (LID or phone-based) ─────────────────────
|
|
610
|
+
// Used when we already know the routing JID and want to look up devices for it.
|
|
611
|
+
async _doUsyncIqByJid(jid, cacheKey, lidUser) {
|
|
612
|
+
const iqId = this._client._genMsgId();
|
|
613
|
+
const sid = this._client._genMsgId();
|
|
614
|
+
|
|
615
|
+
const iqNode = new BinaryNode('iq',
|
|
616
|
+
{ id: iqId, to: 's.whatsapp.net', type: 'get', xmlns: 'usync' },
|
|
617
|
+
[new BinaryNode('usync',
|
|
618
|
+
{ sid, mode: 'query', last: 'true', index: '0', context: 'interactive' },
|
|
619
|
+
[
|
|
620
|
+
new BinaryNode('query', {},
|
|
621
|
+
[new BinaryNode('devices', { version: '2' }, null)]
|
|
622
|
+
),
|
|
623
|
+
new BinaryNode('list', {},
|
|
624
|
+
[new BinaryNode('user', { jid }, null)]
|
|
625
|
+
),
|
|
626
|
+
new BinaryNode('side_list', {}, null)
|
|
627
|
+
]
|
|
628
|
+
)]
|
|
629
|
+
);
|
|
630
|
+
|
|
631
|
+
process.stderr.write('[DBG] USYNC_LID_IQ jid=' + jid + '\n');
|
|
632
|
+
|
|
633
|
+
const response = await this._client._sendIq(iqNode).catch(err => {
|
|
634
|
+
process.stderr.write('[DBG] USYNC_LID_IQ_ERR ' + (err && err.message) + '\n');
|
|
635
|
+
return null;
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
if (!this._deviceCache.has(cacheKey)) {
|
|
639
|
+
this._deviceCache.set(cacheKey, new Set());
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
if (response) {
|
|
643
|
+
const usyncNode = findChild(response, 'usync');
|
|
644
|
+
const listNode = usyncNode ? findChild(usyncNode, 'list') : findChild(response, 'list');
|
|
645
|
+
|
|
646
|
+
if (listNode && Array.isArray(listNode.content)) {
|
|
647
|
+
for (const userNode of listNode.content) {
|
|
648
|
+
if (!userNode || userNode.description !== 'user') continue;
|
|
649
|
+
|
|
650
|
+
const devicesNode = findChild(userNode, 'devices');
|
|
651
|
+
const deviceListNode = devicesNode ? findChild(devicesNode, 'device-list') : null;
|
|
652
|
+
|
|
653
|
+
if (deviceListNode && Array.isArray(deviceListNode.content)) {
|
|
654
|
+
for (const devNode of deviceListNode.content) {
|
|
655
|
+
if (!devNode || devNode.description !== 'device') continue;
|
|
656
|
+
const devId = devNode.attrs && devNode.attrs.id != null
|
|
657
|
+
? parseInt(String(devNode.attrs.id), 10) : 0;
|
|
658
|
+
this._deviceCache.get(cacheKey).add(devId);
|
|
659
|
+
}
|
|
660
|
+
process.stderr.write('[DBG] USYNC_LID_DEVICES jid=' + jid +
|
|
661
|
+
' ids=[' + [...this._deviceCache.get(cacheKey)].join(',') + ']\n');
|
|
662
|
+
} else {
|
|
663
|
+
this._deviceCache.get(cacheKey).add(0);
|
|
664
|
+
process.stderr.write('[DBG] USYNC_LID_NO_DEVLIST jid=' + jid + ' → fallback id=0\n');
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
} else {
|
|
668
|
+
process.stderr.write('[DBG] USYNC_LID_NO_LIST jid=' + jid + '\n');
|
|
669
|
+
this._deviceCache.get(cacheKey).add(0);
|
|
670
|
+
}
|
|
671
|
+
} else {
|
|
672
|
+
process.stderr.write('[DBG] USYNC_LID_NULL_RESP jid=' + jid + '\n');
|
|
673
|
+
this._deviceCache.get(cacheKey).add(0);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
300
677
|
// ─── Ensure sessions for own linked devices (device != 0) ─────────────────
|
|
301
678
|
//
|
|
302
679
|
// OPTIMISATION: own device list is cached for the entire connection lifetime.
|
|
@@ -365,7 +742,11 @@ class DeviceManager {
|
|
|
365
742
|
const toNodes = encryptedList.map(({ jid, type, ciphertext }) => {
|
|
366
743
|
const encAttrs = { type, v: '2' };
|
|
367
744
|
if (mediaSubtype) encAttrs.mediatype = mediaSubtype;
|
|
368
|
-
|
|
745
|
+
// Always encode the jid as a binary JID object (JID_PAIR / AD_JID) so the
|
|
746
|
+
// server can route the per-device enc payload to the correct Signal session.
|
|
747
|
+
// Raw UTF-8 strings are silently dropped for @lid recipients.
|
|
748
|
+
const jidAttr = jidStrToObj(jid);
|
|
749
|
+
return new BinaryNode('to', { jid: jidAttr },
|
|
369
750
|
[new BinaryNode('enc', encAttrs, ciphertext)]
|
|
370
751
|
);
|
|
371
752
|
});
|
|
@@ -373,4 +754,4 @@ class DeviceManager {
|
|
|
373
754
|
}
|
|
374
755
|
}
|
|
375
756
|
|
|
376
|
-
module.exports = { DeviceManager, makeDeviceJid, stripUser, phoneFromJid, parseBundleFromUserNode };
|
|
757
|
+
module.exports = { DeviceManager, makeDeviceJid, stripUser, phoneFromJid, parseBundleFromUserNode, jidStrToObj };
|
package/lib/MediaService.js
CHANGED
|
@@ -120,7 +120,7 @@ function _tryUpload(host, mediaPath, token, auth, encrypted, encSha256, sha256)
|
|
|
120
120
|
'Content-Type': 'application/octet-stream',
|
|
121
121
|
'Content-Length': encrypted.length,
|
|
122
122
|
'Accept': 'application/json',
|
|
123
|
-
'User-Agent': (() => { const d = getDeviceConfig(); return `WhatsApp/${d.version || '2.26.
|
|
123
|
+
'User-Agent': (() => { const d = getDeviceConfig(); return d.os === 'android' ? `WhatsApp/${d.version || '2.26.7.75'} A` : `WhatsApp/${d.version || '2.26.7.75'} iOS/${d.osVersion} Device/${d.model}`; })()
|
|
124
124
|
}
|
|
125
125
|
};
|
|
126
126
|
|