@hiroosy/baileys-lite 0.0.4 → 0.3.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 +658 -10
- package/WAProto/index.js +2 -2558
- package/engine-requirements.js +3 -4
- package/package.json +8 -3
- package/src/binary/wa-binary.js +3 -25
- package/src/constants.js +7 -15
- package/src/defaults.js +2 -5
- package/src/foundation/ai-rich.js +530 -0
- package/src/foundation/audio-duration.js +43 -103
- package/src/foundation/audio-samples.js +19 -37
- package/src/foundation/boom.js +0 -7
- package/src/foundation/cache.js +4 -26
- package/src/foundation/concurrency.js +1 -19
- package/src/foundation/curve25519.js +7 -39
- package/src/foundation/link-preview.js +15 -57
- package/src/foundation/logger.js +0 -11
- package/src/foundation/long.js +4 -14
- package/src/foundation/protobuf-minimal.js +21 -53
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +702 -0
- package/src/foundation/wa-crypto.js +2 -31
- package/src/foundation/zip.js +25 -41
- package/src/index.js +8 -18
- package/src/signal/libsignal.js +23 -78
- package/src/signal/signal-group.js +0 -35
- package/src/signal/signal-repository.js +10 -29
- package/src/socket/business-communities.js +12 -31
- package/src/socket/chats.js +5 -8
- package/src/socket/groups-newsletter.js +18 -17
- package/src/socket/messages-recv.js +81 -127
- package/src/socket/messages-send.js +372 -139
- package/src/socket/socket-core.js +227 -157
- package/src/socket/usync.js +2 -6
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +84 -38
- package/src/utils/auth-state-core.js +14 -56
- package/src/utils/auth-state-storage.js +7 -42
- package/src/utils/chat-sync.js +4 -26
- package/src/utils/media.js +15 -71
- package/src/utils/message-compose.js +208 -85
- package/src/utils/message-processing.js +10 -65
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +71 -127
- package/src/vendor/jpeg-js/decoder.js +75 -136
- package/src/vendor/jpeg-js/encoder.js +179 -264
- package/src/vendor/pngjs/bitmapper.js +5 -27
- package/src/vendor/pngjs/bitpacker.js +4 -12
- package/src/vendor/pngjs/constants.js +3 -11
- package/src/vendor/pngjs/crc.js +0 -6
- package/src/vendor/pngjs/filter-pack.js +1 -7
- package/src/vendor/pngjs/filter-parse-sync.js +0 -6
- package/src/vendor/pngjs/filter-parse.js +1 -11
- package/src/vendor/pngjs/format-normaliser.js +5 -11
- package/src/vendor/pngjs/interlace.js +7 -24
- package/src/vendor/pngjs/packer-sync.js +0 -11
- package/src/vendor/pngjs/packer.js +7 -15
- package/src/vendor/pngjs/paeth-predictor.js +0 -6
- package/src/vendor/pngjs/parser-sync.js +0 -8
- package/src/vendor/pngjs/parser.js +5 -29
- package/src/vendor/pngjs/png-sync.js +0 -7
- package/src/vendor/pngjs/sync-inflate.js +7 -16
- package/src/vendor/pngjs/sync-reader.js +4 -11
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1705 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +628 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +0 -1
- package/src/wam/wam.js +2 -7
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
import { createNoopLogger, unpadPkcs7, writeRandomPadMax16 } from '../shim/core.js';
|
|
2
|
+
import { getProto } from '../shim/proto.js';
|
|
3
|
+
import { parseSignalAddressFromJid, toUserJid } from '../shim/protocol.js';
|
|
4
|
+
import { buildReceiptNode, findNodeChild, getFirstNodeChild, getNodeChildren, getNodeChildrenByTag } from '../shim/transport.js';
|
|
5
|
+
import { bytesToHex, toError } from '../shim/util.js';
|
|
6
|
+
import { randomBytes } from '../crypto/primitives.js';
|
|
7
|
+
export async function encodeWAMessage(message) {
|
|
8
|
+
const proto = await getProto();
|
|
9
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish());
|
|
10
|
+
}
|
|
11
|
+
export function generateCallId() {
|
|
12
|
+
const bytes = new Uint8Array(16);
|
|
13
|
+
for (let i = 0; i < 16; i++) {
|
|
14
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
15
|
+
}
|
|
16
|
+
return bytesToHex(bytes).toUpperCase();
|
|
17
|
+
}
|
|
18
|
+
export function generateCallStanzaId() {
|
|
19
|
+
return bytesToHex(randomBytes(16)).toUpperCase();
|
|
20
|
+
}
|
|
21
|
+
export function extractNodeInfo(node) {
|
|
22
|
+
const innerNode = getFirstNodeChild(node);
|
|
23
|
+
if (!innerNode) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
tag: innerNode.tag,
|
|
28
|
+
peerJid: node.attrs.from,
|
|
29
|
+
callId: innerNode.attrs?.['call-id'] || '',
|
|
30
|
+
peerPlatform: node.attrs.platform || '',
|
|
31
|
+
peerAppVersion: node.attrs.version || '',
|
|
32
|
+
epochId: innerNode.attrs?.e,
|
|
33
|
+
timestamp: innerNode.attrs?.t,
|
|
34
|
+
innerNode
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function toRelayEndpoint(node) {
|
|
38
|
+
const relay = {
|
|
39
|
+
ip: node.attrs?.ip || '',
|
|
40
|
+
port: parseInt(node.attrs?.port || '3480', 10),
|
|
41
|
+
token: node.attrs?.token || '',
|
|
42
|
+
key: node.attrs?.['relay-key'] || node.attrs?.key || '',
|
|
43
|
+
relayId: parseInt(node.attrs?.['relay-id'] || '0', 10),
|
|
44
|
+
c2rRtt: node.attrs?.['c2r-rtt'] ? parseInt(node.attrs['c2r-rtt'], 10) : undefined
|
|
45
|
+
};
|
|
46
|
+
return relay.ip && relay.token ? relay : null;
|
|
47
|
+
}
|
|
48
|
+
export function extractRelayEndpoints(node) {
|
|
49
|
+
const relayNodes = [...getNodeChildrenByTag(node, 'relay')];
|
|
50
|
+
for (const wrapper of getNodeChildrenByTag(node, 'relays')) {
|
|
51
|
+
relayNodes.push(...getNodeChildrenByTag(wrapper, 'relay'));
|
|
52
|
+
}
|
|
53
|
+
const relays = [];
|
|
54
|
+
for (const relayNode of relayNodes) {
|
|
55
|
+
const relay = toRelayEndpoint(relayNode);
|
|
56
|
+
if (relay) {
|
|
57
|
+
relays.push(relay);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
relays.sort((a, b) => (a.c2rRtt ?? Infinity) - (b.c2rRtt ?? Infinity));
|
|
61
|
+
return relays;
|
|
62
|
+
}
|
|
63
|
+
export async function decryptCallKey(deps, node, peerJid, logger) {
|
|
64
|
+
const log = logger ?? createNoopLogger();
|
|
65
|
+
const proto = await getProto();
|
|
66
|
+
const isEnc = (child) => child.tag === 'enc' && !!child.attrs?.type;
|
|
67
|
+
const encNodes = getNodeChildren(node).filter(isEnc);
|
|
68
|
+
const destinationNode = findNodeChild(node, 'destination');
|
|
69
|
+
if (destinationNode) {
|
|
70
|
+
for (const toNode of getNodeChildren(destinationNode)) {
|
|
71
|
+
if (toNode.tag === 'to') {
|
|
72
|
+
encNodes.push(...getNodeChildren(toNode).filter(isEnc));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const address = parseSignalAddressFromJid(peerJid);
|
|
77
|
+
for (const encNode of encNodes) {
|
|
78
|
+
if (!(encNode.content instanceof Uint8Array)) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const decrypted = await deps.signalProtocol.decryptMessage(address, {
|
|
83
|
+
type: encNode.attrs.type,
|
|
84
|
+
ciphertext: encNode.content
|
|
85
|
+
});
|
|
86
|
+
const message = proto.Message.decode(unpadPkcs7(decrypted));
|
|
87
|
+
const callKey = message.call?.callKey;
|
|
88
|
+
if (callKey && callKey.length === 32) {
|
|
89
|
+
return callKey;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
log.trace('call key decrypt candidate failed', { message: toError(err).message });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
const CAPABILITY_OFFER = new Uint8Array([0x01, 0x05, 0xf7, 0x09, 0xe4, 0xbb, 0x07]);
|
|
99
|
+
const CAPABILITY_PREACCEPT = new Uint8Array([0x01, 0x05, 0xff, 0x09, 0xe4, 0xbb, 0x07]);
|
|
100
|
+
export async function buildCallParticipantNodes(deps, devices, callKey) {
|
|
101
|
+
await deps.sessionResolver.ensureSessionsBatch(devices);
|
|
102
|
+
const plaintext = await encodeWAMessage({ call: { callKey } });
|
|
103
|
+
const encrypted = await Promise.all(devices.map((jid) => deps.signalProtocol.encryptMessage(parseSignalAddressFromJid(jid), plaintext)));
|
|
104
|
+
const nodes = devices.map((jid, index) => ({
|
|
105
|
+
tag: 'to',
|
|
106
|
+
attrs: { jid },
|
|
107
|
+
content: [
|
|
108
|
+
{
|
|
109
|
+
tag: 'enc',
|
|
110
|
+
attrs: { v: '2', type: encrypted[index].type, count: '0' },
|
|
111
|
+
content: encrypted[index].ciphertext
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
}));
|
|
115
|
+
return {
|
|
116
|
+
nodes,
|
|
117
|
+
shouldIncludeDeviceIdentity: encrypted.some((entry) => entry.type === 'pkmsg')
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
export async function buildOfferStanza(deps, stores, callId, callKey, peerJid, isVideo, logger) {
|
|
121
|
+
const log = logger ?? createNoopLogger();
|
|
122
|
+
const creds = deps.authClient.getCurrentCredentials();
|
|
123
|
+
const callCreator = creds?.meLid || creds?.meJid || '';
|
|
124
|
+
const synced = await deps.signalDeviceSync.syncDeviceList([peerJid]);
|
|
125
|
+
const devices = synced.flatMap((entry) => entry.deviceJids);
|
|
126
|
+
if (devices.length === 0) {
|
|
127
|
+
throw new Error(`no device sessions to encrypt the call offer for ${peerJid}`);
|
|
128
|
+
}
|
|
129
|
+
const { nodes: destinations, shouldIncludeDeviceIdentity } = await buildCallParticipantNodes(deps, devices, callKey);
|
|
130
|
+
const offerContent = [];
|
|
131
|
+
try {
|
|
132
|
+
const peerJidNormalized = toUserJid(peerJid);
|
|
133
|
+
const tcTokenRecord = await stores.privacyToken.getByJid(peerJidNormalized);
|
|
134
|
+
const tctoken = tcTokenRecord?.tcToken;
|
|
135
|
+
if (tctoken) {
|
|
136
|
+
offerContent.push({
|
|
137
|
+
tag: 'privacy',
|
|
138
|
+
attrs: {},
|
|
139
|
+
content: tctoken instanceof Uint8Array ? tctoken : new Uint8Array(tctoken)
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
catch (err) {
|
|
144
|
+
log.trace('tctoken lookup failed', { message: toError(err).message });
|
|
145
|
+
}
|
|
146
|
+
offerContent.push({ tag: 'audio', attrs: { enc: 'opus', rate: '8000' }, content: undefined }, { tag: 'audio', attrs: { enc: 'opus', rate: '16000' }, content: undefined });
|
|
147
|
+
if (isVideo) {
|
|
148
|
+
|
|
149
|
+
offerContent.push({
|
|
150
|
+
tag: 'video',
|
|
151
|
+
attrs: {
|
|
152
|
+
enc: 'h.264',
|
|
153
|
+
dec: 'H264',
|
|
154
|
+
screen_width: '1920',
|
|
155
|
+
screen_height: '1080',
|
|
156
|
+
device_orientation: '0'
|
|
157
|
+
},
|
|
158
|
+
content: undefined
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
offerContent.push({ tag: 'net', attrs: { medium: '3' }, content: undefined });
|
|
162
|
+
offerContent.push({
|
|
163
|
+
tag: 'capability',
|
|
164
|
+
attrs: { ver: '1' },
|
|
165
|
+
content: CAPABILITY_OFFER
|
|
166
|
+
});
|
|
167
|
+
offerContent.push({ tag: 'destination', attrs: {}, content: destinations });
|
|
168
|
+
offerContent.push({
|
|
169
|
+
tag: 'encopt',
|
|
170
|
+
attrs: { keygen: '2' },
|
|
171
|
+
content: undefined
|
|
172
|
+
});
|
|
173
|
+
if (shouldIncludeDeviceIdentity && creds?.signedIdentity) {
|
|
174
|
+
offerContent.push({
|
|
175
|
+
tag: 'device-identity',
|
|
176
|
+
attrs: {},
|
|
177
|
+
content: creds.signedIdentity
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
tag: 'call',
|
|
182
|
+
attrs: { to: peerJid, id: generateCallStanzaId() },
|
|
183
|
+
content: [
|
|
184
|
+
{
|
|
185
|
+
tag: 'offer',
|
|
186
|
+
attrs: { 'call-id': callId, 'call-creator': callCreator },
|
|
187
|
+
content: offerContent
|
|
188
|
+
}
|
|
189
|
+
]
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
export async function buildAcceptStanza(deps, callId, callKey, peerJid, callCreator, isVideo) {
|
|
193
|
+
await deps.messageDispatch.syncSignalSession(callCreator);
|
|
194
|
+
const bytes = await encodeWAMessage({ call: { callKey } });
|
|
195
|
+
let encNode;
|
|
196
|
+
let shouldIncludeDeviceIdentity = false;
|
|
197
|
+
try {
|
|
198
|
+
const { type, ciphertext } = await deps.signalProtocol.encryptMessage(parseSignalAddressFromJid(callCreator), bytes);
|
|
199
|
+
if (type === 'pkmsg') {
|
|
200
|
+
shouldIncludeDeviceIdentity = true;
|
|
201
|
+
}
|
|
202
|
+
encNode = {
|
|
203
|
+
tag: 'enc',
|
|
204
|
+
attrs: { v: '2', type, count: '0' },
|
|
205
|
+
content: ciphertext
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
catch (err) {
|
|
209
|
+
throw new Error(`Failed to encrypt accept for ${callCreator}: ${err.message}`);
|
|
210
|
+
}
|
|
211
|
+
const acceptContent = [
|
|
212
|
+
{ tag: 'audio', attrs: { enc: 'opus', rate: '16000' } },
|
|
213
|
+
{ tag: 'net', attrs: { medium: '3' } },
|
|
214
|
+
encNode,
|
|
215
|
+
{ tag: 'encopt', attrs: { keygen: '2' } }
|
|
216
|
+
];
|
|
217
|
+
const acceptSignedIdentity = deps.authClient.getCurrentCredentials()?.signedIdentity;
|
|
218
|
+
if (shouldIncludeDeviceIdentity && acceptSignedIdentity) {
|
|
219
|
+
acceptContent.push({
|
|
220
|
+
tag: 'device-identity',
|
|
221
|
+
attrs: {},
|
|
222
|
+
content: acceptSignedIdentity
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (isVideo) {
|
|
226
|
+
|
|
227
|
+
acceptContent.push({ tag: 'video', attrs: { dec: 'H264', device_orientation: '0' } });
|
|
228
|
+
}
|
|
229
|
+
const toJidClean = toUserJid(peerJid);
|
|
230
|
+
return {
|
|
231
|
+
tag: 'call',
|
|
232
|
+
attrs: { to: toJidClean, id: generateCallStanzaId() },
|
|
233
|
+
content: [
|
|
234
|
+
{
|
|
235
|
+
tag: 'accept',
|
|
236
|
+
attrs: { 'call-id': callId, 'call-creator': callCreator },
|
|
237
|
+
content: acceptContent
|
|
238
|
+
}
|
|
239
|
+
]
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
export function buildTerminateStanza(peerJid, callId, callCreator, audioDurationMs, reason) {
|
|
243
|
+
const attrs = {
|
|
244
|
+
'call-id': callId,
|
|
245
|
+
'call-creator': callCreator
|
|
246
|
+
};
|
|
247
|
+
if (audioDurationMs !== undefined && audioDurationMs >= 0) {
|
|
248
|
+
const ms = String(Math.floor(audioDurationMs));
|
|
249
|
+
attrs.duration = ms;
|
|
250
|
+
attrs.audio_duration = ms;
|
|
251
|
+
}
|
|
252
|
+
if (reason !== undefined) {
|
|
253
|
+
attrs.reason = reason;
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
tag: 'call',
|
|
257
|
+
attrs: { to: peerJid, id: generateCallStanzaId() },
|
|
258
|
+
content: [
|
|
259
|
+
{
|
|
260
|
+
tag: 'terminate',
|
|
261
|
+
attrs,
|
|
262
|
+
content: undefined
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
export function buildRelaylatencyForwardStanza(peerJid, callId, callCreator, teNodes, destinationJids) {
|
|
268
|
+
const destinationContent = destinationJids.map((jid) => ({
|
|
269
|
+
tag: 'to',
|
|
270
|
+
attrs: { jid },
|
|
271
|
+
content: undefined
|
|
272
|
+
}));
|
|
273
|
+
return {
|
|
274
|
+
tag: 'call',
|
|
275
|
+
attrs: { to: toUserJid(peerJid), id: generateCallStanzaId() },
|
|
276
|
+
content: [
|
|
277
|
+
{
|
|
278
|
+
tag: 'relaylatency',
|
|
279
|
+
attrs: { 'call-id': callId, 'call-creator': callCreator },
|
|
280
|
+
content: [
|
|
281
|
+
...teNodes,
|
|
282
|
+
{ tag: 'destination', attrs: {}, content: destinationContent }
|
|
283
|
+
]
|
|
284
|
+
}
|
|
285
|
+
]
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
export function buildRejectStanza(peerJid, callId, callCreator) {
|
|
289
|
+
const toJidClean = toUserJid(peerJid);
|
|
290
|
+
return {
|
|
291
|
+
tag: 'call',
|
|
292
|
+
attrs: { to: toJidClean, id: generateCallStanzaId() },
|
|
293
|
+
content: [
|
|
294
|
+
{
|
|
295
|
+
tag: 'reject',
|
|
296
|
+
attrs: { 'call-id': callId, 'call-creator': callCreator }
|
|
297
|
+
}
|
|
298
|
+
]
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
export function buildPreacceptStanza(peerJid, callId, callCreator) {
|
|
302
|
+
return {
|
|
303
|
+
tag: 'call',
|
|
304
|
+
attrs: { to: peerJid, id: generateCallStanzaId() },
|
|
305
|
+
content: [
|
|
306
|
+
{
|
|
307
|
+
tag: 'preaccept',
|
|
308
|
+
attrs: { 'call-id': callId, 'call-creator': callCreator },
|
|
309
|
+
content: [
|
|
310
|
+
{ tag: 'audio', attrs: { enc: 'opus', rate: '16000' } },
|
|
311
|
+
{ tag: 'encopt', attrs: { keygen: '2' } },
|
|
312
|
+
{ tag: 'capability', attrs: { ver: '1' }, content: CAPABILITY_PREACCEPT }
|
|
313
|
+
]
|
|
314
|
+
}
|
|
315
|
+
]
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
export function buildRelayLatencyStanza(peerJid, callId, callCreator, relays, destinationJids, meId) {
|
|
319
|
+
const seenRelays = new Set();
|
|
320
|
+
const teNodes = [];
|
|
321
|
+
for (const relay of relays) {
|
|
322
|
+
if (!relay.relayName || seenRelays.has(relay.relayName))
|
|
323
|
+
continue;
|
|
324
|
+
seenRelays.add(relay.relayName);
|
|
325
|
+
const encodedLatency = 0x2000000 + (relay.latency || 0);
|
|
326
|
+
teNodes.push({
|
|
327
|
+
tag: 'te',
|
|
328
|
+
attrs: {
|
|
329
|
+
latency: String(encodedLatency),
|
|
330
|
+
relay_name: relay.relayName
|
|
331
|
+
},
|
|
332
|
+
content: relay.addressBytes || undefined
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
const destinationContent = destinationJids.map((jid) => ({
|
|
336
|
+
tag: 'to',
|
|
337
|
+
attrs: { jid },
|
|
338
|
+
content: undefined
|
|
339
|
+
}));
|
|
340
|
+
const relayLatencyContent = [...teNodes];
|
|
341
|
+
if (destinationContent.length > 0) {
|
|
342
|
+
relayLatencyContent.push({
|
|
343
|
+
tag: 'destination',
|
|
344
|
+
attrs: {},
|
|
345
|
+
content: destinationContent
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
const toJidClean = toUserJid(peerJid);
|
|
349
|
+
return {
|
|
350
|
+
tag: 'call',
|
|
351
|
+
attrs: { to: toJidClean, id: generateCallStanzaId() },
|
|
352
|
+
content: [
|
|
353
|
+
{
|
|
354
|
+
tag: 'relaylatency',
|
|
355
|
+
attrs: { 'call-id': callId, 'call-creator': callCreator },
|
|
356
|
+
content: relayLatencyContent
|
|
357
|
+
}
|
|
358
|
+
]
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
export function buildTransportStanza(peerJid, callId, callCreator, meId, messageType = '0', p2pCandRound = '0') {
|
|
362
|
+
return {
|
|
363
|
+
tag: 'call',
|
|
364
|
+
attrs: { to: peerJid, id: generateCallStanzaId() },
|
|
365
|
+
content: [
|
|
366
|
+
{
|
|
367
|
+
tag: 'transport',
|
|
368
|
+
attrs: {
|
|
369
|
+
'call-id': callId,
|
|
370
|
+
'call-creator': callCreator,
|
|
371
|
+
'transport-message-type': messageType,
|
|
372
|
+
'p2p-cand-round': p2pCandRound
|
|
373
|
+
},
|
|
374
|
+
content: [
|
|
375
|
+
{
|
|
376
|
+
tag: 'net',
|
|
377
|
+
attrs: { medium: '2', protocol: '0' },
|
|
378
|
+
content: undefined
|
|
379
|
+
}
|
|
380
|
+
]
|
|
381
|
+
}
|
|
382
|
+
]
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
export function buildMuteV2Stanza(peerDeviceJid, callId, callCreator, muteState, meId) {
|
|
386
|
+
return {
|
|
387
|
+
tag: 'call',
|
|
388
|
+
attrs: { to: peerDeviceJid, id: generateCallStanzaId() },
|
|
389
|
+
content: [
|
|
390
|
+
{
|
|
391
|
+
tag: 'mute_v2',
|
|
392
|
+
attrs: {
|
|
393
|
+
'call-id': callId,
|
|
394
|
+
'call-creator': callCreator,
|
|
395
|
+
'mute-state': String(muteState)
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
]
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
export function parseMuteV2(inner) {
|
|
402
|
+
const rawMuteState = inner.attrs?.['mute-state'];
|
|
403
|
+
let muted = null;
|
|
404
|
+
if (rawMuteState === '1') {
|
|
405
|
+
muted = true;
|
|
406
|
+
}
|
|
407
|
+
else if (rawMuteState === '0') {
|
|
408
|
+
muted = false;
|
|
409
|
+
}
|
|
410
|
+
return { muted, isRequest: inner.attrs?.['request-state'] !== undefined };
|
|
411
|
+
}
|
|
412
|
+
const RAISE_HAND_ACTION = 'raise_hand';
|
|
413
|
+
const RAISE_HAND_STATE_ATTR = 'raise-hand-state';
|
|
414
|
+
export function buildRaiseHandStanza(peerDeviceJid, callId, callCreator, raised, broadcast = false) {
|
|
415
|
+
const attrs = {
|
|
416
|
+
'call-id': callId,
|
|
417
|
+
'call-creator': callCreator,
|
|
418
|
+
action: RAISE_HAND_ACTION
|
|
419
|
+
};
|
|
420
|
+
if (broadcast) {
|
|
421
|
+
attrs.broadcast = '1';
|
|
422
|
+
}
|
|
423
|
+
return {
|
|
424
|
+
tag: 'call',
|
|
425
|
+
attrs: { to: peerDeviceJid, id: generateCallStanzaId() },
|
|
426
|
+
content: [
|
|
427
|
+
{
|
|
428
|
+
tag: 'user_action',
|
|
429
|
+
attrs,
|
|
430
|
+
content: [
|
|
431
|
+
{
|
|
432
|
+
tag: RAISE_HAND_ACTION,
|
|
433
|
+
attrs: { [RAISE_HAND_STATE_ATTR]: raised ? '1' : '0' }
|
|
434
|
+
}
|
|
435
|
+
]
|
|
436
|
+
}
|
|
437
|
+
]
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
export function parseRaiseHandState(node) {
|
|
441
|
+
const stateNode = node.tag === RAISE_HAND_ACTION ? node : findNodeChild(node, RAISE_HAND_ACTION);
|
|
442
|
+
const raw = stateNode?.attrs?.[RAISE_HAND_STATE_ATTR];
|
|
443
|
+
if (raw === '1')
|
|
444
|
+
return true;
|
|
445
|
+
if (raw === '0')
|
|
446
|
+
return false;
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
export function buildAcceptReceiptStanza(peerDeviceJid, acceptMsgId, callId, callCreator, ourJid) {
|
|
450
|
+
return buildReceiptNode({
|
|
451
|
+
kind: 'custom',
|
|
452
|
+
attrs: { to: peerDeviceJid, id: acceptMsgId, from: ourJid },
|
|
453
|
+
content: [{ tag: 'accept', attrs: { 'call-id': callId, 'call-creator': callCreator } }]
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export const VideoState = {
|
|
458
|
+
Disabled: 0,
|
|
459
|
+
Enabled: 1,
|
|
460
|
+
UpgradeRequest: 3,
|
|
461
|
+
UpgradeAccept: 4,
|
|
462
|
+
UpgradeReject: 5,
|
|
463
|
+
Stopped: 6,
|
|
464
|
+
UpgradeCancel: 8,
|
|
465
|
+
UpgradeRequestV2: 11
|
|
466
|
+
};
|
|
467
|
+
export const VideoDecRequest = 'H264';
|
|
468
|
+
export const VideoDecAccept = 'H264,AV1';
|
|
469
|
+
|
|
470
|
+
export function buildVideoStateStanza(peerJid, callId, callCreator, state, { dec, deviceOrientation } = {}) {
|
|
471
|
+
const attrs = {
|
|
472
|
+
'call-id': callId,
|
|
473
|
+
'call-creator': callCreator,
|
|
474
|
+
state: String(state)
|
|
475
|
+
};
|
|
476
|
+
if (dec)
|
|
477
|
+
attrs.dec = dec;
|
|
478
|
+
if (state === VideoState.UpgradeRequestV2)
|
|
479
|
+
attrs.voip_settings = 'video';
|
|
480
|
+
if (deviceOrientation !== undefined && deviceOrientation !== null)
|
|
481
|
+
attrs.device_orientation = String(deviceOrientation);
|
|
482
|
+
return {
|
|
483
|
+
tag: 'call',
|
|
484
|
+
attrs: { to: peerJid, id: generateCallStanzaId() },
|
|
485
|
+
content: [{ tag: 'video', attrs }]
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
export const ENCRYPTED_TAGS = ['preaccept', 'accept'];
|
|
489
|
+
export function needsDecryption(tag) {
|
|
490
|
+
return ENCRYPTED_TAGS.includes(tag);
|
|
491
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export var CallState;
|
|
2
|
+
(function (CallState) {
|
|
3
|
+
CallState["Initiating"] = "initiating";
|
|
4
|
+
CallState["Ringing"] = "ringing";
|
|
5
|
+
CallState["IncomingRinging"] = "incoming_ringing";
|
|
6
|
+
CallState["Connecting"] = "connecting";
|
|
7
|
+
CallState["Active"] = "active";
|
|
8
|
+
CallState["OnHold"] = "on_hold";
|
|
9
|
+
CallState["Ended"] = "ended";
|
|
10
|
+
})(CallState || (CallState = {}));
|
|
11
|
+
export var CallDirection;
|
|
12
|
+
(function (CallDirection) {
|
|
13
|
+
CallDirection["Outgoing"] = "outgoing";
|
|
14
|
+
CallDirection["Incoming"] = "incoming";
|
|
15
|
+
})(CallDirection || (CallDirection = {}));
|
|
16
|
+
export var CallMediaType;
|
|
17
|
+
(function (CallMediaType) {
|
|
18
|
+
CallMediaType["Audio"] = "audio";
|
|
19
|
+
CallMediaType["Video"] = "video";
|
|
20
|
+
})(CallMediaType || (CallMediaType = {}));
|
|
21
|
+
export var EndCallReason;
|
|
22
|
+
(function (EndCallReason) {
|
|
23
|
+
EndCallReason["UserEnded"] = "user_ended";
|
|
24
|
+
EndCallReason["Declined"] = "declined";
|
|
25
|
+
EndCallReason["Timeout"] = "timeout";
|
|
26
|
+
EndCallReason["Busy"] = "busy";
|
|
27
|
+
EndCallReason["Cancelled"] = "cancelled";
|
|
28
|
+
EndCallReason["Failed"] = "failed";
|
|
29
|
+
EndCallReason["DoNotDisturb"] = "do_not_disturb";
|
|
30
|
+
EndCallReason["Unknown"] = "unknown";
|
|
31
|
+
})(EndCallReason || (EndCallReason = {}));
|
|
32
|
+
export var PayloadType;
|
|
33
|
+
(function (PayloadType) {
|
|
34
|
+
PayloadType[PayloadType["WhatsAppOpus"] = 120] = "WhatsAppOpus";
|
|
35
|
+
|
|
36
|
+
PayloadType[PayloadType["H264"] = 97] = "H264";
|
|
37
|
+
})(PayloadType || (PayloadType = {}));
|
|
38
|
+
export const DEFAULT_VIDEO_CONFIG = {
|
|
39
|
+
|
|
40
|
+
width: 640,
|
|
41
|
+
height: 360,
|
|
42
|
+
frameRate: 20,
|
|
43
|
+
|
|
44
|
+
clockRate: 90000
|
|
45
|
+
};
|
|
46
|
+
export const DEFAULT_AUDIO_CONFIG = {
|
|
47
|
+
sampleRate: 16000,
|
|
48
|
+
captureChunkSize: 960,
|
|
49
|
+
playbackOutputSize: 256,
|
|
50
|
+
maxBufferSize: 1600,
|
|
51
|
+
intervalMs: 60
|
|
52
|
+
};
|
|
53
|
+
export const SRTP_SEND_AUTH_TAG_LEN = 4;
|
|
54
|
+
export const SRTP_RECV_AUTH_TAG_LEN = 4;
|
|
55
|
+
export const SRTP_AUTH_TAG_LEN = 4;
|
|
56
|
+
export const SRTP_LABEL = {
|
|
57
|
+
ENCRYPTION: 0x00,
|
|
58
|
+
AUTH: 0x01,
|
|
59
|
+
SALT: 0x02
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const SRTCP_LABEL = {
|
|
63
|
+
ENCRYPTION: 0x03,
|
|
64
|
+
AUTH: 0x04,
|
|
65
|
+
SALT: 0x05
|
|
66
|
+
};
|
|
67
|
+
export const RTCP_AUTH_TAG_LEN = 10;
|
|
68
|
+
export const WA_RELAY_PORT = 3480;
|
|
69
|
+
export const WA_DTLS_FINGERPRINT = 'sha-256 F9:CA:0C:98:A3:CC:71:D6:42:CE:5A:E2:53:D2:15:20:D3:1B:BA:D8:57:A4:F0:AF:BE:0B:FB:F3:6B:0C:A0:68';
|