@vinzsocket/baileys 2.8.30 → 3.0.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/WAProto/fix-imports.js +7 -16
- package/WAProto/index.js +1 -37306
- package/lib/Defaults/index.js +37 -33
- package/lib/Signal/Group/ciphertext-message.js +6 -2
- package/lib/Signal/Group/group-session-builder.js +12 -7
- package/lib/Signal/Group/group_cipher.js +18 -13
- package/lib/Signal/Group/index.js +77 -12
- package/lib/Signal/Group/keyhelper.js +15 -8
- package/lib/Signal/Group/queue-job.js +7 -2
- package/lib/Signal/Group/sender-chain-key.js +11 -6
- package/lib/Signal/Group/sender-key-distribution-message.js +10 -6
- package/lib/Signal/Group/sender-key-message.js +14 -9
- package/lib/Signal/Group/sender-key-name.js +6 -2
- package/lib/Signal/Group/sender-key-record.js +13 -9
- package/lib/Signal/Group/sender-key-state.js +10 -6
- package/lib/Signal/Group/sender-message-key.js +9 -4
- package/lib/Signal/libsignal.js +34 -28
- package/lib/Signal/lid-mapping.js +24 -19
- package/lib/Socket/Client/index.js +25 -3
- package/lib/Socket/Client/types.js +8 -4
- package/lib/Socket/Client/websocket.js +16 -11
- package/lib/Socket/business.js +38 -34
- package/lib/Socket/chats.js +100 -95
- package/lib/Socket/communities.js +55 -49
- package/lib/Socket/groups.js +56 -51
- package/lib/Socket/index.js +9 -6
- package/lib/Socket/messages-recv.js +150 -145
- package/lib/Socket/messages-send.js +109 -104
- package/lib/Socket/mex.js +12 -8
- package/lib/Socket/newsletter.js +27 -23
- package/lib/Socket/socket.js +111 -107
- package/lib/Store/index.js +47 -4
- package/lib/Store/make-cache-manager-store.js +17 -11
- package/lib/Store/make-in-memory-store.js +31 -29
- package/lib/Store/make-ordered-dictionary.js +7 -2
- package/lib/Store/object-repository.js +7 -2
- package/lib/Types/Auth.js +3 -2
- package/lib/Types/Bussines.js +3 -2
- package/lib/Types/Call.js +3 -2
- package/lib/Types/Chat.js +5 -2
- package/lib/Types/Contact.js +3 -2
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +3 -2
- package/lib/Types/Label.js +6 -3
- package/lib/Types/LabelAssociation.js +6 -3
- package/lib/Types/Message.js +16 -7
- package/lib/Types/Newsletter.js +8 -5
- package/lib/Types/Product.js +3 -2
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +7 -4
- package/lib/Types/USync.js +1 -2
- package/lib/Types/index.js +153 -15
- package/lib/Utils/auth-utils.js +32 -25
- package/lib/Utils/baileys-event-stream.js +21 -15
- package/lib/Utils/business.js +71 -61
- package/lib/Utils/chat-utils.js +91 -77
- package/lib/Utils/crypto.js +54 -32
- package/lib/Utils/decode-wa-message.js +46 -39
- package/lib/Utils/event-buffer.js +24 -19
- package/lib/Utils/extended-messages.js +14 -9
- package/lib/Utils/generics.js +100 -64
- package/lib/Utils/history.js +30 -23
- package/lib/Utils/index.js +234 -22
- package/lib/Utils/lid-resolver.js +12 -5
- package/lib/Utils/link-preview.js +11 -7
- package/lib/Utils/logger.js +8 -4
- package/lib/Utils/lt-hash.js +8 -5
- package/lib/Utils/make-mutex.js +8 -3
- package/lib/Utils/mention.js +12 -4
- package/lib/Utils/message-retry-manager.js +10 -6
- package/lib/Utils/messages-media.js +127 -95
- package/lib/Utils/messages.js +122 -98
- package/lib/Utils/noise-handler.js +25 -21
- package/lib/Utils/process-message.js +72 -63
- package/lib/Utils/signal.js +45 -33
- package/lib/Utils/use-multi-file-auth-state.js +24 -20
- package/lib/Utils/validate-connection.js +54 -47
- package/lib/WABinary/constants.js +9 -6
- package/lib/WABinary/decode.js +17 -10
- package/lib/WABinary/encode.js +11 -6
- package/lib/WABinary/generic-utils.js +31 -17
- package/lib/WABinary/index.js +58 -6
- package/lib/WABinary/jid-utils.js +36 -20
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +6 -2
- package/lib/WAM/constants.js +1 -1
- package/lib/WAM/encode.js +16 -12
- package/lib/WAM/index.js +36 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +9 -5
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +10 -6
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +8 -4
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +8 -4
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +9 -4
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +23 -19
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +6 -2
- package/lib/WAUSync/Protocols/index.js +58 -6
- package/lib/WAUSync/USyncQuery.js +21 -17
- package/lib/WAUSync/USyncUser.js +6 -2
- package/lib/WAUSync/index.js +36 -4
- package/lib/index.js +112 -12
- package/package.json +8 -6
|
@@ -1,19 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
Object.defineProperty(exports, "__esModule", {
|
|
2
|
+
value: true
|
|
3
|
+
});
|
|
4
|
+
exports.encryptedStream = exports.encryptMediaRetryRequest = exports.encodeBase64EncodedStringForUpload = exports.downloadEncryptedContent = exports.downloadContentFromMessage = exports.decryptMediaRetryData = exports.decodeMediaRetryNode = void 0;
|
|
5
|
+
exports.extensionForMediaMessage = extensionForMediaMessage;
|
|
6
|
+
exports.generateProfilePicture = exports.extractImageThumb = void 0;
|
|
7
|
+
exports.generateThumbnail = generateThumbnail;
|
|
8
|
+
exports.getAudioDuration = getAudioDuration;
|
|
9
|
+
exports.getAudioWaveform = getAudioWaveform;
|
|
10
|
+
exports.getHttpStream = void 0;
|
|
11
|
+
exports.getMediaKeys = getMediaKeys;
|
|
12
|
+
exports.toReadable = exports.toBuffer = exports.mediaMessageSHA256B64 = exports.hkdfInfoKey = exports.getWAUploadToServer = exports.getUrlFromDirectPath = exports.getStream = exports.getStatusCodeForMediaRetry = exports.getRawMediaUploadData = void 0;
|
|
13
|
+
var _boom = require("@hapi/boom");
|
|
14
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
15
|
+
var _child_process = require("child_process");
|
|
16
|
+
var Crypto = _interopRequireWildcard(require("crypto"));
|
|
17
|
+
var _events = require("events");
|
|
18
|
+
var _fs = require("fs");
|
|
19
|
+
var _os = require("os");
|
|
20
|
+
var _path = require("path");
|
|
21
|
+
var _stream = require("stream");
|
|
22
|
+
var _url = require("url");
|
|
23
|
+
var _index = require("../../WAProto/index.js");
|
|
24
|
+
var _index2 = require("../Defaults/index.js");
|
|
25
|
+
var _index3 = require("../WABinary/index.js");
|
|
26
|
+
var _crypto2 = require("./crypto.js");
|
|
27
|
+
var _generics = require("./generics.js");
|
|
28
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
29
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
30
|
+
const getTmpFilesDirectory = () => (0, _os.tmpdir)();
|
|
17
31
|
const getImageProcessingLibrary = async () => {
|
|
18
32
|
//@ts-ignore
|
|
19
33
|
const [jimp, sharp] = await Promise.all([import('jimp').catch(() => {}), import('sharp').catch(() => {})]);
|
|
@@ -27,31 +41,32 @@ const getImageProcessingLibrary = async () => {
|
|
|
27
41
|
jimp
|
|
28
42
|
};
|
|
29
43
|
}
|
|
30
|
-
throw new Boom('No image processing library available');
|
|
44
|
+
throw new _boom.Boom('No image processing library available');
|
|
31
45
|
};
|
|
32
|
-
|
|
33
|
-
const hkdfInfo = MEDIA_HKDF_KEY_MAPPING[type];
|
|
46
|
+
const hkdfInfoKey = type => {
|
|
47
|
+
const hkdfInfo = _index2.MEDIA_HKDF_KEY_MAPPING[type];
|
|
34
48
|
return `WhatsApp ${hkdfInfo} Keys`;
|
|
35
49
|
};
|
|
36
|
-
|
|
50
|
+
exports.hkdfInfoKey = hkdfInfoKey;
|
|
51
|
+
const getRawMediaUploadData = async (media, mediaType, logger) => {
|
|
37
52
|
const {
|
|
38
53
|
stream
|
|
39
54
|
} = await getStream(media);
|
|
40
55
|
logger?.debug('got stream for raw upload');
|
|
41
56
|
const hasher = Crypto.createHash('sha256');
|
|
42
|
-
const filePath = join(tmpdir(), mediaType + generateMessageIDV2());
|
|
43
|
-
const fileWriteStream = createWriteStream(filePath);
|
|
57
|
+
const filePath = (0, _path.join)((0, _os.tmpdir)(), mediaType + (0, _generics.generateMessageIDV2)());
|
|
58
|
+
const fileWriteStream = (0, _fs.createWriteStream)(filePath);
|
|
44
59
|
let fileLength = 0;
|
|
45
60
|
try {
|
|
46
61
|
for await (const data of stream) {
|
|
47
62
|
fileLength += data.length;
|
|
48
63
|
hasher.update(data);
|
|
49
64
|
if (!fileWriteStream.write(data)) {
|
|
50
|
-
await once(fileWriteStream, 'drain');
|
|
65
|
+
await (0, _events.once)(fileWriteStream, 'drain');
|
|
51
66
|
}
|
|
52
67
|
}
|
|
53
68
|
fileWriteStream.end();
|
|
54
|
-
await once(fileWriteStream, 'finish');
|
|
69
|
+
await (0, _events.once)(fileWriteStream, 'finish');
|
|
55
70
|
stream.destroy();
|
|
56
71
|
const fileSha256 = hasher.digest();
|
|
57
72
|
logger?.debug('hashed data for raw upload');
|
|
@@ -64,7 +79,7 @@ export const getRawMediaUploadData = async (media, mediaType, logger) => {
|
|
|
64
79
|
fileWriteStream.destroy();
|
|
65
80
|
stream.destroy();
|
|
66
81
|
try {
|
|
67
|
-
await
|
|
82
|
+
await _fs.promises.unlink(filePath);
|
|
68
83
|
} catch {
|
|
69
84
|
//
|
|
70
85
|
}
|
|
@@ -72,15 +87,16 @@ export const getRawMediaUploadData = async (media, mediaType, logger) => {
|
|
|
72
87
|
}
|
|
73
88
|
};
|
|
74
89
|
/** generates all the keys required to encrypt/decrypt & sign a media message */
|
|
75
|
-
|
|
90
|
+
exports.getRawMediaUploadData = getRawMediaUploadData;
|
|
91
|
+
async function getMediaKeys(buffer, mediaType) {
|
|
76
92
|
if (!buffer) {
|
|
77
|
-
throw new Boom('Cannot derive from empty media key');
|
|
93
|
+
throw new _boom.Boom('Cannot derive from empty media key');
|
|
78
94
|
}
|
|
79
95
|
if (typeof buffer === 'string') {
|
|
80
96
|
buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64');
|
|
81
97
|
}
|
|
82
98
|
// expand using HKDF to 112 bytes, also pass in the relevant app info
|
|
83
|
-
const expandedMediaKey = await hkdf(buffer, 112, {
|
|
99
|
+
const expandedMediaKey = await (0, _crypto2.hkdf)(buffer, 112, {
|
|
84
100
|
info: hkdfInfoKey(mediaType)
|
|
85
101
|
});
|
|
86
102
|
return {
|
|
@@ -92,7 +108,7 @@ export async function getMediaKeys(buffer, mediaType) {
|
|
|
92
108
|
/** Extracts video thumb using FFMPEG */
|
|
93
109
|
const extractVideoThumb = async (path, destPath, time, size) => new Promise((resolve, reject) => {
|
|
94
110
|
const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`;
|
|
95
|
-
exec(cmd, err => {
|
|
111
|
+
(0, _child_process.exec)(cmd, err => {
|
|
96
112
|
if (err) {
|
|
97
113
|
reject(err);
|
|
98
114
|
} else {
|
|
@@ -100,10 +116,10 @@ const extractVideoThumb = async (path, destPath, time, size) => new Promise((res
|
|
|
100
116
|
}
|
|
101
117
|
});
|
|
102
118
|
});
|
|
103
|
-
|
|
119
|
+
const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
104
120
|
// TODO: Move entirely to sharp, removing jimp as it supports readable streams
|
|
105
121
|
// This will have positive speed and performance impacts as well as minimizing RAM usage.
|
|
106
|
-
if (bufferOrFilePath instanceof Readable) {
|
|
122
|
+
if (bufferOrFilePath instanceof _stream.Readable) {
|
|
107
123
|
bufferOrFilePath = await toBuffer(bufferOrFilePath);
|
|
108
124
|
}
|
|
109
125
|
const lib = await getImageProcessingLibrary();
|
|
@@ -137,11 +153,13 @@ export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
|
137
153
|
original: dimensions
|
|
138
154
|
};
|
|
139
155
|
} else {
|
|
140
|
-
throw new Boom('No image processing library available');
|
|
156
|
+
throw new _boom.Boom('No image processing library available');
|
|
141
157
|
}
|
|
142
158
|
};
|
|
143
|
-
|
|
144
|
-
|
|
159
|
+
exports.extractImageThumb = extractImageThumb;
|
|
160
|
+
const encodeBase64EncodedStringForUpload = b64 => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''));
|
|
161
|
+
exports.encodeBase64EncodedStringForUpload = encodeBase64EncodedStringForUpload;
|
|
162
|
+
const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
145
163
|
let buffer;
|
|
146
164
|
const {
|
|
147
165
|
width: w = 640,
|
|
@@ -180,18 +198,20 @@ export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
|
180
198
|
quality: 50
|
|
181
199
|
});
|
|
182
200
|
} else {
|
|
183
|
-
throw new Boom('No image processing library available');
|
|
201
|
+
throw new _boom.Boom('No image processing library available');
|
|
184
202
|
}
|
|
185
203
|
return {
|
|
186
204
|
img: await img
|
|
187
205
|
};
|
|
188
206
|
};
|
|
189
207
|
/** gets the SHA256 of the given media message */
|
|
190
|
-
|
|
208
|
+
exports.generateProfilePicture = generateProfilePicture;
|
|
209
|
+
const mediaMessageSHA256B64 = message => {
|
|
191
210
|
const media = Object.values(message)[0];
|
|
192
211
|
return media?.fileSha256 && Buffer.from(media.fileSha256).toString('base64');
|
|
193
212
|
};
|
|
194
|
-
|
|
213
|
+
exports.mediaMessageSHA256B64 = mediaMessageSHA256B64;
|
|
214
|
+
async function getAudioDuration(buffer) {
|
|
195
215
|
const musicMetadata = await import('music-metadata');
|
|
196
216
|
let metadata;
|
|
197
217
|
const options = {
|
|
@@ -209,7 +229,7 @@ export async function getAudioDuration(buffer) {
|
|
|
209
229
|
/**
|
|
210
230
|
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
211
231
|
*/
|
|
212
|
-
|
|
232
|
+
async function getAudioWaveform(buffer, logger) {
|
|
213
233
|
try {
|
|
214
234
|
// @ts-ignore
|
|
215
235
|
const {
|
|
@@ -219,7 +239,7 @@ export async function getAudioWaveform(buffer, logger) {
|
|
|
219
239
|
if (Buffer.isBuffer(buffer)) {
|
|
220
240
|
audioData = buffer;
|
|
221
241
|
} else if (typeof buffer === 'string') {
|
|
222
|
-
const rStream = createReadStream(buffer);
|
|
242
|
+
const rStream = (0, _fs.createReadStream)(buffer);
|
|
223
243
|
audioData = await toBuffer(rStream);
|
|
224
244
|
} else {
|
|
225
245
|
audioData = await toBuffer(buffer);
|
|
@@ -247,15 +267,16 @@ export async function getAudioWaveform(buffer, logger) {
|
|
|
247
267
|
logger?.debug('Failed to generate waveform: ' + e);
|
|
248
268
|
}
|
|
249
269
|
}
|
|
250
|
-
|
|
251
|
-
const readable = new Readable({
|
|
270
|
+
const toReadable = buffer => {
|
|
271
|
+
const readable = new _stream.Readable({
|
|
252
272
|
read: () => {}
|
|
253
273
|
});
|
|
254
274
|
readable.push(buffer);
|
|
255
275
|
readable.push(null);
|
|
256
276
|
return readable;
|
|
257
277
|
};
|
|
258
|
-
|
|
278
|
+
exports.toReadable = toReadable;
|
|
279
|
+
const toBuffer = async stream => {
|
|
259
280
|
const chunks = [];
|
|
260
281
|
for await (const chunk of stream) {
|
|
261
282
|
chunks.push(chunk);
|
|
@@ -263,7 +284,8 @@ export const toBuffer = async stream => {
|
|
|
263
284
|
stream.destroy();
|
|
264
285
|
return Buffer.concat(chunks);
|
|
265
286
|
};
|
|
266
|
-
|
|
287
|
+
exports.toBuffer = toBuffer;
|
|
288
|
+
const getStream = async (item, opts) => {
|
|
267
289
|
if (Buffer.isBuffer(item)) {
|
|
268
290
|
return {
|
|
269
291
|
stream: toReadable(item),
|
|
@@ -291,12 +313,13 @@ export const getStream = async (item, opts) => {
|
|
|
291
313
|
};
|
|
292
314
|
}
|
|
293
315
|
return {
|
|
294
|
-
stream: createReadStream(item.url),
|
|
316
|
+
stream: (0, _fs.createReadStream)(item.url),
|
|
295
317
|
type: 'file'
|
|
296
318
|
};
|
|
297
319
|
};
|
|
298
320
|
/** generates a thumbnail for a given media, if required */
|
|
299
|
-
|
|
321
|
+
exports.getStream = getStream;
|
|
322
|
+
async function generateThumbnail(file, mediaType, options) {
|
|
300
323
|
let thumbnail;
|
|
301
324
|
let originalImageDimensions;
|
|
302
325
|
if (mediaType === 'image') {
|
|
@@ -312,15 +335,15 @@ export async function generateThumbnail(file, mediaType, options) {
|
|
|
312
335
|
};
|
|
313
336
|
}
|
|
314
337
|
} else if (mediaType === 'video') {
|
|
315
|
-
const imgFilename = join(getTmpFilesDirectory(), generateMessageIDV2() + '.jpg');
|
|
338
|
+
const imgFilename = (0, _path.join)(getTmpFilesDirectory(), (0, _generics.generateMessageIDV2)() + '.jpg');
|
|
316
339
|
try {
|
|
317
340
|
await extractVideoThumb(file, imgFilename, '00:00:00', {
|
|
318
341
|
width: 32,
|
|
319
342
|
height: 32
|
|
320
343
|
});
|
|
321
|
-
const buff = await
|
|
344
|
+
const buff = await _fs.promises.readFile(imgFilename);
|
|
322
345
|
thumbnail = buff.toString('base64');
|
|
323
|
-
await
|
|
346
|
+
await _fs.promises.unlink(imgFilename);
|
|
324
347
|
} catch (err) {
|
|
325
348
|
options.logger?.debug('could not generate video thumb: ' + err);
|
|
326
349
|
}
|
|
@@ -330,14 +353,15 @@ export async function generateThumbnail(file, mediaType, options) {
|
|
|
330
353
|
originalImageDimensions
|
|
331
354
|
};
|
|
332
355
|
}
|
|
333
|
-
|
|
334
|
-
const fetched = await
|
|
356
|
+
const getHttpStream = async (url, options = {}) => {
|
|
357
|
+
const fetched = await _axios.default.get(url.toString(), {
|
|
335
358
|
...options,
|
|
336
359
|
responseType: 'stream'
|
|
337
360
|
});
|
|
338
361
|
return fetched.data;
|
|
339
362
|
};
|
|
340
|
-
|
|
363
|
+
exports.getHttpStream = getHttpStream;
|
|
364
|
+
const encryptedStream = async (media, mediaType, {
|
|
341
365
|
logger,
|
|
342
366
|
saveOriginalFileIfRequired,
|
|
343
367
|
opts
|
|
@@ -353,13 +377,13 @@ export const encryptedStream = async (media, mediaType, {
|
|
|
353
377
|
iv,
|
|
354
378
|
macKey
|
|
355
379
|
} = await getMediaKeys(mediaKey, mediaType);
|
|
356
|
-
const encFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-enc');
|
|
357
|
-
const encFileWriteStream = createWriteStream(encFilePath);
|
|
380
|
+
const encFilePath = (0, _path.join)(getTmpFilesDirectory(), mediaType + (0, _generics.generateMessageIDV2)() + '-enc');
|
|
381
|
+
const encFileWriteStream = (0, _fs.createWriteStream)(encFilePath);
|
|
358
382
|
let originalFileStream;
|
|
359
383
|
let originalFilePath;
|
|
360
384
|
if (saveOriginalFileIfRequired) {
|
|
361
|
-
originalFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-original');
|
|
362
|
-
originalFileStream = createWriteStream(originalFilePath);
|
|
385
|
+
originalFilePath = (0, _path.join)(getTmpFilesDirectory(), mediaType + (0, _generics.generateMessageIDV2)() + '-original');
|
|
386
|
+
originalFileStream = (0, _fs.createWriteStream)(originalFilePath);
|
|
363
387
|
}
|
|
364
388
|
let fileLength = 0;
|
|
365
389
|
const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
|
|
@@ -375,7 +399,7 @@ export const encryptedStream = async (media, mediaType, {
|
|
|
375
399
|
for await (const data of stream) {
|
|
376
400
|
fileLength += data.length;
|
|
377
401
|
if (type === 'remote' && opts?.maxContentLength && fileLength + data.length > opts.maxContentLength) {
|
|
378
|
-
throw new Boom(`content length exceeded when encrypting "${type}"`, {
|
|
402
|
+
throw new _boom.Boom(`content length exceeded when encrypting "${type}"`, {
|
|
379
403
|
data: {
|
|
380
404
|
media,
|
|
381
405
|
type
|
|
@@ -384,7 +408,7 @@ export const encryptedStream = async (media, mediaType, {
|
|
|
384
408
|
}
|
|
385
409
|
if (originalFileStream) {
|
|
386
410
|
if (!originalFileStream.write(data)) {
|
|
387
|
-
await once(originalFileStream, 'drain');
|
|
411
|
+
await (0, _events.once)(originalFileStream, 'drain');
|
|
388
412
|
}
|
|
389
413
|
}
|
|
390
414
|
sha256Plain.update(data);
|
|
@@ -419,9 +443,9 @@ export const encryptedStream = async (media, mediaType, {
|
|
|
419
443
|
sha256Enc.destroy();
|
|
420
444
|
stream.destroy();
|
|
421
445
|
try {
|
|
422
|
-
await
|
|
446
|
+
await _fs.promises.unlink(encFilePath);
|
|
423
447
|
if (originalFilePath) {
|
|
424
|
-
await
|
|
448
|
+
await _fs.promises.unlink(originalFilePath);
|
|
425
449
|
}
|
|
426
450
|
} catch (err) {
|
|
427
451
|
logger?.error({
|
|
@@ -431,13 +455,15 @@ export const encryptedStream = async (media, mediaType, {
|
|
|
431
455
|
throw error;
|
|
432
456
|
}
|
|
433
457
|
};
|
|
458
|
+
exports.encryptedStream = encryptedStream;
|
|
434
459
|
const DEF_HOST = 'mmg.whatsapp.net';
|
|
435
460
|
const AES_CHUNK_SIZE = 16;
|
|
436
461
|
const toSmallestChunkSize = num => {
|
|
437
462
|
return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
|
|
438
463
|
};
|
|
439
|
-
|
|
440
|
-
|
|
464
|
+
const getUrlFromDirectPath = directPath => `https://${DEF_HOST}${directPath}`;
|
|
465
|
+
exports.getUrlFromDirectPath = getUrlFromDirectPath;
|
|
466
|
+
const downloadContentFromMessage = async ({
|
|
441
467
|
mediaKey,
|
|
442
468
|
directPath,
|
|
443
469
|
url
|
|
@@ -445,7 +471,7 @@ export const downloadContentFromMessage = async ({
|
|
|
445
471
|
const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/');
|
|
446
472
|
const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
|
|
447
473
|
if (!downloadUrl) {
|
|
448
|
-
throw new Boom('No valid media URL or directPath present in message', {
|
|
474
|
+
throw new _boom.Boom('No valid media URL or directPath present in message', {
|
|
449
475
|
statusCode: 400
|
|
450
476
|
});
|
|
451
477
|
}
|
|
@@ -456,7 +482,8 @@ export const downloadContentFromMessage = async ({
|
|
|
456
482
|
* Decrypts and downloads an AES256-CBC encrypted file given the keys.
|
|
457
483
|
* Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
|
|
458
484
|
* */
|
|
459
|
-
|
|
485
|
+
exports.downloadContentFromMessage = downloadContentFromMessage;
|
|
486
|
+
const downloadEncryptedContent = async (downloadUrl, {
|
|
460
487
|
cipherKey,
|
|
461
488
|
iv
|
|
462
489
|
}, {
|
|
@@ -479,7 +506,7 @@ export const downloadEncryptedContent = async (downloadUrl, {
|
|
|
479
506
|
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
|
|
480
507
|
const headers = {
|
|
481
508
|
...(options?.headers || {}),
|
|
482
|
-
Origin: DEFAULT_ORIGIN
|
|
509
|
+
Origin: _index2.DEFAULT_ORIGIN
|
|
483
510
|
};
|
|
484
511
|
if (startChunk || endChunk) {
|
|
485
512
|
headers.Range = `bytes=${startChunk}-`;
|
|
@@ -506,7 +533,7 @@ export const downloadEncryptedContent = async (downloadUrl, {
|
|
|
506
533
|
push(bytes);
|
|
507
534
|
}
|
|
508
535
|
};
|
|
509
|
-
const output = new Transform({
|
|
536
|
+
const output = new _stream.Transform({
|
|
510
537
|
transform(chunk, _, callback) {
|
|
511
538
|
let data = Buffer.concat([remainingBytes, chunk]);
|
|
512
539
|
const decryptLength = toSmallestChunkSize(data.length);
|
|
@@ -545,7 +572,8 @@ export const downloadEncryptedContent = async (downloadUrl, {
|
|
|
545
572
|
end: true
|
|
546
573
|
});
|
|
547
574
|
};
|
|
548
|
-
|
|
575
|
+
exports.downloadEncryptedContent = downloadEncryptedContent;
|
|
576
|
+
function extensionForMediaMessage(message) {
|
|
549
577
|
const getExtension = mimetype => mimetype.split(';')[0]?.split('/')[1];
|
|
550
578
|
const type = Object.keys(message)[0];
|
|
551
579
|
let extension;
|
|
@@ -557,7 +585,7 @@ export function extensionForMediaMessage(message) {
|
|
|
557
585
|
}
|
|
558
586
|
return extension;
|
|
559
587
|
}
|
|
560
|
-
|
|
588
|
+
const getWAUploadToServer = ({
|
|
561
589
|
customUploadHosts,
|
|
562
590
|
fetchAgent,
|
|
563
591
|
logger,
|
|
@@ -578,17 +606,17 @@ export const getWAUploadToServer = ({
|
|
|
578
606
|
} of hosts) {
|
|
579
607
|
logger.debug(`uploading to "${hostname}"`);
|
|
580
608
|
const auth = encodeURIComponent(uploadInfo.auth); // the auth token
|
|
581
|
-
const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
609
|
+
const url = `https://${hostname}${_index2.MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
582
610
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
583
611
|
let result;
|
|
584
612
|
try {
|
|
585
|
-
const body = await
|
|
613
|
+
const body = await _axios.default.post(url, (0, _fs.createReadStream)(filePath), {
|
|
586
614
|
...options,
|
|
587
615
|
maxRedirects: 0,
|
|
588
616
|
headers: {
|
|
589
617
|
...(options.headers || {}),
|
|
590
618
|
'Content-Type': 'application/octet-stream',
|
|
591
|
-
Origin: DEFAULT_ORIGIN
|
|
619
|
+
Origin: _index2.DEFAULT_ORIGIN
|
|
592
620
|
},
|
|
593
621
|
httpsAgent: fetchAgent,
|
|
594
622
|
timeout: timeoutMs,
|
|
@@ -611,7 +639,7 @@ export const getWAUploadToServer = ({
|
|
|
611
639
|
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
612
640
|
}
|
|
613
641
|
} catch (error) {
|
|
614
|
-
if (
|
|
642
|
+
if (_axios.default.isAxiosError(error)) {
|
|
615
643
|
result = error.response?.data;
|
|
616
644
|
}
|
|
617
645
|
const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
|
|
@@ -622,34 +650,35 @@ export const getWAUploadToServer = ({
|
|
|
622
650
|
}
|
|
623
651
|
}
|
|
624
652
|
if (!urls) {
|
|
625
|
-
throw new Boom('Media upload failed on all hosts', {
|
|
653
|
+
throw new _boom.Boom('Media upload failed on all hosts', {
|
|
626
654
|
statusCode: 500
|
|
627
655
|
});
|
|
628
656
|
}
|
|
629
657
|
return urls;
|
|
630
658
|
};
|
|
631
659
|
};
|
|
660
|
+
exports.getWAUploadToServer = getWAUploadToServer;
|
|
632
661
|
const getMediaRetryKey = mediaKey => {
|
|
633
|
-
return hkdf(mediaKey, 32, {
|
|
662
|
+
return (0, _crypto2.hkdf)(mediaKey, 32, {
|
|
634
663
|
info: 'WhatsApp Media Retry Notification'
|
|
635
664
|
});
|
|
636
665
|
};
|
|
637
666
|
/**
|
|
638
667
|
* Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
|
|
639
668
|
*/
|
|
640
|
-
|
|
669
|
+
const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
|
|
641
670
|
const recp = {
|
|
642
671
|
stanzaId: key.id
|
|
643
672
|
};
|
|
644
|
-
const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
|
|
673
|
+
const recpBuffer = _index.proto.ServerErrorReceipt.encode(recp).finish();
|
|
645
674
|
const iv = Crypto.randomBytes(12);
|
|
646
675
|
const retryKey = await getMediaRetryKey(mediaKey);
|
|
647
|
-
const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
|
|
676
|
+
const ciphertext = (0, _crypto2.aesEncryptGCM)(recpBuffer, retryKey, iv, Buffer.from(key.id));
|
|
648
677
|
const req = {
|
|
649
678
|
tag: 'receipt',
|
|
650
679
|
attrs: {
|
|
651
680
|
id: key.id,
|
|
652
|
-
to: jidNormalizedUser(meId),
|
|
681
|
+
to: (0, _index3.jidNormalizedUser)(meId),
|
|
653
682
|
type: 'server-error'
|
|
654
683
|
},
|
|
655
684
|
content: [
|
|
@@ -680,8 +709,9 @@ export const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
|
|
|
680
709
|
};
|
|
681
710
|
return req;
|
|
682
711
|
};
|
|
683
|
-
|
|
684
|
-
|
|
712
|
+
exports.encryptMediaRetryRequest = encryptMediaRetryRequest;
|
|
713
|
+
const decodeMediaRetryNode = node => {
|
|
714
|
+
const rmrNode = (0, _index3.getBinaryNodeChild)(node, 'rmr');
|
|
685
715
|
const event = {
|
|
686
716
|
key: {
|
|
687
717
|
id: node.attrs.id,
|
|
@@ -690,43 +720,45 @@ export const decodeMediaRetryNode = node => {
|
|
|
690
720
|
participant: rmrNode.attrs.participant
|
|
691
721
|
}
|
|
692
722
|
};
|
|
693
|
-
const errorNode = getBinaryNodeChild(node, 'error');
|
|
723
|
+
const errorNode = (0, _index3.getBinaryNodeChild)(node, 'error');
|
|
694
724
|
if (errorNode) {
|
|
695
725
|
const errorCode = +errorNode.attrs.code;
|
|
696
|
-
event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
|
|
726
|
+
event.error = new _boom.Boom(`Failed to re-upload media (${errorCode})`, {
|
|
697
727
|
data: errorNode.attrs,
|
|
698
728
|
statusCode: getStatusCodeForMediaRetry(errorCode)
|
|
699
729
|
});
|
|
700
730
|
} else {
|
|
701
|
-
const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt');
|
|
702
|
-
const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p');
|
|
703
|
-
const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv');
|
|
731
|
+
const encryptedInfoNode = (0, _index3.getBinaryNodeChild)(node, 'encrypt');
|
|
732
|
+
const ciphertext = (0, _index3.getBinaryNodeChildBuffer)(encryptedInfoNode, 'enc_p');
|
|
733
|
+
const iv = (0, _index3.getBinaryNodeChildBuffer)(encryptedInfoNode, 'enc_iv');
|
|
704
734
|
if (ciphertext && iv) {
|
|
705
735
|
event.media = {
|
|
706
736
|
ciphertext,
|
|
707
737
|
iv
|
|
708
738
|
};
|
|
709
739
|
} else {
|
|
710
|
-
event.error = new Boom('Failed to re-upload media (missing ciphertext)', {
|
|
740
|
+
event.error = new _boom.Boom('Failed to re-upload media (missing ciphertext)', {
|
|
711
741
|
statusCode: 404
|
|
712
742
|
});
|
|
713
743
|
}
|
|
714
744
|
}
|
|
715
745
|
return event;
|
|
716
746
|
};
|
|
717
|
-
|
|
747
|
+
exports.decodeMediaRetryNode = decodeMediaRetryNode;
|
|
748
|
+
const decryptMediaRetryData = async ({
|
|
718
749
|
ciphertext,
|
|
719
750
|
iv
|
|
720
751
|
}, mediaKey, msgId) => {
|
|
721
752
|
const retryKey = await getMediaRetryKey(mediaKey);
|
|
722
|
-
const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId));
|
|
723
|
-
return proto.MediaRetryNotification.decode(plaintext);
|
|
753
|
+
const plaintext = (0, _crypto2.aesDecryptGCM)(ciphertext, retryKey, iv, Buffer.from(msgId));
|
|
754
|
+
return _index.proto.MediaRetryNotification.decode(plaintext);
|
|
724
755
|
};
|
|
725
|
-
|
|
756
|
+
exports.decryptMediaRetryData = decryptMediaRetryData;
|
|
757
|
+
const getStatusCodeForMediaRetry = code => MEDIA_RETRY_STATUS_MAP[code];
|
|
758
|
+
exports.getStatusCodeForMediaRetry = getStatusCodeForMediaRetry;
|
|
726
759
|
const MEDIA_RETRY_STATUS_MAP = {
|
|
727
|
-
[proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
|
|
728
|
-
[proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
|
|
729
|
-
[proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
|
730
|
-
[proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
|
|
731
|
-
};
|
|
732
|
-
//# sourceMappingURL=messages-media.js.map
|
|
760
|
+
[_index.proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
|
|
761
|
+
[_index.proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
|
|
762
|
+
[_index.proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
|
763
|
+
[_index.proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
|
|
764
|
+
};
|