socketon 1.31.2-rc → 1.51.16
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 +313 -159
- package/WAProto/WAProto.proto +5311 -0
- package/WAProto/index.js +65801 -141371
- package/lib/Defaults/index.js +117 -141
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -14
- package/lib/Signal/Group/group-session-builder.js +10 -42
- package/lib/Signal/Group/group_cipher.js +75 -87
- package/lib/Signal/Group/index.js +13 -57
- package/lib/Signal/Group/keyhelper.js +17 -52
- package/lib/Signal/Group/sender-chain-key.js +27 -33
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -63
- package/lib/Signal/Group/sender-key-message.js +65 -66
- package/lib/Signal/Group/sender-key-name.js +45 -44
- package/lib/Signal/Group/sender-key-record.js +39 -49
- package/lib/Signal/Group/sender-key-state.js +80 -93
- package/lib/Signal/Group/sender-message-key.js +27 -28
- package/lib/Signal/libsignal.js +313 -163
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -19
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/Client/websocket.js.bak +53 -0
- package/lib/Socket/business.js +359 -242
- package/lib/Socket/chats.js +846 -935
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +304 -309
- package/lib/Socket/index.js +15 -10
- package/lib/Socket/messages-recv.js +1107 -1054
- package/lib/Socket/messages-send.js +639 -448
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +240 -324
- package/lib/Socket/socket.js +794 -651
- package/lib/Socket/socketon.js +402 -0
- package/lib/Store/index.js +6 -10
- package/lib/Store/make-cache-manager-store.js +73 -81
- package/lib/Store/make-in-memory-store.js +286 -423
- package/lib/Store/make-ordered-dictionary.js +77 -79
- package/lib/Store/object-repository.js +24 -26
- package/lib/Types/Auth.js +3 -2
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -2
- package/lib/Types/Chat.js +9 -4
- package/lib/Types/Contact.js +3 -2
- package/lib/Types/Events.js +3 -2
- package/lib/Types/GroupMetadata.js +3 -2
- package/lib/Types/Label.js +24 -26
- package/lib/Types/LabelAssociation.js +6 -8
- package/lib/Types/Message.js +12 -9
- package/lib/Types/Newsletter.js +33 -38
- package/lib/Types/Newsletter.js.bak +33 -0
- package/lib/Types/Product.js +3 -2
- package/lib/Types/Signal.js +3 -2
- package/lib/Types/Socket.js +4 -2
- package/lib/Types/State.js +11 -2
- package/lib/Types/USync.js +3 -2
- package/lib/Types/index.js +27 -41
- package/lib/Utils/auth-utils.js +211 -198
- package/lib/Utils/baileys-event-stream.js +42 -61
- package/lib/Utils/browser-utils.js +25 -0
- package/lib/Utils/business.js +213 -214
- package/lib/Utils/chat-utils.js +710 -687
- package/lib/Utils/crypto.js +112 -133
- package/lib/Utils/decode-wa-message.js +252 -183
- package/lib/Utils/decode-wa-message.js.bak +267 -0
- package/lib/Utils/event-buffer.js +510 -496
- package/lib/Utils/generics.js +319 -392
- package/lib/Utils/history.js +83 -92
- package/lib/Utils/index.js +21 -33
- package/lib/Utils/link-preview.js +71 -83
- package/lib/Utils/logger.js +5 -7
- package/lib/Utils/lt-hash.js +40 -46
- package/lib/Utils/make-mutex.js +34 -41
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +550 -768
- package/lib/Utils/messages.js +354 -263
- package/lib/Utils/noise-handler.js +138 -149
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +323 -303
- package/lib/Utils/signal.js +149 -141
- package/lib/Utils/use-multi-file-auth-state.js +95 -103
- package/lib/Utils/validate-connection.js +183 -214
- package/lib/WABinary/constants.js +1298 -35
- package/lib/WABinary/decode.js +237 -249
- package/lib/WABinary/encode.js +213 -260
- package/lib/WABinary/generic-utils.js +56 -65
- package/lib/WABinary/index.js +7 -21
- package/lib/WABinary/jid-utils.js +89 -58
- package/lib/WABinary/types.js +3 -2
- package/lib/WAM/BinaryInfo.js +10 -12
- package/lib/WAM/constants.js +22851 -15348
- package/lib/WAM/encode.js +135 -136
- package/lib/WAM/index.js +5 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -30
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +49 -53
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -28
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -39
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +26 -20
- package/lib/WAUSync/Protocols/index.js +6 -20
- package/lib/WAUSync/USyncQuery.js +86 -85
- package/lib/WAUSync/USyncUser.js +23 -25
- package/lib/WAUSync/index.js +5 -19
- package/lib/index.js +27 -35
- package/package.json +85 -95
- package/engine-requirements.js +0 -10
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/index.d.ts +0 -53
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
- package/lib/Signal/Group/group-session-builder.d.ts +0 -14
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -11
- package/lib/Signal/Group/keyhelper.d.ts +0 -10
- package/lib/Signal/Group/queue-job.d.ts +0 -1
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -13
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -16
- package/lib/Signal/Group/sender-key-message.d.ts +0 -18
- package/lib/Signal/Group/sender-key-name.d.ts +0 -17
- package/lib/Signal/Group/sender-key-record.d.ts +0 -30
- package/lib/Signal/Group/sender-key-state.d.ts +0 -38
- package/lib/Signal/Group/sender-message-key.d.ts +0 -11
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.d.ts +0 -12
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/business.d.ts +0 -171
- package/lib/Socket/chats.d.ts +0 -267
- package/lib/Socket/dugong.d.ts +0 -254
- package/lib/Socket/dugong.js +0 -484
- package/lib/Socket/groups.d.ts +0 -115
- package/lib/Socket/index.d.ts +0 -173
- package/lib/Socket/messages-recv.d.ts +0 -161
- package/lib/Socket/messages-send.d.ts +0 -149
- package/lib/Socket/newsletter.d.ts +0 -134
- package/lib/Socket/registration.d.ts +0 -267
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/socket.d.ts +0 -43
- package/lib/Socket/usync.d.ts +0 -36
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -13
- package/lib/Store/make-in-memory-store.d.ts +0 -118
- package/lib/Store/make-ordered-dictionary.d.ts +0 -13
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -110
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -102
- package/lib/Types/Contact.d.ts +0 -19
- package/lib/Types/Events.d.ts +0 -157
- package/lib/Types/GroupMetadata.d.ts +0 -55
- package/lib/Types/Label.d.ts +0 -35
- package/lib/Types/LabelAssociation.d.ts +0 -29
- package/lib/Types/Message.d.ts +0 -273
- package/lib/Types/Newsletter.d.ts +0 -103
- package/lib/Types/Product.d.ts +0 -78
- package/lib/Types/Signal.d.ts +0 -57
- package/lib/Types/Socket.d.ts +0 -111
- package/lib/Types/State.d.ts +0 -27
- package/lib/Types/USync.d.ts +0 -25
- package/lib/Types/index.d.ts +0 -57
- package/lib/Utils/auth-utils.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/business.d.ts +0 -22
- package/lib/Utils/chat-utils.d.ts +0 -71
- package/lib/Utils/crypto.d.ts +0 -41
- package/lib/Utils/decode-wa-message.d.ts +0 -19
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/generics.js.bak +0 -433
- package/lib/Utils/history.d.ts +0 -15
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -4
- package/lib/Utils/lt-hash.d.ts +0 -12
- package/lib/Utils/make-mutex.d.ts +0 -7
- package/lib/Utils/messages-media.d.ts +0 -116
- package/lib/Utils/messages.d.ts +0 -77
- package/lib/Utils/noise-handler.d.ts +0 -21
- package/lib/Utils/process-message.d.ts +0 -41
- package/lib/Utils/signal.d.ts +0 -32
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/Utils/validate-connection.js.bak +0 -237
- package/lib/WABinary/constants.d.ts +0 -30
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -17
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -31
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/WAM/BinaryInfo.d.ts +0 -17
- package/lib/WAM/constants.d.ts +0 -38
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -25
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -8
- package/lib/WAUSync/Protocols/index.d.ts +0 -4
- package/lib/WAUSync/USyncQuery.d.ts +0 -28
- package/lib/WAUSync/USyncUser.d.ts +0 -12
- package/lib/WAUSync/index.d.ts +0 -3
- package/lib/index.d.ts +0 -12
|
@@ -1,819 +1,601 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return result;
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from "../WABinary/index.js";
|
|
3
|
+
import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP } from "../Defaults/index.js";
|
|
4
|
+
import { createReadStream, createWriteStream, promises as fs, WriteStream } from "fs";
|
|
5
|
+
import { aesDecryptGCM, aesEncryptGCM, hkdf } from "./crypto.js";
|
|
6
|
+
import { generateMessageIDV2 } from "./generics.js";
|
|
7
|
+
import { proto } from "../../WAProto/index.js";
|
|
8
|
+
import { Readable, Transform } from "stream";
|
|
9
|
+
import { exec } from "child_process";
|
|
10
|
+
import { Boom } from "@hapi/boom";
|
|
11
|
+
import * as Crypto from "crypto";
|
|
12
|
+
import { once } from "events";
|
|
13
|
+
import { tmpdir } from "os";
|
|
14
|
+
import { join } from "path";
|
|
15
|
+
import { URL } from "url";
|
|
16
|
+
import jimp from "jimp";
|
|
17
|
+
//=======================================================//
|
|
18
|
+
const getTmpFilesDirectory = () => tmpdir();
|
|
19
|
+
//=======================================================//
|
|
20
|
+
export const hkdfInfoKey = (type) => {
|
|
21
|
+
const hkdfInfo = MEDIA_HKDF_KEY_MAPPING[type];
|
|
22
|
+
return `WhatsApp ${hkdfInfo} Keys`;
|
|
24
23
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
24
|
+
//=======================================================//
|
|
25
|
+
export const getRawMediaUploadData = async (media, mediaType, logger) => {
|
|
26
|
+
const { stream } = await getStream(media);
|
|
27
|
+
logger?.debug("got stream for raw upload");
|
|
28
|
+
const hasher = Crypto.createHash("sha256");
|
|
29
|
+
const filePath = join(tmpdir(), mediaType + generateMessageIDV2());
|
|
30
|
+
const fileWriteStream = createWriteStream(filePath);
|
|
31
|
+
let fileLength = 0;
|
|
32
|
+
try {
|
|
33
|
+
for await (const data of stream) {
|
|
34
|
+
fileLength += data.length;
|
|
35
|
+
hasher.update(data);
|
|
36
|
+
if (!fileWriteStream.write(data)) {
|
|
37
|
+
await once(fileWriteStream, "drain");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
fileWriteStream.end();
|
|
41
|
+
await once(fileWriteStream, "finish");
|
|
42
|
+
stream.destroy();
|
|
43
|
+
const fileSha256 = hasher.digest();
|
|
44
|
+
logger?.debug("hashed data for raw upload");
|
|
45
|
+
return {
|
|
46
|
+
filePath: filePath,
|
|
47
|
+
fileSha256,
|
|
48
|
+
fileLength
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
fileWriteStream.destroy();
|
|
53
|
+
stream.destroy();
|
|
54
|
+
try {
|
|
55
|
+
await fs.unlink(filePath);
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
if (jimp) {
|
|
59
|
-
return { jimp };
|
|
57
|
+
catch {
|
|
60
58
|
}
|
|
61
|
-
throw
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
62
61
|
};
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
};
|
|
62
|
+
//=======================================================//
|
|
63
|
+
export async function getMediaKeys(buffer, mediaType) {
|
|
64
|
+
if (!buffer) {
|
|
65
|
+
throw new Boom("Cannot derive from empty media key");
|
|
66
|
+
}
|
|
67
|
+
if (typeof buffer === "string") {
|
|
68
|
+
buffer = Buffer.from(buffer.replace("data:;base64,", ""), "base64");
|
|
69
|
+
}
|
|
70
|
+
const expandedMediaKey = await hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) });
|
|
71
|
+
return {
|
|
72
|
+
iv: expandedMediaKey.slice(0, 16),
|
|
73
|
+
cipherKey: expandedMediaKey.slice(16, 48),
|
|
74
|
+
macKey: expandedMediaKey.slice(48, 80)
|
|
75
|
+
};
|
|
83
76
|
}
|
|
84
|
-
|
|
85
|
-
/** Extracts video thumb using FFMPEG */
|
|
77
|
+
//=======================================================//
|
|
86
78
|
const extractVideoThumb = async (path, destPath, time, size) => new Promise((resolve, reject) => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
};
|
|
79
|
+
const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`;
|
|
80
|
+
exec(cmd, err => {
|
|
81
|
+
if (err) {
|
|
82
|
+
reject(err);
|
|
133
83
|
}
|
|
134
84
|
else {
|
|
135
|
-
|
|
85
|
+
resolve();
|
|
136
86
|
}
|
|
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 };
|
|
137
99
|
};
|
|
138
|
-
|
|
139
|
-
const encodeBase64EncodedStringForUpload = (b64) =>
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
};
|
|
100
|
+
//=======================================================//
|
|
101
|
+
export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=+$/, ""));
|
|
102
|
+
export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
103
|
+
let buffer;
|
|
104
|
+
const { width: w = 640, height: h = 640 } = dimensions || {};
|
|
105
|
+
if (Buffer.isBuffer(mediaUpload)) {
|
|
106
|
+
buffer = mediaUpload;
|
|
107
|
+
} else {
|
|
108
|
+
const { stream } = await getStream(mediaUpload);
|
|
109
|
+
buffer = await toBuffer(stream);
|
|
110
|
+
}
|
|
111
|
+
const jimp = await Jimp.read(buffer);
|
|
112
|
+
const min = Math.min(jimp.bitmap.width, jimp.bitmap.height);
|
|
113
|
+
const cropped = jimp.crop(0, 0, min, min);
|
|
114
|
+
const resized = cropped.resize(w, h, Jimp.RESIZE_BILINEAR).quality(50);
|
|
115
|
+
const img = await resized.getBufferAsync(Jimp.MIME_JPEG);
|
|
116
|
+
return { img };
|
|
182
117
|
};
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
return (media === null || media === void 0 ? void 0 : media.fileSha256) && Buffer.from(media.fileSha256).toString('base64');
|
|
118
|
+
//=======================================================//
|
|
119
|
+
export const mediaMessageSHA256B64 = (message) => {
|
|
120
|
+
const media = Object.values(message)[0];
|
|
121
|
+
return media?.fileSha256 && Buffer.from(media.fileSha256).toString("base64");
|
|
188
122
|
};
|
|
189
|
-
|
|
190
|
-
async function getAudioDuration(buffer) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
-
}
|
|
228
|
-
}
|
|
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);
|
|
281
|
-
}
|
|
123
|
+
//=======================================================//
|
|
124
|
+
export async function getAudioDuration(buffer) {
|
|
125
|
+
const musicMetadata = await import("music-metadata");
|
|
126
|
+
let metadata;
|
|
127
|
+
const options = {
|
|
128
|
+
duration: true
|
|
129
|
+
};
|
|
130
|
+
if (Buffer.isBuffer(buffer)) {
|
|
131
|
+
metadata = await musicMetadata.parseBuffer(buffer, undefined, options);
|
|
132
|
+
}
|
|
133
|
+
else if (typeof buffer === "string") {
|
|
134
|
+
metadata = await musicMetadata.parseFile(buffer, options);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
metadata = await musicMetadata.parseStream(buffer, undefined, options);
|
|
138
|
+
}
|
|
139
|
+
return metadata.format.duration;
|
|
282
140
|
}
|
|
283
|
-
|
|
284
|
-
async function
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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;
|
|
141
|
+
//=======================================================//
|
|
142
|
+
export async function getAudioWaveform(buffer, logger) {
|
|
143
|
+
try {
|
|
144
|
+
const { default: decoder } = await import("audio-decode");
|
|
145
|
+
let audioData;
|
|
146
|
+
if (Buffer.isBuffer(buffer)) {
|
|
147
|
+
audioData = buffer;
|
|
148
|
+
}
|
|
149
|
+
else if (typeof buffer === "string") {
|
|
150
|
+
const rStream = createReadStream(buffer);
|
|
151
|
+
audioData = await toBuffer(rStream);
|
|
320
152
|
}
|
|
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
|
+
}
|
|
321
177
|
}
|
|
322
|
-
|
|
323
|
-
const toReadable = (buffer) => {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
178
|
+
//=======================================================//
|
|
179
|
+
export const toReadable = (buffer) => {
|
|
180
|
+
const readable = new Readable({ read: () => { } });
|
|
181
|
+
readable.push(buffer);
|
|
182
|
+
readable.push(null);
|
|
183
|
+
return readable;
|
|
328
184
|
};
|
|
329
|
-
|
|
330
|
-
const toBuffer = async (stream) => {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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);
|
|
337
193
|
};
|
|
338
|
-
|
|
339
|
-
const getStream = async (item, opts) => {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
return { stream: (
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
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
|
-
}
|
|
379
|
-
return {
|
|
380
|
-
thumbnail,
|
|
381
|
-
originalImageDimensions
|
|
382
|
-
};
|
|
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;
|
|
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" };
|
|
389
211
|
};
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
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");
|
|
396
228
|
try {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
|
|
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
|
-
}
|
|
229
|
+
await extractVideoThumb(file, imgFilename, "00:00:00", { width: 32, height: 32 });
|
|
230
|
+
const buff = await fs.readFile(imgFilename);
|
|
231
|
+
thumbnail = buff.toString("base64");
|
|
232
|
+
await fs.unlink(imgFilename);
|
|
233
|
+
}
|
|
234
|
+
catch (err) {
|
|
235
|
+
options.logger?.debug("could not generate video thumb: " + err);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
thumbnail,
|
|
240
|
+
originalImageDimensions
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
//=======================================================//
|
|
244
|
+
export const getHttpStream = async (url, options = {}) => {
|
|
245
|
+
const response = await fetch(url.toString(), {
|
|
246
|
+
dispatcher: options.dispatcher,
|
|
247
|
+
method: "GET",
|
|
248
|
+
headers: options.headers
|
|
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);
|
|
433
254
|
};
|
|
434
|
-
|
|
435
|
-
const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
255
|
+
//=======================================================//
|
|
256
|
+
export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
|
|
257
|
+
const { stream, type } = await getStream(media, opts);
|
|
258
|
+
logger?.debug("fetched media stream");
|
|
259
|
+
const mediaKey = Crypto.randomBytes(32);
|
|
260
|
+
const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType);
|
|
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");
|
|
447
292
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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?.();
|
|
305
|
+
stream.destroy();
|
|
306
|
+
logger?.debug("encrypted data successfully");
|
|
307
|
+
return {
|
|
308
|
+
mediaKey,
|
|
309
|
+
originalFilePath,
|
|
310
|
+
encFilePath,
|
|
311
|
+
mac,
|
|
312
|
+
fileEncSha256,
|
|
313
|
+
fileSha256,
|
|
314
|
+
fileLength
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
encFileWriteStream.destroy();
|
|
319
|
+
originalFileStream?.destroy?.();
|
|
320
|
+
aes.destroy();
|
|
321
|
+
hmac.destroy();
|
|
322
|
+
sha256Plain.destroy();
|
|
323
|
+
sha256Enc.destroy();
|
|
324
|
+
stream.destroy();
|
|
472
325
|
try {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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;
|
|
326
|
+
await fs.unlink(encFilePath);
|
|
327
|
+
if (originalFilePath) {
|
|
328
|
+
await fs.unlink(originalFilePath);
|
|
329
|
+
}
|
|
531
330
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
sha256Enc = sha256Enc.update(buff);
|
|
535
|
-
hmac = hmac.update(buff);
|
|
536
|
-
encWriteStream.push(buff);
|
|
331
|
+
catch (err) {
|
|
332
|
+
logger?.error({ err }, "failed deleting tmp files");
|
|
537
333
|
}
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
538
336
|
};
|
|
539
|
-
|
|
540
|
-
const DEF_HOST =
|
|
337
|
+
//=======================================================//
|
|
338
|
+
const DEF_HOST = "mmg.whatsapp.net";
|
|
541
339
|
const AES_CHUNK_SIZE = 16;
|
|
542
340
|
const toSmallestChunkSize = (num) => {
|
|
543
|
-
|
|
341
|
+
return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
|
|
544
342
|
};
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
const downloadContentFromMessage = ({ mediaKey, directPath, url }, type, opts = {}) => {
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
343
|
+
//=======================================================//
|
|
344
|
+
export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
|
|
345
|
+
export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
|
|
346
|
+
const isValidMediaUrl = url?.startsWith("https://mmg.whatsapp.net/");
|
|
347
|
+
const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
|
|
348
|
+
if (!downloadUrl) {
|
|
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);
|
|
551
353
|
};
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
354
|
+
//=======================================================//
|
|
355
|
+
export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
|
|
356
|
+
let bytesFetched = 0;
|
|
357
|
+
let startChunk = 0;
|
|
358
|
+
let firstBlockIsIV = false;
|
|
359
|
+
if (startByte) {
|
|
360
|
+
const chunk = toSmallestChunkSize(startByte || 0);
|
|
361
|
+
if (chunk) {
|
|
362
|
+
startChunk = chunk - AES_CHUNK_SIZE;
|
|
363
|
+
bytesFetched = chunk;
|
|
364
|
+
firstBlockIsIV = true;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
|
|
368
|
+
const headersInit = options?.headers ? options.headers : undefined;
|
|
369
|
+
const headers = {
|
|
370
|
+
...(headersInit
|
|
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;
|
|
580
395
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
396
|
+
else {
|
|
397
|
+
push(bytes);
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
const output = new Transform({
|
|
401
|
+
transform(chunk, _, callback) {
|
|
402
|
+
let data = Buffer.concat([remainingBytes, chunk]);
|
|
403
|
+
const decryptLength = toSmallestChunkSize(data.length);
|
|
404
|
+
remainingBytes = data.slice(decryptLength);
|
|
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);
|
|
596
411
|
}
|
|
597
|
-
|
|
598
|
-
|
|
412
|
+
aes = Crypto.createDecipheriv("aes-256-cbc", cipherKey, ivValue);
|
|
413
|
+
if (endByte) {
|
|
414
|
+
aes.setAutoPadding(false);
|
|
599
415
|
}
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
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 });
|
|
416
|
+
}
|
|
417
|
+
try {
|
|
418
|
+
pushBytes(aes.update(data), b => this.push(b));
|
|
419
|
+
callback();
|
|
420
|
+
}
|
|
421
|
+
catch (error) {
|
|
422
|
+
callback(error);
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
final(callback) {
|
|
426
|
+
try {
|
|
427
|
+
pushBytes(aes.final(), b => this.push(b));
|
|
428
|
+
callback();
|
|
429
|
+
}
|
|
430
|
+
catch (error) {
|
|
431
|
+
callback(error);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
return fetched.pipe(output, { end: true });
|
|
639
436
|
};
|
|
640
|
-
|
|
641
|
-
function extensionForMediaMessage(message) {
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
}
|
|
654
|
-
return extension;
|
|
437
|
+
//=======================================================//
|
|
438
|
+
export function extensionForMediaMessage(message) {
|
|
439
|
+
const getExtension = (mimetype) => mimetype.split(";")[0]?.split("/")[1];
|
|
440
|
+
const type = Object.keys(message)[0];
|
|
441
|
+
let extension;
|
|
442
|
+
if (type === "locationMessage" || type === "liveLocationMessage" || type === "productMessage") {
|
|
443
|
+
extension = ".jpeg";
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
const messageContent = message[type];
|
|
447
|
+
extension = getExtension(messageContent.mimetype);
|
|
448
|
+
}
|
|
449
|
+
return extension;
|
|
655
450
|
}
|
|
656
|
-
|
|
657
|
-
const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
451
|
+
//=======================================================//
|
|
452
|
+
export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
|
|
453
|
+
return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
|
|
454
|
+
let uploadInfo = await refreshMediaConn(false);
|
|
455
|
+
let urls;
|
|
456
|
+
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
457
|
+
fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
|
|
458
|
+
for (const { hostname } of hosts) {
|
|
459
|
+
logger.debug(`uploading to "${hostname}"`);
|
|
460
|
+
const auth = encodeURIComponent(uploadInfo.auth);
|
|
461
|
+
const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
462
|
+
let result;
|
|
463
|
+
try {
|
|
464
|
+
const stream = createReadStream(filePath);
|
|
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();
|
|
670
485
|
}
|
|
671
|
-
|
|
672
|
-
|
|
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-');
|
|
486
|
+
catch {
|
|
487
|
+
parsed = undefined;
|
|
676
488
|
}
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
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
|
-
}
|
|
489
|
+
result = parsed;
|
|
490
|
+
if (result?.url || result?.directPath) {
|
|
491
|
+
urls = {
|
|
492
|
+
mediaUrl: result.url,
|
|
493
|
+
directPath: result.direct_path,
|
|
494
|
+
meta_hmac: result.meta_hmac,
|
|
495
|
+
fbid: result.fbid,
|
|
496
|
+
ts: result.ts
|
|
497
|
+
};
|
|
498
|
+
break;
|
|
720
499
|
}
|
|
721
|
-
|
|
722
|
-
|
|
500
|
+
else {
|
|
501
|
+
uploadInfo = await refreshMediaConn(true);
|
|
502
|
+
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
723
503
|
}
|
|
724
|
-
|
|
725
|
-
|
|
504
|
+
}
|
|
505
|
+
catch (error) {
|
|
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
|
+
};
|
|
726
515
|
};
|
|
727
|
-
|
|
516
|
+
//=======================================================//
|
|
728
517
|
const getMediaRetryKey = (mediaKey) => {
|
|
729
|
-
|
|
518
|
+
return hkdf(mediaKey, 32, { info: "WhatsApp Media Retry Notification" });
|
|
730
519
|
};
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
const
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
520
|
+
//=======================================================//
|
|
521
|
+
export const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
|
|
522
|
+
const recp = { stanzaId: key.id };
|
|
523
|
+
const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
|
|
524
|
+
const iv = Crypto.randomBytes(12);
|
|
525
|
+
const retryKey = await getMediaRetryKey(mediaKey);
|
|
526
|
+
const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
|
|
527
|
+
const req = {
|
|
528
|
+
tag: "receipt",
|
|
529
|
+
attrs: {
|
|
530
|
+
id: key.id,
|
|
531
|
+
to: jidNormalizedUser(meId),
|
|
532
|
+
type: "server-error"
|
|
533
|
+
},
|
|
534
|
+
content: [
|
|
535
|
+
{
|
|
536
|
+
tag: "encrypt",
|
|
537
|
+
attrs: {},
|
|
747
538
|
content: [
|
|
748
|
-
|
|
749
|
-
|
|
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
|
-
}
|
|
539
|
+
{ tag: "enc_p", attrs: {}, content: ciphertext },
|
|
540
|
+
{ tag: "enc_iv", attrs: {}, content: iv }
|
|
768
541
|
]
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
key: {
|
|
777
|
-
id: node.attrs.id,
|
|
778
|
-
remoteJid: rmrNode.attrs.jid,
|
|
779
|
-
fromMe: rmrNode.attrs.from_me === 'true',
|
|
780
|
-
participant: rmrNode.attrs.participant
|
|
542
|
+
},
|
|
543
|
+
{
|
|
544
|
+
tag: "rmr",
|
|
545
|
+
attrs: {
|
|
546
|
+
jid: key.remoteJid,
|
|
547
|
+
from_me: (!!key.fromMe).toString(),
|
|
548
|
+
participant: key.participant || undefined
|
|
781
549
|
}
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
550
|
+
}
|
|
551
|
+
]
|
|
552
|
+
};
|
|
553
|
+
return req;
|
|
554
|
+
};
|
|
555
|
+
//=======================================================//
|
|
556
|
+
export const decodeMediaRetryNode = (node) => {
|
|
557
|
+
const rmrNode = getBinaryNodeChild(node, "rmr");
|
|
558
|
+
const event = {
|
|
559
|
+
key: {
|
|
560
|
+
id: node.attrs.id,
|
|
561
|
+
remoteJid: rmrNode.attrs.jid,
|
|
562
|
+
fromMe: rmrNode.attrs.from_me === "true",
|
|
563
|
+
participant: rmrNode.attrs.participant
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
const errorNode = getBinaryNodeChild(node, "error");
|
|
567
|
+
if (errorNode) {
|
|
568
|
+
const errorCode = +errorNode.attrs.code;
|
|
569
|
+
event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
|
|
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 };
|
|
787
580
|
}
|
|
788
581
|
else {
|
|
789
|
-
|
|
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
|
-
}
|
|
582
|
+
event.error = new Boom("Failed to re-upload media (missing ciphertext)", { statusCode: 404 });
|
|
798
583
|
}
|
|
799
|
-
|
|
584
|
+
}
|
|
585
|
+
return event;
|
|
800
586
|
};
|
|
801
|
-
|
|
802
|
-
const decryptMediaRetryData = ({ ciphertext, iv }, mediaKey, msgId) => {
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
587
|
+
//=======================================================//
|
|
588
|
+
export const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
|
|
589
|
+
const retryKey = await getMediaRetryKey(mediaKey);
|
|
590
|
+
const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId));
|
|
591
|
+
return proto.MediaRetryNotification.decode(plaintext);
|
|
806
592
|
};
|
|
807
|
-
|
|
808
|
-
const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
|
|
809
|
-
exports.getStatusCodeForMediaRetry = getStatusCodeForMediaRetry;
|
|
593
|
+
//=======================================================//
|
|
594
|
+
export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
|
|
810
595
|
const MEDIA_RETRY_STATUS_MAP = {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
596
|
+
[proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
|
|
597
|
+
[proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
|
|
598
|
+
[proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
|
599
|
+
[proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
|
|
815
600
|
};
|
|
816
|
-
|
|
817
|
-
function __importStar(arg0) {
|
|
818
|
-
throw new Error('Function not implemented.');
|
|
819
|
-
}
|
|
601
|
+
//=======================================================//
|