@itsliaaa/baileys 0.1.0
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/LICENSE +22 -0
- package/README.md +1078 -0
- package/WAProto/index.js +100441 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.js +144 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.js +402 -0
- package/lib/Signal/lid-mapping.js +270 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.js +378 -0
- package/lib/Socket/chats.js +1048 -0
- package/lib/Socket/communities.js +430 -0
- package/lib/Socket/groups.js +328 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1442 -0
- package/lib/Socket/messages-send.js +1153 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.js +227 -0
- package/lib/Socket/socket.js +936 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.js +421 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +12 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +25 -0
- package/lib/Utils/auth-utils.js +289 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +230 -0
- package/lib/Utils/chat-utils.js +811 -0
- package/lib/Utils/crypto.js +117 -0
- package/lib/Utils/decode-wa-message.js +282 -0
- package/lib/Utils/event-buffer.js +573 -0
- package/lib/Utils/generics.js +385 -0
- package/lib/Utils/history.js +130 -0
- package/lib/Utils/identity-change-handler.js +48 -0
- package/lib/Utils/index.js +19 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +7 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/message-retry-manager.js +224 -0
- package/lib/Utils/messages-media.js +789 -0
- package/lib/Utils/messages.js +1832 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.js +527 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/signal.js +158 -0
- package/lib/Utils/sync-action-utils.js +47 -0
- package/lib/Utils/tc-token-utils.js +17 -0
- package/lib/Utils/use-multi-file-auth-state.js +120 -0
- package/lib/Utils/validate-connection.js +206 -0
- package/lib/WABinary/constants.js +1300 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +197 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +93 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +11 -0
- package/package.json +72 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Mutex as AsyncMutex } from 'async-mutex';
|
|
2
|
+
export const makeMutex = () => {
|
|
3
|
+
const mutex = new AsyncMutex();
|
|
4
|
+
return {
|
|
5
|
+
mutex(code) {
|
|
6
|
+
return mutex.runExclusive(code);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export const makeKeyedMutex = () => {
|
|
11
|
+
const map = new Map();
|
|
12
|
+
return {
|
|
13
|
+
async mutex(key, task) {
|
|
14
|
+
let entry = map.get(key);
|
|
15
|
+
if (!entry) {
|
|
16
|
+
entry = { mutex: new AsyncMutex(), refCount: 0 };
|
|
17
|
+
map.set(key, entry);
|
|
18
|
+
}
|
|
19
|
+
entry.refCount++;
|
|
20
|
+
try {
|
|
21
|
+
return await entry.mutex.runExclusive(task);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
entry.refCount--;
|
|
25
|
+
// only delete it if this is still the current entry
|
|
26
|
+
if (entry.refCount === 0 && map.get(key) === entry) {
|
|
27
|
+
map.delete(key);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
/** Number of sent messages to cache in memory for handling retry receipts */
|
|
3
|
+
const RECENT_MESSAGES_SIZE = 512;
|
|
4
|
+
const MESSAGE_KEY_SEPARATOR = '\u0000';
|
|
5
|
+
/** Timeout for session recreation - 1 hour */
|
|
6
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
|
|
7
|
+
const PHONE_REQUEST_DELAY = 3000;
|
|
8
|
+
// Retry reason codes matching WhatsApp Web's Signal error codes.
|
|
9
|
+
export var RetryReason;
|
|
10
|
+
(function (RetryReason) {
|
|
11
|
+
RetryReason[RetryReason["UnknownError"] = 0] = "UnknownError";
|
|
12
|
+
RetryReason[RetryReason["SignalErrorNoSession"] = 1] = "SignalErrorNoSession";
|
|
13
|
+
RetryReason[RetryReason["SignalErrorInvalidKey"] = 2] = "SignalErrorInvalidKey";
|
|
14
|
+
RetryReason[RetryReason["SignalErrorInvalidKeyId"] = 3] = "SignalErrorInvalidKeyId";
|
|
15
|
+
/** MAC verification failed - most common cause of decryption failures */
|
|
16
|
+
RetryReason[RetryReason["SignalErrorInvalidMessage"] = 4] = "SignalErrorInvalidMessage";
|
|
17
|
+
RetryReason[RetryReason["SignalErrorInvalidSignature"] = 5] = "SignalErrorInvalidSignature";
|
|
18
|
+
RetryReason[RetryReason["SignalErrorFutureMessage"] = 6] = "SignalErrorFutureMessage";
|
|
19
|
+
/** Explicit MAC failure - session is definitely out of sync */
|
|
20
|
+
RetryReason[RetryReason["SignalErrorBadMac"] = 7] = "SignalErrorBadMac";
|
|
21
|
+
RetryReason[RetryReason["SignalErrorInvalidSession"] = 8] = "SignalErrorInvalidSession";
|
|
22
|
+
RetryReason[RetryReason["SignalErrorInvalidMsgKey"] = 9] = "SignalErrorInvalidMsgKey";
|
|
23
|
+
RetryReason[RetryReason["BadBroadcastEphemeralSetting"] = 10] = "BadBroadcastEphemeralSetting";
|
|
24
|
+
RetryReason[RetryReason["UnknownCompanionNoPrekey"] = 11] = "UnknownCompanionNoPrekey";
|
|
25
|
+
RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
|
|
26
|
+
RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
|
|
27
|
+
})(RetryReason || (RetryReason = {}));
|
|
28
|
+
/** Error codes that indicate a MAC failure and require immediate session recreation */
|
|
29
|
+
const MAC_ERROR_CODES = new Set([RetryReason.SignalErrorInvalidMessage, RetryReason.SignalErrorBadMac]);
|
|
30
|
+
export class MessageRetryManager {
|
|
31
|
+
constructor(logger, maxMsgRetryCount) {
|
|
32
|
+
this.logger = logger;
|
|
33
|
+
this.recentMessagesMap = new LRUCache({
|
|
34
|
+
max: RECENT_MESSAGES_SIZE,
|
|
35
|
+
ttl: 5 * 60 * 1000,
|
|
36
|
+
ttlAutopurge: true,
|
|
37
|
+
dispose: (_value, key) => {
|
|
38
|
+
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
|
|
39
|
+
if (separatorIndex > -1) {
|
|
40
|
+
const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
|
|
41
|
+
this.messageKeyIndex.delete(messageId);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
this.messageKeyIndex = new Map();
|
|
46
|
+
this.sessionRecreateHistory = new LRUCache({
|
|
47
|
+
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
48
|
+
ttlAutopurge: true
|
|
49
|
+
});
|
|
50
|
+
this.retryCounters = new LRUCache({
|
|
51
|
+
ttl: 15 * 60 * 1000,
|
|
52
|
+
ttlAutopurge: true,
|
|
53
|
+
updateAgeOnGet: true
|
|
54
|
+
}); // 15 minutes TTL
|
|
55
|
+
this.pendingPhoneRequests = {};
|
|
56
|
+
this.maxMsgRetryCount = 5;
|
|
57
|
+
this.statistics = {
|
|
58
|
+
totalRetries: 0,
|
|
59
|
+
successfulRetries: 0,
|
|
60
|
+
failedRetries: 0,
|
|
61
|
+
mediaRetries: 0,
|
|
62
|
+
sessionRecreations: 0,
|
|
63
|
+
phoneRequests: 0
|
|
64
|
+
};
|
|
65
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Add a recent message to the cache for retry handling
|
|
69
|
+
*/
|
|
70
|
+
addRecentMessage(to, id, message) {
|
|
71
|
+
const key = { to, id };
|
|
72
|
+
const keyStr = this.keyToString(key);
|
|
73
|
+
// Add new message
|
|
74
|
+
this.recentMessagesMap.set(keyStr, {
|
|
75
|
+
message,
|
|
76
|
+
timestamp: Date.now()
|
|
77
|
+
});
|
|
78
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
79
|
+
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get a recent message from the cache
|
|
83
|
+
*/
|
|
84
|
+
getRecentMessage(to, id) {
|
|
85
|
+
const key = { to, id };
|
|
86
|
+
const keyStr = this.keyToString(key);
|
|
87
|
+
return this.recentMessagesMap.get(keyStr);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Check if a session should be recreated based on retry count, history, and error code.
|
|
91
|
+
* MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
|
|
92
|
+
*/
|
|
93
|
+
shouldRecreateSession(jid, hasSession, errorCode) {
|
|
94
|
+
// If we don't have a session, always recreate
|
|
95
|
+
if (!hasSession) {
|
|
96
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
97
|
+
this.statistics.sessionRecreations++;
|
|
98
|
+
return {
|
|
99
|
+
reason: "we don't have a Signal session with them",
|
|
100
|
+
recreate: true
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// IMMEDIATE recreation for MAC errors - session is definitely out of sync
|
|
104
|
+
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
|
105
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
106
|
+
this.statistics.sessionRecreations++;
|
|
107
|
+
this.logger.warn({ jid, errorCode: RetryReason[errorCode] }, 'MAC error detected, forcing immediate session recreation');
|
|
108
|
+
return {
|
|
109
|
+
reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
|
|
110
|
+
recreate: true
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
115
|
+
// If no previous recreation or it's been more than an hour
|
|
116
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
117
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
118
|
+
this.statistics.sessionRecreations++;
|
|
119
|
+
return {
|
|
120
|
+
reason: 'retry count > 1 and over an hour since last recreation',
|
|
121
|
+
recreate: true
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return { reason: '', recreate: false };
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Parse error code from retry receipt's retry node.
|
|
128
|
+
* Returns undefined if no error code is present.
|
|
129
|
+
*/
|
|
130
|
+
parseRetryErrorCode(errorAttr) {
|
|
131
|
+
if (errorAttr === undefined || errorAttr === '') {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
const code = parseInt(errorAttr, 10);
|
|
135
|
+
if (Number.isNaN(code)) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
// Validate it's a known RetryReason
|
|
139
|
+
if (code >= RetryReason.UnknownError && code <= RetryReason.StatusRevokeDelay) {
|
|
140
|
+
return code;
|
|
141
|
+
}
|
|
142
|
+
return RetryReason.UnknownError;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Check if an error code indicates a MAC failure
|
|
146
|
+
*/
|
|
147
|
+
isMacError(errorCode) {
|
|
148
|
+
return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Increment retry counter for a message
|
|
152
|
+
*/
|
|
153
|
+
incrementRetryCount(messageId) {
|
|
154
|
+
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
|
|
155
|
+
this.statistics.totalRetries++;
|
|
156
|
+
return this.retryCounters.get(messageId);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get retry count for a message
|
|
160
|
+
*/
|
|
161
|
+
getRetryCount(messageId) {
|
|
162
|
+
return this.retryCounters.get(messageId) || 0;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Check if message has exceeded maximum retry attempts
|
|
166
|
+
*/
|
|
167
|
+
hasExceededMaxRetries(messageId) {
|
|
168
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Mark retry as successful
|
|
172
|
+
*/
|
|
173
|
+
markRetrySuccess(messageId) {
|
|
174
|
+
this.statistics.successfulRetries++;
|
|
175
|
+
// Clean up retry counter for successful message
|
|
176
|
+
this.retryCounters.delete(messageId);
|
|
177
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
178
|
+
this.removeRecentMessage(messageId);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Mark retry as failed
|
|
182
|
+
*/
|
|
183
|
+
markRetryFailed(messageId) {
|
|
184
|
+
this.statistics.failedRetries++;
|
|
185
|
+
this.retryCounters.delete(messageId);
|
|
186
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
187
|
+
this.removeRecentMessage(messageId);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Schedule a phone request with delay
|
|
191
|
+
*/
|
|
192
|
+
schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
193
|
+
// Cancel any existing request for this message
|
|
194
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
195
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
196
|
+
delete this.pendingPhoneRequests[messageId];
|
|
197
|
+
this.statistics.phoneRequests++;
|
|
198
|
+
callback();
|
|
199
|
+
}, delay);
|
|
200
|
+
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Cancel pending phone request
|
|
204
|
+
*/
|
|
205
|
+
cancelPendingPhoneRequest(messageId) {
|
|
206
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
207
|
+
if (timeout) {
|
|
208
|
+
clearTimeout(timeout);
|
|
209
|
+
delete this.pendingPhoneRequests[messageId];
|
|
210
|
+
this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
keyToString(key) {
|
|
214
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
215
|
+
}
|
|
216
|
+
removeRecentMessage(messageId) {
|
|
217
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
218
|
+
if (!keyStr) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
this.recentMessagesMap.delete(keyStr);
|
|
222
|
+
this.messageKeyIndex.delete(messageId);
|
|
223
|
+
}
|
|
224
|
+
}
|