@hiroosy/baileys-lite 0.0.4 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +716 -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,66 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
const noop = () => { };
|
|
3
|
+
export function createNoopLogger(level = 'trace') {
|
|
4
|
+
const logger = {
|
|
5
|
+
level,
|
|
6
|
+
trace: noop,
|
|
7
|
+
debug: noop,
|
|
8
|
+
media: noop,
|
|
9
|
+
info: noop,
|
|
10
|
+
warn: noop,
|
|
11
|
+
error: noop,
|
|
12
|
+
child: (_bindings, options) => (options?.level ? createNoopLogger(options.level) : logger)
|
|
13
|
+
};
|
|
14
|
+
return logger;
|
|
15
|
+
}
|
|
16
|
+
const LEVELS = { trace: 0, debug: 1, media: 2, info: 3, warn: 4, error: 5, silent: 6 };
|
|
17
|
+
export function createConsoleLogger(level = 'info', bindings = {}) {
|
|
18
|
+
const threshold = LEVELS[level] ?? LEVELS.info;
|
|
19
|
+
const prefix = Object.entries(bindings)
|
|
20
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
21
|
+
.join(' ');
|
|
22
|
+
const line = (lvl, msg, meta) => {
|
|
23
|
+
if (LEVELS[lvl] < threshold)
|
|
24
|
+
return;
|
|
25
|
+
const extra = meta ? ` ${safeJson(meta)}` : '';
|
|
26
|
+
const tag = prefix ? `[voip ${prefix}]` : '[voip]';
|
|
27
|
+
const out = lvl === 'error' || lvl === 'warn' ? console.error : console.log;
|
|
28
|
+
out(`${tag} ${msg}${extra}`);
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
level,
|
|
32
|
+
trace: (msg, meta) => line('trace', msg, meta),
|
|
33
|
+
debug: (msg, meta) => line('debug', msg, meta),
|
|
34
|
+
media: (msg, meta) => line('media', msg, meta),
|
|
35
|
+
info: (msg, meta) => line('info', msg, meta),
|
|
36
|
+
warn: (msg, meta) => line('warn', msg, meta),
|
|
37
|
+
error: (msg, meta) => line('error', msg, meta),
|
|
38
|
+
child: (childBindings = {}, options) => createConsoleLogger(options?.level ?? level, { ...bindings, ...childBindings })
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function safeJson(meta) {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.stringify(meta, (_k, v) => (v instanceof Uint8Array ? `<${v.length} bytes>` : v));
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return String(meta);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const RANDOM_PAD_MAX_16_MASK = 0x0f;
|
|
50
|
+
export function writeRandomPadMax16(message) {
|
|
51
|
+
const padLength = (randomBytes(1)[0] & RANDOM_PAD_MAX_16_MASK) + 1;
|
|
52
|
+
const out = new Uint8Array(message.length + padLength);
|
|
53
|
+
out.set(message, 0);
|
|
54
|
+
out.fill(padLength, message.length);
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
export function unpadPkcs7(bytes) {
|
|
58
|
+
if (bytes.length === 0) {
|
|
59
|
+
throw new Error('unpadPkcs7 given empty bytes');
|
|
60
|
+
}
|
|
61
|
+
const padLength = bytes[bytes.length - 1];
|
|
62
|
+
if (padLength > bytes.length) {
|
|
63
|
+
throw new Error(`unpadPkcs7 given ${bytes.length} bytes, but pad is ${padLength}`);
|
|
64
|
+
}
|
|
65
|
+
return bytes.subarray(0, bytes.length - padLength);
|
|
66
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { hkdfSync } from 'node:crypto';
|
|
2
|
+
import { toBytesView } from './util.js';
|
|
3
|
+
const EMPTY_BYTES = Object.freeze(new Uint8Array(0));
|
|
4
|
+
export function hkdf(ikm, salt, info, outLength) {
|
|
5
|
+
return toBytesView(hkdfSync('sha256', ikm, salt ?? EMPTY_BYTES, info, outLength));
|
|
6
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
export const WA_DEFAULTS = Object.freeze({
|
|
2
|
+
HOST_DOMAIN: 's.whatsapp.net',
|
|
3
|
+
GROUP_SERVER: 'g.us',
|
|
4
|
+
BROADCAST_SERVER: 'broadcast',
|
|
5
|
+
LID_SERVER: 'lid',
|
|
6
|
+
HOSTED_SERVER: 'hosted',
|
|
7
|
+
HOSTED_LID_SERVER: 'hosted.lid',
|
|
8
|
+
HOSTED_DEVICE_ID: 99,
|
|
9
|
+
MSGR_SERVER: 'msgr',
|
|
10
|
+
INTEROP_SERVER: 'interop',
|
|
11
|
+
NEWSLETTER_SERVER: 'newsletter',
|
|
12
|
+
BOT_SERVER: 'bot',
|
|
13
|
+
STATUS_BROADCAST_JID: 'status@broadcast'
|
|
14
|
+
});
|
|
15
|
+
export const WA_MESSAGE_TAGS = Object.freeze({
|
|
16
|
+
MESSAGE: 'message',
|
|
17
|
+
ENC: 'enc',
|
|
18
|
+
RECEIPT: 'receipt',
|
|
19
|
+
ACK: 'ack',
|
|
20
|
+
ERROR: 'error'
|
|
21
|
+
});
|
|
22
|
+
const KNOWN_SERVERS = [
|
|
23
|
+
WA_DEFAULTS.LID_SERVER,
|
|
24
|
+
WA_DEFAULTS.HOST_DOMAIN,
|
|
25
|
+
WA_DEFAULTS.GROUP_SERVER,
|
|
26
|
+
WA_DEFAULTS.BROADCAST_SERVER,
|
|
27
|
+
WA_DEFAULTS.NEWSLETTER_SERVER,
|
|
28
|
+
WA_DEFAULTS.HOSTED_SERVER,
|
|
29
|
+
WA_DEFAULTS.HOSTED_LID_SERVER,
|
|
30
|
+
WA_DEFAULTS.BOT_SERVER,
|
|
31
|
+
WA_DEFAULTS.MSGR_SERVER,
|
|
32
|
+
WA_DEFAULTS.INTEROP_SERVER
|
|
33
|
+
];
|
|
34
|
+
function internServerAt(jid, from) {
|
|
35
|
+
const length = jid.length - from;
|
|
36
|
+
for (let index = 0; index < KNOWN_SERVERS.length; index += 1) {
|
|
37
|
+
const server = KNOWN_SERVERS[index];
|
|
38
|
+
if (server.length === length && jid.startsWith(server, from))
|
|
39
|
+
return server;
|
|
40
|
+
}
|
|
41
|
+
return jid.slice(from);
|
|
42
|
+
}
|
|
43
|
+
function findAtIndex(jid) {
|
|
44
|
+
const atIndex = jid.indexOf('@');
|
|
45
|
+
if (atIndex < 1 || atIndex >= jid.length - 1)
|
|
46
|
+
throw new Error(`invalid jid: ${jid}`);
|
|
47
|
+
return atIndex;
|
|
48
|
+
}
|
|
49
|
+
function isJidType(jid, type) {
|
|
50
|
+
const atIndex = jid.length - type.length - 1;
|
|
51
|
+
if (atIndex < 1 || jid.charCodeAt(atIndex) !== 64 || !jid.endsWith(type))
|
|
52
|
+
return false;
|
|
53
|
+
for (let i = 0; i < atIndex; i += 1) {
|
|
54
|
+
if (jid.charCodeAt(i) === 64)
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
export function isLidJid(jid) {
|
|
60
|
+
return isJidType(jid, WA_DEFAULTS.LID_SERVER);
|
|
61
|
+
}
|
|
62
|
+
export function isUserJid(jid) {
|
|
63
|
+
return isJidType(jid, WA_DEFAULTS.HOST_DOMAIN);
|
|
64
|
+
}
|
|
65
|
+
export function isGroupJid(jid) {
|
|
66
|
+
return isJidType(jid, WA_DEFAULTS.GROUP_SERVER);
|
|
67
|
+
}
|
|
68
|
+
export function parseSignalAddressFromJid(jid) {
|
|
69
|
+
const atIndex = findAtIndex(jid);
|
|
70
|
+
const colonIndex = jid.indexOf(':', 0);
|
|
71
|
+
const server = internServerAt(jid, atIndex + 1);
|
|
72
|
+
if (colonIndex === -1 || colonIndex > atIndex) {
|
|
73
|
+
return { user: jid.slice(0, atIndex), server, device: 0 };
|
|
74
|
+
}
|
|
75
|
+
if (colonIndex >= atIndex - 1)
|
|
76
|
+
throw new Error(`invalid jid device: ${jid}`);
|
|
77
|
+
let device = 0;
|
|
78
|
+
for (let i = colonIndex + 1; i < atIndex; i += 1) {
|
|
79
|
+
const digit = jid.charCodeAt(i) - 48;
|
|
80
|
+
if (digit < 0 || digit > 9)
|
|
81
|
+
throw new Error(`invalid jid device: ${jid}`);
|
|
82
|
+
device = device * 10 + digit;
|
|
83
|
+
if (device > Number.MAX_SAFE_INTEGER)
|
|
84
|
+
throw new Error(`invalid jid device: ${jid}`);
|
|
85
|
+
}
|
|
86
|
+
return { user: jid.slice(0, colonIndex), server, device };
|
|
87
|
+
}
|
|
88
|
+
function isHostedServerAt(jid, from) {
|
|
89
|
+
const length = jid.length - from;
|
|
90
|
+
if (length === WA_DEFAULTS.HOSTED_SERVER.length) {
|
|
91
|
+
return jid.startsWith(WA_DEFAULTS.HOSTED_SERVER, from);
|
|
92
|
+
}
|
|
93
|
+
if (length === WA_DEFAULTS.HOSTED_LID_SERVER.length) {
|
|
94
|
+
return jid.startsWith(WA_DEFAULTS.HOSTED_LID_SERVER, from);
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
function canonicalizeSignalServer(server, hostDomain = WA_DEFAULTS.HOST_DOMAIN) {
|
|
99
|
+
if (server === WA_DEFAULTS.HOSTED_SERVER)
|
|
100
|
+
return hostDomain;
|
|
101
|
+
if (server === WA_DEFAULTS.HOSTED_LID_SERVER)
|
|
102
|
+
return WA_DEFAULTS.LID_SERVER;
|
|
103
|
+
return server;
|
|
104
|
+
}
|
|
105
|
+
export function toUserJid(jid, options = {}) {
|
|
106
|
+
const canonicalize = options.canonicalizeSignalServer === true;
|
|
107
|
+
const atIndex = jid.indexOf('@');
|
|
108
|
+
if (atIndex >= 1 && atIndex < jid.length - 1) {
|
|
109
|
+
const colonIndex = jid.indexOf(':', 0);
|
|
110
|
+
if ((colonIndex === -1 || colonIndex > atIndex) &&
|
|
111
|
+
(!canonicalize || !isHostedServerAt(jid, atIndex + 1))) {
|
|
112
|
+
return jid;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const address = parseSignalAddressFromJid(jid);
|
|
116
|
+
const baseServer = address.server ?? WA_DEFAULTS.HOST_DOMAIN;
|
|
117
|
+
const server = canonicalize
|
|
118
|
+
? canonicalizeSignalServer(baseServer, options.hostDomain ?? WA_DEFAULTS.HOST_DOMAIN)
|
|
119
|
+
: baseServer;
|
|
120
|
+
if (server === baseServer && jid.length === address.user.length + 1 + server.length) {
|
|
121
|
+
return jid;
|
|
122
|
+
}
|
|
123
|
+
return `${address.user}@${server}`;
|
|
124
|
+
}
|
|
125
|
+
export function normalizeDeviceJid(jid) {
|
|
126
|
+
const address = parseSignalAddressFromJid(jid);
|
|
127
|
+
if (address.device === 0)
|
|
128
|
+
return `${address.user}@${address.server}`;
|
|
129
|
+
return `${address.user}:${address.device}@${address.server}`;
|
|
130
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { WA_MESSAGE_TAGS } from './protocol.js';
|
|
2
|
+
import { TEXT_DECODER } from './util.js';
|
|
3
|
+
const EMPTY_NODE_CHILDREN = Object.freeze([]);
|
|
4
|
+
export function getNodeChildren(node) {
|
|
5
|
+
return Array.isArray(node.content) ? node.content : EMPTY_NODE_CHILDREN;
|
|
6
|
+
}
|
|
7
|
+
export function getNodeTextContent(node) {
|
|
8
|
+
if (!node)
|
|
9
|
+
return undefined;
|
|
10
|
+
const content = node.content;
|
|
11
|
+
if (content instanceof Uint8Array)
|
|
12
|
+
return TEXT_DECODER.decode(content);
|
|
13
|
+
if (typeof content === 'string')
|
|
14
|
+
return content;
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
export function findNodeChild(node, tag) {
|
|
18
|
+
const content = node.content;
|
|
19
|
+
if (!Array.isArray(content))
|
|
20
|
+
return undefined;
|
|
21
|
+
for (let i = 0; i < content.length; i++) {
|
|
22
|
+
if (content[i].tag === tag)
|
|
23
|
+
return content[i];
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
export function getFirstNodeChild(node) {
|
|
28
|
+
return getNodeChildren(node)[0];
|
|
29
|
+
}
|
|
30
|
+
export function getNodeChildrenByTag(node, tag) {
|
|
31
|
+
const content = node.content;
|
|
32
|
+
if (!Array.isArray(content))
|
|
33
|
+
return EMPTY_NODE_CHILDREN;
|
|
34
|
+
let tagged = null;
|
|
35
|
+
for (let i = 0; i < content.length; i++) {
|
|
36
|
+
if (content[i].tag !== tag)
|
|
37
|
+
continue;
|
|
38
|
+
if (!tagged)
|
|
39
|
+
tagged = [];
|
|
40
|
+
tagged.push(content[i]);
|
|
41
|
+
}
|
|
42
|
+
return tagged ?? EMPTY_NODE_CHILDREN;
|
|
43
|
+
}
|
|
44
|
+
export function hasNodeChild(node, tag) {
|
|
45
|
+
return findNodeChild(node, tag) !== undefined;
|
|
46
|
+
}
|
|
47
|
+
export function buildAckNode(input) {
|
|
48
|
+
if (input.kind !== 'custom') {
|
|
49
|
+
throw new Error(`buildAckNode: unsupported kind "${input.kind}"`);
|
|
50
|
+
}
|
|
51
|
+
const attrs = {
|
|
52
|
+
class: input.ackClass,
|
|
53
|
+
to: input.to
|
|
54
|
+
};
|
|
55
|
+
if (input.id)
|
|
56
|
+
attrs.id = input.id;
|
|
57
|
+
if (input.type)
|
|
58
|
+
attrs.type = input.type;
|
|
59
|
+
if (input.participant)
|
|
60
|
+
attrs.participant = input.participant;
|
|
61
|
+
if (input.recipient)
|
|
62
|
+
attrs.recipient = input.recipient;
|
|
63
|
+
if (input.from)
|
|
64
|
+
attrs.from = input.from;
|
|
65
|
+
if (input.error !== undefined)
|
|
66
|
+
attrs.error = String(input.error);
|
|
67
|
+
return {
|
|
68
|
+
tag: WA_MESSAGE_TAGS.ACK,
|
|
69
|
+
attrs,
|
|
70
|
+
content: input.content
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export function buildReceiptNode(input) {
|
|
74
|
+
if (input.kind !== 'custom') {
|
|
75
|
+
throw new Error(`buildReceiptNode: unsupported kind "${input.kind}"`);
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
tag: WA_MESSAGE_TAGS.RECEIPT,
|
|
79
|
+
attrs: { ...input.attrs },
|
|
80
|
+
content: input.content
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
2
|
+
export const TEXT_ENCODER = new TextEncoder();
|
|
3
|
+
export const TEXT_DECODER = new TextDecoder();
|
|
4
|
+
export const EMPTY_BYTES = Object.freeze(new Uint8Array(0));
|
|
5
|
+
export function concatBytes(parts) {
|
|
6
|
+
let total = 0;
|
|
7
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
8
|
+
total += parts[i].length;
|
|
9
|
+
}
|
|
10
|
+
const out = new Uint8Array(total);
|
|
11
|
+
let offset = 0;
|
|
12
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
13
|
+
out.set(parts[i], offset);
|
|
14
|
+
offset += parts[i].length;
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
const HEX_CHARS = '0123456789abcdef';
|
|
19
|
+
const HEX_TABLE = (() => {
|
|
20
|
+
const table = new Array(256);
|
|
21
|
+
for (let i = 0; i < 256; i += 1) {
|
|
22
|
+
table[i] = HEX_CHARS[i >> 4] + HEX_CHARS[i & 0x0f];
|
|
23
|
+
}
|
|
24
|
+
return table;
|
|
25
|
+
})();
|
|
26
|
+
const HEX_LOOKUP = (() => {
|
|
27
|
+
const table = new Int8Array(128).fill(-1);
|
|
28
|
+
for (let i = 0x30; i <= 0x39; i += 1)
|
|
29
|
+
table[i] = i - 0x30;
|
|
30
|
+
for (let i = 0x41; i <= 0x46; i += 1)
|
|
31
|
+
table[i] = i - 0x41 + 10;
|
|
32
|
+
for (let i = 0x61; i <= 0x66; i += 1)
|
|
33
|
+
table[i] = i - 0x61 + 10;
|
|
34
|
+
return table;
|
|
35
|
+
})();
|
|
36
|
+
const BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
37
|
+
const BASE64_LOOKUP = (() => {
|
|
38
|
+
const table = new Uint8Array(128).fill(0xff);
|
|
39
|
+
for (let i = 0; i < BASE64_CHARS.length; i += 1) {
|
|
40
|
+
table[BASE64_CHARS.charCodeAt(i)] = i;
|
|
41
|
+
}
|
|
42
|
+
table[0x3d] = 0;
|
|
43
|
+
return table;
|
|
44
|
+
})();
|
|
45
|
+
export function bytesToHex(value) {
|
|
46
|
+
let out = '';
|
|
47
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
48
|
+
out += HEX_TABLE[value[i]];
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
export function hexToBytes(value) {
|
|
53
|
+
const len = value.length;
|
|
54
|
+
if (len & 1) {
|
|
55
|
+
throw new Error('hex string must have even length');
|
|
56
|
+
}
|
|
57
|
+
const out = new Uint8Array(len >> 1);
|
|
58
|
+
for (let i = 0; i < len; i += 2) {
|
|
59
|
+
const hiCode = value.charCodeAt(i);
|
|
60
|
+
const loCode = value.charCodeAt(i + 1);
|
|
61
|
+
const hi = hiCode < 128 ? HEX_LOOKUP[hiCode] : -1;
|
|
62
|
+
const lo = loCode < 128 ? HEX_LOOKUP[loCode] : -1;
|
|
63
|
+
if ((hi | lo) < 0) {
|
|
64
|
+
throw new Error('invalid hex character');
|
|
65
|
+
}
|
|
66
|
+
out[i >> 1] = (hi << 4) | lo;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
function lookupBase64(code) {
|
|
71
|
+
if (code > 127) {
|
|
72
|
+
throw new Error('invalid base64 character');
|
|
73
|
+
}
|
|
74
|
+
const v = BASE64_LOOKUP[code];
|
|
75
|
+
if (v === 0xff) {
|
|
76
|
+
throw new Error('invalid base64 character');
|
|
77
|
+
}
|
|
78
|
+
return v;
|
|
79
|
+
}
|
|
80
|
+
function encodeBase64(value, alphabet, pad) {
|
|
81
|
+
const len = value.length;
|
|
82
|
+
if (len === 0)
|
|
83
|
+
return '';
|
|
84
|
+
const remainder = len % 3;
|
|
85
|
+
const mainLen = len - remainder;
|
|
86
|
+
const chunks = Math.ceil(len / 3);
|
|
87
|
+
const out = new Array(pad ? chunks * 4 : Math.ceil((len * 4) / 3));
|
|
88
|
+
let k = 0;
|
|
89
|
+
for (let i = 0; i < mainLen; i += 3) {
|
|
90
|
+
const a = value[i];
|
|
91
|
+
const b = value[i + 1];
|
|
92
|
+
const c = value[i + 2];
|
|
93
|
+
out[k++] = alphabet[a >> 2];
|
|
94
|
+
out[k++] = alphabet[((a & 0x03) << 4) | (b >> 4)];
|
|
95
|
+
out[k++] = alphabet[((b & 0x0f) << 2) | (c >> 6)];
|
|
96
|
+
out[k++] = alphabet[c & 0x3f];
|
|
97
|
+
}
|
|
98
|
+
if (remainder === 1) {
|
|
99
|
+
const a = value[mainLen];
|
|
100
|
+
out[k++] = alphabet[a >> 2];
|
|
101
|
+
out[k++] = alphabet[(a & 0x03) << 4];
|
|
102
|
+
if (pad) {
|
|
103
|
+
out[k++] = '=';
|
|
104
|
+
out[k++] = '=';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (remainder === 2) {
|
|
108
|
+
const a = value[mainLen];
|
|
109
|
+
const b = value[mainLen + 1];
|
|
110
|
+
out[k++] = alphabet[a >> 2];
|
|
111
|
+
out[k++] = alphabet[((a & 0x03) << 4) | (b >> 4)];
|
|
112
|
+
out[k++] = alphabet[(b & 0x0f) << 2];
|
|
113
|
+
if (pad) {
|
|
114
|
+
out[k++] = '=';
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return out.join('');
|
|
118
|
+
}
|
|
119
|
+
export function bytesToBase64(value) {
|
|
120
|
+
return encodeBase64(value, BASE64_CHARS, true);
|
|
121
|
+
}
|
|
122
|
+
export function base64ToBytes(value) {
|
|
123
|
+
const len = value.length;
|
|
124
|
+
if (len === 0)
|
|
125
|
+
return EMPTY_BYTES;
|
|
126
|
+
if ((len & 3) !== 0) {
|
|
127
|
+
throw new Error('base64 string length must be multiple of 4');
|
|
128
|
+
}
|
|
129
|
+
let padding = 0;
|
|
130
|
+
if (value.charCodeAt(len - 1) === 0x3d)
|
|
131
|
+
padding += 1;
|
|
132
|
+
if (value.charCodeAt(len - 2) === 0x3d)
|
|
133
|
+
padding += 1;
|
|
134
|
+
const outLen = ((len * 3) >> 2) - padding;
|
|
135
|
+
const out = new Uint8Array(outLen);
|
|
136
|
+
let j = 0;
|
|
137
|
+
const mainLen = len - 4;
|
|
138
|
+
let i = 0;
|
|
139
|
+
for (; i < mainLen; i += 4) {
|
|
140
|
+
const a = lookupBase64(value.charCodeAt(i));
|
|
141
|
+
const b = lookupBase64(value.charCodeAt(i + 1));
|
|
142
|
+
const c = lookupBase64(value.charCodeAt(i + 2));
|
|
143
|
+
const d = lookupBase64(value.charCodeAt(i + 3));
|
|
144
|
+
out[j++] = (a << 2) | (b >> 4);
|
|
145
|
+
out[j++] = ((b & 0x0f) << 4) | (c >> 2);
|
|
146
|
+
out[j++] = ((c & 0x03) << 6) | d;
|
|
147
|
+
}
|
|
148
|
+
const a = lookupBase64(value.charCodeAt(i));
|
|
149
|
+
const b = lookupBase64(value.charCodeAt(i + 1));
|
|
150
|
+
out[j++] = (a << 2) | (b >> 4);
|
|
151
|
+
if (j < outLen) {
|
|
152
|
+
const c = lookupBase64(value.charCodeAt(i + 2));
|
|
153
|
+
out[j++] = ((b & 0x0f) << 4) | (c >> 2);
|
|
154
|
+
if (j < outLen) {
|
|
155
|
+
const d = lookupBase64(value.charCodeAt(i + 3));
|
|
156
|
+
out[j++] = ((c & 0x03) << 6) | d;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
export function toBytesView(value) {
|
|
162
|
+
if (value instanceof Uint8Array) {
|
|
163
|
+
return value.constructor === Uint8Array
|
|
164
|
+
? value
|
|
165
|
+
: new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
166
|
+
}
|
|
167
|
+
if (value instanceof ArrayBuffer) {
|
|
168
|
+
return new Uint8Array(value);
|
|
169
|
+
}
|
|
170
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
171
|
+
}
|
|
172
|
+
export function uint8TimingSafeEqual(left, right) {
|
|
173
|
+
if (left.byteLength !== right.byteLength) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
return timingSafeEqual(left, right);
|
|
177
|
+
}
|
|
178
|
+
export function toError(value) {
|
|
179
|
+
if (value instanceof Error)
|
|
180
|
+
return value;
|
|
181
|
+
if (typeof value === 'string')
|
|
182
|
+
return new Error(value);
|
|
183
|
+
if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') {
|
|
184
|
+
return new Error(String(value));
|
|
185
|
+
}
|
|
186
|
+
if (value && typeof value === 'object') {
|
|
187
|
+
const message = value.message;
|
|
188
|
+
if (typeof message === 'string' && message.length > 0) {
|
|
189
|
+
return new Error(message);
|
|
190
|
+
}
|
|
191
|
+
const code = value.code;
|
|
192
|
+
if (typeof code === 'string' || typeof code === 'number') {
|
|
193
|
+
return new Error(`unknown error (${code})`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return new Error('unknown error');
|
|
197
|
+
}
|
|
198
|
+
function resolveOptionalPositive(value, name) {
|
|
199
|
+
if (value === undefined)
|
|
200
|
+
return undefined;
|
|
201
|
+
if (Number.isSafeInteger(value) && value > 0)
|
|
202
|
+
return value;
|
|
203
|
+
throw new Error(`${name} must be a positive safe integer`);
|
|
204
|
+
}
|
|
205
|
+
export function resolvePositive(value, fallback, name) {
|
|
206
|
+
return resolveOptionalPositive(value, name) ?? fallback;
|
|
207
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createNoopLogger } from '../shim/core.js';
|
|
2
|
+
import { normalizeDeviceJid } from '../shim/protocol.js';
|
|
3
|
+
import { buildAckNode, getFirstNodeChild } from '../shim/transport.js';
|
|
4
|
+
import { toError } from '../shim/util.js';
|
|
5
|
+
const RECEIPT_CALL_TAGS = new Set([
|
|
6
|
+
'offer',
|
|
7
|
+
'accept',
|
|
8
|
+
'preaccept',
|
|
9
|
+
'terminate',
|
|
10
|
+
'transport',
|
|
11
|
+
'relaylatency',
|
|
12
|
+
'mute_v2'
|
|
13
|
+
]);
|
|
14
|
+
export async function routeCallStanza(manager, deps, node, logger) {
|
|
15
|
+
const log = logger ?? createNoopLogger();
|
|
16
|
+
const inner = getFirstNodeChild(node);
|
|
17
|
+
if (!inner)
|
|
18
|
+
return null;
|
|
19
|
+
const tag = inner.tag;
|
|
20
|
+
const peerJid = node.attrs.from;
|
|
21
|
+
await deps.lowLevelCoordinator.sendNode(buildAckNode({
|
|
22
|
+
kind: 'custom',
|
|
23
|
+
ackClass: 'call',
|
|
24
|
+
to: peerJid,
|
|
25
|
+
id: node.attrs.id,
|
|
26
|
+
type: tag
|
|
27
|
+
}));
|
|
28
|
+
let normalizedPeerJid;
|
|
29
|
+
try {
|
|
30
|
+
normalizedPeerJid = normalizeDeviceJid(peerJid);
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
log.warn('failed to normalize call peer jid', {
|
|
34
|
+
from: peerJid,
|
|
35
|
+
message: toError(err).message
|
|
36
|
+
});
|
|
37
|
+
return tag;
|
|
38
|
+
}
|
|
39
|
+
switch (tag) {
|
|
40
|
+
case 'offer':
|
|
41
|
+
await manager.handleCallOffer(node, normalizedPeerJid);
|
|
42
|
+
break;
|
|
43
|
+
case 'preaccept':
|
|
44
|
+
await manager.handleCallPreaccept(node, normalizedPeerJid);
|
|
45
|
+
break;
|
|
46
|
+
case 'accept':
|
|
47
|
+
await manager.handleCallAccept(node, normalizedPeerJid);
|
|
48
|
+
break;
|
|
49
|
+
case 'transport':
|
|
50
|
+
await manager.handleCallTransport(node, normalizedPeerJid);
|
|
51
|
+
break;
|
|
52
|
+
case 'terminate':
|
|
53
|
+
await manager.handleCallTerminate(node);
|
|
54
|
+
break;
|
|
55
|
+
case 'relaylatency':
|
|
56
|
+
await manager.handleCallRelaylatency(node, normalizedPeerJid);
|
|
57
|
+
break;
|
|
58
|
+
case 'mute_v2':
|
|
59
|
+
await manager.handleCallMuteV2(node, normalizedPeerJid);
|
|
60
|
+
break;
|
|
61
|
+
case 'user_action':
|
|
62
|
+
manager.handleCallUserAction(node, normalizedPeerJid);
|
|
63
|
+
break;
|
|
64
|
+
case 'raise_hand':
|
|
65
|
+
manager.handleCallRaiseHand(node, normalizedPeerJid);
|
|
66
|
+
break;
|
|
67
|
+
case 'screen_share':
|
|
68
|
+
case 'screen':
|
|
69
|
+
manager.handleCallScreenShare(node);
|
|
70
|
+
break;
|
|
71
|
+
case 'video':
|
|
72
|
+
await manager.handleCallVideo(node, normalizedPeerJid);
|
|
73
|
+
break;
|
|
74
|
+
case 'relay_election':
|
|
75
|
+
manager.handleRelayElection(node);
|
|
76
|
+
break;
|
|
77
|
+
case 'reject':
|
|
78
|
+
break;
|
|
79
|
+
default:
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
return tag;
|
|
83
|
+
}
|
|
84
|
+
export async function routeCallAck(manager, node) {
|
|
85
|
+
await manager.handleCallAck(node);
|
|
86
|
+
}
|
|
87
|
+
export async function routeCallReceipt(deps, node) {
|
|
88
|
+
const inner = getFirstNodeChild(node);
|
|
89
|
+
if (!inner)
|
|
90
|
+
return false;
|
|
91
|
+
if (!RECEIPT_CALL_TAGS.has(inner.tag))
|
|
92
|
+
return false;
|
|
93
|
+
const peerJid = node.attrs.from;
|
|
94
|
+
await deps.lowLevelCoordinator.sendNode(buildAckNode({
|
|
95
|
+
kind: 'custom',
|
|
96
|
+
ackClass: 'receipt',
|
|
97
|
+
to: peerJid,
|
|
98
|
+
id: node.attrs.id,
|
|
99
|
+
type: node.attrs.type || 'retry'
|
|
100
|
+
}));
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { generateCallStanzaId } from './signaling.js';
|
|
2
|
+
|
|
3
|
+
function tryAsNumber(value) {
|
|
4
|
+
if (value === undefined || value === null || value === '')
|
|
5
|
+
return null;
|
|
6
|
+
const n = Number(value);
|
|
7
|
+
return Number.isFinite(n) ? n : null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const WA_SCREEN_SHARE_STATE = Object.freeze({
|
|
11
|
+
NotSupported: 0,
|
|
12
|
+
Started: 1,
|
|
13
|
+
Stopped: 2,
|
|
14
|
+
Failed: 3
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export const WA_SCREEN_SHARE_VERSION = Object.freeze({
|
|
18
|
+
Invalid: -1,
|
|
19
|
+
Legacy: 0,
|
|
20
|
+
V1: 1,
|
|
21
|
+
V2: 2,
|
|
22
|
+
V3: 3,
|
|
23
|
+
V4: 4
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const WA_SCREEN_SHARE_SEND_VERSION = WA_SCREEN_SHARE_VERSION.V2;
|
|
27
|
+
|
|
28
|
+
export function parseScreenShareNode(node) {
|
|
29
|
+
const attrs = node.attrs ?? {};
|
|
30
|
+
const state = tryAsNumber(attrs.screenshare_state);
|
|
31
|
+
const requestState = tryAsNumber(attrs['request-state'] ?? attrs.request_state);
|
|
32
|
+
if (state === null && requestState === null)
|
|
33
|
+
return null;
|
|
34
|
+
return {
|
|
35
|
+
state,
|
|
36
|
+
requestState,
|
|
37
|
+
version: tryAsNumber(attrs.version),
|
|
38
|
+
screenWidth: tryAsNumber(attrs.screen_width),
|
|
39
|
+
screenHeight: tryAsNumber(attrs.screen_height),
|
|
40
|
+
deviceOrientation: tryAsNumber(attrs.device_orientation)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function buildScreenShareStanza(peerDeviceJid, callId, callCreator, screenShareState) {
|
|
45
|
+
return {
|
|
46
|
+
tag: 'call',
|
|
47
|
+
attrs: { to: peerDeviceJid, id: generateCallStanzaId() },
|
|
48
|
+
content: [
|
|
49
|
+
{
|
|
50
|
+
tag: 'screen_share',
|
|
51
|
+
attrs: {
|
|
52
|
+
'call-id': callId,
|
|
53
|
+
'call-creator': callCreator,
|
|
54
|
+
screenshare_state: String(screenShareState),
|
|
55
|
+
version: String(WA_SCREEN_SHARE_SEND_VERSION)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
};
|
|
60
|
+
}
|