kage-baileys 3.0.6 → 3.2.4
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/Example/connection.cjs +4 -0
- package/README.MD +4726 -48
- package/README.md +5941 -0
- package/WAProto/WAProto.proto +3395 -415
- package/WAProto/compat.cjs +12 -0
- package/WAProto/index.d.ts +47104 -19888
- package/WAProto/index.js +131979 -57361
- package/lib/Addon/http-error.js +43 -0
- package/lib/Addon/index.js +32 -0
- package/lib/Addon/keyed-db.js +250 -0
- package/lib/Addon/link-preview.js +82 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +110 -0
- package/lib/Defaults/constants.js +156 -0
- package/lib/Defaults/history.js +46 -0
- package/lib/Defaults/index.js +230 -0
- package/lib/Defaults/media.js +104 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +44 -0
- package/lib/Extensions/business-advanced.js +382 -0
- package/lib/Extensions/business.js +159 -0
- package/lib/Extensions/chats-advanced.js +1106 -0
- package/lib/Extensions/chats.js +349 -0
- package/lib/Extensions/communities-advanced.js +430 -0
- package/lib/Extensions/community.js +94 -0
- package/lib/Extensions/groups-advanced.js +343 -0
- package/lib/Extensions/index.js +30 -0
- package/lib/Extensions/messages-recv-advanced.js +1690 -0
- package/lib/Extensions/messages-send-advanced.js +622 -0
- package/lib/Extensions/messages-send.js +1380 -0
- package/lib/Extensions/newsletter-advanced.js +228 -0
- package/lib/Extensions/newsletter.js +72 -0
- package/lib/Extensions/socket.js +389 -0
- package/lib/Modded/message_builder.js +2084 -4
- package/lib/Signal/Group/ciphertext-message.js +54 -0
- package/lib/Signal/Group/group-session-builder.js +124 -0
- package/lib/Signal/Group/group_cipher.js +216 -0
- package/lib/Signal/Group/index.js +124 -0
- package/lib/Signal/Group/keyhelper.js +108 -0
- package/lib/Signal/Group/sender-chain-key.js +83 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +157 -0
- package/lib/Signal/Group/sender-key-message.js +166 -0
- package/lib/Signal/Group/sender-key-name.js +130 -0
- package/lib/Signal/Group/sender-key-record.js +113 -0
- package/lib/Signal/Group/sender-key-state.js +208 -0
- package/lib/Signal/Group/sender-message-key.js +82 -0
- package/lib/Signal/libsignal.js +935 -0
- package/lib/Signal/lid-mapping.js +545 -1
- package/lib/Socket/Client/index.js +60 -0
- package/lib/Socket/Client/types.js +51 -0
- package/lib/Socket/Client/websocket.js +152 -0
- package/lib/Socket/business.js +831 -0
- package/lib/Socket/chats.js +2535 -49
- package/lib/Socket/communities.js +450 -1
- package/lib/Socket/community.js +1022 -0
- package/lib/Socket/dugong.js +1 -1
- package/lib/Socket/groups.js +747 -0
- package/lib/Socket/index.js +60 -2
- package/lib/Socket/messages-recv.js +3450 -5
- package/lib/Socket/messages-send.js +3123 -0
- package/lib/Socket/mex.js +126 -1
- package/lib/Socket/newsletter.js +465 -0
- package/lib/Socket/socket.js +2217 -1
- package/lib/Store/index.js +79 -0
- package/lib/Store/make-cache-manager-store.js +186 -0
- package/lib/Store/make-in-memory-store.js +878 -0
- package/lib/Store/make-ordered-dictionary.js +187 -0
- package/lib/Store/object-repository.js +78 -0
- package/lib/Types/Auth.js +25 -0
- package/lib/Types/Bussines.js +25 -1
- package/lib/Types/Call.js +25 -0
- package/lib/Types/Chat.js +39 -0
- package/lib/Types/Contact.js +25 -0
- package/lib/Types/Events.js +25 -0
- package/lib/Types/GroupMetadata.js +25 -0
- package/lib/Types/Label.js +76 -0
- package/lib/Types/LabelAssociation.js +40 -0
- package/lib/Types/Message.js +57 -0
- package/lib/Types/Mex.js +56 -1
- package/lib/Types/MexUpdates.js +46 -0
- package/lib/Types/Newsletter.js +56 -0
- package/lib/Types/Product.js +25 -0
- package/lib/Types/RichType.js +23 -0
- package/lib/Types/Signal.js +25 -0
- package/lib/Types/Socket.js +25 -0
- package/lib/Types/State.js +92 -0
- package/lib/Types/USync.js +25 -0
- package/lib/Types/index.js +133 -0
- package/lib/Utils/auth-utils.js +703 -0
- package/lib/Utils/automation-tools.js +51 -16
- package/lib/Utils/baileys-event-stream.js +146 -0
- package/lib/Utils/browser-utils.js +95 -0
- package/lib/Utils/business.js +525 -0
- package/lib/Utils/chat-utils.js +1861 -0
- package/lib/Utils/companion-reg-client-utils.js +55 -1
- package/lib/Utils/crypto.js +355 -0
- package/lib/Utils/decode-wa-message.js +709 -0
- package/lib/Utils/event-buffer.js +1323 -8
- package/lib/Utils/generics.js +945 -1
- package/lib/Utils/history.js +266 -0
- package/lib/Utils/identity-change-handler.js +62 -1
- package/lib/Utils/index.js +114 -0
- package/lib/Utils/link-preview.js +179 -0
- package/lib/Utils/logger.js +42 -0
- package/lib/Utils/lt-hash.js +73 -0
- package/lib/Utils/make-mutex.js +104 -0
- package/lib/Utils/message-inspector.js +110 -0
- package/lib/Utils/message-retry-manager.js +507 -2
- package/lib/Utils/messages-media.js +1931 -1
- package/lib/Utils/messages.js +3748 -0
- package/lib/Utils/noise-handler.js +423 -0
- package/lib/Utils/offline-node-processor.js +53 -1
- package/lib/Utils/pre-key-manager.js +255 -1
- package/lib/Utils/process-message.js +1592 -0
- package/lib/Utils/reporting-utils.js +272 -1
- package/lib/Utils/rich-message-utils.js +377 -0
- package/lib/Utils/rich-messages.js +1 -1
- package/lib/Utils/signal.js +437 -0
- package/lib/Utils/stanza-ack.js +51 -1
- package/lib/Utils/status-refresh.js +37 -0
- package/lib/Utils/status-tools.js +4 -2
- package/lib/Utils/sync-action-utils.js +63 -1
- package/lib/Utils/tc-token-utils.js +185 -1
- package/lib/Utils/terminal-banner.js +31 -0
- package/lib/Utils/use-multi-file-auth-state.js +271 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +142 -0
- package/lib/Utils/validate-connection.js +497 -1
- package/lib/Utils/view-once-compat.js +13 -5
- package/lib/VoIP/audio-feeder.js +150 -0
- package/lib/VoIP/index.js +289 -0
- package/lib/VoIP/relay-transport.js +608 -0
- package/lib/VoIP/signaling.js +583 -0
- package/lib/VoIP/types.js +30 -0
- package/lib/VoIP/wasm-engine.js +1261 -2
- package/lib/VoIP/worker-bootstrap.js +1090 -2
- package/lib/WABinary/constants.js +2805 -0
- package/lib/WABinary/decode.js +649 -2
- package/lib/WABinary/encode.js +564 -2
- package/lib/WABinary/generic-utils.js +401 -0
- package/lib/WABinary/index.js +82 -0
- package/lib/WABinary/jid-utils.js +290 -0
- package/lib/WABinary/types.js +25 -0
- package/lib/WAM/BinaryInfo.js +50 -0
- package/lib/WAM/constants.js +45747 -0
- package/lib/WAM/encode.js +393 -0
- package/lib/WAM/index.js +78 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +135 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +110 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +142 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +85 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +69 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +107 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +38 -1
- package/lib/WAUSync/Protocols/index.js +83 -0
- package/lib/WAUSync/USyncQuery.js +241 -2
- package/lib/WAUSync/USyncUser.js +86 -0
- package/lib/WAUSync/index.js +78 -0
- package/lib/assets/wasm/loader.js +1875 -1
- package/lib/assets/wasm/worker-modules.js +11047 -1
- package/lib/index.d.ts +126 -0
- package/lib/index.js +53 -21
- package/lib/index.mjs +437 -0
- package/licenses/upstream-MIT.txt +22 -0
- package/maintenance/api-coverage.json +1642 -0
- package/maintenance/archivos-sha256.json +207 -0
- package/maintenance/consumer-validation.log +3 -0
- package/maintenance/dependency-audit.json +4 -4
- package/maintenance/extension-inventory.json +57 -0
- package/maintenance/protocol-update.json +1101 -0
- package/maintenance/protocol-wire-audit.json +4 -0
- package/maintenance/reference-socket-surface.json +209 -0
- package/maintenance/resumen-validacion.json +50 -0
- package/maintenance/validation.log +2725 -0
- package/maintenance/verification.json +566 -0
- package/package.json +107 -9
- package/scripts/check-imports.cjs +10 -0
- package/scripts/check-package.cjs +40 -0
- package/scripts/check-syntax.cjs +1 -1
- package/scripts/generate-protobuf.cjs +6 -1
- package/scripts/generate-public-types.cjs +9 -0
- package/scripts/kage-base64-fix.cjs +45 -0
- package/scripts/maintenance.cjs +10 -0
- package/test/advanced.test.cjs +24 -0
- package/test/base64-fix.test.cjs +49 -0
- package/test/maintenance.test.cjs +1 -1
- package/test/npm-publication.test.cjs +141 -0
- package/test/recepcion-reparada.test.cjs +98 -0
- package/test/resilience-binary.test.cjs +69 -0
- package/test/resilience-transport.test.cjs +34 -0
- package/test/types.test.ts +5 -0
- package/test/upgrade.test.cjs +104 -0
- package/tsconfig.json +20 -0
- package/WAProto/p.html +0 -1
- package/lib/KageExtra/Modded/message_builder.js +0 -2330
- package/lib/KageExtra/VoIP/audio-feeder.js +0 -136
- package/lib/KageExtra/VoIP/index.js +0 -282
- package/lib/KageExtra/VoIP/relay-transport.js +0 -559
- package/lib/KageExtra/VoIP/signaling.js +0 -603
- package/lib/KageExtra/VoIP/types.js +0 -17
- package/lib/KageExtra/VoIP/wasm-engine.js +0 -1214
- package/lib/KageExtra/VoIP/worker-bootstrap.js +0 -1042
- package/lib/KageExtra/assets/wasm/loader.js +0 -5
- package/lib/KageExtra/assets/wasm/whatsapp.wasm +0 -0
- package/lib/KageExtra/assets/wasm/worker-modules.js +0 -273
- package/lib/KageExtra/index.js +0 -3
- package/lib/KageExtra/package.json +0 -1
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -53
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -24
- package/maintenance/revision-full-check.txt +0 -1097
- package/maintenance/revision-library-tests.tap +0 -1088
- package/maintenance/revision-protobuf.txt +0 -5
- package/maintenance/revision-syntax.txt +0 -5
- package/maintenance/schema-coverage.json +0 -23
- package/maintenance/schema-merge.json +0 -30
- package/maintenance/validation-node20.txt +0 -191
- package/maintenance/validation-node22.txt +0 -237
|
@@ -1,559 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Relay transport.
|
|
3
|
-
*
|
|
4
|
-
* Tunnels UDP traffic to WhatsApp's edge relay servers via WebRTC data
|
|
5
|
-
* channels (using `@roamhq/wrtc`). Mirrors the browser client's behavior:
|
|
6
|
-
* pre-negotiated SCTP, custom DTLS fingerprint, ICE restart on idle.
|
|
7
|
-
*
|
|
8
|
-
* @author ShellTear
|
|
9
|
-
*/
|
|
10
|
-
import { appendFileSync } from "node:fs";
|
|
11
|
-
const RELAY_PROTO_UDP = 0;
|
|
12
|
-
const FAUX_WEB_CLIENT_RELAY_PORT = 3478;
|
|
13
|
-
const TRUE_WEB_CLIENT_RELAY_PORT = 3480;
|
|
14
|
-
const CONNECTION_TIMEOUT_MS = 20_000;
|
|
15
|
-
const ICE_RESTART_IDLE_THRESHOLD_MS = 10_000;
|
|
16
|
-
const ICE_RTT_POLL_MS = 1_000;
|
|
17
|
-
const MAX_BUFFER_SIZE = 256 * 1024;
|
|
18
|
-
const RELAY_PACKET_LOG_PATH = process.env.CALL_DUMP_RELAY_PACKETS_PATH ?? "";
|
|
19
|
-
const DISABLE_IPV6 = process.env.CALL_DISABLE_IPV6 !== "0";
|
|
20
|
-
const RELAY_PORT_MODE = process.env.CALL_RELAY_PORT_MODE === "original" ? "original" : "web";
|
|
21
|
-
const USE_ORIGINAL_RELAY_PORTS = RELAY_PORT_MODE === "original";
|
|
22
|
-
const RELAY_DTLS_FINGERPRINT = "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";
|
|
23
|
-
const getConnectionIdentifier = (ip, port) => ip.includes(":") ? `[${ip}]:${port}` : `${ip}:${port}`;
|
|
24
|
-
const createEmptyStats = () => ({
|
|
25
|
-
sentPackets: 0,
|
|
26
|
-
receivedPackets: 0,
|
|
27
|
-
sentBytes: 0,
|
|
28
|
-
receivedBytes: 0,
|
|
29
|
-
droppedPackets: 0,
|
|
30
|
-
openConnections: 0,
|
|
31
|
-
});
|
|
32
|
-
const getRelayLookupId = (ip, port) => getConnectionIdentifier(ip, port === TRUE_WEB_CLIENT_RELAY_PORT ? TRUE_WEB_CLIENT_RELAY_PORT : port);
|
|
33
|
-
const getRtcConnectPort = (info) => info.ip === "157.240.24.133" ? FAUX_WEB_CLIENT_RELAY_PORT : info.port;
|
|
34
|
-
const getVoipStackPort = (info) => {
|
|
35
|
-
if (USE_ORIGINAL_RELAY_PORTS)
|
|
36
|
-
return info.originalPort || info.port;
|
|
37
|
-
const sourcePort = info.originalPort || info.port;
|
|
38
|
-
return sourcePort === TRUE_WEB_CLIENT_RELAY_PORT
|
|
39
|
-
? TRUE_WEB_CLIENT_RELAY_PORT
|
|
40
|
-
: FAUX_WEB_CLIENT_RELAY_PORT;
|
|
41
|
-
};
|
|
42
|
-
const bufferPacket = (connection, packet) => {
|
|
43
|
-
if (packet.byteLength > MAX_BUFFER_SIZE) {
|
|
44
|
-
connection.stats.droppedPackets += 1;
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
while (connection.packetBuffer.length > 0 &&
|
|
48
|
-
connection.bufferedBytes + packet.byteLength > MAX_BUFFER_SIZE) {
|
|
49
|
-
const dropped = connection.packetBuffer.shift();
|
|
50
|
-
if (dropped) {
|
|
51
|
-
connection.bufferedBytes -= dropped.byteLength;
|
|
52
|
-
connection.stats.droppedPackets += 1;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
connection.packetBuffer.push(packet);
|
|
56
|
-
connection.bufferedBytes += packet.byteLength;
|
|
57
|
-
return true;
|
|
58
|
-
};
|
|
59
|
-
const shiftPacket = (connection) => {
|
|
60
|
-
const packet = connection.packetBuffer.shift() ?? null;
|
|
61
|
-
if (packet)
|
|
62
|
-
connection.bufferedBytes -= packet.byteLength;
|
|
63
|
-
return packet;
|
|
64
|
-
};
|
|
65
|
-
const replaceIceCredentials = (sdp, ufrag, pwd) => sdp
|
|
66
|
-
.replace(/a=ice-ufrag:[^\r\n]+/g, `a=ice-ufrag:${ufrag}`)
|
|
67
|
-
.replace(/a=ice-pwd:[^\r\n]+/g, `a=ice-pwd:${pwd}`);
|
|
68
|
-
const replaceDtlsFingerprint = (sdp, algorithm, fingerprint) => sdp.replace(/a=fingerprint:[^\r\n]+/g, `a=fingerprint:${algorithm} ${fingerprint}`);
|
|
69
|
-
const removeIceCandidates = (sdp) => sdp
|
|
70
|
-
.replace(/a=candidate:[^\r\n]+\r?\n/g, "")
|
|
71
|
-
.replace(/a=end-of-candidates\r?\n?/g, "");
|
|
72
|
-
const appendRelayCandidate = (sdp, ip, port) => {
|
|
73
|
-
const candidate = `a=candidate:2 1 udp 2122262783 ${ip} ${port} typ host generation 0 network-cost 5`;
|
|
74
|
-
return `${removeIceCandidates(sdp)}${candidate}\r\na=end-of-candidates\r\n`;
|
|
75
|
-
};
|
|
76
|
-
const buildRemoteRelayAnswer = (offerSdp, info) => {
|
|
77
|
-
const setupLine = info.enableEdgerayDtlsActiveMode ? "a=setup:active" : "a=setup:passive";
|
|
78
|
-
let answerSdp = offerSdp.replace(/a=setup:actpass/g, setupLine);
|
|
79
|
-
answerSdp = replaceIceCredentials(answerSdp, info.authToken ?? info.token, info.key);
|
|
80
|
-
answerSdp = replaceDtlsFingerprint(answerSdp, "sha-256", RELAY_DTLS_FINGERPRINT);
|
|
81
|
-
answerSdp = answerSdp.replace(/a=ice-options:[^\r\n]+\r\n/g, "");
|
|
82
|
-
answerSdp = answerSdp.replace(/a=max-message-size:[^\r\n]+/g, "a=max-message-size:1200");
|
|
83
|
-
return appendRelayCandidate(answerSdp, info.ip, info.port);
|
|
84
|
-
};
|
|
85
|
-
const toArrayBuffer = (data) => {
|
|
86
|
-
const copy = new Uint8Array(data.byteLength);
|
|
87
|
-
copy.set(data);
|
|
88
|
-
return copy.buffer;
|
|
89
|
-
};
|
|
90
|
-
const toUint8Array = (data) => {
|
|
91
|
-
if (data instanceof Uint8Array)
|
|
92
|
-
return new Uint8Array(data);
|
|
93
|
-
if (Buffer.isBuffer(data))
|
|
94
|
-
return new Uint8Array(data);
|
|
95
|
-
if (data instanceof ArrayBuffer)
|
|
96
|
-
return new Uint8Array(data);
|
|
97
|
-
if (data && typeof data === "object" && "byteLength" in data && "buffer" in data) {
|
|
98
|
-
const view = data;
|
|
99
|
-
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
100
|
-
}
|
|
101
|
-
return null;
|
|
102
|
-
};
|
|
103
|
-
const classifyRelayPacket = (packet) => {
|
|
104
|
-
if (packet.byteLength < 2)
|
|
105
|
-
return "non_stun";
|
|
106
|
-
const bytes = new Uint8Array(packet);
|
|
107
|
-
const first = bytes[0];
|
|
108
|
-
const second = bytes[1];
|
|
109
|
-
if ((first & 0xc0) !== 0)
|
|
110
|
-
return "non_stun";
|
|
111
|
-
const stunType = ((first & 0x3f) << 8) | second;
|
|
112
|
-
if (stunType === 0x0001)
|
|
113
|
-
return "stun_bind";
|
|
114
|
-
if (stunType === 0x0003)
|
|
115
|
-
return "stun_alloc";
|
|
116
|
-
return "stun_unknown";
|
|
117
|
-
};
|
|
118
|
-
const appendRelayPacketLog = (direction, connection, packet) => {
|
|
119
|
-
if (!RELAY_PACKET_LOG_PATH)
|
|
120
|
-
return;
|
|
121
|
-
const buffer = packet instanceof ArrayBuffer
|
|
122
|
-
? Buffer.from(new Uint8Array(packet))
|
|
123
|
-
: Buffer.from(packet.buffer, packet.byteOffset, packet.byteLength);
|
|
124
|
-
appendFileSync(RELAY_PACKET_LOG_PATH, JSON.stringify({
|
|
125
|
-
ts: new Date().toISOString(),
|
|
126
|
-
direction,
|
|
127
|
-
id: connection.info.id,
|
|
128
|
-
relayId: connection.info.relayId,
|
|
129
|
-
relayName: connection.info.name,
|
|
130
|
-
ip: connection.info.ip,
|
|
131
|
-
rtcPort: connection.info.port,
|
|
132
|
-
voipPort: getVoipStackPort(connection.info),
|
|
133
|
-
size: buffer.byteLength,
|
|
134
|
-
hex: buffer.toString("hex"),
|
|
135
|
-
}) + "\n");
|
|
136
|
-
};
|
|
137
|
-
export class RelayRtcTransport {
|
|
138
|
-
config;
|
|
139
|
-
#relayInfoById = new Map();
|
|
140
|
-
#connections = new Map();
|
|
141
|
-
#totals = createEmptyStats();
|
|
142
|
-
#wrtcPromise = null;
|
|
143
|
-
constructor(config) {
|
|
144
|
-
this.config = config;
|
|
145
|
-
}
|
|
146
|
-
updateRelayList = (update) => {
|
|
147
|
-
const nextInfoById = new Map();
|
|
148
|
-
for (const relay of update.relays ?? []) {
|
|
149
|
-
if (relay.token_id == null || relay.token_id < 0 ||
|
|
150
|
-
relay.token_id >= (update.relay_tokens ?? []).length) {
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const authToken = update.auth_tokens && relay.auth_token_id != null && relay.auth_token_id >= 0
|
|
154
|
-
? update.auth_tokens[relay.auth_token_id]
|
|
155
|
-
: undefined;
|
|
156
|
-
for (const address of relay.addresses ?? []) {
|
|
157
|
-
if (address.protocol !== RELAY_PROTO_UDP)
|
|
158
|
-
continue;
|
|
159
|
-
if (address.ipv4 && address.port != null) {
|
|
160
|
-
const clientPort = USE_ORIGINAL_RELAY_PORTS ? address.port : TRUE_WEB_CLIENT_RELAY_PORT;
|
|
161
|
-
const id = getConnectionIdentifier(address.ipv4, clientPort);
|
|
162
|
-
nextInfoById.set(id, {
|
|
163
|
-
id,
|
|
164
|
-
relayId: relay.relay_id,
|
|
165
|
-
ip: address.ipv4,
|
|
166
|
-
port: clientPort,
|
|
167
|
-
originalPort: address.port,
|
|
168
|
-
isIPv6: false,
|
|
169
|
-
token: update.relay_tokens[relay.token_id],
|
|
170
|
-
authToken,
|
|
171
|
-
key: update.relay_key,
|
|
172
|
-
name: relay.relay_name,
|
|
173
|
-
enableEdgerayDtlsActiveMode: update.enable_edgeray_dtls_active_mode === true,
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
if (!DISABLE_IPV6 && address.ipv6 && address.port_v6 != null) {
|
|
177
|
-
const clientPort = USE_ORIGINAL_RELAY_PORTS ? address.port_v6 : TRUE_WEB_CLIENT_RELAY_PORT;
|
|
178
|
-
const id = getConnectionIdentifier(address.ipv6, clientPort);
|
|
179
|
-
nextInfoById.set(id, {
|
|
180
|
-
id,
|
|
181
|
-
relayId: relay.relay_id,
|
|
182
|
-
ip: address.ipv6,
|
|
183
|
-
port: clientPort,
|
|
184
|
-
originalPort: address.port_v6,
|
|
185
|
-
isIPv6: true,
|
|
186
|
-
token: update.relay_tokens[relay.token_id],
|
|
187
|
-
authToken,
|
|
188
|
-
key: update.relay_key,
|
|
189
|
-
name: relay.relay_name,
|
|
190
|
-
enableEdgerayDtlsActiveMode: update.enable_edgeray_dtls_active_mode === true,
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
for (const id of this.#relayInfoById.keys()) {
|
|
196
|
-
if (!nextInfoById.has(id))
|
|
197
|
-
this.#closeConnection(id);
|
|
198
|
-
}
|
|
199
|
-
this.#relayInfoById.clear();
|
|
200
|
-
for (const [id, info] of nextInfoById) {
|
|
201
|
-
this.#relayInfoById.set(id, info);
|
|
202
|
-
void this.#ensureConnection(info);
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
send = (packet, ip, port) => {
|
|
206
|
-
const requestedId = getConnectionIdentifier(ip, port);
|
|
207
|
-
const preferredPort = USE_ORIGINAL_RELAY_PORTS ? port : TRUE_WEB_CLIENT_RELAY_PORT;
|
|
208
|
-
const candidateIds = [
|
|
209
|
-
requestedId,
|
|
210
|
-
getConnectionIdentifier(ip, preferredPort),
|
|
211
|
-
getRelayLookupId(ip, TRUE_WEB_CLIENT_RELAY_PORT),
|
|
212
|
-
getRelayLookupId(ip, FAUX_WEB_CLIENT_RELAY_PORT),
|
|
213
|
-
];
|
|
214
|
-
let info;
|
|
215
|
-
for (const candidateId of candidateIds) {
|
|
216
|
-
info = this.#relayInfoById.get(candidateId);
|
|
217
|
-
if (info)
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
if (!info) {
|
|
221
|
-
if (DISABLE_IPV6 && ip.includes(":"))
|
|
222
|
-
return 0;
|
|
223
|
-
const earlyConnection = this.#getOrCreateEarlyConnection(ip, port);
|
|
224
|
-
bufferPacket(earlyConnection, toArrayBuffer(packet));
|
|
225
|
-
return packet.byteLength;
|
|
226
|
-
}
|
|
227
|
-
const connection = this.#getOrCreateConnection(info);
|
|
228
|
-
const arrayBuffer = toArrayBuffer(packet);
|
|
229
|
-
if (classifyRelayPacket(arrayBuffer) === "stun_alloc" &&
|
|
230
|
-
connection.state === "open" &&
|
|
231
|
-
connection.sentMedia &&
|
|
232
|
-
Date.now() - connection.lastRxPacketTime > ICE_RESTART_IDLE_THRESHOLD_MS) {
|
|
233
|
-
connection.packetBuffer = [];
|
|
234
|
-
connection.bufferedBytes = 0;
|
|
235
|
-
bufferPacket(connection, arrayBuffer);
|
|
236
|
-
void this.#restartIce(connection);
|
|
237
|
-
return packet.byteLength;
|
|
238
|
-
}
|
|
239
|
-
if (connection.state === "open" && connection.dataChannel?.readyState === "open") {
|
|
240
|
-
if (!this.#sendBufferedPacket(connection, arrayBuffer)) {
|
|
241
|
-
connection.stats.droppedPackets += 1;
|
|
242
|
-
}
|
|
243
|
-
return packet.byteLength;
|
|
244
|
-
}
|
|
245
|
-
bufferPacket(connection, arrayBuffer);
|
|
246
|
-
void this.#ensureConnection(info);
|
|
247
|
-
return packet.byteLength;
|
|
248
|
-
};
|
|
249
|
-
getStats = () => {
|
|
250
|
-
let openConnections = 0;
|
|
251
|
-
for (const connection of this.#connections.values()) {
|
|
252
|
-
if (connection.state === "open")
|
|
253
|
-
openConnections += 1;
|
|
254
|
-
}
|
|
255
|
-
return { ...this.#totals, openConnections };
|
|
256
|
-
};
|
|
257
|
-
closeAll = async () => {
|
|
258
|
-
for (const id of [...this.#connections.keys()])
|
|
259
|
-
this.#closeConnection(id);
|
|
260
|
-
};
|
|
261
|
-
// ─── private ──────────────────────────────────────────────────────────────
|
|
262
|
-
#getOrCreateConnection = (info) => {
|
|
263
|
-
const existing = this.#connections.get(info.id);
|
|
264
|
-
if (existing) {
|
|
265
|
-
existing.info = info;
|
|
266
|
-
return existing;
|
|
267
|
-
}
|
|
268
|
-
const created = {
|
|
269
|
-
info, state: "none", peerConnection: null, dataChannel: null,
|
|
270
|
-
iceCandidate: null, packetBuffer: [], bufferedBytes: 0,
|
|
271
|
-
connectPromise: null, connectionTimeout: null, iceStatsInterval: null,
|
|
272
|
-
lastIceRttMs: null, hasReceivedFirstPacket: false,
|
|
273
|
-
hasNonStunPacketSent: false, sentMedia: false, lastRxPacketTime: 0,
|
|
274
|
-
isReconnecting: false, stats: createEmptyStats(),
|
|
275
|
-
};
|
|
276
|
-
this.#connections.set(info.id, created);
|
|
277
|
-
return created;
|
|
278
|
-
};
|
|
279
|
-
#getOrCreateEarlyConnection = (ip, port) => {
|
|
280
|
-
const id = getConnectionIdentifier(ip, port);
|
|
281
|
-
const existing = this.#connections.get(id);
|
|
282
|
-
if (existing)
|
|
283
|
-
return existing;
|
|
284
|
-
const placeholderInfo = {
|
|
285
|
-
id, relayId: 0, ip, port, originalPort: port,
|
|
286
|
-
isIPv6: ip.includes(":"), token: "", key: "",
|
|
287
|
-
name: "early-packet", enableEdgerayDtlsActiveMode: false,
|
|
288
|
-
};
|
|
289
|
-
const created = {
|
|
290
|
-
info: placeholderInfo, state: "none", peerConnection: null,
|
|
291
|
-
dataChannel: null, iceCandidate: null, packetBuffer: [],
|
|
292
|
-
bufferedBytes: 0, connectPromise: null, connectionTimeout: null,
|
|
293
|
-
iceStatsInterval: null, lastIceRttMs: null,
|
|
294
|
-
hasReceivedFirstPacket: false, hasNonStunPacketSent: false,
|
|
295
|
-
sentMedia: false, lastRxPacketTime: 0, isReconnecting: false,
|
|
296
|
-
stats: createEmptyStats(),
|
|
297
|
-
};
|
|
298
|
-
this.#connections.set(id, created);
|
|
299
|
-
return created;
|
|
300
|
-
};
|
|
301
|
-
#ensureConnection = async (info) => {
|
|
302
|
-
const connection = this.#getOrCreateConnection(info);
|
|
303
|
-
if (connection.state === "open" || connection.state === "connecting") {
|
|
304
|
-
return connection.connectPromise ?? Promise.resolve();
|
|
305
|
-
}
|
|
306
|
-
const promise = this.#connect(connection);
|
|
307
|
-
connection.connectPromise = promise;
|
|
308
|
-
try {
|
|
309
|
-
await promise;
|
|
310
|
-
}
|
|
311
|
-
finally {
|
|
312
|
-
connection.connectPromise = null;
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
#connect = async (connection) => {
|
|
316
|
-
const wrtcModule = await this.#loadWrtc();
|
|
317
|
-
const { RTCPeerConnection } = wrtcModule;
|
|
318
|
-
if (typeof RTCPeerConnection !== "function") {
|
|
319
|
-
throw new Error("RTCPeerConnection unavailable from @roamhq/wrtc");
|
|
320
|
-
}
|
|
321
|
-
this.#closePeerObjects(connection);
|
|
322
|
-
connection.state = "connecting";
|
|
323
|
-
const pc = new RTCPeerConnection();
|
|
324
|
-
const dc = pc.createDataChannel("pre-negotiated", {
|
|
325
|
-
negotiated: true, id: 0, ordered: false, maxRetransmits: 0, priority: "high",
|
|
326
|
-
});
|
|
327
|
-
connection.peerConnection = pc;
|
|
328
|
-
connection.dataChannel = dc;
|
|
329
|
-
dc.binaryType = "arraybuffer";
|
|
330
|
-
dc.onopen = () => {
|
|
331
|
-
connection.state = "open";
|
|
332
|
-
connection.isReconnecting = false;
|
|
333
|
-
if (connection.connectionTimeout) {
|
|
334
|
-
clearTimeout(connection.connectionTimeout);
|
|
335
|
-
connection.connectionTimeout = null;
|
|
336
|
-
}
|
|
337
|
-
this.#flushBufferedPackets(connection);
|
|
338
|
-
this.#startIceRttPolling(connection);
|
|
339
|
-
};
|
|
340
|
-
dc.onclose = () => {
|
|
341
|
-
if (connection.state !== "failed")
|
|
342
|
-
connection.state = "closed";
|
|
343
|
-
};
|
|
344
|
-
dc.onerror = () => {
|
|
345
|
-
connection.state = "failed";
|
|
346
|
-
};
|
|
347
|
-
dc.onmessage = (event) => {
|
|
348
|
-
this.#handleIncomingPacket(connection, event.data);
|
|
349
|
-
};
|
|
350
|
-
pc.onicecandidate = (event) => {
|
|
351
|
-
if (event.candidate?.candidate && !connection.iceCandidate) {
|
|
352
|
-
connection.iceCandidate = event.candidate.candidate;
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
pc.oniceconnectionstatechange = () => {
|
|
356
|
-
if (pc.iceConnectionState === "failed" || pc.iceConnectionState === "closed") {
|
|
357
|
-
connection.state = "failed";
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
connection.connectionTimeout = setTimeout(() => {
|
|
361
|
-
if (connection.state === "connecting") {
|
|
362
|
-
connection.state = "failed";
|
|
363
|
-
this.#closePeerObjects(connection);
|
|
364
|
-
}
|
|
365
|
-
}, CONNECTION_TIMEOUT_MS);
|
|
366
|
-
const offer = await pc.createOffer();
|
|
367
|
-
await pc.setLocalDescription(offer);
|
|
368
|
-
const remoteSdp = buildRemoteRelayAnswer(offer.sdp ?? "", {
|
|
369
|
-
...connection.info,
|
|
370
|
-
port: getRtcConnectPort(connection.info),
|
|
371
|
-
});
|
|
372
|
-
await pc.setRemoteDescription({ type: "answer", sdp: remoteSdp });
|
|
373
|
-
};
|
|
374
|
-
#restartIce = async (connection) => {
|
|
375
|
-
if (connection.isReconnecting || !connection.hasNonStunPacketSent)
|
|
376
|
-
return;
|
|
377
|
-
const { RTCPeerConnection } = await this.#loadWrtc();
|
|
378
|
-
if (typeof RTCPeerConnection !== "function")
|
|
379
|
-
return;
|
|
380
|
-
connection.isReconnecting = true;
|
|
381
|
-
try {
|
|
382
|
-
this.#closePeerObjects(connection);
|
|
383
|
-
connection.state = "connecting";
|
|
384
|
-
const pc = new RTCPeerConnection();
|
|
385
|
-
const dc = pc.createDataChannel("pre-negotiated", {
|
|
386
|
-
negotiated: true, id: 0, ordered: false, maxRetransmits: 0, priority: "high",
|
|
387
|
-
});
|
|
388
|
-
connection.peerConnection = pc;
|
|
389
|
-
connection.dataChannel = dc;
|
|
390
|
-
dc.binaryType = "arraybuffer";
|
|
391
|
-
dc.onopen = () => {
|
|
392
|
-
connection.state = "open";
|
|
393
|
-
connection.isReconnecting = false;
|
|
394
|
-
this.#flushBufferedPackets(connection);
|
|
395
|
-
this.#startIceRttPolling(connection);
|
|
396
|
-
};
|
|
397
|
-
dc.onclose = () => { if (connection.state !== "failed")
|
|
398
|
-
connection.state = "closed"; };
|
|
399
|
-
dc.onerror = () => { connection.state = "failed"; };
|
|
400
|
-
dc.onmessage = (event) => {
|
|
401
|
-
this.#handleIncomingPacket(connection, event.data);
|
|
402
|
-
};
|
|
403
|
-
pc.onicecandidate = (event) => {
|
|
404
|
-
if (event.candidate?.candidate && !connection.iceCandidate) {
|
|
405
|
-
connection.iceCandidate = event.candidate.candidate;
|
|
406
|
-
}
|
|
407
|
-
};
|
|
408
|
-
const offer = await pc.createOffer({ iceRestart: false });
|
|
409
|
-
let localSdp = offer.sdp ?? "";
|
|
410
|
-
if (connection.iceCandidate) {
|
|
411
|
-
localSdp = `${removeIceCandidates(localSdp)}a=${connection.iceCandidate}\r\na=end-of-candidates\r\n`;
|
|
412
|
-
}
|
|
413
|
-
await pc.setLocalDescription({ type: "offer", sdp: localSdp });
|
|
414
|
-
const remoteSdp = buildRemoteRelayAnswer(localSdp, {
|
|
415
|
-
...connection.info,
|
|
416
|
-
port: getRtcConnectPort(connection.info),
|
|
417
|
-
});
|
|
418
|
-
await pc.setRemoteDescription({ type: "answer", sdp: remoteSdp });
|
|
419
|
-
}
|
|
420
|
-
catch {
|
|
421
|
-
connection.state = "failed";
|
|
422
|
-
}
|
|
423
|
-
finally {
|
|
424
|
-
connection.isReconnecting = false;
|
|
425
|
-
}
|
|
426
|
-
};
|
|
427
|
-
#handleIncomingPacket = (connection, raw) => {
|
|
428
|
-
const packet = toUint8Array(raw);
|
|
429
|
-
if (!packet)
|
|
430
|
-
return;
|
|
431
|
-
connection.stats.receivedPackets += 1;
|
|
432
|
-
connection.stats.receivedBytes += packet.byteLength;
|
|
433
|
-
connection.hasReceivedFirstPacket = true;
|
|
434
|
-
connection.lastRxPacketTime = Date.now();
|
|
435
|
-
this.#totals.receivedPackets += 1;
|
|
436
|
-
this.#totals.receivedBytes += packet.byteLength;
|
|
437
|
-
appendRelayPacketLog("recv", connection, packet);
|
|
438
|
-
this.config.onTransportMessage(packet, connection.info.ip, getVoipStackPort(connection.info));
|
|
439
|
-
};
|
|
440
|
-
#flushBufferedPackets = (connection) => {
|
|
441
|
-
while (connection.state === "open" &&
|
|
442
|
-
connection.dataChannel?.readyState === "open" &&
|
|
443
|
-
connection.packetBuffer.length > 0) {
|
|
444
|
-
const packet = shiftPacket(connection);
|
|
445
|
-
if (!packet)
|
|
446
|
-
break;
|
|
447
|
-
if (!this.#sendBufferedPacket(connection, packet)) {
|
|
448
|
-
connection.stats.droppedPackets += 1;
|
|
449
|
-
break;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
#sendBufferedPacket = (connection, packet) => {
|
|
454
|
-
try {
|
|
455
|
-
connection.dataChannel?.send(packet);
|
|
456
|
-
if (classifyRelayPacket(packet) === "non_stun") {
|
|
457
|
-
connection.hasNonStunPacketSent = true;
|
|
458
|
-
connection.sentMedia = true;
|
|
459
|
-
}
|
|
460
|
-
connection.stats.sentPackets += 1;
|
|
461
|
-
connection.stats.sentBytes += packet.byteLength;
|
|
462
|
-
this.#totals.sentPackets += 1;
|
|
463
|
-
this.#totals.sentBytes += packet.byteLength;
|
|
464
|
-
appendRelayPacketLog("send", connection, packet);
|
|
465
|
-
return true;
|
|
466
|
-
}
|
|
467
|
-
catch {
|
|
468
|
-
return false;
|
|
469
|
-
}
|
|
470
|
-
};
|
|
471
|
-
#startIceRttPolling = (connection) => {
|
|
472
|
-
this.#stopIceRttPolling(connection);
|
|
473
|
-
const pc = connection.peerConnection;
|
|
474
|
-
if (!pc || typeof pc.getStats !== "function" || !this.config.onIceRtt)
|
|
475
|
-
return;
|
|
476
|
-
const poll = async () => {
|
|
477
|
-
const rttMs = await this.#readCurrentRoundTripTimeMs(pc);
|
|
478
|
-
if (rttMs == null || connection.lastIceRttMs === rttMs)
|
|
479
|
-
return;
|
|
480
|
-
connection.lastIceRttMs = rttMs;
|
|
481
|
-
this.config.onIceRtt?.(rttMs, connection.info.ip, connection.info.port);
|
|
482
|
-
};
|
|
483
|
-
void poll();
|
|
484
|
-
connection.iceStatsInterval = setInterval(() => { void poll(); }, ICE_RTT_POLL_MS);
|
|
485
|
-
connection.iceStatsInterval.unref?.();
|
|
486
|
-
};
|
|
487
|
-
#stopIceRttPolling = (connection) => {
|
|
488
|
-
if (connection.iceStatsInterval) {
|
|
489
|
-
clearInterval(connection.iceStatsInterval);
|
|
490
|
-
connection.iceStatsInterval = null;
|
|
491
|
-
}
|
|
492
|
-
connection.lastIceRttMs = null;
|
|
493
|
-
};
|
|
494
|
-
#readCurrentRoundTripTimeMs = async (pc) => {
|
|
495
|
-
try {
|
|
496
|
-
const stats = await pc.getStats();
|
|
497
|
-
const reports = stats && typeof stats.values === "function"
|
|
498
|
-
? Array.from(stats.values())
|
|
499
|
-
: Array.isArray(stats) ? stats : Object.values(stats ?? {});
|
|
500
|
-
if (!reports.length)
|
|
501
|
-
return null;
|
|
502
|
-
let selectedCandidatePairId = "";
|
|
503
|
-
for (const report of reports) {
|
|
504
|
-
if (report?.type === "transport" && typeof report.selectedCandidatePairId === "string") {
|
|
505
|
-
selectedCandidatePairId = report.selectedCandidatePairId;
|
|
506
|
-
break;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
const candidatePairs = reports.filter((r) => r?.type === "candidate-pair");
|
|
510
|
-
const selectedPair = (selectedCandidatePairId && candidatePairs.find((r) => r?.id === selectedCandidatePairId)) ||
|
|
511
|
-
candidatePairs.find((r) => r?.selected || r?.nominated) ||
|
|
512
|
-
candidatePairs[0];
|
|
513
|
-
const rttSeconds = typeof selectedPair?.currentRoundTripTime === "number"
|
|
514
|
-
? selectedPair.currentRoundTripTime
|
|
515
|
-
: typeof selectedPair?.totalRoundTripTime === "number" &&
|
|
516
|
-
typeof selectedPair?.responsesReceived === "number" &&
|
|
517
|
-
selectedPair.responsesReceived > 0
|
|
518
|
-
? selectedPair.totalRoundTripTime / selectedPair.responsesReceived
|
|
519
|
-
: null;
|
|
520
|
-
if (rttSeconds == null || !Number.isFinite(rttSeconds) || rttSeconds <= 0)
|
|
521
|
-
return null;
|
|
522
|
-
return Math.max(1, Math.round(rttSeconds * 1000));
|
|
523
|
-
}
|
|
524
|
-
catch {
|
|
525
|
-
return null;
|
|
526
|
-
}
|
|
527
|
-
};
|
|
528
|
-
#closeConnection = (id) => {
|
|
529
|
-
const connection = this.#connections.get(id);
|
|
530
|
-
if (!connection)
|
|
531
|
-
return;
|
|
532
|
-
if (connection.connectionTimeout) {
|
|
533
|
-
clearTimeout(connection.connectionTimeout);
|
|
534
|
-
connection.connectionTimeout = null;
|
|
535
|
-
}
|
|
536
|
-
this.#closePeerObjects(connection);
|
|
537
|
-
connection.state = "closed";
|
|
538
|
-
connection.packetBuffer = [];
|
|
539
|
-
connection.bufferedBytes = 0;
|
|
540
|
-
this.#connections.delete(id);
|
|
541
|
-
};
|
|
542
|
-
#closePeerObjects = (connection) => {
|
|
543
|
-
this.#stopIceRttPolling(connection);
|
|
544
|
-
try {
|
|
545
|
-
connection.dataChannel?.close?.();
|
|
546
|
-
}
|
|
547
|
-
catch { }
|
|
548
|
-
try {
|
|
549
|
-
connection.peerConnection?.close?.();
|
|
550
|
-
}
|
|
551
|
-
catch { }
|
|
552
|
-
connection.dataChannel = null;
|
|
553
|
-
connection.peerConnection = null;
|
|
554
|
-
};
|
|
555
|
-
#loadWrtc = () => {
|
|
556
|
-
this.#wrtcPromise ??= import("@roamhq/wrtc").then((module) => (module.default ?? module));
|
|
557
|
-
return this.#wrtcPromise;
|
|
558
|
-
};
|
|
559
|
-
}
|