@badzz88/baileys 8.4.7 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1055 -337
- package/WAProto/index.js +5 -5
- package/lib/Defaults/index.js +187 -127
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.js +14 -11
- package/lib/Signal/Group/group-session-builder.js +91 -29
- package/lib/Signal/Group/group_cipher.js +88 -81
- package/lib/Signal/Group/index.js +136 -12
- package/lib/Signal/Group/keyhelper.js +70 -15
- package/lib/Signal/Group/sender-chain-key.js +31 -25
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -62
- package/lib/Signal/Group/sender-key-message.js +68 -65
- package/lib/Signal/Group/sender-key-name.js +43 -41
- package/lib/Signal/Group/sender-key-record.js +43 -40
- package/lib/Signal/Group/sender-key-state.js +96 -83
- package/lib/Signal/Group/sender-message-key.js +29 -25
- package/lib/Signal/libsignal.js +483 -424
- package/lib/Signal/lid-mapping.js +261 -276
- package/lib/Socket/Client/index.js +30 -3
- package/lib/Socket/Client/types.js +12 -10
- package/lib/Socket/Client/websocket.js +61 -53
- package/lib/Socket/aigroups.js +240 -0
- package/lib/Socket/business.js +422 -379
- package/lib/Socket/chats.js +1816 -1193
- package/lib/Socket/communities.js +549 -431
- package/lib/Socket/graphql.js +656 -0
- package/lib/Socket/groups.js +764 -374
- package/lib/Socket/index.js +38 -11
- package/lib/Socket/interactive-handler.js +522 -0
- package/lib/Socket/interop.js +549 -0
- package/lib/Socket/luxu.js +6 -5
- package/lib/Socket/managed-account.js +183 -0
- package/lib/Socket/messages-recv.js +2439 -1915
- package/lib/Socket/messages-send.js +1642 -1242
- package/lib/Socket/mex.js +59 -40
- package/lib/Socket/newsletter.js +812 -249
- package/lib/Socket/privacy.js +449 -0
- package/lib/Socket/registration.js +427 -0
- package/lib/Socket/socket.js +1077 -965
- package/lib/Socket/text-router.js +83 -0
- package/lib/Socket/username.js +237 -140
- package/lib/Store/index.js +36 -10
- package/lib/Store/make-cache-manager-store.js +85 -80
- package/lib/Store/make-in-memory-store.js +591 -185
- package/lib/Store/make-ordered-dictionary.js +78 -72
- package/lib/Store/object-repository.js +25 -28
- package/lib/Types/Auth.js +38 -2
- package/lib/Types/Bussines.js +2 -2
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Chat.js +4 -8
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Events.js +2 -2
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/Label.js +26 -24
- package/lib/Types/LabelAssociation.js +8 -6
- package/lib/Types/Message.js +95 -11
- package/lib/Types/Mex.js +112 -37
- package/lib/Types/Newsletter.js +121 -0
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +70 -56
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.js +55 -26
- package/lib/Utils/auth-utils.js +292 -288
- package/lib/Utils/browser-utils.js +113 -48
- package/lib/Utils/business.js +240 -224
- package/lib/Utils/chat-utils.js +1205 -869
- package/lib/Utils/command-loader.d.ts +31 -0
- package/lib/Utils/command-loader.js +100 -0
- package/lib/Utils/companion-reg-client-utils.js +41 -34
- package/lib/Utils/consumer-application.js +107 -0
- package/lib/Utils/crypto.js +144 -107
- package/lib/Utils/curve25519-js.js +275 -0
- package/lib/Utils/decode-wa-message.js +518 -308
- package/lib/Utils/event-buffer.js +596 -611
- package/lib/Utils/generics.js +514 -354
- package/lib/Utils/group-history.js +60 -0
- package/lib/Utils/history.js +244 -134
- package/lib/Utils/identity-change-handler.js +51 -49
- package/lib/Utils/index.js +57 -23
- package/lib/Utils/jid-display-normalization.js +218 -0
- package/lib/Utils/link-preview.js +135 -77
- package/lib/Utils/logger.js +9 -3
- package/lib/Utils/lt-hash.js +37 -6
- package/lib/Utils/make-mutex.js +36 -33
- package/lib/Utils/message-composer.js +439 -233
- package/lib/Utils/message-retry-manager.js +265 -260
- package/lib/Utils/messages-media.js +855 -747
- package/lib/Utils/messages.js +2528 -884
- package/lib/Utils/meta-ai-msmsg.js +262 -0
- package/lib/Utils/native-bridge.js +82 -0
- package/lib/Utils/noise-handler.js +195 -200
- package/lib/Utils/offline-node-processor.js +35 -33
- package/lib/Utils/pre-key-manager.js +103 -102
- package/lib/Utils/process-message.js +978 -560
- package/lib/Utils/reporting-utils.js +257 -253
- package/lib/Utils/session-pool.d.ts +16 -0
- package/lib/Utils/session-pool.js +94 -0
- package/lib/Utils/signal.js +218 -195
- package/lib/Utils/stanza-ack.js +31 -29
- package/lib/Utils/sticker.d.ts +17 -0
- package/lib/Utils/sticker.js +145 -0
- package/lib/Utils/sync-action-utils.js +50 -45
- package/lib/Utils/tc-token-utils.js +137 -139
- package/lib/Utils/use-multi-file-auth-state.js +109 -109
- package/lib/Utils/validate-connection.js +218 -202
- package/lib/Utils/view-once-cache.d.ts +9 -0
- package/lib/Utils/view-once-cache.js +79 -0
- package/lib/Utils/voip-rekey.js +22 -0
- package/lib/WABinary/constants.js +1302 -1299
- package/lib/WABinary/decode.js +343 -262
- package/lib/WABinary/encode.js +12 -4
- package/lib/WABinary/generic-utils.js +223 -187
- package/lib/WABinary/index.js +33 -6
- package/lib/WABinary/jid-utils.js +351 -88
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +12 -9
- package/lib/WAM/constants.js +39486 -22853
- package/lib/WAM/encode.js +132 -140
- package/lib/WAM/index.js +31 -4
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +55 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +53 -51
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +56 -53
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -26
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +59 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +31 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -37
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +26 -24
- package/lib/WAUSync/Protocols/index.js +39 -6
- package/lib/WAUSync/USyncQuery.js +132 -97
- package/lib/WAUSync/USyncUser.js +49 -30
- package/lib/WAUSync/index.js +31 -4
- package/lib/antiban.js +4637 -0
- package/lib/index.js +48 -15
- package/package.json +55 -92
- package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -263
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -51
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -29
|
@@ -1,258 +1,262 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true })
|
|
3
|
+
exports.getMessageReportingToken = exports.shouldIncludeReportingToken = void 0
|
|
4
|
+
const Crypto_1 = require('./crypto')
|
|
5
|
+
const crypto_2 = require('./crypto')
|
|
4
6
|
const reportingFields = [
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
]
|
|
60
|
-
const compileReportingFields =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
const compiledReportingFields = compileReportingFields(reportingFields)
|
|
71
|
-
const EMPTY_MAP = new Map()
|
|
72
|
-
const ENC_SECRET_REPORT_TOKEN = 'Report Token'
|
|
7
|
+
{ f: 1 },
|
|
8
|
+
{
|
|
9
|
+
f: 3,
|
|
10
|
+
s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
|
|
11
|
+
},
|
|
12
|
+
{ f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
13
|
+
{ f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
14
|
+
{ f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
|
|
15
|
+
{ f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
|
|
16
|
+
{ f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
|
|
17
|
+
{ f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
|
|
18
|
+
{ f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
|
|
19
|
+
{ f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
20
|
+
{ f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
|
|
21
|
+
{ f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
|
|
22
|
+
{ f: 37, s: [{ f: 1, m: true }] },
|
|
23
|
+
{
|
|
24
|
+
f: 49,
|
|
25
|
+
s: [
|
|
26
|
+
{ f: 2 },
|
|
27
|
+
{ f: 3, s: [{ f: 1 }, { f: 2 }] },
|
|
28
|
+
{ f: 5, s: [{ f: 21 }, { f: 22 }] },
|
|
29
|
+
{ f: 8, s: [{ f: 1 }, { f: 2 }] }
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
{ f: 53, s: [{ f: 1, m: true }] },
|
|
33
|
+
{ f: 55, s: [{ f: 1, m: true }] },
|
|
34
|
+
{ f: 58, s: [{ f: 1, m: true }] },
|
|
35
|
+
{ f: 59, s: [{ f: 1, m: true }] },
|
|
36
|
+
{
|
|
37
|
+
f: 60,
|
|
38
|
+
s: [
|
|
39
|
+
{ f: 2 },
|
|
40
|
+
{ f: 3, s: [{ f: 1 }, { f: 2 }] },
|
|
41
|
+
{ f: 5, s: [{ f: 21 }, { f: 22 }] },
|
|
42
|
+
{ f: 8, s: [{ f: 1 }, { f: 2 }] }
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
f: 64,
|
|
47
|
+
s: [
|
|
48
|
+
{ f: 2 },
|
|
49
|
+
{ f: 3, s: [{ f: 1 }, { f: 2 }] },
|
|
50
|
+
{ f: 5, s: [{ f: 21 }, { f: 22 }] },
|
|
51
|
+
{ f: 8, s: [{ f: 1 }, { f: 2 }] }
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
{ f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
|
|
55
|
+
{ f: 74, s: [{ f: 1, m: true }] },
|
|
56
|
+
{ f: 87, s: [{ f: 1, m: true }] },
|
|
57
|
+
{ f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
|
|
58
|
+
{ f: 92, s: [{ f: 1, m: true }] },
|
|
59
|
+
{ f: 93, s: [{ f: 1, m: true }] },
|
|
60
|
+
{ f: 94, s: [{ f: 1, m: true }] }
|
|
61
|
+
]
|
|
62
|
+
const compileReportingFields = fields => {
|
|
63
|
+
const map = new Map()
|
|
64
|
+
for (const f of fields) {
|
|
65
|
+
map.set(f.f, {
|
|
66
|
+
m: f.m,
|
|
67
|
+
children: f.s ? compileReportingFields(f.s) : undefined
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
return map
|
|
71
|
+
}
|
|
72
|
+
const compiledReportingFields = compileReportingFields(reportingFields)
|
|
73
|
+
const EMPTY_MAP = new Map()
|
|
74
|
+
const ENC_SECRET_REPORT_TOKEN = 'Report Token'
|
|
73
75
|
const WIRE = {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
VARINT: 0,
|
|
77
|
+
FIXED64: 1,
|
|
78
|
+
BYTES: 2,
|
|
79
|
+
FIXED32: 5
|
|
80
|
+
}
|
|
81
|
+
const shouldIncludeReportingToken = message =>
|
|
82
|
+
!message.reactionMessage &&
|
|
83
|
+
!message.encReactionMessage &&
|
|
84
|
+
!message.encEventResponseMessage &&
|
|
85
|
+
!message.pollUpdateMessage
|
|
86
|
+
exports.shouldIncludeReportingToken = shouldIncludeReportingToken
|
|
83
87
|
const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
88
|
+
const useCaseSecret = Buffer.concat([
|
|
89
|
+
Buffer.from(origMsgId, 'utf8'),
|
|
90
|
+
Buffer.from(origMsgSender, 'utf8'),
|
|
91
|
+
Buffer.from(modificationSender, 'utf8'),
|
|
92
|
+
Buffer.from(modificationType, 'utf8')
|
|
93
|
+
])
|
|
94
|
+
return (0, crypto_2.hkdf)(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') })
|
|
95
|
+
}
|
|
92
96
|
const extractReportingTokenContent = (data, cfg) => {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
97
|
+
const out = []
|
|
98
|
+
let i = 0
|
|
99
|
+
while (i < data.length) {
|
|
100
|
+
const tag = decodeVarint(data, i)
|
|
101
|
+
if (!tag.ok) {
|
|
102
|
+
return null
|
|
103
|
+
}
|
|
104
|
+
const fieldNum = tag.value >> 3
|
|
105
|
+
const wireType = tag.value & 0x7
|
|
106
|
+
const fieldStart = i
|
|
107
|
+
i += tag.bytes
|
|
108
|
+
const fieldCfg = cfg.get(fieldNum)
|
|
109
|
+
const pushSlice = end => {
|
|
110
|
+
if (end > data.length) {
|
|
111
|
+
return false
|
|
112
|
+
}
|
|
113
|
+
out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) })
|
|
114
|
+
i = end
|
|
115
|
+
return true
|
|
116
|
+
}
|
|
117
|
+
const skip = end => {
|
|
118
|
+
if (end > data.length) {
|
|
119
|
+
return false
|
|
120
|
+
}
|
|
121
|
+
i = end
|
|
122
|
+
return true
|
|
123
|
+
}
|
|
124
|
+
if (wireType === WIRE.VARINT) {
|
|
125
|
+
const v = decodeVarint(data, i)
|
|
126
|
+
if (!v.ok) {
|
|
127
|
+
return null
|
|
128
|
+
}
|
|
129
|
+
const end = i + v.bytes
|
|
130
|
+
if (!fieldCfg) {
|
|
131
|
+
if (!skip(end)) {
|
|
132
|
+
return null
|
|
133
|
+
}
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
136
|
+
if (!pushSlice(end)) {
|
|
137
|
+
return null
|
|
138
|
+
}
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (wireType === WIRE.FIXED64) {
|
|
142
|
+
const end = i + 8
|
|
143
|
+
if (!fieldCfg) {
|
|
144
|
+
if (!skip(end)) {
|
|
145
|
+
return null
|
|
146
|
+
}
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
if (!pushSlice(end)) {
|
|
150
|
+
return null
|
|
151
|
+
}
|
|
152
|
+
continue
|
|
153
|
+
}
|
|
154
|
+
if (wireType === WIRE.FIXED32) {
|
|
155
|
+
const end = i + 4
|
|
156
|
+
if (!fieldCfg) {
|
|
157
|
+
if (!skip(end)) {
|
|
158
|
+
return null
|
|
159
|
+
}
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
if (!pushSlice(end)) {
|
|
163
|
+
return null
|
|
164
|
+
}
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
167
|
+
if (wireType === WIRE.BYTES) {
|
|
168
|
+
const len = decodeVarint(data, i)
|
|
169
|
+
if (!len.ok) {
|
|
170
|
+
return null
|
|
171
|
+
}
|
|
172
|
+
const valStart = i + len.bytes
|
|
173
|
+
const valEnd = valStart + len.value
|
|
174
|
+
if (valEnd > data.length) {
|
|
175
|
+
return null
|
|
176
|
+
}
|
|
177
|
+
if (!fieldCfg) {
|
|
178
|
+
i = valEnd
|
|
179
|
+
continue
|
|
180
|
+
}
|
|
181
|
+
if (fieldCfg.m || fieldCfg.children) {
|
|
182
|
+
const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP)
|
|
183
|
+
if (sub === null) {
|
|
184
|
+
return null
|
|
185
|
+
}
|
|
186
|
+
if (sub.length > 0) {
|
|
187
|
+
const newTag = encodeVarint(tag.value)
|
|
188
|
+
const newLen = encodeVarint(sub.length)
|
|
189
|
+
out.push({
|
|
190
|
+
num: fieldNum,
|
|
191
|
+
bytes: Buffer.concat([newTag, newLen, sub])
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
i = valEnd
|
|
195
|
+
continue
|
|
196
|
+
}
|
|
197
|
+
out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) })
|
|
198
|
+
i = valEnd
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
return null
|
|
202
|
+
}
|
|
203
|
+
if (out.length === 0) {
|
|
204
|
+
return Buffer.alloc(0)
|
|
205
|
+
}
|
|
206
|
+
out.sort((a, b) => a.num - b.num)
|
|
207
|
+
return Buffer.concat(out.map(f => f.bytes))
|
|
208
|
+
}
|
|
205
209
|
const decodeVarint = (buffer, offset) => {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
const encodeVarint =
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
|
|
210
|
+
let value = 0
|
|
211
|
+
let bytes = 0
|
|
212
|
+
let shift = 0
|
|
213
|
+
while (offset + bytes < buffer.length) {
|
|
214
|
+
const current = buffer[offset + bytes]
|
|
215
|
+
value |= (current & 0x7f) << shift
|
|
216
|
+
bytes++
|
|
217
|
+
if ((current & 0x80) === 0) {
|
|
218
|
+
return { value, bytes, ok: true }
|
|
219
|
+
}
|
|
220
|
+
shift += 7
|
|
221
|
+
if (shift > 35) {
|
|
222
|
+
return { value: 0, bytes: 0, ok: false }
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return { value: 0, bytes: 0, ok: false }
|
|
226
|
+
}
|
|
227
|
+
const encodeVarint = value => {
|
|
228
|
+
const parts = []
|
|
229
|
+
let remaining = value >>> 0
|
|
230
|
+
while (remaining > 0x7f) {
|
|
231
|
+
parts.push((remaining & 0x7f) | 0x80)
|
|
232
|
+
remaining >>>= 7
|
|
233
|
+
}
|
|
234
|
+
parts.push(remaining)
|
|
235
|
+
return Buffer.from(parts)
|
|
236
|
+
}
|
|
237
|
+
const getMessageReportingToken = async (msgProtobuf, message, key) => {
|
|
238
|
+
const msgSecret = message.messageContextInfo?.messageSecret
|
|
239
|
+
if (!msgSecret || !key.id) {
|
|
240
|
+
return null
|
|
241
|
+
}
|
|
242
|
+
const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid
|
|
243
|
+
const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid
|
|
244
|
+
const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret)
|
|
245
|
+
const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields)
|
|
246
|
+
if (!content || content.length === 0) {
|
|
247
|
+
return null
|
|
248
|
+
}
|
|
249
|
+
const reportingToken = Buffer.from(Crypto_1.hmacSign(content, reportingSecret)).subarray(0, 16)
|
|
250
|
+
return {
|
|
251
|
+
tag: 'reporting',
|
|
252
|
+
attrs: {},
|
|
253
|
+
content: [
|
|
254
|
+
{
|
|
255
|
+
tag: 'reporting_token',
|
|
256
|
+
attrs: { v: '2' },
|
|
257
|
+
content: reportingToken
|
|
258
|
+
}
|
|
259
|
+
]
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
exports.getMessageReportingToken = getMessageReportingToken
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface SessionPoolOptions {
|
|
2
|
+
makeSocket: (sessionId: string) => Promise<any> | any
|
|
3
|
+
logger?: any
|
|
4
|
+
maxBackoffMs?: number
|
|
5
|
+
onSessionUpdate?: (sessionId: string, sock: any) => void
|
|
6
|
+
onLoggedOut?: (sessionId: string) => void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface SessionPool {
|
|
10
|
+
add: (sessionId: string) => Promise<void>
|
|
11
|
+
remove: (sessionId: string) => void
|
|
12
|
+
get: (sessionId: string) => any
|
|
13
|
+
list: () => string[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createSessionPool(options: SessionPoolOptions): SessionPool
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true })
|
|
3
|
+
exports.createSessionPool = void 0
|
|
4
|
+
|
|
5
|
+
const DISCONNECT_REASON_LOGGED_OUT = 401
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Manages multiple Baileys socket instances (one per account/session), automatically
|
|
9
|
+
* restarting any session that disconnects -- except when it was logged out -- using
|
|
10
|
+
* exponential backoff with jitter, instead of every project re-implementing its own
|
|
11
|
+
* reconnect loop.
|
|
12
|
+
*
|
|
13
|
+
* @param {{
|
|
14
|
+
* makeSocket: (sessionId: string) => Promise<any> | any,
|
|
15
|
+
* logger?: any,
|
|
16
|
+
* maxBackoffMs?: number,
|
|
17
|
+
* onSessionUpdate?: (sessionId: string, sock: any) => void,
|
|
18
|
+
* onLoggedOut?: (sessionId: string) => void
|
|
19
|
+
* }} options
|
|
20
|
+
* @returns {{
|
|
21
|
+
* add: (sessionId: string) => Promise<void>,
|
|
22
|
+
* remove: (sessionId: string) => void,
|
|
23
|
+
* get: (sessionId: string) => any,
|
|
24
|
+
* list: () => string[]
|
|
25
|
+
* }}
|
|
26
|
+
*/
|
|
27
|
+
const createSessionPool = options => {
|
|
28
|
+
const logger = options.logger
|
|
29
|
+
const maxBackoffMs = options.maxBackoffMs ?? 60000
|
|
30
|
+
const sessions = new Map() // sessionId -> { sock, attempts, stopped }
|
|
31
|
+
|
|
32
|
+
const computeBackoffMs = attempts => {
|
|
33
|
+
const base = Math.min(1000 * 2 ** attempts, maxBackoffMs)
|
|
34
|
+
return base + Math.floor(Math.random() * (base * 0.2))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const start = async sessionId => {
|
|
38
|
+
const entry = sessions.get(sessionId) || { attempts: 0, stopped: false }
|
|
39
|
+
sessions.set(sessionId, entry)
|
|
40
|
+
if (entry.stopped) return
|
|
41
|
+
try {
|
|
42
|
+
const sock = await options.makeSocket(sessionId)
|
|
43
|
+
entry.sock = sock
|
|
44
|
+
options.onSessionUpdate?.(sessionId, sock)
|
|
45
|
+
sock.ev.on('connection.update', update => {
|
|
46
|
+
if (update.connection === 'open') {
|
|
47
|
+
entry.attempts = 0
|
|
48
|
+
}
|
|
49
|
+
if (update.connection === 'close') {
|
|
50
|
+
const statusCode = update.lastDisconnect?.error?.output?.statusCode
|
|
51
|
+
if (statusCode === DISCONNECT_REASON_LOGGED_OUT) {
|
|
52
|
+
logger?.info?.({ sessionId }, 'session logged out, removing from pool')
|
|
53
|
+
sessions.delete(sessionId)
|
|
54
|
+
options.onLoggedOut?.(sessionId)
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
if (entry.stopped) return
|
|
58
|
+
entry.attempts += 1
|
|
59
|
+
const backoffMs = computeBackoffMs(entry.attempts)
|
|
60
|
+
logger?.warn?.(
|
|
61
|
+
{ sessionId, attempt: entry.attempts, backoffMs },
|
|
62
|
+
'session disconnected, reconnecting with backoff'
|
|
63
|
+
)
|
|
64
|
+
setTimeout(() => start(sessionId), backoffMs)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
} catch (error) {
|
|
68
|
+
entry.attempts += 1
|
|
69
|
+
const backoffMs = computeBackoffMs(entry.attempts)
|
|
70
|
+
logger?.error?.({ error, sessionId, backoffMs }, 'failed to start session, retrying with backoff')
|
|
71
|
+
if (!entry.stopped) {
|
|
72
|
+
setTimeout(() => start(sessionId), backoffMs)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const add = sessionId => start(sessionId)
|
|
78
|
+
const remove = sessionId => {
|
|
79
|
+
const entry = sessions.get(sessionId)
|
|
80
|
+
if (!entry) return
|
|
81
|
+
entry.stopped = true
|
|
82
|
+
if (entry.sock?.end) {
|
|
83
|
+
try {
|
|
84
|
+
entry.sock.end(undefined)
|
|
85
|
+
} catch {}
|
|
86
|
+
}
|
|
87
|
+
sessions.delete(sessionId)
|
|
88
|
+
}
|
|
89
|
+
const get = sessionId => sessions.get(sessionId)?.sock
|
|
90
|
+
const list = () => [...sessions.keys()]
|
|
91
|
+
|
|
92
|
+
return { add, remove, get, list }
|
|
93
|
+
}
|
|
94
|
+
exports.createSessionPool = createSessionPool
|