@hiroosy/baileys-lite 0.0.3 → 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 +660 -12
- 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,226 @@
|
|
|
1
|
+
import { crc32c } from './crc32c.js';
|
|
2
|
+
|
|
3
|
+
export const ChunkType = Object.freeze({
|
|
4
|
+
DATA: 0,
|
|
5
|
+
INIT: 1,
|
|
6
|
+
INIT_ACK: 2,
|
|
7
|
+
SACK: 3,
|
|
8
|
+
HEARTBEAT: 4,
|
|
9
|
+
HEARTBEAT_ACK: 5,
|
|
10
|
+
ABORT: 6,
|
|
11
|
+
SHUTDOWN: 7,
|
|
12
|
+
SHUTDOWN_ACK: 8,
|
|
13
|
+
ERROR: 9,
|
|
14
|
+
COOKIE_ECHO: 10,
|
|
15
|
+
COOKIE_ACK: 11,
|
|
16
|
+
SHUTDOWN_COMPLETE: 14,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const COMMON_HEADER_LEN = 12;
|
|
20
|
+
|
|
21
|
+
function pad4(len) { return (4 - (len % 4)) % 4; }
|
|
22
|
+
|
|
23
|
+
export function buildPacket({ sourcePort, destinationPort, verificationTag, chunks }) {
|
|
24
|
+
const header = Buffer.alloc(COMMON_HEADER_LEN);
|
|
25
|
+
header.writeUInt16BE(sourcePort, 0);
|
|
26
|
+
header.writeUInt16BE(destinationPort, 2);
|
|
27
|
+
header.writeUInt32BE(verificationTag >>> 0, 4);
|
|
28
|
+
|
|
29
|
+
const body = Buffer.concat(chunks);
|
|
30
|
+
const packetWithZeroChecksum = Buffer.concat([header, body]);
|
|
31
|
+
const checksum = crc32c(packetWithZeroChecksum);
|
|
32
|
+
|
|
33
|
+
packetWithZeroChecksum.writeUInt32LE(checksum, 8);
|
|
34
|
+
return packetWithZeroChecksum;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function parsePacketHeader(datagram) {
|
|
38
|
+
if (datagram.length < COMMON_HEADER_LEN) throw new Error('SCTP packet shorter than common header');
|
|
39
|
+
const sourcePort = datagram.readUInt16BE(0);
|
|
40
|
+
const destinationPort = datagram.readUInt16BE(2);
|
|
41
|
+
const verificationTag = datagram.readUInt32BE(4) >>> 0;
|
|
42
|
+
|
|
43
|
+
const receivedChecksum = datagram.readUInt32LE(8) >>> 0;
|
|
44
|
+
const forVerification = Buffer.from(datagram);
|
|
45
|
+
forVerification.writeUInt32BE(0, 8);
|
|
46
|
+
const computedChecksum = crc32c(forVerification);
|
|
47
|
+
if (computedChecksum !== receivedChecksum) {
|
|
48
|
+
throw new Error(`SCTP checksum mismatch: got 0x${receivedChecksum.toString(16)}, computed 0x${computedChecksum.toString(16)}`);
|
|
49
|
+
}
|
|
50
|
+
return { sourcePort, destinationPort, verificationTag, chunksStart: COMMON_HEADER_LEN };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function splitChunks(datagram, chunksStart) {
|
|
54
|
+
const chunks = [];
|
|
55
|
+
let offset = chunksStart;
|
|
56
|
+
while (offset + 4 <= datagram.length) {
|
|
57
|
+
const type = datagram.readUInt8(offset);
|
|
58
|
+
const flags = datagram.readUInt8(offset + 1);
|
|
59
|
+
const length = datagram.readUInt16BE(offset + 2);
|
|
60
|
+
if (length < 4 || offset + length > datagram.length) break;
|
|
61
|
+
chunks.push({ type, flags, value: datagram.subarray(offset + 4, offset + length) });
|
|
62
|
+
offset += length + pad4(length);
|
|
63
|
+
}
|
|
64
|
+
return chunks;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function encodeChunk(type, flags, value) {
|
|
68
|
+
const length = 4 + value.length;
|
|
69
|
+
const header = Buffer.alloc(4);
|
|
70
|
+
header.writeUInt8(type, 0);
|
|
71
|
+
header.writeUInt8(flags, 1);
|
|
72
|
+
header.writeUInt16BE(length, 2);
|
|
73
|
+
const padding = Buffer.alloc(pad4(length));
|
|
74
|
+
return Buffer.concat([header, value, padding]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const SUPPORTED_EXTENSIONS_PARAM_TYPE = 0x8008;
|
|
78
|
+
const CHUNK_TYPE_RECONFIG = 130;
|
|
79
|
+
const CHUNK_TYPE_FORWARD_TSN = 192;
|
|
80
|
+
|
|
81
|
+
function buildSupportedExtensionsParam() {
|
|
82
|
+
const chunkTypes = Buffer.from([CHUNK_TYPE_RECONFIG, CHUNK_TYPE_FORWARD_TSN]);
|
|
83
|
+
const header = Buffer.alloc(4);
|
|
84
|
+
header.writeUInt16BE(SUPPORTED_EXTENSIONS_PARAM_TYPE, 0);
|
|
85
|
+
header.writeUInt16BE(4 + chunkTypes.length, 2);
|
|
86
|
+
return Buffer.concat([header, chunkTypes, Buffer.alloc(pad4(4 + chunkTypes.length))]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function buildInit({ initiateTag, advertisedReceiverWindow, outboundStreams, inboundStreams, initialTsn }) {
|
|
90
|
+
const fixed = Buffer.alloc(16);
|
|
91
|
+
fixed.writeUInt32BE(initiateTag >>> 0, 0);
|
|
92
|
+
fixed.writeUInt32BE(advertisedReceiverWindow >>> 0, 4);
|
|
93
|
+
fixed.writeUInt16BE(outboundStreams, 8);
|
|
94
|
+
fixed.writeUInt16BE(inboundStreams, 10);
|
|
95
|
+
fixed.writeUInt32BE(initialTsn >>> 0, 12);
|
|
96
|
+
const value = Buffer.concat([fixed, buildSupportedExtensionsParam()]);
|
|
97
|
+
return encodeChunk(ChunkType.INIT, 0, value);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function parseInitOrInitAck(chunkValue) {
|
|
101
|
+
const initiateTag = chunkValue.readUInt32BE(0) >>> 0;
|
|
102
|
+
const advertisedReceiverWindow = chunkValue.readUInt32BE(4) >>> 0;
|
|
103
|
+
const outboundStreams = chunkValue.readUInt16BE(8);
|
|
104
|
+
const inboundStreams = chunkValue.readUInt16BE(10);
|
|
105
|
+
const initialTsn = chunkValue.readUInt32BE(12) >>> 0;
|
|
106
|
+
|
|
107
|
+
let offset = 16;
|
|
108
|
+
let stateCookie = null;
|
|
109
|
+
while (offset + 4 <= chunkValue.length) {
|
|
110
|
+
const paramType = chunkValue.readUInt16BE(offset);
|
|
111
|
+
const paramLength = chunkValue.readUInt16BE(offset + 2);
|
|
112
|
+
if (paramLength < 4 || offset + paramLength > chunkValue.length) break;
|
|
113
|
+
const paramValue = chunkValue.subarray(offset + 4, offset + paramLength);
|
|
114
|
+
if (paramType === 7) stateCookie = paramValue;
|
|
115
|
+
offset += paramLength + pad4(paramLength);
|
|
116
|
+
}
|
|
117
|
+
return { initiateTag, advertisedReceiverWindow, outboundStreams, inboundStreams, initialTsn, stateCookie };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function buildInitAck({ initiateTag, advertisedReceiverWindow, outboundStreams, inboundStreams, initialTsn, stateCookie }) {
|
|
121
|
+
const fixed = Buffer.alloc(16);
|
|
122
|
+
fixed.writeUInt32BE(initiateTag >>> 0, 0);
|
|
123
|
+
fixed.writeUInt32BE(advertisedReceiverWindow >>> 0, 4);
|
|
124
|
+
fixed.writeUInt16BE(outboundStreams, 8);
|
|
125
|
+
fixed.writeUInt16BE(inboundStreams, 10);
|
|
126
|
+
fixed.writeUInt32BE(initialTsn >>> 0, 12);
|
|
127
|
+
const cookieParamHeader = Buffer.alloc(4);
|
|
128
|
+
cookieParamHeader.writeUInt16BE(7, 0);
|
|
129
|
+
cookieParamHeader.writeUInt16BE(4 + stateCookie.length, 2);
|
|
130
|
+
const cookieParam = Buffer.concat([cookieParamHeader, stateCookie, Buffer.alloc(pad4(4 + stateCookie.length))]);
|
|
131
|
+
return encodeChunk(ChunkType.INIT_ACK, 0, Buffer.concat([fixed, cookieParam]));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function buildCookieEcho(stateCookie) {
|
|
135
|
+
return encodeChunk(ChunkType.COOKIE_ECHO, 0, stateCookie);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function buildCookieAck() {
|
|
139
|
+
return encodeChunk(ChunkType.COOKIE_ACK, 0, Buffer.alloc(0));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const DATA_HEADER_LEN = 12;
|
|
143
|
+
|
|
144
|
+
export function buildData({ tsn, streamId, streamSeq, ppid, payload, unordered = false, beginning = true, ending = true }) {
|
|
145
|
+
let flags = 0;
|
|
146
|
+
if (unordered) flags |= 0x04;
|
|
147
|
+
if (beginning) flags |= 0x02;
|
|
148
|
+
if (ending) flags |= 0x01;
|
|
149
|
+
const value = Buffer.alloc(DATA_HEADER_LEN + payload.length);
|
|
150
|
+
value.writeUInt32BE(tsn >>> 0, 0);
|
|
151
|
+
value.writeUInt16BE(streamId, 4);
|
|
152
|
+
value.writeUInt16BE(streamSeq, 6);
|
|
153
|
+
value.writeUInt32BE(ppid >>> 0, 8);
|
|
154
|
+
payload.copy(value, DATA_HEADER_LEN);
|
|
155
|
+
return encodeChunk(ChunkType.DATA, flags, value);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function parseData(chunkFlags, chunkValue) {
|
|
159
|
+
return {
|
|
160
|
+
unordered: (chunkFlags & 0x04) !== 0,
|
|
161
|
+
beginning: (chunkFlags & 0x02) !== 0,
|
|
162
|
+
ending: (chunkFlags & 0x01) !== 0,
|
|
163
|
+
tsn: chunkValue.readUInt32BE(0) >>> 0,
|
|
164
|
+
streamId: chunkValue.readUInt16BE(4),
|
|
165
|
+
streamSeq: chunkValue.readUInt16BE(6),
|
|
166
|
+
ppid: chunkValue.readUInt32BE(8) >>> 0,
|
|
167
|
+
payload: chunkValue.subarray(DATA_HEADER_LEN),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function buildSack({ cumulativeTsnAck, advertisedReceiverWindow, gapAckBlocks = [] }) {
|
|
172
|
+
const value = Buffer.alloc(12 + gapAckBlocks.length * 4);
|
|
173
|
+
value.writeUInt32BE(cumulativeTsnAck >>> 0, 0);
|
|
174
|
+
value.writeUInt32BE(advertisedReceiverWindow >>> 0, 4);
|
|
175
|
+
value.writeUInt16BE(gapAckBlocks.length, 8);
|
|
176
|
+
value.writeUInt16BE(0, 10);
|
|
177
|
+
let offset = 12;
|
|
178
|
+
for (const block of gapAckBlocks) {
|
|
179
|
+
value.writeUInt16BE(block.start & 0xffff, offset);
|
|
180
|
+
value.writeUInt16BE(block.end & 0xffff, offset + 2);
|
|
181
|
+
offset += 4;
|
|
182
|
+
}
|
|
183
|
+
return encodeChunk(ChunkType.SACK, 0, value);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function parseSack(chunkValue) {
|
|
187
|
+
const cumulativeTsnAck = chunkValue.readUInt32BE(0) >>> 0;
|
|
188
|
+
const advertisedReceiverWindow = chunkValue.readUInt32BE(4) >>> 0;
|
|
189
|
+
const gapAckBlocks = [];
|
|
190
|
+
if (chunkValue.length >= 12) {
|
|
191
|
+
const numGapBlocks = chunkValue.readUInt16BE(8);
|
|
192
|
+
let offset = 12;
|
|
193
|
+
for (let i = 0; i < numGapBlocks && offset + 4 <= chunkValue.length; i++) {
|
|
194
|
+
gapAckBlocks.push({
|
|
195
|
+
start: chunkValue.readUInt16BE(offset),
|
|
196
|
+
end: chunkValue.readUInt16BE(offset + 2)
|
|
197
|
+
});
|
|
198
|
+
offset += 4;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { cumulativeTsnAck, advertisedReceiverWindow, gapAckBlocks };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function buildHeartbeat(heartbeatInfo) {
|
|
205
|
+
const paramHeader = Buffer.alloc(4);
|
|
206
|
+
paramHeader.writeUInt16BE(1, 0);
|
|
207
|
+
paramHeader.writeUInt16BE(4 + heartbeatInfo.length, 2);
|
|
208
|
+
return encodeChunk(ChunkType.HEARTBEAT, 0, Buffer.concat([paramHeader, heartbeatInfo, Buffer.alloc(pad4(4 + heartbeatInfo.length))]));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function buildHeartbeatAck(heartbeatParamValue) {
|
|
212
|
+
|
|
213
|
+
const paramHeader = Buffer.alloc(4);
|
|
214
|
+
paramHeader.writeUInt16BE(1, 0);
|
|
215
|
+
paramHeader.writeUInt16BE(4 + heartbeatParamValue.length, 2);
|
|
216
|
+
return encodeChunk(ChunkType.HEARTBEAT_ACK, 0, Buffer.concat([paramHeader, heartbeatParamValue, Buffer.alloc(pad4(4 + heartbeatParamValue.length))]));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function parseHeartbeatInfo(chunkValue) {
|
|
220
|
+
const paramLength = chunkValue.readUInt16BE(2);
|
|
221
|
+
return chunkValue.subarray(4, paramLength);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function buildAbort(reasonText = '') {
|
|
225
|
+
return encodeChunk(ChunkType.ABORT, 0, Buffer.from(reasonText, 'utf8'));
|
|
226
|
+
}
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { bytesToHex } from '../shim/util.js';
|
|
2
|
+
import { concatBytes, readBigUInt64BE, readUInt16BE, readUInt32BE, TEXT_DECODER, writeUInt16BE, writeUInt32BE } from '../bytes.js';
|
|
3
|
+
import { hmacSha1, randomBytes } from '../crypto/primitives.js';
|
|
4
|
+
const STUN_MAGIC_COOKIE = 0x2112a442;
|
|
5
|
+
const STUN_FINGERPRINT_XOR = 0x5354554e;
|
|
6
|
+
const STUN_BINDING_REQUEST = 0x0001;
|
|
7
|
+
const STUN_ALLOCATE_REQUEST = 0x0003;
|
|
8
|
+
const WHATSAPP_PING = 0x0801;
|
|
9
|
+
const WHATSAPP_PONG = 0x0802;
|
|
10
|
+
const ATTR_USERNAME = 0x0006;
|
|
11
|
+
const ATTR_MESSAGE_INTEGRITY = 0x0008;
|
|
12
|
+
const ATTR_LIFETIME = 0x000d;
|
|
13
|
+
const ATTR_XOR_RELAYED_ADDRESS = 0x0016;
|
|
14
|
+
const ATTR_REQUESTED_TRANSPORT = 0x0019;
|
|
15
|
+
const ATTR_PRIORITY = 0x0024;
|
|
16
|
+
|
|
17
|
+
const ATTR_SENDER_SUBSCRIPTIONS = 0x4000;
|
|
18
|
+
const ATTR_SSRC_LIST = 0x4024;
|
|
19
|
+
const ATTR_ICE_CONTROLLED = 0x8029;
|
|
20
|
+
const ATTR_XOR_MAPPED_ADDRESS = 0x0020;
|
|
21
|
+
const ATTR_ICE_CONTROLLING = 0x802a;
|
|
22
|
+
const ATTR_FINGERPRINT = 0x8028;
|
|
23
|
+
const DEFAULT_ICE_PRIORITY = 16_777_215;
|
|
24
|
+
function generateTransactionId() {
|
|
25
|
+
return randomBytes(12);
|
|
26
|
+
}
|
|
27
|
+
function encodeAttribute(attrType, data) {
|
|
28
|
+
const header = new Uint8Array(4);
|
|
29
|
+
writeUInt16BE(header, attrType, 0);
|
|
30
|
+
writeUInt16BE(header, data.length, 2);
|
|
31
|
+
const padding = (4 - (data.length % 4)) % 4;
|
|
32
|
+
const pad = new Uint8Array(padding);
|
|
33
|
+
return concatBytes([header, data, pad]);
|
|
34
|
+
}
|
|
35
|
+
function crc32(data) {
|
|
36
|
+
let crc = 0xffffffff;
|
|
37
|
+
for (let i = 0; i < data.length; i++) {
|
|
38
|
+
crc ^= data[i];
|
|
39
|
+
for (let j = 0; j < 8; j++) {
|
|
40
|
+
if (crc & 1) {
|
|
41
|
+
crc = (crc >>> 1) ^ 0xedb88320;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
crc >>>= 1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
49
|
+
}
|
|
50
|
+
function buildStunMessage(msgType, attrs, transactionId, integrityKey, includeFingerprint = true) {
|
|
51
|
+
let attrsData = attrs;
|
|
52
|
+
if (integrityKey) {
|
|
53
|
+
const msgLenForHmac = attrsData.length + 24;
|
|
54
|
+
const hmacHeader = new Uint8Array(20);
|
|
55
|
+
writeUInt16BE(hmacHeader, msgType, 0);
|
|
56
|
+
writeUInt16BE(hmacHeader, msgLenForHmac, 2);
|
|
57
|
+
writeUInt32BE(hmacHeader, STUN_MAGIC_COOKIE, 4);
|
|
58
|
+
hmacHeader.set(transactionId, 8);
|
|
59
|
+
const hmacInput = concatBytes([hmacHeader, attrsData]);
|
|
60
|
+
const hmac = hmacSha1(integrityKey, hmacInput);
|
|
61
|
+
const miAttr = encodeAttribute(ATTR_MESSAGE_INTEGRITY, hmac);
|
|
62
|
+
attrsData = concatBytes([attrsData, miAttr]);
|
|
63
|
+
}
|
|
64
|
+
if (includeFingerprint) {
|
|
65
|
+
const msgLenForCrc = attrsData.length + 8;
|
|
66
|
+
const crcHeader = new Uint8Array(20);
|
|
67
|
+
writeUInt16BE(crcHeader, msgType, 0);
|
|
68
|
+
writeUInt16BE(crcHeader, msgLenForCrc, 2);
|
|
69
|
+
writeUInt32BE(crcHeader, STUN_MAGIC_COOKIE, 4);
|
|
70
|
+
crcHeader.set(transactionId, 8);
|
|
71
|
+
const crcInput = concatBytes([crcHeader, attrsData]);
|
|
72
|
+
const fingerprint = (crc32(crcInput) ^ STUN_FINGERPRINT_XOR) >>> 0;
|
|
73
|
+
const fpBuf = new Uint8Array(4);
|
|
74
|
+
writeUInt32BE(fpBuf, fingerprint, 0);
|
|
75
|
+
const fpAttr = encodeAttribute(ATTR_FINGERPRINT, fpBuf);
|
|
76
|
+
attrsData = concatBytes([attrsData, fpAttr]);
|
|
77
|
+
}
|
|
78
|
+
const header = new Uint8Array(20);
|
|
79
|
+
writeUInt16BE(header, msgType, 0);
|
|
80
|
+
writeUInt16BE(header, attrsData.length, 2);
|
|
81
|
+
writeUInt32BE(header, STUN_MAGIC_COOKIE, 4);
|
|
82
|
+
header.set(transactionId, 8);
|
|
83
|
+
return concatBytes([header, attrsData]);
|
|
84
|
+
}
|
|
85
|
+
function encodeVarint(value) {
|
|
86
|
+
const bytes = [];
|
|
87
|
+
let v = value >>> 0;
|
|
88
|
+
while (v > 0x7f) {
|
|
89
|
+
bytes.push((v & 0x7f) | 0x80);
|
|
90
|
+
v >>>= 7;
|
|
91
|
+
}
|
|
92
|
+
bytes.push(v & 0x7f);
|
|
93
|
+
return new Uint8Array(bytes);
|
|
94
|
+
}
|
|
95
|
+
function encodeProtobufVarintField(fieldNumber, value) {
|
|
96
|
+
const tag = encodeVarint((fieldNumber << 3) | 0);
|
|
97
|
+
const val = encodeVarint(value);
|
|
98
|
+
return concatBytes([tag, val]);
|
|
99
|
+
}
|
|
100
|
+
function encodeProtobufLengthDelimited(fieldNumber, data) {
|
|
101
|
+
const tag = encodeVarint((fieldNumber << 3) | 2);
|
|
102
|
+
const len = encodeVarint(data.length);
|
|
103
|
+
return concatBytes([tag, len, data]);
|
|
104
|
+
}
|
|
105
|
+
export function buildSenderSubscriptions(ssrc) {
|
|
106
|
+
const inner = concatBytes([
|
|
107
|
+
encodeProtobufVarintField(3, ssrc),
|
|
108
|
+
encodeProtobufVarintField(5, 0),
|
|
109
|
+
encodeProtobufVarintField(6, 0)
|
|
110
|
+
]);
|
|
111
|
+
return encodeProtobufLengthDelimited(1, inner);
|
|
112
|
+
}
|
|
113
|
+
export function buildSSRCSubscriptionList(selfSsrcs, peerSsrcs, selfPid, peerPid) {
|
|
114
|
+
const entries = [];
|
|
115
|
+
for (const ssrc of selfSsrcs) {
|
|
116
|
+
if (ssrc === 0)
|
|
117
|
+
continue;
|
|
118
|
+
const inner = concatBytes([
|
|
119
|
+
encodeProtobufVarintField(1, selfPid),
|
|
120
|
+
encodeProtobufVarintField(2, 1),
|
|
121
|
+
encodeProtobufVarintField(3, ssrc)
|
|
122
|
+
]);
|
|
123
|
+
entries.push(encodeProtobufLengthDelimited(1, inner));
|
|
124
|
+
}
|
|
125
|
+
for (const peerSsrc of peerSsrcs) {
|
|
126
|
+
if (peerSsrc === 0)
|
|
127
|
+
continue;
|
|
128
|
+
const inner = concatBytes([
|
|
129
|
+
encodeProtobufVarintField(1, peerPid),
|
|
130
|
+
encodeProtobufVarintField(2, 1),
|
|
131
|
+
encodeProtobufVarintField(3, peerSsrc)
|
|
132
|
+
]);
|
|
133
|
+
entries.push(encodeProtobufLengthDelimited(1, inner));
|
|
134
|
+
}
|
|
135
|
+
return concatBytes(entries);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const WASM_STREAM_DESCRIPTOR_PLAN = [
|
|
139
|
+
{ participant: 0, layer: 0 },
|
|
140
|
+
{ participant: 0, layer: 1 },
|
|
141
|
+
{ participant: 0, layer: 2 },
|
|
142
|
+
{ participant: 1, layer: 0 },
|
|
143
|
+
{ participant: 1, layer: 1 },
|
|
144
|
+
{ participant: 1, layer: 2 },
|
|
145
|
+
{ participant: 2, layer: 0 },
|
|
146
|
+
{ participant: 2, layer: 1 },
|
|
147
|
+
{ participant: 2, layer: 2 }
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
export function createWasmStreamDescriptors(streamSsrcs) {
|
|
151
|
+
const entries = [];
|
|
152
|
+
for (let i = 0; i < streamSsrcs.length; i++) {
|
|
153
|
+
const ssrc = streamSsrcs[i];
|
|
154
|
+
if (ssrc === 0)
|
|
155
|
+
continue;
|
|
156
|
+
const plan = WASM_STREAM_DESCRIPTOR_PLAN[i];
|
|
157
|
+
const parts = [];
|
|
158
|
+
if (plan.participant !== 0)
|
|
159
|
+
parts.push(encodeProtobufVarintField(1, plan.participant));
|
|
160
|
+
if (plan.layer !== 0)
|
|
161
|
+
parts.push(encodeProtobufVarintField(2, plan.layer));
|
|
162
|
+
parts.push(encodeProtobufVarintField(3, ssrc));
|
|
163
|
+
entries.push(encodeProtobufLengthDelimited(1, concatBytes(parts)));
|
|
164
|
+
}
|
|
165
|
+
return concatBytes(entries);
|
|
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);
|
|
175
|
+
return data;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function buildAllocateForRelay(relayToken, streamDescriptors, hmacKey, relayIp, relayPort) {
|
|
179
|
+
const transactionId = generateTransactionId();
|
|
180
|
+
const parts = [];
|
|
181
|
+
parts.push(encodeAttribute(ATTR_SENDER_SUBSCRIPTIONS, relayToken));
|
|
182
|
+
parts.push(encodeAttribute(ATTR_SSRC_LIST, streamDescriptors));
|
|
183
|
+
parts.push(encodeAttribute(ATTR_XOR_RELAYED_ADDRESS, encodeXorRelayedAddress(relayIp, relayPort)));
|
|
184
|
+
const attrs = concatBytes(parts);
|
|
185
|
+
return buildStunMessage(STUN_ALLOCATE_REQUEST, attrs, transactionId, hmacKey, false);
|
|
186
|
+
}
|
|
187
|
+
export function buildBindingRequest(username, hmacKey, senderSubscriptions, includeIceControllingOrOptions = true) {
|
|
188
|
+
const options = typeof includeIceControllingOrOptions === 'boolean'
|
|
189
|
+
? { iceRole: includeIceControllingOrOptions ? 'controlling' : 'none' }
|
|
190
|
+
: (includeIceControllingOrOptions ?? {});
|
|
191
|
+
const iceRole = options.iceRole ?? 'controlling';
|
|
192
|
+
const includePriority = options.includePriority ?? true;
|
|
193
|
+
const includeUsername = options.includeUsername ?? true;
|
|
194
|
+
const transactionId = generateTransactionId();
|
|
195
|
+
const usernameAttr = includeUsername ? encodeAttribute(ATTR_USERNAME, username) : undefined;
|
|
196
|
+
const priorityAttr = includePriority
|
|
197
|
+
? (() => {
|
|
198
|
+
const priorityBuf = new Uint8Array(4);
|
|
199
|
+
writeUInt32BE(priorityBuf, DEFAULT_ICE_PRIORITY, 0);
|
|
200
|
+
return encodeAttribute(ATTR_PRIORITY, priorityBuf);
|
|
201
|
+
})()
|
|
202
|
+
: undefined;
|
|
203
|
+
const parts = [];
|
|
204
|
+
if (usernameAttr)
|
|
205
|
+
parts.push(usernameAttr);
|
|
206
|
+
if (priorityAttr)
|
|
207
|
+
parts.push(priorityAttr);
|
|
208
|
+
if (iceRole === 'controlling' || iceRole === 'controlled') {
|
|
209
|
+
const tieBreaker = randomBytes(8);
|
|
210
|
+
const attrType = iceRole === 'controlled' ? ATTR_ICE_CONTROLLED : ATTR_ICE_CONTROLLING;
|
|
211
|
+
parts.push(encodeAttribute(attrType, tieBreaker));
|
|
212
|
+
}
|
|
213
|
+
if (senderSubscriptions && senderSubscriptions.length > 0) {
|
|
214
|
+
parts.push(encodeAttribute(ATTR_SENDER_SUBSCRIPTIONS, senderSubscriptions));
|
|
215
|
+
}
|
|
216
|
+
const attrs = concatBytes(parts);
|
|
217
|
+
return buildStunMessage(STUN_BINDING_REQUEST, attrs, transactionId, hmacKey, true);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function encodeXorMappedAddressV4(ip, port) {
|
|
221
|
+
const octets = ip.split('.').map((s) => Number(s) & 0xff);
|
|
222
|
+
const buf = new Uint8Array(8);
|
|
223
|
+
buf[0] = 0x00;
|
|
224
|
+
buf[1] = 0x01;
|
|
225
|
+
const xPort = port ^ (STUN_MAGIC_COOKIE >>> 16);
|
|
226
|
+
writeUInt16BE(buf, xPort, 2);
|
|
227
|
+
const cookieBytes = new Uint8Array(4);
|
|
228
|
+
writeUInt32BE(cookieBytes, STUN_MAGIC_COOKIE, 0);
|
|
229
|
+
for (let i = 0; i < 4; i++) buf[4 + i] = octets[i] ^ cookieBytes[i];
|
|
230
|
+
return buf;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function buildBindingSuccessResponse(transactionId, mappedIp, mappedPort, hmacKey) {
|
|
234
|
+
const xorAddr = encodeXorMappedAddressV4(mappedIp, mappedPort);
|
|
235
|
+
const attrs = encodeAttribute(ATTR_XOR_MAPPED_ADDRESS, xorAddr);
|
|
236
|
+
const STUN_BINDING_SUCCESS_RESPONSE = 0x0101;
|
|
237
|
+
return buildStunMessage(STUN_BINDING_SUCCESS_RESPONSE, attrs, transactionId, hmacKey, true);
|
|
238
|
+
}
|
|
239
|
+
export function buildBindingRequestWithSubs(username, hmacKey, senderSubscriptions, includeIceControlling, includeFingerprint) {
|
|
240
|
+
const transactionId = generateTransactionId();
|
|
241
|
+
const parts = [];
|
|
242
|
+
if (username && username.length > 0) {
|
|
243
|
+
parts.push(encodeAttribute(ATTR_USERNAME, username));
|
|
244
|
+
}
|
|
245
|
+
const priorityBuf = new Uint8Array(4);
|
|
246
|
+
writeUInt32BE(priorityBuf, DEFAULT_ICE_PRIORITY, 0);
|
|
247
|
+
parts.push(encodeAttribute(ATTR_PRIORITY, priorityBuf));
|
|
248
|
+
if (includeIceControlling) {
|
|
249
|
+
const tieBreaker = randomBytes(8);
|
|
250
|
+
parts.push(encodeAttribute(ATTR_ICE_CONTROLLING, tieBreaker));
|
|
251
|
+
}
|
|
252
|
+
if (senderSubscriptions && senderSubscriptions.length > 0) {
|
|
253
|
+
parts.push(encodeAttribute(ATTR_SENDER_SUBSCRIPTIONS, senderSubscriptions));
|
|
254
|
+
}
|
|
255
|
+
const attrs = concatBytes(parts);
|
|
256
|
+
return buildStunMessage(STUN_BINDING_REQUEST, attrs, transactionId, hmacKey, includeFingerprint);
|
|
257
|
+
}
|
|
258
|
+
export function buildMinimalBindingWithSubs(senderSubscriptions, includeFingerprint = false) {
|
|
259
|
+
const transactionId = generateTransactionId();
|
|
260
|
+
const attrs = encodeAttribute(ATTR_SENDER_SUBSCRIPTIONS, senderSubscriptions);
|
|
261
|
+
return buildStunMessage(STUN_BINDING_REQUEST, attrs, transactionId, undefined, includeFingerprint);
|
|
262
|
+
}
|
|
263
|
+
export function buildMinimalAllocateWithSubs(senderSubscriptions, includeFingerprint = false) {
|
|
264
|
+
const transactionId = generateTransactionId();
|
|
265
|
+
const attrs = encodeAttribute(ATTR_SENDER_SUBSCRIPTIONS, senderSubscriptions);
|
|
266
|
+
return buildStunMessage(STUN_ALLOCATE_REQUEST, attrs, transactionId, undefined, includeFingerprint);
|
|
267
|
+
}
|
|
268
|
+
export function buildAllocateRequest(username, hmacKey, lifetime = 3600) {
|
|
269
|
+
const transactionId = generateTransactionId();
|
|
270
|
+
const parts = [];
|
|
271
|
+
parts.push(encodeAttribute(ATTR_REQUESTED_TRANSPORT, new Uint8Array([17, 0, 0, 0])));
|
|
272
|
+
parts.push(encodeAttribute(ATTR_USERNAME, username));
|
|
273
|
+
const lifetimeBuf = new Uint8Array(4);
|
|
274
|
+
writeUInt32BE(lifetimeBuf, lifetime, 0);
|
|
275
|
+
parts.push(encodeAttribute(ATTR_LIFETIME, lifetimeBuf));
|
|
276
|
+
const attrs = concatBytes(parts);
|
|
277
|
+
return buildStunMessage(STUN_ALLOCATE_REQUEST, attrs, transactionId, hmacKey, true);
|
|
278
|
+
}
|
|
279
|
+
export function buildRelayBindingSuccess(transactionId, hmacKey) {
|
|
280
|
+
return buildStunMessage(0x0101, new Uint8Array(0), transactionId, hmacKey, true);
|
|
281
|
+
}
|
|
282
|
+
export function buildWhatsAppPing() {
|
|
283
|
+
const transactionId = generateTransactionId();
|
|
284
|
+
const header = new Uint8Array(20);
|
|
285
|
+
writeUInt16BE(header, WHATSAPP_PING, 0);
|
|
286
|
+
writeUInt16BE(header, 0, 2);
|
|
287
|
+
writeUInt32BE(header, STUN_MAGIC_COOKIE, 4);
|
|
288
|
+
header.set(transactionId, 8);
|
|
289
|
+
return header;
|
|
290
|
+
}
|
|
291
|
+
export function isStunPacket(data) {
|
|
292
|
+
if (data.length < 2)
|
|
293
|
+
return false;
|
|
294
|
+
if ((data[0] & 0xc0) !== 0)
|
|
295
|
+
return false;
|
|
296
|
+
const type = readUInt16BE(data, 0);
|
|
297
|
+
if (type === WHATSAPP_PING || type === WHATSAPP_PONG)
|
|
298
|
+
return true;
|
|
299
|
+
return data.length >= 8 && readUInt32BE(data, 4) === STUN_MAGIC_COOKIE;
|
|
300
|
+
}
|
|
301
|
+
export function isRtpPacket(data) {
|
|
302
|
+
if (data.length < 2)
|
|
303
|
+
return false;
|
|
304
|
+
return (data[0] & 0xc0) === 0x80;
|
|
305
|
+
}
|
|
306
|
+
const STUN_ATTR_NAMES = {
|
|
307
|
+
0x0001: 'MAPPED-ADDRESS',
|
|
308
|
+
0x0006: 'USERNAME',
|
|
309
|
+
0x0008: 'MESSAGE-INTEGRITY',
|
|
310
|
+
0x0009: 'ERROR-CODE',
|
|
311
|
+
0x000a: 'UNKNOWN-ATTRIBUTES',
|
|
312
|
+
0x0014: 'REALM',
|
|
313
|
+
0x0015: 'NONCE',
|
|
314
|
+
0x0019: 'REQUESTED-TRANSPORT',
|
|
315
|
+
0x0020: 'XOR-MAPPED-ADDRESS',
|
|
316
|
+
0x0024: 'PRIORITY',
|
|
317
|
+
0x0025: 'USE-CANDIDATE',
|
|
318
|
+
0x4000: 'SENDER-SUBSCRIPTIONS',
|
|
319
|
+
0x4001: 'RECEIVER-SUBSCRIPTION',
|
|
320
|
+
0x4002: 'SUBSCRIPTION-ACK',
|
|
321
|
+
0x8022: 'SOFTWARE',
|
|
322
|
+
0x8028: 'FINGERPRINT',
|
|
323
|
+
0x8029: 'ICE-CONTROLLED',
|
|
324
|
+
0x802a: 'ICE-CONTROLLING',
|
|
325
|
+
0x4033: 'STABLE-ROUTING-CONN-ID'
|
|
326
|
+
};
|
|
327
|
+
export function parseStunResponse(data) {
|
|
328
|
+
if (data.length < 20)
|
|
329
|
+
return null;
|
|
330
|
+
const cookie = readUInt32BE(data, 4);
|
|
331
|
+
if (cookie !== STUN_MAGIC_COOKIE) {
|
|
332
|
+
const msgType = readUInt16BE(data, 0);
|
|
333
|
+
if (msgType === 0x0801 || msgType === 0x0802) {
|
|
334
|
+
return {
|
|
335
|
+
rawType: msgType,
|
|
336
|
+
method: msgType === 0x0801 ? 'wa-ping' : 'wa-pong',
|
|
337
|
+
stunClass: 'indication',
|
|
338
|
+
isSuccess: false,
|
|
339
|
+
isError: false,
|
|
340
|
+
transactionId: bytesToHex(data.subarray(8, 20)),
|
|
341
|
+
length: data.length,
|
|
342
|
+
attributes: []
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
const rawType = readUInt16BE(data, 0);
|
|
348
|
+
const msgLength = readUInt16BE(data, 2);
|
|
349
|
+
const transactionId = bytesToHex(data.subarray(8, 20));
|
|
350
|
+
const c0 = (rawType >> 4) & 0x1;
|
|
351
|
+
const c1 = (rawType >> 8) & 0x1;
|
|
352
|
+
const stunClassNum = (c1 << 1) | c0;
|
|
353
|
+
const stunClass = ['request', 'indication', 'success', 'error'][stunClassNum] || 'unknown';
|
|
354
|
+
const method_bits = ((rawType & 0x3e00) >> 2) | ((rawType & 0x00e0) >> 1) | (rawType & 0x000f);
|
|
355
|
+
let method = 'unknown';
|
|
356
|
+
switch (method_bits) {
|
|
357
|
+
case 0x001:
|
|
358
|
+
method = 'binding';
|
|
359
|
+
break;
|
|
360
|
+
case 0x003:
|
|
361
|
+
method = 'allocate';
|
|
362
|
+
break;
|
|
363
|
+
case 0x004:
|
|
364
|
+
method = 'refresh';
|
|
365
|
+
break;
|
|
366
|
+
case 0x006:
|
|
367
|
+
method = 'send';
|
|
368
|
+
break;
|
|
369
|
+
case 0x007:
|
|
370
|
+
method = 'data';
|
|
371
|
+
break;
|
|
372
|
+
case 0x008:
|
|
373
|
+
method = 'create-permission';
|
|
374
|
+
break;
|
|
375
|
+
case 0x009:
|
|
376
|
+
method = 'channel-bind';
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
if (rawType === 0x0801)
|
|
380
|
+
method = 'wa-ping';
|
|
381
|
+
if (rawType === 0x0802)
|
|
382
|
+
method = 'wa-pong';
|
|
383
|
+
const attributes = [];
|
|
384
|
+
let errorCode;
|
|
385
|
+
let errorReason;
|
|
386
|
+
let stableRoutingConnId;
|
|
387
|
+
let offset = 20;
|
|
388
|
+
while (offset + 4 <= 20 + msgLength && offset + 4 <= data.length) {
|
|
389
|
+
const attrType = readUInt16BE(data, offset);
|
|
390
|
+
const attrLength = readUInt16BE(data, offset + 2);
|
|
391
|
+
const attrEnd = offset + 4 + attrLength;
|
|
392
|
+
if (attrEnd > data.length)
|
|
393
|
+
break;
|
|
394
|
+
const attrData = data.subarray(offset + 4, attrEnd);
|
|
395
|
+
attributes.push({
|
|
396
|
+
type: attrType,
|
|
397
|
+
typeName: STUN_ATTR_NAMES[attrType] || `0x${attrType.toString(16).padStart(4, '0')}`,
|
|
398
|
+
length: attrLength,
|
|
399
|
+
data: attrData
|
|
400
|
+
});
|
|
401
|
+
if (attrType === 0x0009 && attrLength >= 4) {
|
|
402
|
+
const errorClass = attrData[2] & 0x07;
|
|
403
|
+
const errorNumber = attrData[3];
|
|
404
|
+
errorCode = errorClass * 100 + errorNumber;
|
|
405
|
+
if (attrLength > 4) {
|
|
406
|
+
errorReason = TEXT_DECODER.decode(attrData.subarray(4));
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (attrType === 0x4033 && stunClass === 'success' && attrLength === 8) {
|
|
410
|
+
stableRoutingConnId = readBigUInt64BE(attrData, 0);
|
|
411
|
+
}
|
|
412
|
+
offset = attrEnd + ((4 - (attrLength % 4)) % 4);
|
|
413
|
+
}
|
|
414
|
+
return {
|
|
415
|
+
rawType,
|
|
416
|
+
method,
|
|
417
|
+
stunClass,
|
|
418
|
+
isSuccess: stunClass === 'success',
|
|
419
|
+
isError: stunClass === 'error',
|
|
420
|
+
errorCode,
|
|
421
|
+
errorReason,
|
|
422
|
+
stableRoutingConnId,
|
|
423
|
+
transactionId,
|
|
424
|
+
length: data.length,
|
|
425
|
+
attributes
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
export function formatStunResponse(info) {
|
|
429
|
+
let result = `STUN ${info.method} ${info.stunClass} (0x${info.rawType.toString(16).padStart(4, '0')}, ${info.length}B)`;
|
|
430
|
+
if (info.isError && info.errorCode) {
|
|
431
|
+
result += ` ERROR ${info.errorCode}`;
|
|
432
|
+
if (info.errorReason)
|
|
433
|
+
result += `: ${info.errorReason}`;
|
|
434
|
+
}
|
|
435
|
+
if (info.attributes.length > 0) {
|
|
436
|
+
const attrNames = info.attributes.map((a) => a.typeName).join(', ');
|
|
437
|
+
result += ` [${attrNames}]`;
|
|
438
|
+
}
|
|
439
|
+
return result;
|
|
440
|
+
}
|
|
441
|
+
export function classifyPacket(data) {
|
|
442
|
+
if (data.length < 2)
|
|
443
|
+
return `tiny(${data.length}B)`;
|
|
444
|
+
const firstByte = data[0];
|
|
445
|
+
const twoBits = (firstByte & 0xc0) >> 6;
|
|
446
|
+
if (twoBits === 0) {
|
|
447
|
+
const info = parseStunResponse(data);
|
|
448
|
+
if (info)
|
|
449
|
+
return formatStunResponse(info);
|
|
450
|
+
const msgType = (data[0] << 8) | data[1];
|
|
451
|
+
return `STUN? 0x${msgType.toString(16)} (${data.length}B)`;
|
|
452
|
+
}
|
|
453
|
+
if (twoBits === 2) {
|
|
454
|
+
const pt = data[1] & 0x7f;
|
|
455
|
+
const marker = (data[1] >> 7) & 1;
|
|
456
|
+
const seq = data.length >= 4 ? (data[2] << 8) | data[3] : 0;
|
|
457
|
+
return `RTP/SRTP PT=${pt} M=${marker} seq=${seq} (${data.length}B)`;
|
|
458
|
+
}
|
|
459
|
+
if (twoBits === 1) {
|
|
460
|
+
return `DTLS? 0x${firstByte.toString(16)} (${data.length}B)`;
|
|
461
|
+
}
|
|
462
|
+
return `unknown 0x${firstByte.toString(16)} (${data.length}B)`;
|
|
463
|
+
}
|