@skyzopedia/baileys-mod 5.0.8 → 6.0.1
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/index.js +133384 -57814
- package/engine-requirements.js +10 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +141 -117
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +14 -12
- package/lib/Signal/Group/group-session-builder.d.ts +14 -0
- package/lib/Signal/Group/group-session-builder.js +42 -10
- package/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/lib/Signal/Group/group_cipher.js +87 -75
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +57 -13
- package/lib/Signal/Group/keyhelper.d.ts +10 -0
- package/lib/Signal/Group/keyhelper.js +52 -17
- package/lib/Signal/Group/queue-job.d.ts +1 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +33 -27
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -62
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +66 -65
- package/lib/Signal/Group/sender-key-name.d.ts +17 -0
- package/lib/Signal/Group/sender-key-name.js +44 -45
- package/lib/Signal/Group/sender-key-record.d.ts +30 -0
- package/lib/Signal/Group/sender-key-record.js +49 -39
- package/lib/Signal/Group/sender-key-state.d.ts +38 -0
- package/lib/Signal/Group/sender-key-state.js +93 -80
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +28 -27
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +163 -313
- package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.js +19 -4
- package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.d.ts +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.d.ts +171 -0
- package/lib/Socket/business.js +242 -359
- package/lib/Socket/chats.d.ts +267 -0
- package/lib/Socket/chats.js +935 -846
- package/lib/Socket/dugong.d.ts +254 -0
- package/lib/Socket/dugong.js +484 -0
- package/lib/Socket/groups.d.ts +115 -0
- package/lib/Socket/groups.js +309 -304
- package/lib/Socket/index.d.ts +173 -0
- package/lib/Socket/index.js +10 -15
- package/lib/Socket/messages-recv.d.ts +161 -0
- package/lib/Socket/messages-recv.js +1054 -1107
- package/lib/Socket/messages-send.d.ts +149 -0
- package/lib/Socket/messages-send.js +447 -706
- package/lib/Socket/newsletter.d.ts +134 -0
- package/lib/Socket/newsletter.js +314 -199
- package/lib/Socket/registration.d.ts +267 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.d.ts +43 -0
- package/lib/Socket/socket.js +650 -777
- package/lib/Socket/usync.d.ts +36 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -6
- package/lib/Store/make-cache-manager-store.d.ts +13 -0
- package/lib/Store/make-cache-manager-store.js +81 -73
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +423 -286
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +79 -77
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +26 -24
- package/lib/Types/Auth.d.ts +110 -0
- package/lib/Types/Auth.js +2 -3
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -3
- package/lib/Types/Chat.d.ts +102 -0
- package/lib/Types/Chat.js +4 -9
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -3
- package/lib/Types/Events.d.ts +157 -0
- package/lib/Types/Events.js +2 -3
- package/lib/Types/GroupMetadata.d.ts +55 -0
- package/lib/Types/GroupMetadata.js +2 -3
- package/lib/Types/Label.d.ts +35 -0
- package/lib/Types/Label.js +26 -24
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +8 -6
- package/lib/Types/Message.d.ts +273 -0
- package/lib/Types/Message.js +9 -12
- package/lib/Types/Newsletter.d.ts +103 -0
- package/lib/Types/Newsletter.js +38 -33
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -3
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -3
- package/lib/Types/Socket.d.ts +111 -0
- package/lib/Types/Socket.js +2 -4
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -11
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -3
- package/lib/Types/index.d.ts +57 -0
- package/lib/Types/index.js +41 -27
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +198 -211
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +61 -42
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +214 -213
- package/lib/Utils/chat-utils.d.ts +71 -0
- package/lib/Utils/chat-utils.js +687 -710
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +133 -112
- package/lib/Utils/decode-wa-message.d.ts +19 -0
- package/lib/Utils/decode-wa-message.js +183 -252
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +496 -510
- package/lib/Utils/generics.d.ts +92 -0
- package/lib/Utils/generics.js +392 -319
- package/lib/Utils/generics.js.bak +433 -0
- package/lib/Utils/history.d.ts +15 -0
- package/lib/Utils/history.js +92 -83
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -21
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +83 -71
- package/lib/Utils/logger.d.ts +4 -0
- package/lib/Utils/logger.js +7 -5
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +46 -40
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +41 -34
- package/lib/Utils/messages-media.d.ts +116 -0
- package/lib/Utils/messages-media.js +768 -550
- package/lib/Utils/messages.d.ts +77 -0
- package/lib/Utils/messages.js +263 -362
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +149 -138
- package/lib/Utils/process-message.d.ts +41 -0
- package/lib/Utils/process-message.js +303 -323
- package/lib/Utils/signal.d.ts +32 -0
- package/lib/Utils/signal.js +141 -149
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.js +103 -95
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.js +214 -183
- package/lib/Utils/validate-connection.js.bak +237 -0
- package/lib/WABinary/constants.d.ts +30 -0
- package/lib/WABinary/constants.js +35 -1298
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/decode.js +249 -237
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.js +260 -213
- package/lib/WABinary/generic-utils.d.ts +17 -0
- package/lib/WABinary/generic-utils.js +65 -56
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -7
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +58 -89
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -3
- package/lib/WAM/BinaryInfo.d.ts +17 -0
- package/lib/WAM/BinaryInfo.js +12 -10
- package/lib/WAM/constants.d.ts +38 -0
- package/lib/WAM/constants.js +15348 -22851
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/encode.js +136 -135
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -5
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -28
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -49
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +28 -27
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -36
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +20 -26
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -6
- package/lib/WAUSync/USyncQuery.d.ts +28 -0
- package/lib/WAUSync/USyncQuery.js +85 -86
- package/lib/WAUSync/USyncUser.d.ts +12 -0
- package/lib/WAUSync/USyncUser.js +25 -23
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -5
- package/lib/index.d.ts +12 -0
- package/lib/index.js +36 -24
- package/package.json +106 -98
- package/LICENSE +0 -21
- package/WAProto/WAProto.proto +0 -5311
- package/lib/KeyDB/BinarySearch.js +0 -20
- package/lib/KeyDB/KeyedDB.js +0 -167
- package/lib/KeyDB/index.js +0 -4
- package/lib/Signal/lid-mapping.js +0 -155
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.js +0 -52
- package/lib/Socket/Client/websocket.js.bak +0 -53
- package/lib/Socket/communities.js +0 -413
- package/lib/Socket/mex.js +0 -45
- package/lib/Types/Bussines.js +0 -3
- package/lib/Types/Newsletter.js.bak +0 -33
- package/lib/Utils/browser-utils.js +0 -25
- package/lib/Utils/message-retry-manager.js +0 -113
- package/lib/Utils/messages.js.bak +0 -907
- package/lib/Utils/pre-key-manager.js +0 -85
|
@@ -1,601 +1,819 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
23
24
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
await
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
await fs.unlink(filePath);
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.getStatusCodeForMediaRetry = exports.decryptMediaRetryData = exports.decodeMediaRetryNode = exports.encryptMediaRetryRequest = exports.getWAUploadToServer = exports.extensionForMediaMessage = exports.downloadEncryptedContent = exports.downloadContentFromMessage = exports.getUrlFromDirectPath = exports.encryptedStream = exports.prepareStream = exports.getHttpStream = exports.generateThumbnail = exports.getStream = exports.toBuffer = exports.toReadable = exports.getAudioWaveform = exports.getAudioDuration = exports.mediaMessageSHA256B64 = exports.generateProfilePicture = exports.encodeBase64EncodedStringForUpload = exports.extractImageThumb = exports.getMediaKeys = exports.hkdfInfoKey = void 0;
|
|
27
|
+
const boom_1 = require("@hapi/boom");
|
|
28
|
+
const child_process_1 = require("child_process");
|
|
29
|
+
const Crypto = __importStar(require("crypto"));
|
|
30
|
+
const events_1 = require("events");
|
|
31
|
+
const fs_1 = require("fs");
|
|
32
|
+
const os_1 = require("os");
|
|
33
|
+
const path_1 = require("path");
|
|
34
|
+
const stream_1 = require("stream");
|
|
35
|
+
const WAProto_1 = require("../../WAProto");
|
|
36
|
+
const Defaults_1 = require("../Defaults");
|
|
37
|
+
const WABinary_1 = require("../WABinary");
|
|
38
|
+
const crypto_1 = require("./crypto");
|
|
39
|
+
const generics_1 = require("./generics");
|
|
40
|
+
const getTmpFilesDirectory = () => (0, os_1.tmpdir)();
|
|
41
|
+
const getImageProcessingLibrary = async () => {
|
|
42
|
+
const [_jimp, sharp] = await Promise.all([
|
|
43
|
+
(async () => {
|
|
44
|
+
const jimp = await (import('jimp')
|
|
45
|
+
.catch(() => { }));
|
|
46
|
+
return jimp;
|
|
47
|
+
})(),
|
|
48
|
+
(async () => {
|
|
49
|
+
const sharp = await (import('sharp')
|
|
50
|
+
.catch(() => { }));
|
|
51
|
+
return sharp;
|
|
52
|
+
})()
|
|
53
|
+
]);
|
|
54
|
+
if (sharp) {
|
|
55
|
+
return { sharp };
|
|
56
56
|
}
|
|
57
|
-
|
|
57
|
+
const jimp = (_jimp === null || _jimp === void 0 ? void 0 : _jimp.default) || _jimp;
|
|
58
|
+
if (jimp) {
|
|
59
|
+
return { jimp };
|
|
58
60
|
}
|
|
59
|
-
throw
|
|
60
|
-
}
|
|
61
|
+
throw new boom_1.Boom('No image processing library available');
|
|
61
62
|
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
const hkdfInfoKey = (type) => {
|
|
64
|
+
const hkdfInfo = Defaults_1.MEDIA_HKDF_KEY_MAPPING[type];
|
|
65
|
+
return `WhatsApp ${hkdfInfo} Keys`;
|
|
66
|
+
};
|
|
67
|
+
exports.hkdfInfoKey = hkdfInfoKey;
|
|
68
|
+
/** generates all the keys required to encrypt/decrypt & sign a media message */
|
|
69
|
+
function getMediaKeys(buffer, mediaType) {
|
|
70
|
+
if (!buffer) {
|
|
71
|
+
throw new boom_1.Boom('Cannot derive from empty media key');
|
|
72
|
+
}
|
|
73
|
+
if (typeof buffer === 'string') {
|
|
74
|
+
buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64');
|
|
75
|
+
}
|
|
76
|
+
// expand using HKDF to 112 bytes, also pass in the relevant app info
|
|
77
|
+
const expandedMediaKey = (0, crypto_1.hkdf)(buffer, 112, { info: (0, exports.hkdfInfoKey)(mediaType) });
|
|
78
|
+
return {
|
|
79
|
+
iv: expandedMediaKey.slice(0, 16),
|
|
80
|
+
cipherKey: expandedMediaKey.slice(16, 48),
|
|
81
|
+
macKey: expandedMediaKey.slice(48, 80),
|
|
82
|
+
};
|
|
76
83
|
}
|
|
77
|
-
|
|
84
|
+
exports.getMediaKeys = getMediaKeys;
|
|
85
|
+
/** Extracts video thumb using FFMPEG */
|
|
78
86
|
const extractVideoThumb = async (path, destPath, time, size) => new Promise((resolve, reject) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
87
|
+
const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`;
|
|
88
|
+
(0, child_process_1.exec)(cmd, (err) => {
|
|
89
|
+
if (err) {
|
|
90
|
+
reject(err);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
resolve();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
if (bufferOrFilePath instanceof stream_1.Readable) {
|
|
100
|
+
bufferOrFilePath = await (0, exports.toBuffer)(bufferOrFilePath);
|
|
101
|
+
}
|
|
102
|
+
const lib = await getImageProcessingLibrary();
|
|
103
|
+
if ('sharp' in lib && typeof ((_a = lib.sharp) === null || _a === void 0 ? void 0 : _a.default) === 'function') {
|
|
104
|
+
const img = lib.sharp.default(bufferOrFilePath);
|
|
105
|
+
const dimensions = await img.metadata();
|
|
106
|
+
const buffer = await img
|
|
107
|
+
.resize(width)
|
|
108
|
+
.jpeg({ quality: 50 })
|
|
109
|
+
.toBuffer();
|
|
110
|
+
return {
|
|
111
|
+
buffer,
|
|
112
|
+
original: {
|
|
113
|
+
width: dimensions.width,
|
|
114
|
+
height: dimensions.height,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
else if ('jimp' in lib && typeof ((_b = lib.jimp) === null || _b === void 0 ? void 0 : _b.read) === 'function') {
|
|
119
|
+
const { read, MIME_JPEG, RESIZE_BILINEAR, AUTO } = lib.jimp;
|
|
120
|
+
const jimp = await read(bufferOrFilePath);
|
|
121
|
+
const dimensions = {
|
|
122
|
+
width: jimp.getWidth(),
|
|
123
|
+
height: jimp.getHeight()
|
|
124
|
+
};
|
|
125
|
+
const buffer = await jimp
|
|
126
|
+
.quality(50)
|
|
127
|
+
.resize(width, AUTO, RESIZE_BILINEAR)
|
|
128
|
+
.getBufferAsync(MIME_JPEG);
|
|
129
|
+
return {
|
|
130
|
+
buffer,
|
|
131
|
+
original: dimensions
|
|
132
|
+
};
|
|
83
133
|
}
|
|
84
134
|
else {
|
|
85
|
-
|
|
135
|
+
throw new boom_1.Boom('No image processing library available');
|
|
86
136
|
}
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
//=======================================================//
|
|
90
|
-
export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
91
|
-
if (bufferOrFilePath instanceof Readable) {
|
|
92
|
-
bufferOrFilePath = await toBuffer(bufferOrFilePath);
|
|
93
|
-
}
|
|
94
|
-
const image = await Jimp.read(bufferOrFilePath);
|
|
95
|
-
const dimensions = { width: image.bitmap.width, height: image.bitmap.height };
|
|
96
|
-
const resized = image.resize(width, Jimp.RESIZE_BILINEAR).quality(50);
|
|
97
|
-
const buffer = await resized.getBufferAsync(Jimp.MIME_JPEG);
|
|
98
|
-
return { buffer, original: dimensions };
|
|
99
137
|
};
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
138
|
+
exports.extractImageThumb = extractImageThumb;
|
|
139
|
+
const encodeBase64EncodedStringForUpload = (b64) => (encodeURIComponent(b64
|
|
140
|
+
.replace(/\+/g, '-')
|
|
141
|
+
.replace(/\//g, '_')
|
|
142
|
+
.replace(/\=+$/, '')));
|
|
143
|
+
exports.encodeBase64EncodedStringForUpload = encodeBase64EncodedStringForUpload;
|
|
144
|
+
const generateProfilePicture = async (mediaUpload) => {
|
|
145
|
+
var _a, _b;
|
|
146
|
+
let bufferOrFilePath;
|
|
147
|
+
if (Buffer.isBuffer(mediaUpload)) {
|
|
148
|
+
bufferOrFilePath = mediaUpload;
|
|
149
|
+
}
|
|
150
|
+
else if ('url' in mediaUpload) {
|
|
151
|
+
bufferOrFilePath = mediaUpload.url.toString();
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
bufferOrFilePath = await (0, exports.toBuffer)(mediaUpload.stream);
|
|
155
|
+
}
|
|
156
|
+
const lib = await getImageProcessingLibrary();
|
|
157
|
+
let img;
|
|
158
|
+
if ('sharp' in lib && typeof ((_a = lib.sharp) === null || _a === void 0 ? void 0 : _a.default) === 'function') {
|
|
159
|
+
img = lib.sharp.default(bufferOrFilePath)
|
|
160
|
+
.resize(640, 640)
|
|
161
|
+
.jpeg({
|
|
162
|
+
quality: 50,
|
|
163
|
+
})
|
|
164
|
+
.toBuffer();
|
|
165
|
+
}
|
|
166
|
+
else if ('jimp' in lib && typeof ((_b = lib.jimp) === null || _b === void 0 ? void 0 : _b.read) === 'function') {
|
|
167
|
+
const { read, MIME_JPEG, RESIZE_BILINEAR } = lib.jimp;
|
|
168
|
+
const jimp = await read(bufferOrFilePath);
|
|
169
|
+
const min = Math.min(jimp.getWidth(), jimp.getHeight());
|
|
170
|
+
const cropped = jimp.crop(0, 0, min, min);
|
|
171
|
+
img = cropped
|
|
172
|
+
.quality(50)
|
|
173
|
+
.resize(640, 640, RESIZE_BILINEAR)
|
|
174
|
+
.getBufferAsync(MIME_JPEG);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
throw new boom_1.Boom('No image processing library available');
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
img: await img,
|
|
181
|
+
};
|
|
117
182
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
183
|
+
exports.generateProfilePicture = generateProfilePicture;
|
|
184
|
+
/** gets the SHA256 of the given media message */
|
|
185
|
+
const mediaMessageSHA256B64 = (message) => {
|
|
186
|
+
const media = Object.values(message)[0];
|
|
187
|
+
return (media === null || media === void 0 ? void 0 : media.fileSha256) && Buffer.from(media.fileSha256).toString('base64');
|
|
122
188
|
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
189
|
+
exports.mediaMessageSHA256B64 = mediaMessageSHA256B64;
|
|
190
|
+
async function getAudioDuration(buffer) {
|
|
191
|
+
try {
|
|
192
|
+
const { PassThrough } = require('stream');
|
|
193
|
+
const ff = require('fluent-ffmpeg');
|
|
194
|
+
|
|
195
|
+
return await new Promise((resolve, reject) => {
|
|
196
|
+
const inputStream = new PassThrough();
|
|
197
|
+
inputStream.end(buffer);
|
|
198
|
+
|
|
199
|
+
ff(inputStream)
|
|
200
|
+
.ffprobe((err, data) => {
|
|
201
|
+
if (err) reject(err);
|
|
202
|
+
else resolve(data.format.duration);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
} catch (error) {
|
|
206
|
+
const musicMetadata = await import('music-metadata');
|
|
207
|
+
let metadata;
|
|
208
|
+
if (Buffer.isBuffer(buffer)) {
|
|
209
|
+
metadata = await musicMetadata.parseBuffer(buffer, undefined, {
|
|
210
|
+
duration: true
|
|
211
|
+
});
|
|
212
|
+
} else if (typeof buffer === 'string') {
|
|
213
|
+
const rStream = (0, fs_1.createReadStream)(buffer);
|
|
214
|
+
try {
|
|
215
|
+
metadata = await musicMetadata.parseStream(rStream, undefined, {
|
|
216
|
+
duration: true
|
|
217
|
+
});
|
|
218
|
+
} finally {
|
|
219
|
+
rStream.destroy();
|
|
220
|
+
}
|
|
221
|
+
} else {
|
|
222
|
+
metadata = await musicMetadata.parseStream(buffer, undefined, {
|
|
223
|
+
duration: true
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return metadata.format.duration;
|
|
227
|
+
}
|
|
140
228
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
229
|
+
exports.getAudioDuration = getAudioDuration;
|
|
230
|
+
async function getAudioWaveform(buffer, logger) {
|
|
231
|
+
try {
|
|
232
|
+
const { PassThrough } = require('stream');
|
|
233
|
+
const ff = require('fluent-ffmpeg');
|
|
234
|
+
|
|
235
|
+
let audioData;
|
|
236
|
+
if (Buffer.isBuffer(buffer)) {
|
|
237
|
+
audioData = buffer;
|
|
238
|
+
} else if (typeof buffer === 'string') {
|
|
239
|
+
const rStream = require('fs').createReadStream(buffer);
|
|
240
|
+
audioData = await exports.toBuffer(rStream);
|
|
241
|
+
} else {
|
|
242
|
+
audioData = await exports.toBuffer(buffer);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return await new Promise((resolve, reject) => {
|
|
246
|
+
const inputStream = new PassThrough();
|
|
247
|
+
inputStream.end(audioData);
|
|
248
|
+
const chunks = [];
|
|
249
|
+
const bars = 64;
|
|
250
|
+
|
|
251
|
+
ff(inputStream)
|
|
252
|
+
.audioChannels(1)
|
|
253
|
+
.audioFrequency(16000)
|
|
254
|
+
.format('s16le')
|
|
255
|
+
.on('error', reject)
|
|
256
|
+
.on('end', () => {
|
|
257
|
+
const rawData = Buffer.concat(chunks);
|
|
258
|
+
const samples = rawData.length / 2;
|
|
259
|
+
const amplitudes = [];
|
|
260
|
+
|
|
261
|
+
for (let i = 0; i < samples; i++) {
|
|
262
|
+
amplitudes.push(Math.abs(rawData.readInt16LE(i * 2)) / 32768);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const blockSize = Math.floor(amplitudes.length / bars);
|
|
266
|
+
const avg = [];
|
|
267
|
+
for (let i = 0; i < bars; i++) {
|
|
268
|
+
const block = amplitudes.slice(i * blockSize, (i + 1) * blockSize);
|
|
269
|
+
avg.push(block.reduce((a, b) => a + b, 0) / block.length);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const max = Math.max(...avg);
|
|
273
|
+
const normalized = avg.map(v => Math.floor((v / max) * 100));
|
|
274
|
+
resolve(new Uint8Array(normalized));
|
|
275
|
+
})
|
|
276
|
+
.pipe()
|
|
277
|
+
.on('data', chunk => chunks.push(chunk));
|
|
278
|
+
});
|
|
279
|
+
} catch (e) {
|
|
280
|
+
logger?.debug(e);
|
|
152
281
|
}
|
|
153
|
-
else {
|
|
154
|
-
audioData = await toBuffer(buffer);
|
|
155
|
-
}
|
|
156
|
-
const audioBuffer = await decoder(audioData);
|
|
157
|
-
const rawData = audioBuffer.getChannelData(0);
|
|
158
|
-
const samples = 64;
|
|
159
|
-
const blockSize = Math.floor(rawData.length / samples);
|
|
160
|
-
const filteredData = [];
|
|
161
|
-
for (let i = 0; i < samples; i++) {
|
|
162
|
-
const blockStart = blockSize * i;
|
|
163
|
-
let sum = 0;
|
|
164
|
-
for (let j = 0; j < blockSize; j++) {
|
|
165
|
-
sum = sum + Math.abs(rawData[blockStart + j]);
|
|
166
|
-
}
|
|
167
|
-
filteredData.push(sum / blockSize);
|
|
168
|
-
}
|
|
169
|
-
const multiplier = Math.pow(Math.max(...filteredData), -1);
|
|
170
|
-
const normalizedData = filteredData.map(n => n * multiplier);
|
|
171
|
-
const waveform = new Uint8Array(normalizedData.map(n => Math.floor(100 * n)));
|
|
172
|
-
return waveform;
|
|
173
|
-
}
|
|
174
|
-
catch (e) {
|
|
175
|
-
logger?.debug("Failed to generate waveform: " + e);
|
|
176
|
-
}
|
|
177
282
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const readable = new Readable({ read: () => { } });
|
|
181
|
-
readable.push(buffer);
|
|
182
|
-
readable.push(null);
|
|
183
|
-
return readable;
|
|
184
|
-
};
|
|
185
|
-
//=======================================================//
|
|
186
|
-
export const toBuffer = async (stream) => {
|
|
187
|
-
const chunks = [];
|
|
188
|
-
for await (const chunk of stream) {
|
|
189
|
-
chunks.push(chunk);
|
|
190
|
-
}
|
|
191
|
-
stream.destroy();
|
|
192
|
-
return Buffer.concat(chunks);
|
|
193
|
-
};
|
|
194
|
-
//=======================================================//
|
|
195
|
-
export const getStream = async (item, opts) => {
|
|
196
|
-
if (Buffer.isBuffer(item)) {
|
|
197
|
-
return { stream: toReadable(item), type: "buffer" };
|
|
198
|
-
}
|
|
199
|
-
if ("stream" in item) {
|
|
200
|
-
return { stream: item.stream, type: "readable" };
|
|
201
|
-
}
|
|
202
|
-
const urlStr = item.url.toString();
|
|
203
|
-
if (urlStr.startsWith("data:")) {
|
|
204
|
-
const buffer = Buffer.from(urlStr.split(",")[1], "base64");
|
|
205
|
-
return { stream: toReadable(buffer), type: "buffer" };
|
|
206
|
-
}
|
|
207
|
-
if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
|
|
208
|
-
return { stream: await getHttpStream(item.url, opts), type: "remote" };
|
|
209
|
-
}
|
|
210
|
-
return { stream: createReadStream(item.url), type: "file" };
|
|
211
|
-
};
|
|
212
|
-
//=======================================================//
|
|
213
|
-
export async function generateThumbnail(file, mediaType, options) {
|
|
214
|
-
let thumbnail;
|
|
215
|
-
let originalImageDimensions;
|
|
216
|
-
if (mediaType === "image") {
|
|
217
|
-
const { buffer, original } = await extractImageThumb(file);
|
|
218
|
-
thumbnail = buffer.toString("base64");
|
|
219
|
-
if (original.width && original.height) {
|
|
220
|
-
originalImageDimensions = {
|
|
221
|
-
width: original.width,
|
|
222
|
-
height: original.height
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
else if (mediaType === "video") {
|
|
227
|
-
const imgFilename = join(getTmpFilesDirectory(), generateMessageIDV2() + ".jpg");
|
|
283
|
+
exports.getAudioWaveform = getAudioWaveform;
|
|
284
|
+
async function convertToOpusBuffer(buffer, logger) {
|
|
228
285
|
try {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
286
|
+
const { PassThrough } = require('stream');
|
|
287
|
+
const ff = require('fluent-ffmpeg');
|
|
288
|
+
|
|
289
|
+
return await new Promise((resolve, reject) => {
|
|
290
|
+
const inStream = new PassThrough();
|
|
291
|
+
const outStream = new PassThrough();
|
|
292
|
+
const chunks = [];
|
|
293
|
+
inStream.end(buffer);
|
|
294
|
+
|
|
295
|
+
ff(inStream)
|
|
296
|
+
.noVideo()
|
|
297
|
+
.audioCodec('libopus')
|
|
298
|
+
.format('ogg')
|
|
299
|
+
.audioBitrate('48k')
|
|
300
|
+
.audioChannels(1)
|
|
301
|
+
.audioFrequency(48000)
|
|
302
|
+
.outputOptions([
|
|
303
|
+
'-vn',
|
|
304
|
+
'-b:a 64k',
|
|
305
|
+
'-ac 2',
|
|
306
|
+
'-ar 48000',
|
|
307
|
+
'-map_metadata', '-1',
|
|
308
|
+
'-application', 'voip'
|
|
309
|
+
])
|
|
310
|
+
.on('error', reject)
|
|
311
|
+
.on('end', () => resolve(Buffer.concat(chunks)))
|
|
312
|
+
.pipe(outStream, {
|
|
313
|
+
end: true
|
|
314
|
+
});
|
|
315
|
+
outStream.on('data', c => chunks.push(c));
|
|
316
|
+
});
|
|
317
|
+
} catch (e) {
|
|
318
|
+
logger?.debug(e);
|
|
319
|
+
throw e;
|
|
320
|
+
}
|
|
242
321
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
});
|
|
250
|
-
if (!response.ok) {
|
|
251
|
-
throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } });
|
|
252
|
-
}
|
|
253
|
-
return Readable.fromWeb(response.body);
|
|
322
|
+
exports.convertToOpusBuffer = convertToOpusBuffer;
|
|
323
|
+
const toReadable = (buffer) => {
|
|
324
|
+
const readable = new stream_1.Readable({ read: () => { } });
|
|
325
|
+
readable.push(buffer);
|
|
326
|
+
readable.push(null);
|
|
327
|
+
return readable;
|
|
254
328
|
};
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const encFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + "-enc");
|
|
262
|
-
const encFileWriteStream = createWriteStream(encFilePath);
|
|
263
|
-
let originalFileStream;
|
|
264
|
-
let originalFilePath;
|
|
265
|
-
if (saveOriginalFileIfRequired) {
|
|
266
|
-
originalFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + "-original");
|
|
267
|
-
originalFileStream = createWriteStream(originalFilePath);
|
|
268
|
-
}
|
|
269
|
-
let fileLength = 0;
|
|
270
|
-
const aes = Crypto.createCipheriv("aes-256-cbc", cipherKey, iv);
|
|
271
|
-
const hmac = Crypto.createHmac("sha256", macKey).update(iv);
|
|
272
|
-
const sha256Plain = Crypto.createHash("sha256");
|
|
273
|
-
const sha256Enc = Crypto.createHash("sha256");
|
|
274
|
-
const onChunk = (buff) => {
|
|
275
|
-
sha256Enc.update(buff);
|
|
276
|
-
hmac.update(buff);
|
|
277
|
-
encFileWriteStream.write(buff);
|
|
278
|
-
};
|
|
279
|
-
try {
|
|
280
|
-
for await (const data of stream) {
|
|
281
|
-
fileLength += data.length;
|
|
282
|
-
if (type === "remote" &&
|
|
283
|
-
opts?.maxContentLength &&
|
|
284
|
-
fileLength + data.length > opts.maxContentLength) {
|
|
285
|
-
throw new Boom(`content length exceeded when encrypting "${type}"`, {
|
|
286
|
-
data: { media, type }
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
if (originalFileStream) {
|
|
290
|
-
if (!originalFileStream.write(data)) {
|
|
291
|
-
await once(originalFileStream, "drain");
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
sha256Plain.update(data);
|
|
295
|
-
onChunk(aes.update(data));
|
|
296
|
-
}
|
|
297
|
-
onChunk(aes.final());
|
|
298
|
-
const mac = hmac.digest().slice(0, 10);
|
|
299
|
-
sha256Enc.update(mac);
|
|
300
|
-
const fileSha256 = sha256Plain.digest();
|
|
301
|
-
const fileEncSha256 = sha256Enc.digest();
|
|
302
|
-
encFileWriteStream.write(mac);
|
|
303
|
-
encFileWriteStream.end();
|
|
304
|
-
originalFileStream?.end?.();
|
|
329
|
+
exports.toReadable = toReadable;
|
|
330
|
+
const toBuffer = async (stream) => {
|
|
331
|
+
const chunks = [];
|
|
332
|
+
for await (const chunk of stream) {
|
|
333
|
+
chunks.push(chunk);
|
|
334
|
+
}
|
|
305
335
|
stream.destroy();
|
|
306
|
-
|
|
336
|
+
return Buffer.concat(chunks);
|
|
337
|
+
};
|
|
338
|
+
exports.toBuffer = toBuffer;
|
|
339
|
+
const getStream = async (item, opts) => {
|
|
340
|
+
if (Buffer.isBuffer(item)) {
|
|
341
|
+
return { stream: (0, exports.toReadable)(item), type: 'buffer' };
|
|
342
|
+
}
|
|
343
|
+
if ('stream' in item) {
|
|
344
|
+
return { stream: item.stream, type: 'readable' };
|
|
345
|
+
}
|
|
346
|
+
if (item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
|
|
347
|
+
return { stream: await (0, exports.getHttpStream)(item.url, opts), type: 'remote' };
|
|
348
|
+
}
|
|
349
|
+
return { stream: (0, fs_1.createReadStream)(item.url), type: 'file' };
|
|
350
|
+
};
|
|
351
|
+
exports.getStream = getStream;
|
|
352
|
+
/** generates a thumbnail for a given media, if required */
|
|
353
|
+
async function generateThumbnail(file, mediaType, options) {
|
|
354
|
+
var _a;
|
|
355
|
+
let thumbnail;
|
|
356
|
+
let originalImageDimensions;
|
|
357
|
+
if (mediaType === 'image') {
|
|
358
|
+
const { buffer, original } = await (0, exports.extractImageThumb)(file);
|
|
359
|
+
thumbnail = buffer.toString('base64');
|
|
360
|
+
if (original.width && original.height) {
|
|
361
|
+
originalImageDimensions = {
|
|
362
|
+
width: original.width,
|
|
363
|
+
height: original.height,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
else if (mediaType === 'video') {
|
|
368
|
+
const imgFilename = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageID)() + '.jpg');
|
|
369
|
+
try {
|
|
370
|
+
await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 });
|
|
371
|
+
const buff = await fs_1.promises.readFile(imgFilename);
|
|
372
|
+
thumbnail = buff.toString('base64');
|
|
373
|
+
await fs_1.promises.unlink(imgFilename);
|
|
374
|
+
}
|
|
375
|
+
catch (err) {
|
|
376
|
+
(_a = options.logger) === null || _a === void 0 ? void 0 : _a.debug('could not generate video thumb: ' + err);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
307
379
|
return {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
encFilePath,
|
|
311
|
-
mac,
|
|
312
|
-
fileEncSha256,
|
|
313
|
-
fileSha256,
|
|
314
|
-
fileLength
|
|
380
|
+
thumbnail,
|
|
381
|
+
originalImageDimensions
|
|
315
382
|
};
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
383
|
+
}
|
|
384
|
+
exports.generateThumbnail = generateThumbnail;
|
|
385
|
+
const getHttpStream = async (url, options = {}) => {
|
|
386
|
+
const { default: axios } = await import('axios');
|
|
387
|
+
const fetched = await axios.get(url.toString(), { ...options, responseType: 'stream' });
|
|
388
|
+
return fetched.data;
|
|
389
|
+
};
|
|
390
|
+
exports.getHttpStream = getHttpStream;
|
|
391
|
+
const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
|
|
392
|
+
const { stream, type } = await (0, exports.getStream)(media, opts);
|
|
393
|
+
logger === null || logger === void 0 ? void 0 : logger.debug('fetched media stream');
|
|
394
|
+
let bodyPath;
|
|
395
|
+
let didSaveToTmpPath = false;
|
|
396
|
+
try {
|
|
397
|
+
const buffer = await (0, exports.toBuffer)(stream);
|
|
398
|
+
if (type === 'file') {
|
|
399
|
+
bodyPath = media.url;
|
|
400
|
+
}
|
|
401
|
+
else if (saveOriginalFileIfRequired) {
|
|
402
|
+
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageID)());
|
|
403
|
+
(0, fs_1.writeFileSync)(bodyPath, buffer);
|
|
404
|
+
didSaveToTmpPath = true;
|
|
405
|
+
}
|
|
406
|
+
const fileLength = buffer.length;
|
|
407
|
+
const fileSha256 = Crypto.createHash('sha256').update(buffer).digest();
|
|
408
|
+
stream === null || stream === void 0 ? void 0 : stream.destroy();
|
|
409
|
+
logger === null || logger === void 0 ? void 0 : logger.debug('prepare stream data successfully');
|
|
410
|
+
return {
|
|
411
|
+
mediaKey: undefined,
|
|
412
|
+
encWriteStream: buffer,
|
|
413
|
+
fileLength,
|
|
414
|
+
fileSha256,
|
|
415
|
+
fileEncSha256: undefined,
|
|
416
|
+
bodyPath,
|
|
417
|
+
didSaveToTmpPath
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
// destroy all streams with error
|
|
422
|
+
stream.destroy();
|
|
423
|
+
if (didSaveToTmpPath) {
|
|
424
|
+
try {
|
|
425
|
+
await fs_1.promises.unlink(bodyPath);
|
|
426
|
+
}
|
|
427
|
+
catch (err) {
|
|
428
|
+
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
throw error;
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
exports.prepareStream = prepareStream;
|
|
435
|
+
const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, isPtt, forceOpus } = {}) => {
|
|
436
|
+
const { stream, type } = await (0, exports.getStream)(media, opts);
|
|
437
|
+
|
|
438
|
+
let finalStream = stream;
|
|
439
|
+
if (mediaType === 'audio' && (isPtt === true || forceOpus === true)) {
|
|
440
|
+
try {
|
|
441
|
+
const buffer = await (0, exports.toBuffer)(stream);
|
|
442
|
+
const opusBuffer = await exports.convertToOpusBuffer(buffer, logger);
|
|
443
|
+
finalStream = (0, exports.toReadable)(opusBuffer);
|
|
444
|
+
} catch (error) {
|
|
445
|
+
const { stream: newStream } = await (0, exports.getStream)(media, opts);
|
|
446
|
+
finalStream = newStream;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const mediaKey = Crypto.randomBytes(32);
|
|
451
|
+
const { cipherKey, iv, macKey } = getMediaKeys(mediaKey, mediaType);
|
|
452
|
+
const encWriteStream = new stream_1.Readable({ read: () => { } });
|
|
453
|
+
let bodyPath;
|
|
454
|
+
let writeStream;
|
|
455
|
+
let didSaveToTmpPath = false;
|
|
456
|
+
|
|
457
|
+
if (type === 'file') {
|
|
458
|
+
bodyPath = media.url;
|
|
459
|
+
}
|
|
460
|
+
else if (saveOriginalFileIfRequired) {
|
|
461
|
+
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageID)());
|
|
462
|
+
writeStream = (0, fs_1.createWriteStream)(bodyPath);
|
|
463
|
+
didSaveToTmpPath = true;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let fileLength = 0;
|
|
467
|
+
const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
|
|
468
|
+
let hmac = Crypto.createHmac('sha256', macKey).update(iv);
|
|
469
|
+
let sha256Plain = Crypto.createHash('sha256');
|
|
470
|
+
let sha256Enc = Crypto.createHash('sha256');
|
|
471
|
+
|
|
325
472
|
try {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
473
|
+
for await (const data of finalStream) {
|
|
474
|
+
fileLength += data.length;
|
|
475
|
+
if (type === 'remote'
|
|
476
|
+
&& (opts === null || opts === void 0 ? void 0 : opts.maxContentLength)
|
|
477
|
+
&& fileLength + data.length > opts.maxContentLength) {
|
|
478
|
+
throw new boom_1.Boom(`content length exceeded when encrypting "${type}"`, {
|
|
479
|
+
data: { media, type }
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
sha256Plain = sha256Plain.update(data);
|
|
484
|
+
if (writeStream) {
|
|
485
|
+
if (!writeStream.write(data)) {
|
|
486
|
+
await (0, events_1.once)(writeStream, 'drain');
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
onChunk(aes.update(data));
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
onChunk(aes.final());
|
|
493
|
+
const mac = hmac.digest().slice(0, 10);
|
|
494
|
+
sha256Enc = sha256Enc.update(mac);
|
|
495
|
+
const fileSha256 = sha256Plain.digest();
|
|
496
|
+
const fileEncSha256 = sha256Enc.digest();
|
|
497
|
+
|
|
498
|
+
encWriteStream.push(mac);
|
|
499
|
+
encWriteStream.push(null);
|
|
500
|
+
writeStream === null || writeStream === void 0 ? void 0 : writeStream.end();
|
|
501
|
+
finalStream.destroy();
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
mediaKey,
|
|
505
|
+
encWriteStream,
|
|
506
|
+
bodyPath,
|
|
507
|
+
mac,
|
|
508
|
+
fileEncSha256,
|
|
509
|
+
fileSha256,
|
|
510
|
+
fileLength,
|
|
511
|
+
didSaveToTmpPath
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
catch (error) {
|
|
515
|
+
encWriteStream.destroy();
|
|
516
|
+
writeStream === null || writeStream === void 0 ? void 0 : writeStream.destroy();
|
|
517
|
+
aes.destroy();
|
|
518
|
+
hmac.destroy();
|
|
519
|
+
sha256Plain.destroy();
|
|
520
|
+
sha256Enc.destroy();
|
|
521
|
+
finalStream.destroy();
|
|
522
|
+
|
|
523
|
+
if (didSaveToTmpPath) {
|
|
524
|
+
try {
|
|
525
|
+
await fs_1.promises.unlink(bodyPath);
|
|
526
|
+
}
|
|
527
|
+
catch (err) {
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
throw error;
|
|
330
531
|
}
|
|
331
|
-
|
|
332
|
-
|
|
532
|
+
|
|
533
|
+
function onChunk(buff) {
|
|
534
|
+
sha256Enc = sha256Enc.update(buff);
|
|
535
|
+
hmac = hmac.update(buff);
|
|
536
|
+
encWriteStream.push(buff);
|
|
333
537
|
}
|
|
334
|
-
throw error;
|
|
335
|
-
}
|
|
336
538
|
};
|
|
337
|
-
|
|
338
|
-
const DEF_HOST =
|
|
539
|
+
exports.encryptedStream = encryptedStream;
|
|
540
|
+
const DEF_HOST = 'mmg.whatsapp.net';
|
|
339
541
|
const AES_CHUNK_SIZE = 16;
|
|
340
542
|
const toSmallestChunkSize = (num) => {
|
|
341
|
-
|
|
543
|
+
return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
|
|
342
544
|
};
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
throw new Boom("No valid media URL or directPath present in message", { statusCode: 400 });
|
|
350
|
-
}
|
|
351
|
-
const keys = await getMediaKeys(mediaKey, type);
|
|
352
|
-
return downloadEncryptedContent(downloadUrl, keys, opts);
|
|
545
|
+
const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
|
|
546
|
+
exports.getUrlFromDirectPath = getUrlFromDirectPath;
|
|
547
|
+
const downloadContentFromMessage = ({ mediaKey, directPath, url }, type, opts = {}) => {
|
|
548
|
+
const downloadUrl = url || (0, exports.getUrlFromDirectPath)(directPath);
|
|
549
|
+
const keys = getMediaKeys(mediaKey, type);
|
|
550
|
+
return (0, exports.downloadEncryptedContent)(downloadUrl, keys, opts);
|
|
353
551
|
};
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
? Array.isArray(headersInit)
|
|
372
|
-
? Object.fromEntries(headersInit)
|
|
373
|
-
: headersInit
|
|
374
|
-
: {}),
|
|
375
|
-
Origin: DEFAULT_ORIGIN
|
|
376
|
-
};
|
|
377
|
-
if (startChunk || endChunk) {
|
|
378
|
-
headers.Range = `bytes=${startChunk}-`;
|
|
379
|
-
if (endChunk) {
|
|
380
|
-
headers.Range += endChunk;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
const fetched = await getHttpStream(downloadUrl, {
|
|
384
|
-
...(options || {}),
|
|
385
|
-
headers
|
|
386
|
-
});
|
|
387
|
-
let remainingBytes = Buffer.from([]);
|
|
388
|
-
let aes;
|
|
389
|
-
const pushBytes = (bytes, push) => {
|
|
390
|
-
if (startByte || endByte) {
|
|
391
|
-
const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0);
|
|
392
|
-
const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0);
|
|
393
|
-
push(bytes.slice(start, end));
|
|
394
|
-
bytesFetched += bytes.length;
|
|
552
|
+
exports.downloadContentFromMessage = downloadContentFromMessage;
|
|
553
|
+
/**
|
|
554
|
+
* Decrypts and downloads an AES256-CBC encrypted file given the keys.
|
|
555
|
+
* Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
|
|
556
|
+
* */
|
|
557
|
+
const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
|
|
558
|
+
let bytesFetched = 0;
|
|
559
|
+
let startChunk = 0;
|
|
560
|
+
let firstBlockIsIV = false;
|
|
561
|
+
// if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
|
|
562
|
+
if (startByte) {
|
|
563
|
+
const chunk = toSmallestChunkSize(startByte || 0);
|
|
564
|
+
if (chunk) {
|
|
565
|
+
startChunk = chunk - AES_CHUNK_SIZE;
|
|
566
|
+
bytesFetched = chunk;
|
|
567
|
+
firstBlockIsIV = true;
|
|
568
|
+
}
|
|
395
569
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
data = data.slice(0, decryptLength);
|
|
406
|
-
if (!aes) {
|
|
407
|
-
let ivValue = iv;
|
|
408
|
-
if (firstBlockIsIV) {
|
|
409
|
-
ivValue = data.slice(0, AES_CHUNK_SIZE);
|
|
410
|
-
data = data.slice(AES_CHUNK_SIZE);
|
|
570
|
+
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
|
|
571
|
+
const headers = {
|
|
572
|
+
...(options === null || options === void 0 ? void 0 : options.headers) || {},
|
|
573
|
+
Origin: Defaults_1.DEFAULT_ORIGIN,
|
|
574
|
+
};
|
|
575
|
+
if (startChunk || endChunk) {
|
|
576
|
+
headers.Range = `bytes=${startChunk}-`;
|
|
577
|
+
if (endChunk) {
|
|
578
|
+
headers.Range += endChunk;
|
|
411
579
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
580
|
+
}
|
|
581
|
+
// download the message
|
|
582
|
+
const fetched = await (0, exports.getHttpStream)(downloadUrl, {
|
|
583
|
+
...options || {},
|
|
584
|
+
headers,
|
|
585
|
+
maxBodyLength: Infinity,
|
|
586
|
+
maxContentLength: Infinity,
|
|
587
|
+
});
|
|
588
|
+
let remainingBytes = Buffer.from([]);
|
|
589
|
+
let aes;
|
|
590
|
+
const pushBytes = (bytes, push) => {
|
|
591
|
+
if (startByte || endByte) {
|
|
592
|
+
const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0);
|
|
593
|
+
const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0);
|
|
594
|
+
push(bytes.slice(start, end));
|
|
595
|
+
bytesFetched += bytes.length;
|
|
596
|
+
}
|
|
597
|
+
else {
|
|
598
|
+
push(bytes);
|
|
415
599
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
600
|
+
};
|
|
601
|
+
const output = new stream_1.Transform({
|
|
602
|
+
transform(chunk, _, callback) {
|
|
603
|
+
let data = Buffer.concat([remainingBytes, chunk]);
|
|
604
|
+
const decryptLength = toSmallestChunkSize(data.length);
|
|
605
|
+
remainingBytes = data.slice(decryptLength);
|
|
606
|
+
data = data.slice(0, decryptLength);
|
|
607
|
+
if (!aes) {
|
|
608
|
+
let ivValue = iv;
|
|
609
|
+
if (firstBlockIsIV) {
|
|
610
|
+
ivValue = data.slice(0, AES_CHUNK_SIZE);
|
|
611
|
+
data = data.slice(AES_CHUNK_SIZE);
|
|
612
|
+
}
|
|
613
|
+
aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue);
|
|
614
|
+
// if an end byte that is not EOF is specified
|
|
615
|
+
// stop auto padding (PKCS7) -- otherwise throws an error for decryption
|
|
616
|
+
if (endByte) {
|
|
617
|
+
aes.setAutoPadding(false);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
try {
|
|
621
|
+
pushBytes(aes.update(data), b => this.push(b));
|
|
622
|
+
callback();
|
|
623
|
+
}
|
|
624
|
+
catch (error) {
|
|
625
|
+
callback(error);
|
|
626
|
+
}
|
|
627
|
+
},
|
|
628
|
+
final(callback) {
|
|
629
|
+
try {
|
|
630
|
+
pushBytes(aes.final(), b => this.push(b));
|
|
631
|
+
callback();
|
|
632
|
+
}
|
|
633
|
+
catch (error) {
|
|
634
|
+
callback(error);
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
});
|
|
638
|
+
return fetched.pipe(output, { end: true });
|
|
436
639
|
};
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
640
|
+
exports.downloadEncryptedContent = downloadEncryptedContent;
|
|
641
|
+
function extensionForMediaMessage(message) {
|
|
642
|
+
const getExtension = (mimetype) => mimetype.split(';')[0].split('/')[1];
|
|
643
|
+
const type = Object.keys(message)[0];
|
|
644
|
+
let extension;
|
|
645
|
+
if (type === 'locationMessage' ||
|
|
646
|
+
type === 'liveLocationMessage' ||
|
|
647
|
+
type === 'productMessage') {
|
|
648
|
+
extension = '.jpeg';
|
|
649
|
+
}
|
|
650
|
+
else {
|
|
651
|
+
const messageContent = message[type];
|
|
652
|
+
extension = getExtension(messageContent.mimetype);
|
|
653
|
+
}
|
|
654
|
+
return extension;
|
|
450
655
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
const response = await fetch(url, {
|
|
466
|
-
dispatcher: fetchAgent,
|
|
467
|
-
method: "POST",
|
|
468
|
-
body: stream,
|
|
469
|
-
headers: {
|
|
470
|
-
...(() => {
|
|
471
|
-
const hdrs = options?.headers;
|
|
472
|
-
if (!hdrs)
|
|
473
|
-
return {};
|
|
474
|
-
return Array.isArray(hdrs) ? Object.fromEntries(hdrs) : hdrs;
|
|
475
|
-
})(),
|
|
476
|
-
"Content-Type": "application/octet-stream",
|
|
477
|
-
Origin: DEFAULT_ORIGIN
|
|
478
|
-
},
|
|
479
|
-
duplex: "half",
|
|
480
|
-
signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined
|
|
481
|
-
});
|
|
482
|
-
let parsed = undefined;
|
|
483
|
-
try {
|
|
484
|
-
parsed = await response.json();
|
|
656
|
+
exports.extensionForMediaMessage = extensionForMediaMessage;
|
|
657
|
+
const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
|
|
658
|
+
return async (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) => {
|
|
659
|
+
var _a, _b;
|
|
660
|
+
const { default: axios } = await import('axios');
|
|
661
|
+
// send a query JSON to obtain the url & auth token to upload our media
|
|
662
|
+
let uploadInfo = await refreshMediaConn(false);
|
|
663
|
+
let urls;
|
|
664
|
+
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
665
|
+
const chunks = [];
|
|
666
|
+
if (!Buffer.isBuffer(stream)) {
|
|
667
|
+
for await (const chunk of stream) {
|
|
668
|
+
chunks.push(chunk);
|
|
669
|
+
}
|
|
485
670
|
}
|
|
486
|
-
|
|
487
|
-
|
|
671
|
+
const reqBody = Buffer.isBuffer(stream) ? stream : Buffer.concat(chunks);
|
|
672
|
+
fileEncSha256B64 = (0, exports.encodeBase64EncodedStringForUpload)(fileEncSha256B64);
|
|
673
|
+
let media = Defaults_1.MEDIA_PATH_MAP[mediaType];
|
|
674
|
+
if (newsletter) {
|
|
675
|
+
media = media === null || media === void 0 ? void 0 : media.replace('/mms/', '/newsletter/newsletter-');
|
|
488
676
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
677
|
+
for (const { hostname, maxContentLengthBytes } of hosts) {
|
|
678
|
+
logger.debug(`uploading to "${hostname}"`);
|
|
679
|
+
const auth = encodeURIComponent(uploadInfo.auth); // the auth token
|
|
680
|
+
const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
681
|
+
let result;
|
|
682
|
+
try {
|
|
683
|
+
if (maxContentLengthBytes && reqBody.length > maxContentLengthBytes) {
|
|
684
|
+
throw new boom_1.Boom(`Body too large for "${hostname}"`, { statusCode: 413 });
|
|
685
|
+
}
|
|
686
|
+
const body = await axios.post(url, reqBody, {
|
|
687
|
+
...options,
|
|
688
|
+
headers: {
|
|
689
|
+
...options.headers || {},
|
|
690
|
+
'Content-Type': 'application/octet-stream',
|
|
691
|
+
'Origin': Defaults_1.DEFAULT_ORIGIN
|
|
692
|
+
},
|
|
693
|
+
httpsAgent: fetchAgent,
|
|
694
|
+
timeout: timeoutMs,
|
|
695
|
+
responseType: 'json',
|
|
696
|
+
maxBodyLength: Infinity,
|
|
697
|
+
maxContentLength: Infinity,
|
|
698
|
+
});
|
|
699
|
+
result = body.data;
|
|
700
|
+
if ((result === null || result === void 0 ? void 0 : result.url) || (result === null || result === void 0 ? void 0 : result.directPath)) {
|
|
701
|
+
urls = {
|
|
702
|
+
mediaUrl: result.url,
|
|
703
|
+
directPath: result.direct_path,
|
|
704
|
+
handle: result.handle
|
|
705
|
+
};
|
|
706
|
+
break;
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
uploadInfo = await refreshMediaConn(true);
|
|
710
|
+
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
catch (error) {
|
|
714
|
+
if (axios.isAxiosError(error)) {
|
|
715
|
+
result = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data;
|
|
716
|
+
}
|
|
717
|
+
const isLast = hostname === ((_b = hosts[uploadInfo.hosts.length - 1]) === null || _b === void 0 ? void 0 : _b.hostname);
|
|
718
|
+
logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
|
|
719
|
+
}
|
|
499
720
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
721
|
+
if (!urls) {
|
|
722
|
+
throw new boom_1.Boom('Media upload failed on all hosts', { statusCode: 500 });
|
|
503
723
|
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
|
|
507
|
-
logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? "" : ", retrying..."}`);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
if (!urls) {
|
|
511
|
-
throw new Boom("Media upload failed on all hosts", { statusCode: 500 });
|
|
512
|
-
}
|
|
513
|
-
return urls;
|
|
514
|
-
};
|
|
724
|
+
return urls;
|
|
725
|
+
};
|
|
515
726
|
};
|
|
516
|
-
|
|
727
|
+
exports.getWAUploadToServer = getWAUploadToServer;
|
|
517
728
|
const getMediaRetryKey = (mediaKey) => {
|
|
518
|
-
|
|
729
|
+
return (0, crypto_1.hkdf)(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
|
|
519
730
|
};
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
tag: "encrypt",
|
|
537
|
-
attrs: {},
|
|
731
|
+
/**
|
|
732
|
+
* Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
|
|
733
|
+
*/
|
|
734
|
+
const encryptMediaRetryRequest = (key, mediaKey, meId) => {
|
|
735
|
+
const recp = { stanzaId: key.id };
|
|
736
|
+
const recpBuffer = WAProto_1.proto.ServerErrorReceipt.encode(recp).finish();
|
|
737
|
+
const iv = Crypto.randomBytes(12);
|
|
738
|
+
const retryKey = getMediaRetryKey(mediaKey);
|
|
739
|
+
const ciphertext = (0, crypto_1.aesEncryptGCM)(recpBuffer, retryKey, iv, Buffer.from(key.id));
|
|
740
|
+
const req = {
|
|
741
|
+
tag: 'receipt',
|
|
742
|
+
attrs: {
|
|
743
|
+
id: key.id,
|
|
744
|
+
to: (0, WABinary_1.jidNormalizedUser)(meId),
|
|
745
|
+
type: 'server-error'
|
|
746
|
+
},
|
|
538
747
|
content: [
|
|
539
|
-
|
|
540
|
-
|
|
748
|
+
// this encrypt node is actually pretty useless
|
|
749
|
+
// the media is returned even without this node
|
|
750
|
+
// keeping it here to maintain parity with WA Web
|
|
751
|
+
{
|
|
752
|
+
tag: 'encrypt',
|
|
753
|
+
attrs: {},
|
|
754
|
+
content: [
|
|
755
|
+
{ tag: 'enc_p', attrs: {}, content: ciphertext },
|
|
756
|
+
{ tag: 'enc_iv', attrs: {}, content: iv }
|
|
757
|
+
]
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
tag: 'rmr',
|
|
761
|
+
attrs: {
|
|
762
|
+
jid: key.remoteJid,
|
|
763
|
+
'from_me': (!!key.fromMe).toString(),
|
|
764
|
+
// @ts-ignore
|
|
765
|
+
participant: key.participant || undefined
|
|
766
|
+
}
|
|
767
|
+
}
|
|
541
768
|
]
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
tag: "rmr",
|
|
545
|
-
attrs: {
|
|
546
|
-
jid: key.remoteJid,
|
|
547
|
-
from_me: (!!key.fromMe).toString(),
|
|
548
|
-
participant: key.participant || undefined
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
]
|
|
552
|
-
};
|
|
553
|
-
return req;
|
|
769
|
+
};
|
|
770
|
+
return req;
|
|
554
771
|
};
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
data: errorNode.attrs,
|
|
571
|
-
statusCode: getStatusCodeForMediaRetry(errorCode)
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
else {
|
|
575
|
-
const encryptedInfoNode = getBinaryNodeChild(node, "encrypt");
|
|
576
|
-
const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, "enc_p");
|
|
577
|
-
const iv = getBinaryNodeChildBuffer(encryptedInfoNode, "enc_iv");
|
|
578
|
-
if (ciphertext && iv) {
|
|
579
|
-
event.media = { ciphertext, iv };
|
|
772
|
+
exports.encryptMediaRetryRequest = encryptMediaRetryRequest;
|
|
773
|
+
const decodeMediaRetryNode = (node) => {
|
|
774
|
+
const rmrNode = (0, WABinary_1.getBinaryNodeChild)(node, 'rmr');
|
|
775
|
+
const event = {
|
|
776
|
+
key: {
|
|
777
|
+
id: node.attrs.id,
|
|
778
|
+
remoteJid: rmrNode.attrs.jid,
|
|
779
|
+
fromMe: rmrNode.attrs.from_me === 'true',
|
|
780
|
+
participant: rmrNode.attrs.participant
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
const errorNode = (0, WABinary_1.getBinaryNodeChild)(node, 'error');
|
|
784
|
+
if (errorNode) {
|
|
785
|
+
const errorCode = +errorNode.attrs.code;
|
|
786
|
+
event.error = new boom_1.Boom(`Failed to re-upload media (${errorCode})`, { data: errorNode.attrs, statusCode: (0, exports.getStatusCodeForMediaRetry)(errorCode) });
|
|
580
787
|
}
|
|
581
788
|
else {
|
|
582
|
-
|
|
789
|
+
const encryptedInfoNode = (0, WABinary_1.getBinaryNodeChild)(node, 'encrypt');
|
|
790
|
+
const ciphertext = (0, WABinary_1.getBinaryNodeChildBuffer)(encryptedInfoNode, 'enc_p');
|
|
791
|
+
const iv = (0, WABinary_1.getBinaryNodeChildBuffer)(encryptedInfoNode, 'enc_iv');
|
|
792
|
+
if (ciphertext && iv) {
|
|
793
|
+
event.media = { ciphertext, iv };
|
|
794
|
+
}
|
|
795
|
+
else {
|
|
796
|
+
event.error = new boom_1.Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 });
|
|
797
|
+
}
|
|
583
798
|
}
|
|
584
|
-
|
|
585
|
-
return event;
|
|
799
|
+
return event;
|
|
586
800
|
};
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
801
|
+
exports.decodeMediaRetryNode = decodeMediaRetryNode;
|
|
802
|
+
const decryptMediaRetryData = ({ ciphertext, iv }, mediaKey, msgId) => {
|
|
803
|
+
const retryKey = getMediaRetryKey(mediaKey);
|
|
804
|
+
const plaintext = (0, crypto_1.aesDecryptGCM)(ciphertext, retryKey, iv, Buffer.from(msgId));
|
|
805
|
+
return WAProto_1.proto.MediaRetryNotification.decode(plaintext);
|
|
592
806
|
};
|
|
593
|
-
|
|
594
|
-
|
|
807
|
+
exports.decryptMediaRetryData = decryptMediaRetryData;
|
|
808
|
+
const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
|
|
809
|
+
exports.getStatusCodeForMediaRetry = getStatusCodeForMediaRetry;
|
|
595
810
|
const MEDIA_RETRY_STATUS_MAP = {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
811
|
+
[WAProto_1.proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
|
|
812
|
+
[WAProto_1.proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
|
|
813
|
+
[WAProto_1.proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
|
814
|
+
[WAProto_1.proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418,
|
|
600
815
|
};
|
|
601
|
-
|
|
816
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
817
|
+
function __importStar(arg0) {
|
|
818
|
+
throw new Error('Function not implemented.');
|
|
819
|
+
}
|