@langitdeveloper/baileys 2.2.3 → 2.2.5
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/lib/Socket/messages-send.js +29 -20
- package/lib/Utils/tc-token-utils.js +125 -0
- package/package.json +1 -1
|
@@ -242,27 +242,36 @@ const makeMessagesSocket = (config) => {
|
|
|
242
242
|
const patched = await patchMessageBeforeSending(message, jids);
|
|
243
243
|
const bytes = (0, Utils_1.encodeWAMessage)(patched);
|
|
244
244
|
let shouldIncludeDeviceIdentity = false;
|
|
245
|
-
const nodes = await Promise.all(jids.map(async (jid) => {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
245
|
+
const nodes = (await Promise.all(jids.map(async (jid) => {
|
|
246
|
+
try {
|
|
247
|
+
const { type, ciphertext } = await signalRepository
|
|
248
|
+
.encryptMessage({ jid, data: bytes });
|
|
249
|
+
if (type === 'pkmsg') {
|
|
250
|
+
shouldIncludeDeviceIdentity = true;
|
|
251
|
+
}
|
|
252
|
+
const node = {
|
|
253
|
+
tag: 'to',
|
|
254
|
+
attrs: { jid },
|
|
255
|
+
content: [{
|
|
256
|
+
tag: 'enc',
|
|
257
|
+
attrs: {
|
|
258
|
+
v: '2',
|
|
259
|
+
type,
|
|
260
|
+
...extraAttrs || {}
|
|
261
|
+
},
|
|
262
|
+
content: ciphertext
|
|
263
|
+
}]
|
|
264
|
+
};
|
|
265
|
+
return node;
|
|
250
266
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
...extraAttrs || {}
|
|
260
|
-
},
|
|
261
|
-
content: ciphertext
|
|
262
|
-
}]
|
|
263
|
-
};
|
|
264
|
-
return node;
|
|
265
|
-
}));
|
|
267
|
+
catch (error) {
|
|
268
|
+
// fix: "SessionError: No sessions" (atau error encrypt lain) buat 1 device jangan
|
|
269
|
+
// sampe nge-reject Promise.all dan bikin proses crash total - skip device ini aja,
|
|
270
|
+
// device lain tetep kekirim normal.
|
|
271
|
+
logger.warn({ jid, trace: error?.stack, err: error?.toString?.() ?? error }, 'failed to encrypt message for device, skipping (no session?)');
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
}))).filter(Boolean);
|
|
266
275
|
return { nodes, shouldIncludeDeviceIdentity };
|
|
267
276
|
};
|
|
268
277
|
const relayMessage = async (jid, message, { messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, cachedGroupMetadata, useCachedGroupMetadata, statusJidList, AI = true }) => {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* TC (Trusted Contact) token helpers.
|
|
5
|
+
*
|
|
6
|
+
* File ini sebelumnya HILANG dari lib (di-require di messages-send.js tapi ga pernah dibikin),
|
|
7
|
+
* bikin proses crash instant pas start ("Cannot find module '../Utils/tc-token-utils'").
|
|
8
|
+
*
|
|
9
|
+
* Semua pemanggil fungsi-fungsi ini di messages-send.js udah dibungkus try/catch, jadi kalo
|
|
10
|
+
* logic di sini meleset/gagal, efeknya cuma "kirim tanpa tctoken" - BUKAN crash. Implementasi
|
|
11
|
+
* di bawah sengaja dibikin konservatif (aman & simpel) drpd nebak detail wire-format IQ result
|
|
12
|
+
* WA yang ga kekonfirm, biar resiko rendah.
|
|
13
|
+
*/
|
|
14
|
+
const TC_TOKEN_TTL_SECONDS = 30 * 24 * 60 * 60; // 30 hari, tebakan aman - token dianggap basi kalo lebih lama dari ini
|
|
15
|
+
const TC_TOKEN_REISSUE_INTERVAL_SECONDS = 7 * 24 * 60 * 60; // 7 hari - jangan reissue token tiap kirim pesan
|
|
16
|
+
function nowSeconds() {
|
|
17
|
+
return Math.floor(Date.now() / 1000);
|
|
18
|
+
}
|
|
19
|
+
/** cek apakah tctoken yg tersimpan udah kadaluarsa berdasarkan timestamp-nya */
|
|
20
|
+
function isTcTokenExpired(timestamp) {
|
|
21
|
+
if (!timestamp) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
const ts = Number(timestamp);
|
|
25
|
+
if (!Number.isFinite(ts) || ts <= 0) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return (nowSeconds() - ts) > TC_TOKEN_TTL_SECONDS;
|
|
29
|
+
}
|
|
30
|
+
/** cek apakah perlu issue token baru ke kontak ini (biar ga spam issue tiap kirim pesan) */
|
|
31
|
+
function shouldSendNewTcToken(senderTimestamp) {
|
|
32
|
+
if (!senderTimestamp) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const ts = Number(senderTimestamp);
|
|
36
|
+
if (!Number.isFinite(ts) || ts <= 0) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return (nowSeconds() - ts) > TC_TOKEN_REISSUE_INTERVAL_SECONDS;
|
|
40
|
+
}
|
|
41
|
+
/** tentuin jid mana yg dipake sbg key penyimpanan tctoken (prefer LID kalo ada mapping-nya) */
|
|
42
|
+
async function resolveTcTokenJid(jid, getLIDForPN) {
|
|
43
|
+
try {
|
|
44
|
+
if (typeof getLIDForPN === 'function') {
|
|
45
|
+
const lid = await getLIDForPN(jid);
|
|
46
|
+
if (lid) {
|
|
47
|
+
return lid;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (_a) {
|
|
52
|
+
// biarin fallback ke jid asli
|
|
53
|
+
}
|
|
54
|
+
return jid;
|
|
55
|
+
}
|
|
56
|
+
/** tentuin jid mana yg dituju pas issue token baru (lid atau pn, tergantung server props) */
|
|
57
|
+
async function resolveIssuanceJid(jid, preferLid, getLIDForPN, getPNForLID) {
|
|
58
|
+
try {
|
|
59
|
+
if (preferLid && typeof getLIDForPN === 'function') {
|
|
60
|
+
const lid = await getLIDForPN(jid);
|
|
61
|
+
if (lid) {
|
|
62
|
+
return lid;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (!preferLid && typeof getPNForLID === 'function') {
|
|
66
|
+
const pn = await getPNForLID(jid);
|
|
67
|
+
if (pn) {
|
|
68
|
+
return pn;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch (_a) {
|
|
73
|
+
// biarin fallback ke jid asli
|
|
74
|
+
}
|
|
75
|
+
return jid;
|
|
76
|
+
}
|
|
77
|
+
/** parse hasil IQ dari issuePrivacyTokens jadi entry token per-jid, konservatif - return {} kalo shape ga dikenal */
|
|
78
|
+
async function storeTcTokensFromIqResult({ result, fallbackJid, keys, getLIDForPN }) {
|
|
79
|
+
var _a, _b, _c;
|
|
80
|
+
try {
|
|
81
|
+
const token = (_c = (_b = (_a = result === null || result === void 0 ? void 0 : result.content) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.content) !== null && _c !== void 0 ? _c : null;
|
|
82
|
+
if (!token || !fallbackJid) {
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
const jidKey = await resolveTcTokenJid(fallbackJid, getLIDForPN);
|
|
86
|
+
const entry = {
|
|
87
|
+
[jidKey]: {
|
|
88
|
+
token: Buffer.isBuffer(token) ? token : Buffer.from(token),
|
|
89
|
+
timestamp: nowSeconds(),
|
|
90
|
+
senderTimestamp: nowSeconds()
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
if (keys === null || keys === void 0 ? void 0 : keys.set) {
|
|
94
|
+
await keys.set({ tctoken: entry });
|
|
95
|
+
}
|
|
96
|
+
return entry;
|
|
97
|
+
}
|
|
98
|
+
catch (_d) {
|
|
99
|
+
// ga dikenal formatnya / gagal parse - aman, cuma berarti token ga ke-simpen
|
|
100
|
+
return {};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** gabungin entry tctoken yg udah ada di store dgn yg baru buat ditulis ulang via keys.set */
|
|
104
|
+
async function buildMergedTcTokenIndexWrite(keys, jids) {
|
|
105
|
+
var _a;
|
|
106
|
+
const merged = {};
|
|
107
|
+
try {
|
|
108
|
+
const existing = await (keys === null || keys === void 0 ? void 0 : keys.get('tctoken', jids));
|
|
109
|
+
for (const jid of jids) {
|
|
110
|
+
if ((_a = existing === null || existing === void 0 ? void 0 : existing[jid]) === null || _a === void 0 ? void 0 : _a.token) {
|
|
111
|
+
merged[jid] = existing[jid];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (_b) {
|
|
116
|
+
// kalo gagal baca existing, ya udah return apa adanya (bisa kosong)
|
|
117
|
+
}
|
|
118
|
+
return merged;
|
|
119
|
+
}
|
|
120
|
+
exports.isTcTokenExpired = isTcTokenExpired;
|
|
121
|
+
exports.shouldSendNewTcToken = shouldSendNewTcToken;
|
|
122
|
+
exports.resolveTcTokenJid = resolveTcTokenJid;
|
|
123
|
+
exports.resolveIssuanceJid = resolveIssuanceJid;
|
|
124
|
+
exports.storeTcTokensFromIqResult = storeTcTokensFromIqResult;
|
|
125
|
+
exports.buildMergedTcTokenIndexWrite = buildMergedTcTokenIndexWrite;
|