@hiroosy/baileys-lite 0.3.7 → 0.3.9

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.
@@ -164,23 +164,141 @@ export function createWasmStreamDescriptors(streamSsrcs) {
164
164
  }
165
165
  return concatBytes(entries);
166
166
  }
167
- function encodeXorRelayedAddress(ip, port) {
168
- const data = new Uint8Array(8);
169
- data[0] = 0x00;
170
- data[1] = 0x01;
171
- writeUInt16BE(data, port ^ (STUN_MAGIC_COOKIE >>> 16), 2);
172
- const parts = ip.split('.').map(Number);
173
- const ipNum = ((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]) >>> 0;
174
- writeUInt32BE(data, (ipNum ^ STUN_MAGIC_COOKIE) >>> 0, 4);
167
+ function parseIpv4Bytes(ip) {
168
+ const octets = ip.split('.');
169
+ if (octets.length !== 4) {
170
+ return null;
171
+ }
172
+ const out = new Uint8Array(4);
173
+ for (let i = 0; i < 4; i++) {
174
+ if (!/^\d{1,3}$/.test(octets[i]) || Number(octets[i]) > 255) {
175
+ return null;
176
+ }
177
+ out[i] = Number(octets[i]);
178
+ }
179
+ return out;
180
+ }
181
+ function parseIpv6Bytes(text) {
182
+ let ip = text;
183
+ const zone = ip.indexOf('%');
184
+ if (zone >= 0) {
185
+ ip = ip.slice(0, zone);
186
+ }
187
+ if (ip.includes('.')) {
188
+ const split = ip.lastIndexOf(':');
189
+ const tail = split >= 0 ? parseIpv4Bytes(ip.slice(split + 1)) : null;
190
+ if (!tail) {
191
+ return null;
192
+ }
193
+ ip = `${ip.slice(0, split + 1)}${((tail[0] << 8) | tail[1]).toString(16)}:${((tail[2] << 8) | tail[3]).toString(16)}`;
194
+ }
195
+ const halves = ip.split('::');
196
+ if (halves.length > 2) {
197
+ return null;
198
+ }
199
+ const head = halves[0] ? halves[0].split(':') : [];
200
+ const tail = halves.length === 2 && halves[1] ? halves[1].split(':') : [];
201
+ const gap = 8 - head.length - tail.length;
202
+ if (halves.length === 2 ? gap < 1 : gap !== 0) {
203
+ return null;
204
+ }
205
+ const groups = [...head, ...new Array(halves.length === 2 ? gap : 0).fill('0'), ...tail];
206
+ const out = new Uint8Array(16);
207
+ for (let i = 0; i < 8; i++) {
208
+ if (!/^[0-9a-fA-F]{1,4}$/.test(groups[i])) {
209
+ return null;
210
+ }
211
+ const value = parseInt(groups[i], 16);
212
+ out[i * 2] = value >> 8;
213
+ out[i * 2 + 1] = value & 0xff;
214
+ }
215
+ return out;
216
+ }
217
+ function relayXorKey(transactionId) {
218
+ const key = new Uint8Array(16);
219
+ writeUInt32BE(key, STUN_MAGIC_COOKIE, 0);
220
+ for (let i = 0; i < 12; i++) {
221
+ key[4 + i] = transactionId[(i & ~3) + 3 - (i & 3)];
222
+ }
223
+ return key;
224
+ }
225
+ function stripAddressBrackets(text) {
226
+ if (typeof text !== 'string') {
227
+ return '';
228
+ }
229
+ const trimmed = text.trim();
230
+ return trimmed.startsWith('[') && trimmed.endsWith(']') ? trimmed.slice(1, -1) : trimmed;
231
+ }
232
+ function resolveRelayedAddressBytes(ip, form) {
233
+ const bare = stripAddressBrackets(ip);
234
+ if (!bare) {
235
+ return null;
236
+ }
237
+ const ipv4 = bare.includes(':') ? null : parseIpv4Bytes(bare);
238
+ const ipv6 = ipv4 ? null : parseIpv6Bytes(bare);
239
+ if (!ipv4 && !ipv6) {
240
+ return null;
241
+ }
242
+ if (form !== 'mapped') {
243
+ return ipv4 ?? ipv6;
244
+ }
245
+ if (ipv6) {
246
+ return ipv6;
247
+ }
248
+ const mapped = new Uint8Array(16);
249
+ mapped.set([0xff, 0xff], 10);
250
+ mapped.set(ipv4, 12);
251
+ return mapped;
252
+ }
253
+ function encodeXorRelayedAddress(ip, port, transactionId, form = 'exact') {
254
+ const portNumber = Number(port);
255
+ if (!Number.isInteger(portNumber) || portNumber < 1 || portNumber > 65535) {
256
+ return undefined;
257
+ }
258
+ if (!transactionId || transactionId.length !== 12) {
259
+ return undefined;
260
+ }
261
+ const address = resolveRelayedAddressBytes(ip, form);
262
+ if (!address) {
263
+ return undefined;
264
+ }
265
+ const data = new Uint8Array(4 + address.length);
266
+ data[1] = address.length === 16 ? 0x02 : 0x01;
267
+ writeUInt16BE(data, portNumber ^ (STUN_MAGIC_COOKIE >>> 16), 2);
268
+ const key = relayXorKey(transactionId);
269
+ for (let i = 0; i < address.length; i++) {
270
+ data[4 + i] = address[i] ^ key[i];
271
+ }
175
272
  return data;
176
273
  }
177
-
178
- export function buildAllocateForRelay(relayToken, streamDescriptors, hmacKey, relayIp, relayPort) {
179
- const transactionId = generateTransactionId();
274
+ export function parseRelayedAddressMismatch(reason) {
275
+ if (typeof reason !== 'string') {
276
+ return null;
277
+ }
278
+ const match = /destination address:\s*(.+?)\s+relay address:\s*(.+?)[\s\0]*$/i.exec(reason);
279
+ if (!match) {
280
+ return null;
281
+ }
282
+ const readEndpoint = (text) => {
283
+ const hit = /^\[(.+)\]:(\d{1,5})$/.exec(text) ?? /^([^:\s]+):(\d{1,5})$/.exec(text);
284
+ if (!hit) {
285
+ return null;
286
+ }
287
+ const port = Number(hit[2]);
288
+ return port >= 1 && port <= 65535 ? { ip: hit[1], port } : null;
289
+ };
290
+ return { destination: readEndpoint(match[1]), relay: readEndpoint(match[2]) };
291
+ }
292
+ export function buildAllocateForRelay(relayToken, streamDescriptors, hmacKey, relayIp, relayPort, transactionId = generateTransactionId(), options = {}) {
180
293
  const parts = [];
181
294
  parts.push(encodeAttribute(ATTR_SENDER_SUBSCRIPTIONS, relayToken));
182
295
  parts.push(encodeAttribute(ATTR_SSRC_LIST, streamDescriptors));
183
- parts.push(encodeAttribute(ATTR_XOR_RELAYED_ADDRESS, encodeXorRelayedAddress(relayIp, relayPort)));
296
+ if (relayIp && relayPort && options.relayedAddress !== 'none') {
297
+ const xorRelayedAddress = encodeXorRelayedAddress(relayIp, relayPort, transactionId, options.relayedAddress);
298
+ if (xorRelayedAddress) {
299
+ parts.push(encodeAttribute(ATTR_XOR_RELAYED_ADDRESS, xorRelayedAddress));
300
+ }
301
+ }
184
302
  const attrs = concatBytes(parts);
185
303
  return buildStunMessage(STUN_ALLOCATE_REQUEST, attrs, transactionId, hmacKey, false);
186
304
  }
@@ -191,7 +309,7 @@ export function buildBindingRequest(username, hmacKey, senderSubscriptions, incl
191
309
  const iceRole = options.iceRole ?? 'controlling';
192
310
  const includePriority = options.includePriority ?? true;
193
311
  const includeUsername = options.includeUsername ?? true;
194
- const transactionId = generateTransactionId();
312
+ const transactionId = options.transactionId ?? generateTransactionId();
195
313
  const usernameAttr = includeUsername ? encodeAttribute(ATTR_USERNAME, username) : undefined;
196
314
  const priorityAttr = includePriority
197
315
  ? (() => {
@@ -236,8 +354,7 @@ export function buildBindingSuccessResponse(transactionId, mappedIp, mappedPort,
236
354
  const STUN_BINDING_SUCCESS_RESPONSE = 0x0101;
237
355
  return buildStunMessage(STUN_BINDING_SUCCESS_RESPONSE, attrs, transactionId, hmacKey, true);
238
356
  }
239
- export function buildBindingRequestWithSubs(username, hmacKey, senderSubscriptions, includeIceControlling, includeFingerprint) {
240
- const transactionId = generateTransactionId();
357
+ export function buildBindingRequestWithSubs(username, hmacKey, senderSubscriptions, includeIceControlling, includeFingerprint, transactionId = generateTransactionId()) {
241
358
  const parts = [];
242
359
  if (username && username.length > 0) {
243
360
  parts.push(encodeAttribute(ATTR_USERNAME, username));
@@ -279,8 +396,7 @@ export function buildAllocateRequest(username, hmacKey, lifetime = 3600) {
279
396
  export function buildRelayBindingSuccess(transactionId, hmacKey) {
280
397
  return buildStunMessage(0x0101, new Uint8Array(0), transactionId, hmacKey, true);
281
398
  }
282
- export function buildWhatsAppPing() {
283
- const transactionId = generateTransactionId();
399
+ export function buildWhatsAppPing(transactionId = generateTransactionId()) {
284
400
  const header = new Uint8Array(20);
285
401
  writeUInt16BE(header, WHATSAPP_PING, 0);
286
402
  writeUInt16BE(header, 0, 2);
package/src/voip/types.js CHANGED
@@ -46,7 +46,7 @@ export const DEFAULT_VIDEO_CONFIG = {
46
46
  export const DEFAULT_AUDIO_CONFIG = {
47
47
  sampleRate: 16000,
48
48
  captureChunkSize: 960,
49
- playbackOutputSize: 256,
49
+ playbackOutputSize: 960,
50
50
  maxBufferSize: 1600,
51
51
  intervalMs: 60
52
52
  };
@@ -248,7 +248,9 @@ export class VoipClient {
248
248
  ['call_video_state', 'peer_video', (c, change) => [change]],
249
249
  ['call_inbound_audio', 'inbound_audio', (c, pcm) => [pcm]],
250
250
  ['call_inbound_video', 'inbound_video', (c, frame) => [frame]],
251
- ['call_outbound_audio_finished', 'audio_finished', () => []]
251
+ ['call_outbound_audio_finished', 'audio_finished', () => []],
252
+ ['call_reconnecting', 'reconnecting', (c, detail) => [detail]],
253
+ ['call_reconnected', 'reconnected', () => []]
252
254
  ].map(([source, target, shape]) => {
253
255
  const listener = (info, ...rest) => {
254
256
  if (info?.callId === callId)