xmd-baileys 1.0.16 → 1.0.17
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/Signal/libsignal.js +30 -6
- package/package.json +1 -1
package/lib/Signal/libsignal.js
CHANGED
|
@@ -40,18 +40,33 @@ const WABinary_1 = require("../WABinary");
|
|
|
40
40
|
const sender_key_name_1 = require("./Group/sender-key-name");
|
|
41
41
|
const sender_key_record_1 = require("./Group/sender-key-record");
|
|
42
42
|
const Group_1 = require("./Group");
|
|
43
|
+
|
|
44
|
+
// Session cache - keeps working sessions in memory for fast reuse
|
|
45
|
+
const _sessionCache = new Map();
|
|
46
|
+
const _groupCache = new Map();
|
|
47
|
+
const CACHE_TTL = 300000; // 5 minutes
|
|
48
|
+
|
|
43
49
|
function makeLibSignalRepository(auth) {
|
|
44
50
|
const storage = signalStorage(auth);
|
|
45
51
|
return {
|
|
46
52
|
async decryptGroupMessage({ group, authorJid, msg }) {
|
|
47
53
|
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
48
|
-
const
|
|
54
|
+
const cacheKey = senderName.toString();
|
|
55
|
+
|
|
56
|
+
// Use cached cipher if available
|
|
57
|
+
let cipher = _groupCache.get(cacheKey);
|
|
58
|
+
if (!cipher) {
|
|
59
|
+
cipher = new Group_1.GroupCipher(storage, senderName);
|
|
60
|
+
_groupCache.set(cacheKey, cipher);
|
|
61
|
+
setTimeout(() => _groupCache.delete(cacheKey), CACHE_TTL);
|
|
62
|
+
}
|
|
63
|
+
|
|
49
64
|
try {
|
|
50
65
|
return await cipher.decrypt(msg);
|
|
51
66
|
} catch (err) {
|
|
67
|
+
_groupCache.delete(cacheKey); // Clear bad cache
|
|
52
68
|
if (err.message?.includes('Bad MAC') || err.message?.includes('No valid sessions')) {
|
|
53
|
-
|
|
54
|
-
await auth.keys.set({ 'sender-key': { [senderNameStr]: null } });
|
|
69
|
+
await auth.keys.set({ 'sender-key': { [cacheKey]: null } });
|
|
55
70
|
const e = new Error('SESSION_CIPHER_FAIL');
|
|
56
71
|
e.needsRetry = true;
|
|
57
72
|
throw e;
|
|
@@ -75,7 +90,16 @@ function makeLibSignalRepository(auth) {
|
|
|
75
90
|
},
|
|
76
91
|
async decryptMessage({ jid, type, ciphertext }) {
|
|
77
92
|
const addr = jidToSignalProtocolAddress(jid);
|
|
78
|
-
const
|
|
93
|
+
const cacheKey = addr.toString();
|
|
94
|
+
|
|
95
|
+
// Use cached session cipher if available
|
|
96
|
+
let session = _sessionCache.get(cacheKey);
|
|
97
|
+
if (!session) {
|
|
98
|
+
session = new libsignal.SessionCipher(storage, addr);
|
|
99
|
+
_sessionCache.set(cacheKey, session);
|
|
100
|
+
setTimeout(() => _sessionCache.delete(cacheKey), CACHE_TTL);
|
|
101
|
+
}
|
|
102
|
+
|
|
79
103
|
let result;
|
|
80
104
|
try {
|
|
81
105
|
switch (type) {
|
|
@@ -89,9 +113,9 @@ function makeLibSignalRepository(auth) {
|
|
|
89
113
|
throw new Error(`Unknown message type: ${type}`);
|
|
90
114
|
}
|
|
91
115
|
} catch (err) {
|
|
116
|
+
_sessionCache.delete(cacheKey); // Clear bad cache
|
|
92
117
|
if (err.message?.includes('Bad MAC') || err.message?.includes('No valid sessions')) {
|
|
93
|
-
|
|
94
|
-
await auth.keys.set({ session: { [sessionId]: null } });
|
|
118
|
+
await auth.keys.set({ session: { [cacheKey]: null } });
|
|
95
119
|
const e = new Error('SESSION_CIPHER_FAIL');
|
|
96
120
|
e.needsRetry = true;
|
|
97
121
|
throw e;
|