@zannstore/baileys 2.2.6
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 +13 -0
- package/WAProto/index.js +169661 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.js +147 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Function/Download/tiktok.js +19 -0
- package/lib/Function/Tools/bypass.js +19 -0
- package/lib/Function/index.js +13 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +360 -0
- package/lib/Signal/lid-mapping.js +166 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +19 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.js +310 -0
- package/lib/Socket/chats.js +1145 -0
- package/lib/Socket/community.js +392 -0
- package/lib/Socket/dugong.js +658 -0
- package/lib/Socket/groups.js +317 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1145 -0
- package/lib/Socket/messages-send.js +936 -0
- package/lib/Socket/newsletter.js +437 -0
- package/lib/Socket/registration.js +167 -0
- package/lib/Socket/socket.js +733 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.js +427 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +10 -0
- package/lib/Types/MexUpdates.js +15 -0
- package/lib/Types/Newsletter.js +38 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +45 -0
- package/lib/Utils/auth-utils.js +206 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/bot-toolkit.js +455 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.js +729 -0
- package/lib/Utils/crypto.js +151 -0
- package/lib/Utils/decode-wa-message.js +286 -0
- package/lib/Utils/event-buffer.js +530 -0
- package/lib/Utils/generics.js +527 -0
- package/lib/Utils/history.js +96 -0
- package/lib/Utils/index.js +34 -0
- package/lib/Utils/link-preview.js +93 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/messages-media.js +819 -0
- package/lib/Utils/messages.js +787 -0
- package/lib/Utils/noise-handler.js +155 -0
- package/lib/Utils/process-message.js +321 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/tc-token-utils.js +114 -0
- package/lib/Utils/use-multi-file-auth-state.js +124 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.js +252 -0
- package/lib/WABinary/encode.js +265 -0
- package/lib/WABinary/generic-utils.js +240 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +122 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +40 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +32 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +52 -0
- package/package.json +117 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.makeLibSignalRepository = makeLibSignalRepository;
|
|
37
|
+
const libsignal = __importStar(require("libsignal"));
|
|
38
|
+
const { LRUCache } = require("lru-cache");
|
|
39
|
+
const Utils_1 = require("../Utils");
|
|
40
|
+
const WABinary_1 = require("../WABinary");
|
|
41
|
+
const sender_key_name_1 = require("./Group/sender-key-name");
|
|
42
|
+
const sender_key_record_1 = require("./Group/sender-key-record");
|
|
43
|
+
const Group_1 = require("./Group");
|
|
44
|
+
const { LIDMappingStore } = require("./lid-mapping");
|
|
45
|
+
function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
46
|
+
// logger/pnToLIDFunc are optional so this stays backward compatible
|
|
47
|
+
// with any code still calling makeLibSignalRepository(auth) directly.
|
|
48
|
+
const log = logger || { warn() { }, trace() { }, debug() { } };
|
|
49
|
+
const lidMapping = new LIDMappingStore(auth.keys, log, pnToLIDFunc);
|
|
50
|
+
const storage = signalStorage(auth, lidMapping);
|
|
51
|
+
const parsedKeys = auth.keys;
|
|
52
|
+
const migratedSessionCache = new LRUCache({
|
|
53
|
+
ttl: 3 * 24 * 60 * 60 * 1e3,
|
|
54
|
+
ttlAutopurge: true,
|
|
55
|
+
updateAgeOnGet: true
|
|
56
|
+
});
|
|
57
|
+
const runInTransaction = async (work, label) => {
|
|
58
|
+
if (typeof parsedKeys.transaction === 'function') {
|
|
59
|
+
return parsedKeys.transaction(work, label);
|
|
60
|
+
}
|
|
61
|
+
return work();
|
|
62
|
+
};
|
|
63
|
+
const repository = {
|
|
64
|
+
decryptGroupMessage({ group, authorJid, msg }) {
|
|
65
|
+
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
66
|
+
const cipher = new Group_1.GroupCipher(storage, senderName);
|
|
67
|
+
return runInTransaction(() => cipher.decrypt(msg), group);
|
|
68
|
+
},
|
|
69
|
+
async processSenderKeyDistributionMessage({ item, authorJid }) {
|
|
70
|
+
const builder = new Group_1.GroupSessionBuilder(storage);
|
|
71
|
+
if (!item.groupId) {
|
|
72
|
+
throw new Error('Group ID is required for sender key distribution message');
|
|
73
|
+
}
|
|
74
|
+
const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
|
|
75
|
+
const senderMsg = new Group_1.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
|
|
76
|
+
const senderNameStr = senderName.toString();
|
|
77
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
78
|
+
if (!senderKey) {
|
|
79
|
+
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
80
|
+
}
|
|
81
|
+
return runInTransaction(async () => {
|
|
82
|
+
const { [senderNameStr]: existingKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
83
|
+
if (!existingKey) {
|
|
84
|
+
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
85
|
+
}
|
|
86
|
+
await builder.process(senderName, senderMsg);
|
|
87
|
+
}, item.groupId);
|
|
88
|
+
},
|
|
89
|
+
async decryptMessage({ jid, type, ciphertext }) {
|
|
90
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
91
|
+
const session = new libsignal.SessionCipher(storage, addr);
|
|
92
|
+
const doDecrypt = async () => {
|
|
93
|
+
let result;
|
|
94
|
+
switch (type) {
|
|
95
|
+
case 'pkmsg':
|
|
96
|
+
result = await session.decryptPreKeyWhisperMessage(ciphertext);
|
|
97
|
+
break;
|
|
98
|
+
case 'msg':
|
|
99
|
+
result = await session.decryptWhisperMessage(ciphertext);
|
|
100
|
+
break;
|
|
101
|
+
default:
|
|
102
|
+
throw new Error(`Unknown message type: ${type}`);
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
};
|
|
106
|
+
return runInTransaction(() => doDecrypt(), jid);
|
|
107
|
+
},
|
|
108
|
+
async encryptMessage({ jid, data }) {
|
|
109
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
110
|
+
const cipher = new libsignal.SessionCipher(storage, addr);
|
|
111
|
+
return runInTransaction(async () => {
|
|
112
|
+
const { type: sigType, body } = await cipher.encrypt(data);
|
|
113
|
+
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
114
|
+
return { type, ciphertext: Buffer.from(body, 'binary') };
|
|
115
|
+
}, jid);
|
|
116
|
+
},
|
|
117
|
+
async encryptGroupMessage({ group, meId, data }) {
|
|
118
|
+
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
119
|
+
const builder = new Group_1.GroupSessionBuilder(storage);
|
|
120
|
+
const senderNameStr = senderName.toString();
|
|
121
|
+
return runInTransaction(async () => {
|
|
122
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
123
|
+
if (!senderKey) {
|
|
124
|
+
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
125
|
+
}
|
|
126
|
+
const senderKeyDistributionMessage = await builder.create(senderName);
|
|
127
|
+
const session = new Group_1.GroupCipher(storage, senderName);
|
|
128
|
+
const ciphertext = await session.encrypt(data);
|
|
129
|
+
return {
|
|
130
|
+
ciphertext,
|
|
131
|
+
senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
|
|
132
|
+
};
|
|
133
|
+
}, group);
|
|
134
|
+
},
|
|
135
|
+
async injectE2ESession({ jid, session }) {
|
|
136
|
+
log.trace?.({ jid }, 'injecting E2EE session');
|
|
137
|
+
const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
138
|
+
return runInTransaction(async () => {
|
|
139
|
+
await cipher.initOutgoing(session);
|
|
140
|
+
}, jid);
|
|
141
|
+
},
|
|
142
|
+
jidToSignalProtocolAddress(jid) {
|
|
143
|
+
return jidToSignalProtocolAddress(jid).toString();
|
|
144
|
+
},
|
|
145
|
+
lidMapping,
|
|
146
|
+
async validateSession(jid) {
|
|
147
|
+
try {
|
|
148
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
149
|
+
const session = await storage.loadSession(addr.toString());
|
|
150
|
+
if (!session) {
|
|
151
|
+
return { exists: false, reason: 'no session' };
|
|
152
|
+
}
|
|
153
|
+
if (!session.haveOpenSession()) {
|
|
154
|
+
return { exists: false, reason: 'no open session' };
|
|
155
|
+
}
|
|
156
|
+
return { exists: true };
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
return { exists: false, reason: 'validation error' };
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
async deleteSession(jids) {
|
|
163
|
+
if (!jids.length) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const sessionUpdates = {};
|
|
167
|
+
jids.forEach((jid) => {
|
|
168
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
169
|
+
sessionUpdates[addr.toString()] = null;
|
|
170
|
+
});
|
|
171
|
+
return runInTransaction(async () => {
|
|
172
|
+
await auth.keys.set({ session: sessionUpdates });
|
|
173
|
+
}, `delete-${jids.length}-sessions`);
|
|
174
|
+
},
|
|
175
|
+
/**
|
|
176
|
+
*/
|
|
177
|
+
async migrateSession(fromJid, toJid) {
|
|
178
|
+
if (!fromJid || (!(0, WABinary_1.isLidUser)(toJid) && !(0, WABinary_1.isHostedLidUser)(toJid))) {
|
|
179
|
+
return { migrated: 0, skipped: 0, total: 0 };
|
|
180
|
+
}
|
|
181
|
+
if (!(0, WABinary_1.isPnUser)(fromJid) && !(0, WABinary_1.isHostedPnUser)(fromJid)) {
|
|
182
|
+
return { migrated: 0, skipped: 0, total: 1 };
|
|
183
|
+
}
|
|
184
|
+
const { user } = (0, WABinary_1.jidDecode)(fromJid);
|
|
185
|
+
log.debug?.({ fromJid }, 'bulk device migration - loading all user devices');
|
|
186
|
+
const { [user]: userDevices } = await parsedKeys.get('device-list', [user]);
|
|
187
|
+
if (!userDevices) {
|
|
188
|
+
return { migrated: 0, skipped: 0, total: 0 };
|
|
189
|
+
}
|
|
190
|
+
const { device: fromDevice } = (0, WABinary_1.jidDecode)(fromJid);
|
|
191
|
+
const fromDeviceStr = fromDevice?.toString() || '0';
|
|
192
|
+
if (!userDevices.includes(fromDeviceStr)) {
|
|
193
|
+
userDevices.push(fromDeviceStr);
|
|
194
|
+
}
|
|
195
|
+
const uncachedDevices = userDevices.filter((device) => {
|
|
196
|
+
const deviceKey = `${user}.${device}`;
|
|
197
|
+
return !migratedSessionCache.has(deviceKey);
|
|
198
|
+
});
|
|
199
|
+
const deviceJids = [];
|
|
200
|
+
for (const deviceStr of uncachedDevices) {
|
|
201
|
+
const deviceNum = +deviceStr;
|
|
202
|
+
let jid = deviceNum === 0
|
|
203
|
+
? `${user}@s.whatsapp.net`
|
|
204
|
+
: `${user}:${deviceNum}@s.whatsapp.net`;
|
|
205
|
+
if (deviceNum === 99) {
|
|
206
|
+
jid = `${user}:99@hosted`;
|
|
207
|
+
}
|
|
208
|
+
deviceJids.push(jid);
|
|
209
|
+
}
|
|
210
|
+
log.debug?.({
|
|
211
|
+
fromJid,
|
|
212
|
+
totalDevices: userDevices.length,
|
|
213
|
+
devicesWithSessions: deviceJids.length,
|
|
214
|
+
devices: deviceJids
|
|
215
|
+
}, 'bulk device migration complete - all user devices processed');
|
|
216
|
+
return runInTransaction(async () => {
|
|
217
|
+
const migrationOps = deviceJids.map((jid) => {
|
|
218
|
+
const lidWithDevice = (0, WABinary_1.transferDevice)(jid, toJid);
|
|
219
|
+
const fromDecoded = (0, WABinary_1.jidDecode)(jid);
|
|
220
|
+
const toDecoded = (0, WABinary_1.jidDecode)(lidWithDevice);
|
|
221
|
+
return {
|
|
222
|
+
fromJid: jid,
|
|
223
|
+
toJid: lidWithDevice,
|
|
224
|
+
pnUser: fromDecoded.user,
|
|
225
|
+
lidUser: toDecoded.user,
|
|
226
|
+
deviceId: fromDecoded.device || 0,
|
|
227
|
+
fromAddr: jidToSignalProtocolAddress(jid),
|
|
228
|
+
toAddr: jidToSignalProtocolAddress(lidWithDevice)
|
|
229
|
+
};
|
|
230
|
+
});
|
|
231
|
+
const totalOps = migrationOps.length;
|
|
232
|
+
let migratedCount = 0;
|
|
233
|
+
const pnAddrStrings = Array.from(new Set(migrationOps.map((op) => op.fromAddr.toString())));
|
|
234
|
+
const pnSessions = await parsedKeys.get('session', pnAddrStrings);
|
|
235
|
+
const sessionUpdates = {};
|
|
236
|
+
for (const op of migrationOps) {
|
|
237
|
+
const pnAddrStr = op.fromAddr.toString();
|
|
238
|
+
const lidAddrStr = op.toAddr.toString();
|
|
239
|
+
const pnSession = pnSessions[pnAddrStr];
|
|
240
|
+
if (pnSession) {
|
|
241
|
+
const fromSession = libsignal.SessionRecord.deserialize(pnSession);
|
|
242
|
+
if (fromSession.haveOpenSession()) {
|
|
243
|
+
sessionUpdates[lidAddrStr] = fromSession.serialize();
|
|
244
|
+
sessionUpdates[pnAddrStr] = null;
|
|
245
|
+
migratedCount++;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (Object.keys(sessionUpdates).length > 0) {
|
|
250
|
+
await parsedKeys.set({ session: sessionUpdates });
|
|
251
|
+
log.debug?.({ migratedSessions: migratedCount }, 'bulk session migration complete');
|
|
252
|
+
for (const op of migrationOps) {
|
|
253
|
+
if (sessionUpdates[op.toAddr.toString()]) {
|
|
254
|
+
const deviceKey = `${op.pnUser}.${op.deviceId}`;
|
|
255
|
+
migratedSessionCache.set(deviceKey, true);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const skippedCount = totalOps - migratedCount;
|
|
260
|
+
return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
|
|
261
|
+
}, `migrate-${deviceJids.length}-sessions-${(0, WABinary_1.jidDecode)(toJid)?.user}`);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
return repository;
|
|
265
|
+
}
|
|
266
|
+
const jidToSignalProtocolAddress = (jid) => {
|
|
267
|
+
const decoded = (0, WABinary_1.jidDecode)(jid);
|
|
268
|
+
const { user, device, server, domainType } = decoded;
|
|
269
|
+
if (!user) {
|
|
270
|
+
throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
|
|
271
|
+
}
|
|
272
|
+
const signalUser = domainType !== WABinary_1.WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
|
|
273
|
+
const finalDevice = device || 0;
|
|
274
|
+
return new libsignal.ProtocolAddress(signalUser, finalDevice);
|
|
275
|
+
};
|
|
276
|
+
const jidToSignalSenderKeyName = (group, user) => {
|
|
277
|
+
return new sender_key_name_1.SenderKeyName(group, jidToSignalProtocolAddress(user));
|
|
278
|
+
};
|
|
279
|
+
function signalStorage({ creds, keys }, lidMapping) {
|
|
280
|
+
/**
|
|
281
|
+
*/
|
|
282
|
+
const resolveLIDSignalAddress = async (id) => {
|
|
283
|
+
if (id.includes('.')) {
|
|
284
|
+
const [deviceId, device] = id.split('.');
|
|
285
|
+
const [user, domainType_] = deviceId.split('_');
|
|
286
|
+
const domainType = parseInt(domainType_ || '0');
|
|
287
|
+
if (domainType === WABinary_1.WAJIDDomains.LID || domainType === WABinary_1.WAJIDDomains.HOSTED_LID) {
|
|
288
|
+
return id;
|
|
289
|
+
}
|
|
290
|
+
const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WABinary_1.WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
|
|
291
|
+
const lidForPN = lidMapping ? await lidMapping.getLIDForPN(pnJid) : null;
|
|
292
|
+
if (lidForPN) {
|
|
293
|
+
const lidAddr = jidToSignalProtocolAddress(lidForPN);
|
|
294
|
+
return lidAddr.toString();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return id;
|
|
298
|
+
};
|
|
299
|
+
return {
|
|
300
|
+
loadSession: async (id) => {
|
|
301
|
+
try {
|
|
302
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
303
|
+
const { [wireJid]: sess } = await keys.get('session', [wireJid]);
|
|
304
|
+
if (sess) {
|
|
305
|
+
return libsignal.SessionRecord.deserialize(sess);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
catch (e) {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
return null;
|
|
312
|
+
},
|
|
313
|
+
storeSession: async (id, session) => {
|
|
314
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
315
|
+
await keys.set({ session: { [wireJid]: session.serialize() } });
|
|
316
|
+
},
|
|
317
|
+
isTrustedIdentity: () => {
|
|
318
|
+
return true;
|
|
319
|
+
},
|
|
320
|
+
loadPreKey: async (id) => {
|
|
321
|
+
const keyId = id.toString();
|
|
322
|
+
const { [keyId]: key } = await keys.get('pre-key', [keyId]);
|
|
323
|
+
if (key) {
|
|
324
|
+
return {
|
|
325
|
+
privKey: Buffer.from(key.private),
|
|
326
|
+
pubKey: Buffer.from(key.public)
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
|
|
331
|
+
loadSignedPreKey: () => {
|
|
332
|
+
const key = creds.signedPreKey;
|
|
333
|
+
return {
|
|
334
|
+
privKey: Buffer.from(key.keyPair.private),
|
|
335
|
+
pubKey: Buffer.from(key.keyPair.public)
|
|
336
|
+
};
|
|
337
|
+
},
|
|
338
|
+
loadSenderKey: async (senderKeyName) => {
|
|
339
|
+
const keyId = senderKeyName.toString();
|
|
340
|
+
const { [keyId]: key } = await keys.get('sender-key', [keyId]);
|
|
341
|
+
if (key) {
|
|
342
|
+
return sender_key_record_1.SenderKeyRecord.deserialize(key);
|
|
343
|
+
}
|
|
344
|
+
return new sender_key_record_1.SenderKeyRecord();
|
|
345
|
+
},
|
|
346
|
+
storeSenderKey: async (senderKeyName, key) => {
|
|
347
|
+
const keyId = senderKeyName.toString();
|
|
348
|
+
const serialized = JSON.stringify(key.serialize());
|
|
349
|
+
await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
|
|
350
|
+
},
|
|
351
|
+
getOurRegistrationId: () => creds.registrationId,
|
|
352
|
+
getOurIdentity: () => {
|
|
353
|
+
const { signedIdentityKey } = creds;
|
|
354
|
+
return {
|
|
355
|
+
privKey: Buffer.from(signedIdentityKey.private),
|
|
356
|
+
pubKey: Buffer.from((0, Utils_1.generateSignalPubKey)(signedIdentityKey.public))
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const { LRUCache } = require("lru-cache");
|
|
4
|
+
const {
|
|
5
|
+
isHostedPnUser,
|
|
6
|
+
isLidUser,
|
|
7
|
+
isPnUser,
|
|
8
|
+
jidDecode,
|
|
9
|
+
jidNormalizedUser,
|
|
10
|
+
WAJIDDomains,
|
|
11
|
+
} = require("../WABinary");
|
|
12
|
+
/**
|
|
13
|
+
*/
|
|
14
|
+
class LIDMappingStore {
|
|
15
|
+
constructor(keys, logger, pnToLIDFunc) {
|
|
16
|
+
this.mappingCache = new LRUCache({
|
|
17
|
+
ttl: 3 * 24 * 60 * 60 * 1e3,
|
|
18
|
+
ttlAutopurge: true,
|
|
19
|
+
updateAgeOnGet: true,
|
|
20
|
+
});
|
|
21
|
+
this.keys = keys;
|
|
22
|
+
this.pnToLIDFunc = pnToLIDFunc;
|
|
23
|
+
this.logger = logger;
|
|
24
|
+
}
|
|
25
|
+
async storeLIDPNMappings(pairs) {
|
|
26
|
+
const pairMap = {};
|
|
27
|
+
for (const { lid, pn } of pairs) {
|
|
28
|
+
if (!((isLidUser(lid) && isPnUser(pn)) || (isPnUser(lid) && isLidUser(pn)))) {
|
|
29
|
+
this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const lidDecoded = jidDecode(lid);
|
|
33
|
+
const pnDecoded = jidDecode(pn);
|
|
34
|
+
if (!lidDecoded || !pnDecoded) return;
|
|
35
|
+
const pnUser = pnDecoded.user;
|
|
36
|
+
const lidUser = lidDecoded.user;
|
|
37
|
+
let existingLidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
38
|
+
if (!existingLidUser) {
|
|
39
|
+
this.logger.trace(`Cache miss for PN user ${pnUser}; checking database`);
|
|
40
|
+
const stored = await this.keys.get("lid-mapping", [pnUser]);
|
|
41
|
+
existingLidUser = stored[pnUser];
|
|
42
|
+
if (existingLidUser) {
|
|
43
|
+
this.mappingCache.set(`pn:${pnUser}`, existingLidUser);
|
|
44
|
+
this.mappingCache.set(`lid:${existingLidUser}`, pnUser);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (existingLidUser === lidUser) {
|
|
48
|
+
this.logger.debug(
|
|
49
|
+
{ pnUser, lidUser },
|
|
50
|
+
"LID mapping already exists, skipping"
|
|
51
|
+
);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
pairMap[pnUser] = lidUser;
|
|
55
|
+
}
|
|
56
|
+
this.logger.trace(
|
|
57
|
+
{ pairMap },
|
|
58
|
+
`Storing ${Object.keys(pairMap).length} pn mappings`
|
|
59
|
+
);
|
|
60
|
+
await this.keys.transaction(async () => {
|
|
61
|
+
for (const [pnUser, lidUser] of Object.entries(pairMap)) {
|
|
62
|
+
await this.keys.set({
|
|
63
|
+
"lid-mapping": { [pnUser]: lidUser, [`${lidUser}_reverse`]: pnUser },
|
|
64
|
+
});
|
|
65
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
66
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
67
|
+
}
|
|
68
|
+
}, "lid-mapping");
|
|
69
|
+
}
|
|
70
|
+
async getLIDForPN(pn) {
|
|
71
|
+
return (await this.getLIDsForPNs([pn]))?.[0]?.lid || null;
|
|
72
|
+
}
|
|
73
|
+
async getLIDsForPNs(pns) {
|
|
74
|
+
const usyncFetch = {};
|
|
75
|
+
const successfulPairs = {};
|
|
76
|
+
for (const pn of pns) {
|
|
77
|
+
if (!isPnUser(pn) && !isHostedPnUser(pn)) continue;
|
|
78
|
+
const decoded = jidDecode(pn);
|
|
79
|
+
if (!decoded) continue;
|
|
80
|
+
const pnUser = decoded.user;
|
|
81
|
+
let lidUser = this.mappingCache.get(`pn:${pnUser}`);
|
|
82
|
+
if (!lidUser) {
|
|
83
|
+
const stored = await this.keys.get("lid-mapping", [pnUser]);
|
|
84
|
+
lidUser = stored[pnUser];
|
|
85
|
+
if (lidUser) {
|
|
86
|
+
this.mappingCache.set(`pn:${pnUser}`, lidUser);
|
|
87
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
88
|
+
} else {
|
|
89
|
+
this.logger.trace(
|
|
90
|
+
`No LID mapping found for PN user ${pnUser}; batch getting from USync`
|
|
91
|
+
);
|
|
92
|
+
const device = decoded.device || 0;
|
|
93
|
+
let normalizedPn = jidNormalizedUser(pn);
|
|
94
|
+
if (isHostedPnUser(normalizedPn)) {
|
|
95
|
+
normalizedPn = `${pnUser}@s.whatsapp.net`;
|
|
96
|
+
}
|
|
97
|
+
if (!usyncFetch[normalizedPn]) {
|
|
98
|
+
usyncFetch[normalizedPn] = [device];
|
|
99
|
+
} else {
|
|
100
|
+
usyncFetch[normalizedPn]?.push(device);
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
lidUser = lidUser.toString();
|
|
106
|
+
if (!lidUser) {
|
|
107
|
+
this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const pnDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
111
|
+
const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.domainType === WAJIDDomains.HOSTED ? "hosted.lid" : "lid"}`;
|
|
112
|
+
this.logger.trace(
|
|
113
|
+
`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`
|
|
114
|
+
);
|
|
115
|
+
successfulPairs[pn] = { lid: deviceSpecificLid, pn };
|
|
116
|
+
}
|
|
117
|
+
if (Object.keys(usyncFetch).length > 0) {
|
|
118
|
+
const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch));
|
|
119
|
+
if (result && result.length > 0) {
|
|
120
|
+
await this.storeLIDPNMappings(result);
|
|
121
|
+
for (const pair of result) {
|
|
122
|
+
const pnDecoded = jidDecode(pair.pn);
|
|
123
|
+
const pnUser = pnDecoded?.user;
|
|
124
|
+
if (!pnUser) continue;
|
|
125
|
+
const lidUser = jidDecode(pair.lid)?.user;
|
|
126
|
+
if (!lidUser) continue;
|
|
127
|
+
for (const device of usyncFetch[pair.pn]) {
|
|
128
|
+
const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted.lid" : "lid"}`;
|
|
129
|
+
this.logger.trace(
|
|
130
|
+
`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})`
|
|
131
|
+
);
|
|
132
|
+
const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device === 99 ? "hosted" : "s.whatsapp.net"}`;
|
|
133
|
+
successfulPairs[deviceSpecificPn] = {
|
|
134
|
+
lid: deviceSpecificLid,
|
|
135
|
+
pn: deviceSpecificPn,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return Object.values(successfulPairs);
|
|
144
|
+
}
|
|
145
|
+
async getPNForLID(lid) {
|
|
146
|
+
if (!isLidUser(lid)) return null;
|
|
147
|
+
const decoded = jidDecode(lid);
|
|
148
|
+
if (!decoded) return null;
|
|
149
|
+
const lidUser = decoded.user;
|
|
150
|
+
let pnUser = this.mappingCache.get(`lid:${lidUser}`);
|
|
151
|
+
if (!pnUser || typeof pnUser !== "string") {
|
|
152
|
+
const stored = await this.keys.get("lid-mapping", [`${lidUser}_reverse`]);
|
|
153
|
+
pnUser = stored[`${lidUser}_reverse`];
|
|
154
|
+
if (!pnUser || typeof pnUser !== "string") {
|
|
155
|
+
this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`);
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
this.mappingCache.set(`lid:${lidUser}`, pnUser);
|
|
159
|
+
}
|
|
160
|
+
const lidDevice = decoded.device !== undefined ? decoded.device : 0;
|
|
161
|
+
const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WAJIDDomains.HOSTED_LID ? "hosted" : "s.whatsapp.net"}`;
|
|
162
|
+
this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`);
|
|
163
|
+
return pnJid;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
module.exports = { LIDMappingStore };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractSocketClient = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
5
|
+
class AbstractSocketClient extends events_1.EventEmitter {
|
|
6
|
+
constructor(url, config) {
|
|
7
|
+
super();
|
|
8
|
+
this.url = url;
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.setMaxListeners(0);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.AbstractSocketClient = AbstractSocketClient;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abstract-socket-client"), exports);
|
|
18
|
+
__exportStar(require("./mobile-socket-client"), exports);
|
|
19
|
+
__exportStar(require("./web-socket-client"), exports);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MobileSocketClient = void 0;
|
|
4
|
+
const net_1 = require("net");
|
|
5
|
+
const abstract_socket_client_1 = require("./abstract-socket-client");
|
|
6
|
+
class MobileSocketClient extends abstract_socket_client_1.AbstractSocketClient {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.socket = null;
|
|
10
|
+
}
|
|
11
|
+
get isOpen() {
|
|
12
|
+
var _a;
|
|
13
|
+
return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === 'open';
|
|
14
|
+
}
|
|
15
|
+
get isClosed() {
|
|
16
|
+
var _a;
|
|
17
|
+
return this.socket === null || ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === 'closed';
|
|
18
|
+
}
|
|
19
|
+
get isClosing() {
|
|
20
|
+
var _a;
|
|
21
|
+
return this.socket === null || ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === 'closed';
|
|
22
|
+
}
|
|
23
|
+
get isConnecting() {
|
|
24
|
+
var _a;
|
|
25
|
+
return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === 'opening';
|
|
26
|
+
}
|
|
27
|
+
async connect() {
|
|
28
|
+
var _a;
|
|
29
|
+
if (this.socket) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (this.config.agent) {
|
|
33
|
+
throw new Error('There are not support for proxy agent for mobile connection');
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.socket = (0, net_1.connect)({
|
|
37
|
+
host: this.url.hostname,
|
|
38
|
+
port: Number(this.url.port) || 443
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
this.socket.setMaxListeners(0);
|
|
42
|
+
const events = ['close', 'connect', 'data', 'drain', 'end', 'error', 'lookup', 'ready', 'timeout'];
|
|
43
|
+
for (const event of events) {
|
|
44
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.on(event, (...args) => this.emit(event, ...args));
|
|
45
|
+
}
|
|
46
|
+
this.socket.on('data', (...args) => this.emit('message', ...args));
|
|
47
|
+
this.socket.on('ready', (...args) => this.emit('open', ...args));
|
|
48
|
+
}
|
|
49
|
+
async close() {
|
|
50
|
+
if (!this.socket) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
return new Promise(resolve => {
|
|
54
|
+
this.socket.end(resolve);
|
|
55
|
+
this.socket = null;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
send(str, cb) {
|
|
59
|
+
if (this.socket === null) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return this.socket.write(str, undefined, cb);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.MobileSocketClient = MobileSocketClient;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WebSocketClient = void 0;
|
|
7
|
+
const ws_1 = __importDefault(require("ws"));
|
|
8
|
+
const Defaults_1 = require("../../Defaults");
|
|
9
|
+
const abstract_socket_client_1 = require("./abstract-socket-client");
|
|
10
|
+
class WebSocketClient extends abstract_socket_client_1.AbstractSocketClient {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(...arguments);
|
|
13
|
+
this.socket = null;
|
|
14
|
+
}
|
|
15
|
+
get isOpen() {
|
|
16
|
+
var _a;
|
|
17
|
+
return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN;
|
|
18
|
+
}
|
|
19
|
+
get isClosed() {
|
|
20
|
+
var _a;
|
|
21
|
+
return this.socket === null || ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.CLOSED;
|
|
22
|
+
}
|
|
23
|
+
get isClosing() {
|
|
24
|
+
var _a;
|
|
25
|
+
return this.socket === null || ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.CLOSING;
|
|
26
|
+
}
|
|
27
|
+
get isConnecting() {
|
|
28
|
+
var _a;
|
|
29
|
+
return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.CONNECTING;
|
|
30
|
+
}
|
|
31
|
+
async connect() {
|
|
32
|
+
var _a, _b;
|
|
33
|
+
if (this.socket) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
this.socket = new ws_1.default(this.url, {
|
|
37
|
+
origin: Defaults_1.DEFAULT_ORIGIN,
|
|
38
|
+
headers: (_a = this.config.options) === null || _a === void 0 ? void 0 : _a.headers,
|
|
39
|
+
handshakeTimeout: this.config.connectTimeoutMs,
|
|
40
|
+
timeout: this.config.connectTimeoutMs,
|
|
41
|
+
agent: this.config.agent,
|
|
42
|
+
});
|
|
43
|
+
this.socket.setMaxListeners(0);
|
|
44
|
+
const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response'];
|
|
45
|
+
for (const event of events) {
|
|
46
|
+
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.on(event, (...args) => this.emit(event, ...args));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async close() {
|
|
50
|
+
if (!this.socket) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.socket.close();
|
|
54
|
+
this.socket = null;
|
|
55
|
+
}
|
|
56
|
+
send(str, cb) {
|
|
57
|
+
var _a;
|
|
58
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(str, cb);
|
|
59
|
+
return Boolean(this.socket);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.WebSocketClient = WebSocketClient;
|