fca-nx 7.1.0 → 7.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/README.md +9 -23
- package/module/login.js +3 -1
- package/package.json +1 -1
- package/src/api/messaging/getThemePictures.js +35 -21
- package/src/api/messaging/sendMessage.js +1 -14
- package/src/api/socket/OldMessage.js +8 -8
- package/src/api/socket/core/connectMqtt.js +6 -6
- package/src/api/socket/e2ee/index.js +22 -84
- package/src/api/socket/e2ee/protocol.js +9 -32
- package/src/api/socket/e2ee/ratchet.js +1 -29
- package/src/api/socket/e2ee/store.js +42 -140
- package/src/api/socket/e2ee/vendor/fb-e2ee.cjs +18 -140
- package/src/api/socket/listenE2EE.js +1 -14
- package/src/api/socket/sendMessage.js +195 -82
- package/src/api/socket/sendMessageMqtt.js +1 -8
- package/src/api/users/getUserInfoV2.js +61 -5
- package/src/utils/sendThrottle.js +64 -0
- package/src/utils/textEffects.js +8 -1
package/README.md
CHANGED
|
@@ -138,29 +138,15 @@ api.stopAutoSave();
|
|
|
138
138
|
api.sendMessage("Hello world!", threadID);
|
|
139
139
|
|
|
140
140
|
```
|
|
141
|
-
### Message
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
)
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
Built-in effect names are `heart`, `hearts`, `love`, `gift`, `present`, `fire`,
|
|
153
|
-
`flame`, `confetti`, `confecti`, `party`, `sparkles`, `sparkle`, `star`, and
|
|
154
|
-
`magic`. You can also pass a custom numeric effect ID:
|
|
155
|
-
|
|
156
|
-
```javascript
|
|
157
|
-
api.sendMessage({ body: "Party time!", effect: "confetti" }, threadID);
|
|
158
|
-
api.sendMessage({ body: "Custom effect", textEffect: 27 }, threadID);
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
The aliases `textEffect`, `text_effect`, `effect`, `effectName`, and their
|
|
162
|
-
snake/camel-case ID variants are accepted. Effects are applied through both
|
|
163
|
-
the MQTT sender and the legacy fallback sender.
|
|
141
|
+
### Message effects removed (7.2.5)
|
|
142
|
+
Bubble effects (`gift`, `fire`, `love`, `textEffect`, `text_effect_ids`) are no longer sent.
|
|
143
|
+
Passing those keys is ignored. Sending them was a non-browser payload and a common checkpoint/suspend trigger.
|
|
144
|
+
|
|
145
|
+
### Session safety (7.2.5)
|
|
146
|
+
- Default `online` is `false` (MQTT `chat_on` stays off unless you opt in).
|
|
147
|
+
- `mark_thread_read` is sent only when `autoMarkRead: true`.
|
|
148
|
+
- Outgoing sends are slower by default (about 2.8s + jitter, max 8/min).
|
|
149
|
+
- MQTT reconnect backs off harder (8s → 120s, then a 20 minute cooldown) instead of hammering the socket.
|
|
164
150
|
|
|
165
151
|
### Send Attachment
|
|
166
152
|
```javascript
|
package/module/login.js
CHANGED
|
@@ -135,7 +135,9 @@ function login(loginData, options, callback) {
|
|
|
135
135
|
forceLogin: false,
|
|
136
136
|
autoMarkRead: false,
|
|
137
137
|
autoReconnect: true,
|
|
138
|
-
|
|
138
|
+
// Appear offline by default. chat_on:true on the MQTT hello is a strong
|
|
139
|
+
// always-online bot signal and is a common cause of forced logout / suspend.
|
|
140
|
+
online: false,
|
|
139
141
|
emitReady: false,
|
|
140
142
|
// Single source of truth for the default identity - see src/utils/clientIdentity.js.
|
|
141
143
|
// This used to be hard-coded independently here (Chrome/121), in
|
package/package.json
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
const log = require("../../../func/logAdapter");
|
|
4
4
|
const { parseAndCheckLogin } = require("../../utils/client");
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
// api.getThemePictures(themeID, callback?)
|
|
7
|
+
// themeID = the theme_fbid (digits), e.g. from api.getThreadInfo(tid).threadTheme.id
|
|
8
|
+
// or from an api.createThemeAI() result.
|
|
6
9
|
module.exports = function (defaultFuncs, api, ctx) {
|
|
7
10
|
return function getThemePictures(id, callback) {
|
|
8
11
|
let resolveFunc = function () { };
|
|
@@ -12,24 +15,24 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
12
15
|
rejectFunc = reject;
|
|
13
16
|
});
|
|
14
17
|
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
callback = callback;
|
|
21
|
-
} else {
|
|
22
|
-
callback = function (err, data) {
|
|
23
|
-
if (err) {
|
|
24
|
-
return rejectFunc(err);
|
|
25
|
-
}
|
|
26
|
-
resolveFunc(data);
|
|
27
|
-
};
|
|
28
|
-
}
|
|
18
|
+
if (typeof callback !== "function") {
|
|
19
|
+
callback = function (err, data) {
|
|
20
|
+
if (err) return rejectFunc(err);
|
|
21
|
+
resolveFunc(data);
|
|
22
|
+
};
|
|
29
23
|
}
|
|
30
24
|
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
// [Hardened] Accept numbers/bigint too. Previously any non-string id was silently
|
|
26
|
+
// replaced with "" and Facebook answered messenger_thread_theme: null.
|
|
27
|
+
if (typeof id === "number" || typeof id === "bigint") id = String(id);
|
|
28
|
+
id = typeof id === "string" ? id.trim() : "";
|
|
29
|
+
|
|
30
|
+
if (!/^\d+$/.test(id)) {
|
|
31
|
+
const err = {
|
|
32
|
+
error: "getThemePictures: a numeric theme ID is required (theme_fbid), got: " + JSON.stringify(id)
|
|
33
|
+
};
|
|
34
|
+
callback(err);
|
|
35
|
+
return returnPromise;
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
const form = {
|
|
@@ -37,19 +40,30 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
37
40
|
fb_api_req_friendly_name: "MWPThreadThemeProviderQuery",
|
|
38
41
|
doc_id: "9734829906576883",
|
|
39
42
|
server_timestamps: true,
|
|
40
|
-
variables: JSON.stringify({
|
|
41
|
-
id
|
|
42
|
-
}),
|
|
43
|
+
variables: JSON.stringify({ id }),
|
|
43
44
|
av: ctx.userID
|
|
44
45
|
};
|
|
46
|
+
|
|
45
47
|
defaultFuncs
|
|
46
48
|
.post("https://www.facebook.com/api/graphql/", ctx.jar, form)
|
|
47
49
|
.then(parseAndCheckLogin(ctx, defaultFuncs))
|
|
48
50
|
.then(function (resData) {
|
|
49
|
-
if (resData.errors) {
|
|
51
|
+
if (resData && resData.errors) {
|
|
50
52
|
throw resData;
|
|
51
53
|
}
|
|
52
54
|
|
|
55
|
+
// [Hardened] Request worked but Facebook has no theme with this ID for this account.
|
|
56
|
+
// Report as failure instead of a false "success" with null theme.
|
|
57
|
+
if (!resData || !resData.data || !resData.data.messenger_thread_theme) {
|
|
58
|
+
const notFound = {
|
|
59
|
+
error: "Theme not found: Facebook returned no theme for ID " + id + ". Check the ID (use the theme_fbid of a real theme).",
|
|
60
|
+
themeID: id,
|
|
61
|
+
raw: resData
|
|
62
|
+
};
|
|
63
|
+
log.error("getThemePictures", notFound.error);
|
|
64
|
+
return callback(notFound);
|
|
65
|
+
}
|
|
66
|
+
|
|
53
67
|
return callback(null, resData);
|
|
54
68
|
})
|
|
55
69
|
.catch(function (err) {
|
|
@@ -162,7 +162,7 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
162
162
|
source: 2097153,
|
|
163
163
|
send_type: 1,
|
|
164
164
|
sync_group: 1,
|
|
165
|
-
mark_thread_read: 1,
|
|
165
|
+
mark_thread_read: ctx.globalOptions && ctx.globalOptions.autoMarkRead ? 1 : 0,
|
|
166
166
|
text: baseBody === "" ? null : baseBody,
|
|
167
167
|
initiating_source: 0,
|
|
168
168
|
skip_url_preview_gen: 1,
|
|
@@ -171,19 +171,6 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
171
171
|
metadata_dataclass: JSON.stringify({ media_accessibility_metadata: { alt_text: null } })
|
|
172
172
|
};
|
|
173
173
|
|
|
174
|
-
// --- TEXT EFFECTS SUPPORT FOR MQTT ---
|
|
175
|
-
if (m.text_effect_ids) {
|
|
176
|
-
let effectArray = [];
|
|
177
|
-
if (Array.isArray(m.text_effect_ids)) {
|
|
178
|
-
effectArray = m.text_effect_ids;
|
|
179
|
-
} else if (typeof m.text_effect_ids === "string" || typeof m.text_effect_ids === "number") {
|
|
180
|
-
effectArray = [m.text_effect_ids];
|
|
181
|
-
}
|
|
182
|
-
if (effectArray.length > 0) {
|
|
183
|
-
payload0.text_effect_ids = effectArray.map(id => String(id));
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
174
|
const mentionData = buildMentionData(m, baseBody);
|
|
188
175
|
if (mentionData) payload0.mention_data = mentionData;
|
|
189
176
|
|
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
var utils = require("../../utils/utils");
|
|
4
4
|
var logger = require("../../utils/logger");
|
|
5
5
|
var bluebird = require("bluebird");
|
|
6
|
-
var { getTextEffectId } = require("../../utils/textEffects");
|
|
7
|
-
|
|
8
6
|
var allowedProperties = {
|
|
9
7
|
attachment: true, url: true, sticker: true, emoji: true,
|
|
10
|
-
emojiSize: true, body: true, mentions: true, location: true
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
emojiSize: true, body: true, mentions: true, location: true
|
|
9
|
+
};
|
|
10
|
+
// Effect keys are ignored (effect system removed in 7.2.5) so older callers do not throw.
|
|
11
|
+
var ignoredEffectKeys = {
|
|
12
|
+
text_effect_ids: true, textEffect: true, text_effect: true, text_effect_id: true,
|
|
13
|
+
textEffectId: true, textEffectID: true, effect: true, effect_id: true, effectID: true,
|
|
14
|
+
text_effect_name: true, effectName: true, effect_name: true
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
module.exports = function (defaultFuncs, api, ctx) {
|
|
@@ -131,6 +132,7 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
131
132
|
}
|
|
132
133
|
|
|
133
134
|
for (var key in msg) {
|
|
135
|
+
if (ignoredEffectKeys[key]) { delete msg[key]; continue; }
|
|
134
136
|
if (!allowedProperties[key]) {
|
|
135
137
|
return callback({ error: "OldMessage: Unknown property '" + key + "'" });
|
|
136
138
|
}
|
|
@@ -138,8 +140,6 @@ module.exports = function (defaultFuncs, api, ctx) {
|
|
|
138
140
|
|
|
139
141
|
var form = {};
|
|
140
142
|
if (msg.body) form.body = msg.body;
|
|
141
|
-
var textEffectId = getTextEffectId(msg);
|
|
142
|
-
if (textEffectId) form.text_effect_id = textEffectId;
|
|
143
143
|
|
|
144
144
|
if (msg.mentions && Array.isArray(msg.mentions)) {
|
|
145
145
|
form.body = msg.body || "";
|
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
const { formatID } = require("../../../utils/format");
|
|
7
7
|
const { getSessionIdentity } = require("../../../utils/clientIdentity");
|
|
8
8
|
|
|
9
|
-
const DEFAULT_RECONNECT_DELAY_MS =
|
|
10
|
-
const MAX_RECONNECT_DELAY_MS =
|
|
11
|
-
const MAX_RECONNECT_ATTEMPTS =
|
|
12
|
-
const MAX_RECONNECT_COOLDOWN_MS =
|
|
9
|
+
const DEFAULT_RECONNECT_DELAY_MS = 8000;
|
|
10
|
+
const MAX_RECONNECT_DELAY_MS = 120000;
|
|
11
|
+
const MAX_RECONNECT_ATTEMPTS = 5; // cap consecutive network-failure reconnects; does not apply to confirmed auth failures (those never reconnect)
|
|
12
|
+
const MAX_RECONNECT_COOLDOWN_MS = 20 * 60 * 1000; // after exhausting fast retries, wait this long before trying again (never give up permanently)
|
|
13
13
|
const T_MS_WAIT_TIMEOUT_MS = 5000;
|
|
14
14
|
|
|
15
15
|
// Exponential backoff with jitter, based on how many *consecutive* reconnect
|
|
@@ -136,10 +136,10 @@ module.exports = function createListenMqtt(deps) {
|
|
|
136
136
|
protocolVersion: 13,
|
|
137
137
|
binaryType: "arraybuffer"
|
|
138
138
|
},
|
|
139
|
-
keepalive:
|
|
139
|
+
keepalive: 60,
|
|
140
140
|
reschedulePings: true,
|
|
141
141
|
reconnectPeriod: 0,
|
|
142
|
-
connectTimeout:
|
|
142
|
+
connectTimeout: 12000
|
|
143
143
|
};
|
|
144
144
|
if (ctx.globalOptions.proxy !== undefined) {
|
|
145
145
|
const agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
|
|
@@ -73,14 +73,9 @@ class E2EEBridge {
|
|
|
73
73
|
|
|
74
74
|
userId = userId || this.ctx.userID;
|
|
75
75
|
|
|
76
|
-
// Default to .fca-nx/e2ee_device.json in the user's project directory
|
|
77
|
-
// [Fixed by xalman] On hosts with an ephemeral filesystem (Railway, Heroku...) the
|
|
78
|
-
// device file is wiped on every deploy, which registers a brand-new E2EE device each
|
|
79
|
-
// time and leaves stale devices behind. Set FCA_NX_E2EE_DEVICE_PATH to a path on a
|
|
80
|
-
// persistent volume (e.g. /data/e2ee_device.json) to keep the same device forever.
|
|
76
|
+
// Default to .fca-nx/e2ee_device.json in the user's project directory
|
|
81
77
|
if (!deviceStorePath) {
|
|
82
|
-
deviceStorePath = process.
|
|
83
|
-
|| path.join(process.cwd(), ".fca-nx", "e2ee_device.json");
|
|
78
|
+
deviceStorePath = path.join(process.cwd(), ".fca-nx", "e2ee_device.json");
|
|
84
79
|
}
|
|
85
80
|
|
|
86
81
|
// Auto-create parent directory — users don't need to create it manually
|
|
@@ -207,49 +202,36 @@ class E2EEBridge {
|
|
|
207
202
|
// Auto-reconnect the Noise WebSocket whenever it drops unexpectedly.
|
|
208
203
|
// `this.connected` stays true through reconnect cycles; it's only set
|
|
209
204
|
// false by an explicit api.e2ee.disconnect() call.
|
|
210
|
-
// [Fixed by xalman] Previous version gave up after 2 failed attempts (bot stayed "connected"
|
|
211
|
-
// but deaf forever), could start several reconnects in parallel (duplicate sockets ->
|
|
212
|
-
// duplicate messages), and reacted to non-E2EE "disconnected" events too.
|
|
213
205
|
const _self = this;
|
|
214
|
-
this.
|
|
215
|
-
this.client.onEvent("disconnected", function _onE2EEDisconnected(data) {
|
|
206
|
+
this.client.onEvent("disconnected", function _onE2EEDisconnected() {
|
|
216
207
|
if (!_self.connected) return; // intentional disconnect — skip
|
|
217
|
-
if (data && data.isE2EE === false) return; // MQTT/gateway drop, E2EE socket is fine
|
|
218
|
-
if (_self._reconnecting) return;
|
|
219
|
-
_self._reconnecting = true;
|
|
220
208
|
logger.warn("E2EE", "Noise WebSocket disconnected — reconnecting in 5s...");
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (!_self.connected) { _self._reconnecting = false; return; }
|
|
224
|
-
attempt++;
|
|
209
|
+
setTimeout(async function () {
|
|
210
|
+
if (!_self.connected) return;
|
|
225
211
|
try {
|
|
226
212
|
await _self.client.connectE2EE(_e2eeDevicePath, _e2eeUserId);
|
|
227
|
-
|
|
228
|
-
logger.success("E2EE", "E2EE WebSocket reconnected" + (attempt > 1 ? " (attempt " + attempt + ")" : "") + ".");
|
|
213
|
+
logger.success("E2EE", "E2EE WebSocket reconnected.");
|
|
229
214
|
} catch (err) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
215
|
+
logger.error("E2EE", "E2EE reconnect failed: " + (err && err.message ? err.message : String(err)));
|
|
216
|
+
// Retry again after another 10s if reconnect failed
|
|
217
|
+
if (_self.connected) {
|
|
218
|
+
setTimeout(async function () {
|
|
219
|
+
if (!_self.connected) return;
|
|
220
|
+
try {
|
|
221
|
+
await _self.client.connectE2EE(_e2eeDevicePath, _e2eeUserId);
|
|
222
|
+
logger.success("E2EE", "E2EE WebSocket reconnected (retry).");
|
|
223
|
+
} catch (err2) {
|
|
224
|
+
logger.error("E2EE", "E2EE reconnect retry failed: " + (err2 && err2.message ? err2.message : String(err2)));
|
|
225
|
+
}
|
|
226
|
+
}, 10000);
|
|
227
|
+
}
|
|
233
228
|
}
|
|
234
|
-
};
|
|
235
|
-
setTimeout(tryReconnect, 5000);
|
|
229
|
+
}, 5000);
|
|
236
230
|
});
|
|
237
231
|
|
|
238
232
|
// Forward incoming E2EE messages to the registered callback.
|
|
239
|
-
this._recentIds = this._recentIds || new Set();
|
|
240
233
|
this.client.onEvent("e2ee_message", (msg) => {
|
|
241
|
-
if (!this._messageCallback)
|
|
242
|
-
logger.warn("E2EE", "Incoming E2EE message dropped: no listener registered yet");
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// [Fixed by xalman] never deliver the same message twice (reconnect replay / overlapping
|
|
247
|
-
// sockets) — a duplicate would make the bot answer a command twice.
|
|
248
|
-
if (msg && msg.id) {
|
|
249
|
-
if (this._recentIds.has(msg.id)) return;
|
|
250
|
-
this._recentIds.add(msg.id);
|
|
251
|
-
if (this._recentIds.size > 2000) this._recentIds.delete(this._recentIds.values().next().value);
|
|
252
|
-
}
|
|
234
|
+
if (!this._messageCallback) return;
|
|
253
235
|
|
|
254
236
|
const senderID =
|
|
255
237
|
msg.senderId ||
|
|
@@ -306,19 +288,7 @@ class E2EEBridge {
|
|
|
306
288
|
};
|
|
307
289
|
}
|
|
308
290
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
// [Fixed by xalman] The callback runs synchronously inside the vendor's
|
|
312
|
-
// decrypt pipeline. If the bot handler threw here, the vendor treated it as a
|
|
313
|
-
// decryption failure (retry receipt + dropped message). Isolate it.
|
|
314
|
-
try {
|
|
315
|
-
const r = this._messageCallback(null, event);
|
|
316
|
-
if (r && typeof r.catch === "function") {
|
|
317
|
-
r.catch((e) => logger.error("E2EE", "Message callback rejected: " + (e && e.message ? e.message : String(e))));
|
|
318
|
-
}
|
|
319
|
-
} catch (e) {
|
|
320
|
-
logger.error("E2EE", "Message callback threw: " + (e && e.message ? e.message : String(e)));
|
|
321
|
-
}
|
|
291
|
+
this._messageCallback(null, event);
|
|
322
292
|
});
|
|
323
293
|
|
|
324
294
|
// Surface connection errors so the bot log shows them.
|
|
@@ -339,20 +309,6 @@ class E2EEBridge {
|
|
|
339
309
|
}
|
|
340
310
|
}
|
|
341
311
|
|
|
342
|
-
// [Fixed by xalman] `connected` stays true while the Noise socket reconnects, so sends made in
|
|
343
|
-
// that window used to fail (or fall back to MQTT, which cannot deliver to E2EE threads).
|
|
344
|
-
// Wait (up to timeoutMs) for the live socket instead of failing immediately.
|
|
345
|
-
async _waitReady(timeoutMs) {
|
|
346
|
-
const ctl = this.client && this.client.controller;
|
|
347
|
-
const isReady = () => !ctl || ctl.e2eeConnected !== false;
|
|
348
|
-
const deadline = Date.now() + (timeoutMs || 20000);
|
|
349
|
-
while (!isReady()) {
|
|
350
|
-
if (!this.connected) throw new Error("E2EE disconnected.");
|
|
351
|
-
if (Date.now() > deadline) throw new Error("E2EE socket is reconnecting; message not sent (timeout).");
|
|
352
|
-
await new Promise((r) => setTimeout(r, 250));
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
312
|
isConnected() {
|
|
357
313
|
return this.connected;
|
|
358
314
|
}
|
|
@@ -373,25 +329,11 @@ class E2EEBridge {
|
|
|
373
329
|
*/
|
|
374
330
|
async sendMessage(threadId, msg, replyToMessageId) {
|
|
375
331
|
this.ensureConnected();
|
|
376
|
-
await this._waitReady();
|
|
377
|
-
await require("../../../utils/sendThrottle").waitTurn(); // human-like pacing
|
|
378
332
|
|
|
379
333
|
const text = typeof msg === "string" ? msg : (msg && msg.body != null ? String(msg.body) : "");
|
|
380
334
|
const attachment = (msg && typeof msg === "object") ? (msg.attachment || null) : null;
|
|
381
335
|
|
|
382
336
|
if (!attachment) {
|
|
383
|
-
// Call the controller's E2EE senders directly: client.sendMessage() silently falls
|
|
384
|
-
// back to the regular (MQTT) sender when the socket flag is down, and that sender
|
|
385
|
-
// routes back into this bridge -> endless recursion / message never delivered.
|
|
386
|
-
const ctl = this.client.controller;
|
|
387
|
-
const tid = String(threadId);
|
|
388
|
-
if (ctl && typeof ctl.sendE2EEText === "function" && typeof ctl.sendE2EEGroupText === "function") {
|
|
389
|
-
const isGroupJid = tid.includes("@g.us") || tid.includes(".g.");
|
|
390
|
-
const messageId = isGroupJid
|
|
391
|
-
? await ctl.sendE2EEGroupText(tid, text, replyToMessageId, undefined)
|
|
392
|
-
: await ctl.sendE2EEText(tid, text, replyToMessageId, undefined);
|
|
393
|
-
return { messageId, timestampMs: Date.now() };
|
|
394
|
-
}
|
|
395
337
|
return this.client.sendMessage({ threadId, text, replyToMessageId });
|
|
396
338
|
}
|
|
397
339
|
|
|
@@ -440,25 +382,21 @@ class E2EEBridge {
|
|
|
440
382
|
|
|
441
383
|
async sendReaction(threadId, messageId, reaction, senderJid) {
|
|
442
384
|
this.ensureConnected();
|
|
443
|
-
await this._waitReady();
|
|
444
385
|
return this.client.sendReaction({ threadId, messageId, reaction, senderJid });
|
|
445
386
|
}
|
|
446
387
|
|
|
447
388
|
async sendTyping(threadId, isTyping) {
|
|
448
389
|
this.ensureConnected();
|
|
449
|
-
await this._waitReady(3000);
|
|
450
390
|
return this.client.sendTyping({ threadId, isTyping: isTyping !== false });
|
|
451
391
|
}
|
|
452
392
|
|
|
453
393
|
async unsendMessage(messageId, threadId) {
|
|
454
394
|
this.ensureConnected();
|
|
455
|
-
await this._waitReady();
|
|
456
395
|
return this.client.unsendMessage({ messageId, threadId });
|
|
457
396
|
}
|
|
458
397
|
|
|
459
398
|
async editMessage(threadId, messageId, newText) {
|
|
460
399
|
this.ensureConnected();
|
|
461
|
-
await this._waitReady();
|
|
462
400
|
return this.client.editMessage({ threadId, messageId, newText });
|
|
463
401
|
}
|
|
464
402
|
|
|
@@ -62,49 +62,26 @@ function encodeFrame(type, senderID, threadID, header, payload) {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
function decodeFrame(buf) {
|
|
65
|
-
if (Buffer.isBuffer(buf))
|
|
66
|
-
// Use the caller's buffer directly.
|
|
67
|
-
} else if (buf instanceof Uint8Array) {
|
|
68
|
-
buf = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
69
|
-
} else {
|
|
70
|
-
throw new TypeError("Frame must be a Buffer or Uint8Array");
|
|
71
|
-
}
|
|
65
|
+
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
|
|
72
66
|
if (buf.length < 8) throw new Error("Frame too short");
|
|
73
67
|
let offset = 0;
|
|
74
68
|
|
|
75
|
-
function requireBytes(length, label) {
|
|
76
|
-
if (!Number.isSafeInteger(length) || length < 0 || length > buf.length - offset) {
|
|
77
|
-
throw new Error(`Invalid or truncated frame ${label}`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function readLength(label) {
|
|
82
|
-
requireBytes(4, `${label} length`);
|
|
83
|
-
const length = buf.readUInt32BE(offset);
|
|
84
|
-
offset += 4;
|
|
85
|
-
requireBytes(length, label);
|
|
86
|
-
return length;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
69
|
const magic = buf.readUInt16BE(offset); offset += 2;
|
|
90
70
|
if (magic !== MAGIC) throw new Error("Invalid frame magic: " + magic.toString(16));
|
|
91
71
|
const version = buf.readUInt8(offset); offset += 1;
|
|
92
|
-
if (version !== VERSION) throw new Error("Unsupported frame version: " + version);
|
|
93
72
|
const type = buf.readUInt8(offset); offset += 1;
|
|
94
|
-
if (!Object.values(TYPE).includes(type)) throw new Error("Invalid frame type: " + type);
|
|
95
73
|
|
|
96
|
-
const sidLen =
|
|
97
|
-
const senderID = buf.
|
|
74
|
+
const sidLen = buf.readUInt32BE(offset); offset += 4;
|
|
75
|
+
const senderID = buf.slice(offset, offset + sidLen).toString("utf8"); offset += sidLen;
|
|
98
76
|
|
|
99
|
-
const tidLen =
|
|
100
|
-
const threadID = buf.
|
|
77
|
+
const tidLen = buf.readUInt32BE(offset); offset += 4;
|
|
78
|
+
const threadID = buf.slice(offset, offset + tidLen).toString("utf8"); offset += tidLen;
|
|
101
79
|
|
|
102
|
-
const hdrLen =
|
|
103
|
-
const header = buf.
|
|
80
|
+
const hdrLen = buf.readUInt32BE(offset); offset += 4;
|
|
81
|
+
const header = buf.slice(offset, offset + hdrLen); offset += hdrLen;
|
|
104
82
|
|
|
105
|
-
const payLen =
|
|
106
|
-
const payload = buf.
|
|
107
|
-
if (offset !== buf.length) throw new Error("Unexpected trailing bytes in frame");
|
|
83
|
+
const payLen = buf.readUInt32BE(offset); offset += 4;
|
|
84
|
+
const payload = buf.slice(offset, offset + payLen);
|
|
108
85
|
|
|
109
86
|
return { version, type, senderID, threadID, header, payload };
|
|
110
87
|
}
|
|
@@ -95,29 +95,6 @@ class DoubleRatchetSession {
|
|
|
95
95
|
* @returns {Buffer} decrypted plaintext
|
|
96
96
|
*/
|
|
97
97
|
decrypt(header, ciphertext, aad) {
|
|
98
|
-
const snapshot = {
|
|
99
|
-
DHs: this.DHs,
|
|
100
|
-
DHr: this.DHr,
|
|
101
|
-
RK: this.RK,
|
|
102
|
-
CKs: this.CKs,
|
|
103
|
-
CKr: this.CKr,
|
|
104
|
-
Ns: this.Ns,
|
|
105
|
-
Nr: this.Nr,
|
|
106
|
-
PN: this.PN,
|
|
107
|
-
MKSKIPPED: new Map(this.MKSKIPPED)
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
return this._decryptAndAdvance(header, ciphertext, aad);
|
|
112
|
-
} catch (error) {
|
|
113
|
-
// Authentication failure must not consume a skipped key or advance
|
|
114
|
-
// the ratchet. A later valid copy of the message can then succeed.
|
|
115
|
-
Object.assign(this, snapshot);
|
|
116
|
-
throw error;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
_decryptAndAdvance(header, ciphertext, aad) {
|
|
121
98
|
const { dh_pub, pn, n } = this._parseHeader(header);
|
|
122
99
|
const aadFull = aad ? Buffer.concat([aad, header]) : header;
|
|
123
100
|
|
|
@@ -150,9 +127,6 @@ class DoubleRatchetSession {
|
|
|
150
127
|
if (this.Nr + MAX_SKIP < until) throw new Error("Too many skipped messages: " + until);
|
|
151
128
|
if (this.CKr) {
|
|
152
129
|
while (this.Nr < until) {
|
|
153
|
-
if (this.MKSKIPPED.size >= MAX_SKIP) {
|
|
154
|
-
throw new Error("Skipped message key limit exceeded");
|
|
155
|
-
}
|
|
156
130
|
const { ck, mk } = kdfCK(this.CKr);
|
|
157
131
|
this.CKr = ck;
|
|
158
132
|
const key = `${this.DHr.toString("hex")}:${this.Nr}`;
|
|
@@ -191,9 +165,7 @@ class DoubleRatchetSession {
|
|
|
191
165
|
}
|
|
192
166
|
|
|
193
167
|
_parseHeader(header) {
|
|
194
|
-
if (
|
|
195
|
-
throw new Error("Invalid ratchet header length");
|
|
196
|
-
}
|
|
168
|
+
if (header.length < 40) throw new Error("Invalid ratchet header length");
|
|
197
169
|
return {
|
|
198
170
|
dh_pub: header.slice(0, 32),
|
|
199
171
|
pn: header.readUInt32BE(32),
|