amiudmodz 6.2.7 → 6.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Socket/business.d.ts +6 -1
- package/lib/Socket/business.d.ts.map +1 -1
- package/lib/Socket/communities.d.ts +6 -1
- package/lib/Socket/communities.d.ts.map +1 -1
- package/lib/Socket/index.d.ts.map +1 -1
- package/lib/Socket/index.js +17 -4
- package/lib/Socket/index.js.map +1 -1
- package/lib/Socket/messages-recv.d.ts +6 -1
- package/lib/Socket/messages-recv.d.ts.map +1 -1
- package/lib/Socket/messages-send.d.ts +6 -1
- package/lib/Socket/messages-send.d.ts.map +1 -1
- package/lib/Socket/messages-send.js +85 -21
- package/lib/Socket/messages-send.js.map +1 -1
- package/lib/Utils/baileys-event-stream.js +0 -0
- package/lib/VoIP/audio-feeder.d.ts +42 -4
- package/lib/VoIP/audio-feeder.d.ts.map +1 -1
- package/lib/VoIP/audio-feeder.js +252 -128
- package/lib/VoIP/audio-feeder.js.map +1 -1
- package/lib/VoIP/index.d.ts +91 -8
- package/lib/VoIP/index.d.ts.map +1 -1
- package/lib/VoIP/index.js +813 -184
- package/lib/VoIP/index.js.map +1 -1
- package/lib/VoIP/relay-transport.d.ts +26 -1
- package/lib/VoIP/relay-transport.d.ts.map +1 -1
- package/lib/VoIP/relay-transport.js +257 -181
- package/lib/VoIP/relay-transport.js.map +1 -1
- package/lib/VoIP/signaling.d.ts +57 -3
- package/lib/VoIP/signaling.d.ts.map +1 -1
- package/lib/VoIP/signaling.js +791 -328
- package/lib/VoIP/signaling.js.map +1 -1
- package/lib/VoIP/types.d.ts +12 -11
- package/lib/VoIP/types.d.ts.map +1 -1
- package/lib/VoIP/types.js +4 -9
- package/lib/VoIP/types.js.map +1 -1
- package/lib/VoIP/video-feeder.d.ts +53 -0
- package/lib/VoIP/video-feeder.d.ts.map +1 -0
- package/lib/VoIP/video-feeder.js +269 -0
- package/lib/VoIP/video-feeder.js.map +1 -0
- package/lib/VoIP/wasm-engine.d.ts +10 -2
- package/lib/VoIP/wasm-engine.d.ts.map +1 -1
- package/lib/VoIP/wasm-engine.js +179 -71
- package/lib/VoIP/wasm-engine.js.map +1 -1
- package/lib/VoIP/worker-bootstrap.js +29 -51
- package/lib/VoIP/worker-bootstrap.js.map +1 -1
- package/lib/assets/wasm/README.md +76 -0
- package/lib/assets/wasm/loader.js +0 -1
- package/lib/assets/wasm/loader.js.map +1 -1
- package/lib/assets/wasm/worker-modules.js +0 -1
- package/lib/assets/wasm/worker-modules.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/package.json +146 -142
|
@@ -1,25 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
-
};
|
|
12
|
-
var _RelayRtcTransport_relayInfoById, _RelayRtcTransport_connections, _RelayRtcTransport_totals, _RelayRtcTransport_wrtcPromise, _RelayRtcTransport_getOrCreateConnection, _RelayRtcTransport_getOrCreateEarlyConnection, _RelayRtcTransport_ensureConnection, _RelayRtcTransport_connect, _RelayRtcTransport_restartIce, _RelayRtcTransport_handleIncomingPacket, _RelayRtcTransport_flushBufferedPackets, _RelayRtcTransport_sendBufferedPacket, _RelayRtcTransport_startIceRttPolling, _RelayRtcTransport_stopIceRttPolling, _RelayRtcTransport_readCurrentRoundTripTimeMs, _RelayRtcTransport_closeConnection, _RelayRtcTransport_closePeerObjects, _RelayRtcTransport_loadWrtc;
|
|
13
|
-
/**
|
|
14
|
-
* Relay transport.
|
|
15
|
-
*
|
|
16
|
-
* Tunnels UDP traffic to WhatsApp's edge relay servers via WebRTC data
|
|
17
|
-
* channels (using `@roamhq/wrtc`). Mirrors the browser client's behavior:
|
|
18
|
-
* pre-negotiated SCTP, custom DTLS fingerprint, ICE restart on idle.
|
|
19
|
-
*
|
|
20
|
-
* @author ShellTear
|
|
21
|
-
*/
|
|
22
|
-
import { appendFileSync } from "node:fs";
|
|
1
|
+
import { appendFileSync } from 'node:fs';
|
|
23
2
|
const RELAY_PROTO_UDP = 0;
|
|
24
3
|
const FAUX_WEB_CLIENT_RELAY_PORT = 3478;
|
|
25
4
|
const TRUE_WEB_CLIENT_RELAY_PORT = 3480;
|
|
@@ -29,7 +8,7 @@ const ICE_RTT_POLL_MS = 1000;
|
|
|
29
8
|
const MAX_BUFFER_SIZE = 256 * 1024;
|
|
30
9
|
const RELAY_PACKET_LOG_PATH = process.env.CALL_DUMP_RELAY_PACKETS_PATH ?? "";
|
|
31
10
|
const DISABLE_IPV6 = process.env.CALL_DISABLE_IPV6 !== "0";
|
|
32
|
-
const RELAY_PORT_MODE = process.env.CALL_RELAY_PORT_MODE === "
|
|
11
|
+
const RELAY_PORT_MODE = process.env.CALL_RELAY_PORT_MODE === "web" ? "web" : "original";
|
|
33
12
|
const USE_ORIGINAL_RELAY_PORTS = RELAY_PORT_MODE === "original";
|
|
34
13
|
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";
|
|
35
14
|
const getConnectionIdentifier = (ip, port) => ip.includes(":") ? `[${ip}]:${port}` : `${ip}:${port}`;
|
|
@@ -39,13 +18,14 @@ const createEmptyStats = () => ({
|
|
|
39
18
|
sentBytes: 0,
|
|
40
19
|
receivedBytes: 0,
|
|
41
20
|
droppedPackets: 0,
|
|
42
|
-
openConnections: 0
|
|
21
|
+
openConnections: 0
|
|
43
22
|
});
|
|
44
23
|
const getRelayLookupId = (ip, port) => getConnectionIdentifier(ip, port === TRUE_WEB_CLIENT_RELAY_PORT ? TRUE_WEB_CLIENT_RELAY_PORT : port);
|
|
45
|
-
const getRtcConnectPort =
|
|
46
|
-
const getVoipStackPort =
|
|
47
|
-
if (USE_ORIGINAL_RELAY_PORTS)
|
|
24
|
+
const getRtcConnectPort = info => info.ip === "157.240.24.133" ? FAUX_WEB_CLIENT_RELAY_PORT : info.port;
|
|
25
|
+
const getVoipStackPort = info => {
|
|
26
|
+
if (USE_ORIGINAL_RELAY_PORTS) {
|
|
48
27
|
return info.originalPort || info.port;
|
|
28
|
+
}
|
|
49
29
|
const sourcePort = info.originalPort || info.port;
|
|
50
30
|
return sourcePort === TRUE_WEB_CLIENT_RELAY_PORT
|
|
51
31
|
? TRUE_WEB_CLIENT_RELAY_PORT
|
|
@@ -68,17 +48,19 @@ const bufferPacket = (connection, packet) => {
|
|
|
68
48
|
connection.bufferedBytes += packet.byteLength;
|
|
69
49
|
return true;
|
|
70
50
|
};
|
|
71
|
-
const shiftPacket =
|
|
51
|
+
const shiftPacket = connection => {
|
|
72
52
|
const packet = connection.packetBuffer.shift() ?? null;
|
|
73
|
-
if (packet)
|
|
53
|
+
if (packet) {
|
|
74
54
|
connection.bufferedBytes -= packet.byteLength;
|
|
55
|
+
}
|
|
75
56
|
return packet;
|
|
76
57
|
};
|
|
77
58
|
const replaceIceCredentials = (sdp, ufrag, pwd) => sdp
|
|
78
59
|
.replace(/a=ice-ufrag:[^\r\n]+/g, `a=ice-ufrag:${ufrag}`)
|
|
79
60
|
.replace(/a=ice-pwd:[^\r\n]+/g, `a=ice-pwd:${pwd}`);
|
|
80
|
-
const replaceDtlsFingerprint = (sdp, algorithm, fingerprint) => sdp
|
|
81
|
-
|
|
61
|
+
const replaceDtlsFingerprint = (sdp, algorithm, fingerprint) => sdp
|
|
62
|
+
.replace(/a=fingerprint:[^\r\n]+/g, `a=fingerprint:${algorithm} ${fingerprint}`);
|
|
63
|
+
const removeIceCandidates = sdp => sdp
|
|
82
64
|
.replace(/a=candidate:[^\r\n]+\r?\n/g, "")
|
|
83
65
|
.replace(/a=end-of-candidates\r?\n?/g, "");
|
|
84
66
|
const appendRelayCandidate = (sdp, ip, port) => {
|
|
@@ -94,42 +76,50 @@ const buildRemoteRelayAnswer = (offerSdp, info) => {
|
|
|
94
76
|
answerSdp = answerSdp.replace(/a=max-message-size:[^\r\n]+/g, "a=max-message-size:1200");
|
|
95
77
|
return appendRelayCandidate(answerSdp, info.ip, info.port);
|
|
96
78
|
};
|
|
97
|
-
const toArrayBuffer =
|
|
79
|
+
const toArrayBuffer = data => {
|
|
98
80
|
const copy = new Uint8Array(data.byteLength);
|
|
99
81
|
copy.set(data);
|
|
100
82
|
return copy.buffer;
|
|
101
83
|
};
|
|
102
|
-
const toUint8Array =
|
|
103
|
-
if (data instanceof Uint8Array)
|
|
84
|
+
const toUint8Array = data => {
|
|
85
|
+
if (data instanceof Uint8Array) {
|
|
104
86
|
return new Uint8Array(data);
|
|
105
|
-
|
|
87
|
+
}
|
|
88
|
+
if (Buffer.isBuffer(data)) {
|
|
106
89
|
return new Uint8Array(data);
|
|
107
|
-
|
|
90
|
+
}
|
|
91
|
+
if (data instanceof ArrayBuffer) {
|
|
108
92
|
return new Uint8Array(data);
|
|
93
|
+
}
|
|
109
94
|
if (data && typeof data === "object" && "byteLength" in data && "buffer" in data) {
|
|
110
95
|
const view = data;
|
|
111
96
|
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
112
97
|
}
|
|
113
98
|
return null;
|
|
114
99
|
};
|
|
115
|
-
const classifyRelayPacket =
|
|
116
|
-
if (packet.byteLength < 2)
|
|
100
|
+
const classifyRelayPacket = packet => {
|
|
101
|
+
if (packet.byteLength < 2) {
|
|
117
102
|
return "non_stun";
|
|
103
|
+
}
|
|
118
104
|
const bytes = new Uint8Array(packet);
|
|
119
105
|
const first = bytes[0];
|
|
120
106
|
const second = bytes[1];
|
|
121
|
-
if ((first & 0xc0) !== 0)
|
|
107
|
+
if ((first & 0xc0) !== 0) {
|
|
122
108
|
return "non_stun";
|
|
109
|
+
}
|
|
123
110
|
const stunType = ((first & 0x3f) << 8) | second;
|
|
124
|
-
if (stunType === 0x0001)
|
|
111
|
+
if (stunType === 0x0001) {
|
|
125
112
|
return "stun_bind";
|
|
126
|
-
|
|
113
|
+
}
|
|
114
|
+
if (stunType === 0x0003) {
|
|
127
115
|
return "stun_alloc";
|
|
116
|
+
}
|
|
128
117
|
return "stun_unknown";
|
|
129
118
|
};
|
|
130
119
|
const appendRelayPacketLog = (direction, connection, packet) => {
|
|
131
|
-
if (!RELAY_PACKET_LOG_PATH)
|
|
120
|
+
if (!RELAY_PACKET_LOG_PATH) {
|
|
132
121
|
return;
|
|
122
|
+
}
|
|
133
123
|
const buffer = packet instanceof ArrayBuffer
|
|
134
124
|
? Buffer.from(new Uint8Array(packet))
|
|
135
125
|
: Buffer.from(packet.buffer, packet.byteOffset, packet.byteLength);
|
|
@@ -143,19 +133,21 @@ const appendRelayPacketLog = (direction, connection, packet) => {
|
|
|
143
133
|
rtcPort: connection.info.port,
|
|
144
134
|
voipPort: getVoipStackPort(connection.info),
|
|
145
135
|
size: buffer.byteLength,
|
|
146
|
-
hex: buffer.toString("hex")
|
|
136
|
+
hex: buffer.toString("hex")
|
|
147
137
|
}) + "\n");
|
|
148
138
|
};
|
|
149
139
|
export class RelayRtcTransport {
|
|
150
140
|
constructor(config) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.
|
|
141
|
+
this.config = config;
|
|
142
|
+
this._relayInfoById = new Map();
|
|
143
|
+
this._connections = new Map();
|
|
144
|
+
this._totals = createEmptyStats();
|
|
145
|
+
this._wrtcPromise = null;
|
|
146
|
+
this.updateRelayList = update => {
|
|
156
147
|
const nextInfoById = new Map();
|
|
157
148
|
for (const relay of update.relays ?? []) {
|
|
158
|
-
if (relay.token_id == null ||
|
|
149
|
+
if (relay.token_id == null ||
|
|
150
|
+
relay.token_id < 0 ||
|
|
159
151
|
relay.token_id >= (update.relay_tokens ?? []).length) {
|
|
160
152
|
continue;
|
|
161
153
|
}
|
|
@@ -163,8 +155,9 @@ export class RelayRtcTransport {
|
|
|
163
155
|
? update.auth_tokens[relay.auth_token_id]
|
|
164
156
|
: undefined;
|
|
165
157
|
for (const address of relay.addresses ?? []) {
|
|
166
|
-
if (address.protocol !== RELAY_PROTO_UDP)
|
|
158
|
+
if (address.protocol !== RELAY_PROTO_UDP) {
|
|
167
159
|
continue;
|
|
160
|
+
}
|
|
168
161
|
if (address.ipv4 && address.port != null) {
|
|
169
162
|
const clientPort = USE_ORIGINAL_RELAY_PORTS ? address.port : TRUE_WEB_CLIENT_RELAY_PORT;
|
|
170
163
|
const id = getConnectionIdentifier(address.ipv4, clientPort);
|
|
@@ -179,7 +172,7 @@ export class RelayRtcTransport {
|
|
|
179
172
|
authToken,
|
|
180
173
|
key: update.relay_key,
|
|
181
174
|
name: relay.relay_name,
|
|
182
|
-
enableEdgerayDtlsActiveMode: update.enable_edgeray_dtls_active_mode === true
|
|
175
|
+
enableEdgerayDtlsActiveMode: update.enable_edgeray_dtls_active_mode === true
|
|
183
176
|
});
|
|
184
177
|
}
|
|
185
178
|
if (!DISABLE_IPV6 && address.ipv6 && address.port_v6 != null) {
|
|
@@ -196,19 +189,20 @@ export class RelayRtcTransport {
|
|
|
196
189
|
authToken,
|
|
197
190
|
key: update.relay_key,
|
|
198
191
|
name: relay.relay_name,
|
|
199
|
-
enableEdgerayDtlsActiveMode: update.enable_edgeray_dtls_active_mode === true
|
|
192
|
+
enableEdgerayDtlsActiveMode: update.enable_edgeray_dtls_active_mode === true
|
|
200
193
|
});
|
|
201
194
|
}
|
|
202
195
|
}
|
|
203
196
|
}
|
|
204
|
-
for (const id of
|
|
205
|
-
if (!nextInfoById.has(id))
|
|
206
|
-
|
|
197
|
+
for (const id of this._relayInfoById.keys()) {
|
|
198
|
+
if (!nextInfoById.has(id)) {
|
|
199
|
+
this._closeConnection(id);
|
|
200
|
+
}
|
|
207
201
|
}
|
|
208
|
-
|
|
202
|
+
this._relayInfoById.clear();
|
|
209
203
|
for (const [id, info] of nextInfoById) {
|
|
210
|
-
|
|
211
|
-
void
|
|
204
|
+
this._relayInfoById.set(id, info);
|
|
205
|
+
void this._ensureConnection(info);
|
|
212
206
|
}
|
|
213
207
|
};
|
|
214
208
|
this.send = (packet, ip, port) => {
|
|
@@ -218,22 +212,24 @@ export class RelayRtcTransport {
|
|
|
218
212
|
requestedId,
|
|
219
213
|
getConnectionIdentifier(ip, preferredPort),
|
|
220
214
|
getRelayLookupId(ip, TRUE_WEB_CLIENT_RELAY_PORT),
|
|
221
|
-
getRelayLookupId(ip, FAUX_WEB_CLIENT_RELAY_PORT)
|
|
215
|
+
getRelayLookupId(ip, FAUX_WEB_CLIENT_RELAY_PORT)
|
|
222
216
|
];
|
|
223
217
|
let info;
|
|
224
218
|
for (const candidateId of candidateIds) {
|
|
225
|
-
info =
|
|
226
|
-
if (info)
|
|
219
|
+
info = this._relayInfoById.get(candidateId);
|
|
220
|
+
if (info) {
|
|
227
221
|
break;
|
|
222
|
+
}
|
|
228
223
|
}
|
|
229
224
|
if (!info) {
|
|
230
|
-
if (DISABLE_IPV6 && ip.includes(":"))
|
|
225
|
+
if (DISABLE_IPV6 && ip.includes(":")) {
|
|
231
226
|
return 0;
|
|
232
|
-
|
|
227
|
+
}
|
|
228
|
+
const earlyConnection = this._getOrCreateEarlyConnection(ip, port);
|
|
233
229
|
bufferPacket(earlyConnection, toArrayBuffer(packet));
|
|
234
230
|
return packet.byteLength;
|
|
235
231
|
}
|
|
236
|
-
const connection =
|
|
232
|
+
const connection = this._getOrCreateConnection(info);
|
|
237
233
|
const arrayBuffer = toArrayBuffer(packet);
|
|
238
234
|
if (classifyRelayPacket(arrayBuffer) === "stun_alloc" &&
|
|
239
235
|
connection.state === "open" &&
|
|
@@ -242,77 +238,110 @@ export class RelayRtcTransport {
|
|
|
242
238
|
connection.packetBuffer = [];
|
|
243
239
|
connection.bufferedBytes = 0;
|
|
244
240
|
bufferPacket(connection, arrayBuffer);
|
|
245
|
-
void
|
|
241
|
+
void this._restartIce(connection);
|
|
246
242
|
return packet.byteLength;
|
|
247
243
|
}
|
|
248
244
|
if (connection.state === "open" && connection.dataChannel?.readyState === "open") {
|
|
249
|
-
if (!
|
|
245
|
+
if (!this._sendBufferedPacket(connection, arrayBuffer)) {
|
|
250
246
|
connection.stats.droppedPackets += 1;
|
|
251
247
|
}
|
|
252
248
|
return packet.byteLength;
|
|
253
249
|
}
|
|
254
250
|
bufferPacket(connection, arrayBuffer);
|
|
255
|
-
void
|
|
251
|
+
void this._ensureConnection(info);
|
|
256
252
|
return packet.byteLength;
|
|
257
253
|
};
|
|
258
254
|
this.getStats = () => {
|
|
259
255
|
let openConnections = 0;
|
|
260
|
-
for (const connection of
|
|
261
|
-
if (connection.state === "open")
|
|
256
|
+
for (const connection of this._connections.values()) {
|
|
257
|
+
if (connection.state === "open") {
|
|
262
258
|
openConnections += 1;
|
|
259
|
+
}
|
|
263
260
|
}
|
|
264
|
-
return {
|
|
261
|
+
return {
|
|
262
|
+
...this._totals,
|
|
263
|
+
openConnections
|
|
264
|
+
};
|
|
265
265
|
};
|
|
266
266
|
this.closeAll = async () => {
|
|
267
|
-
for (const id of [...
|
|
268
|
-
|
|
267
|
+
for (const id of [...this._connections.keys()]) {
|
|
268
|
+
this._closeConnection(id);
|
|
269
|
+
}
|
|
269
270
|
};
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const existing = __classPrivateFieldGet(this, _RelayRtcTransport_connections, "f").get(info.id);
|
|
271
|
+
this._getOrCreateConnection = info => {
|
|
272
|
+
const existing = this._connections.get(info.id);
|
|
273
273
|
if (existing) {
|
|
274
274
|
existing.info = info;
|
|
275
275
|
return existing;
|
|
276
276
|
}
|
|
277
277
|
const created = {
|
|
278
|
-
info,
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
278
|
+
info,
|
|
279
|
+
state: "none",
|
|
280
|
+
peerConnection: null,
|
|
281
|
+
dataChannel: null,
|
|
282
|
+
iceCandidate: null,
|
|
283
|
+
packetBuffer: [],
|
|
284
|
+
bufferedBytes: 0,
|
|
285
|
+
connectPromise: null,
|
|
286
|
+
connectionTimeout: null,
|
|
287
|
+
iceStatsInterval: null,
|
|
288
|
+
lastIceRttMs: null,
|
|
289
|
+
hasReceivedFirstPacket: false,
|
|
290
|
+
hasNonStunPacketSent: false,
|
|
291
|
+
sentMedia: false,
|
|
292
|
+
lastRxPacketTime: 0,
|
|
293
|
+
isReconnecting: false,
|
|
294
|
+
stats: createEmptyStats()
|
|
284
295
|
};
|
|
285
|
-
|
|
296
|
+
this._connections.set(info.id, created);
|
|
286
297
|
return created;
|
|
287
|
-
}
|
|
288
|
-
|
|
298
|
+
};
|
|
299
|
+
this._getOrCreateEarlyConnection = (ip, port) => {
|
|
289
300
|
const id = getConnectionIdentifier(ip, port);
|
|
290
|
-
const existing =
|
|
291
|
-
if (existing)
|
|
301
|
+
const existing = this._connections.get(id);
|
|
302
|
+
if (existing) {
|
|
292
303
|
return existing;
|
|
304
|
+
}
|
|
293
305
|
const placeholderInfo = {
|
|
294
|
-
id,
|
|
295
|
-
|
|
296
|
-
|
|
306
|
+
id,
|
|
307
|
+
relayId: 0,
|
|
308
|
+
ip,
|
|
309
|
+
port,
|
|
310
|
+
originalPort: port,
|
|
311
|
+
isIPv6: ip.includes(":"),
|
|
312
|
+
token: "",
|
|
313
|
+
key: "",
|
|
314
|
+
name: "early-packet",
|
|
315
|
+
enableEdgerayDtlsActiveMode: false
|
|
297
316
|
};
|
|
298
317
|
const created = {
|
|
299
|
-
info: placeholderInfo,
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
318
|
+
info: placeholderInfo,
|
|
319
|
+
state: "none",
|
|
320
|
+
peerConnection: null,
|
|
321
|
+
dataChannel: null,
|
|
322
|
+
iceCandidate: null,
|
|
323
|
+
packetBuffer: [],
|
|
324
|
+
bufferedBytes: 0,
|
|
325
|
+
connectPromise: null,
|
|
326
|
+
connectionTimeout: null,
|
|
327
|
+
iceStatsInterval: null,
|
|
328
|
+
lastIceRttMs: null,
|
|
329
|
+
hasReceivedFirstPacket: false,
|
|
330
|
+
hasNonStunPacketSent: false,
|
|
331
|
+
sentMedia: false,
|
|
332
|
+
lastRxPacketTime: 0,
|
|
333
|
+
isReconnecting: false,
|
|
334
|
+
stats: createEmptyStats()
|
|
306
335
|
};
|
|
307
|
-
|
|
336
|
+
this._connections.set(id, created);
|
|
308
337
|
return created;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const connection =
|
|
338
|
+
};
|
|
339
|
+
this._ensureConnection = async (info) => {
|
|
340
|
+
const connection = this._getOrCreateConnection(info);
|
|
312
341
|
if (connection.state === "open" || connection.state === "connecting") {
|
|
313
342
|
return connection.connectPromise ?? Promise.resolve();
|
|
314
343
|
}
|
|
315
|
-
const promise =
|
|
344
|
+
const promise = this._connect(connection);
|
|
316
345
|
connection.connectPromise = promise;
|
|
317
346
|
try {
|
|
318
347
|
await promise;
|
|
@@ -320,18 +349,22 @@ export class RelayRtcTransport {
|
|
|
320
349
|
finally {
|
|
321
350
|
connection.connectPromise = null;
|
|
322
351
|
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
const wrtcModule = await
|
|
352
|
+
};
|
|
353
|
+
this._connect = async (connection) => {
|
|
354
|
+
const wrtcModule = await this._loadWrtc();
|
|
326
355
|
const { RTCPeerConnection } = wrtcModule;
|
|
327
356
|
if (typeof RTCPeerConnection !== "function") {
|
|
328
357
|
throw new Error("RTCPeerConnection unavailable from @roamhq/wrtc");
|
|
329
358
|
}
|
|
330
|
-
|
|
359
|
+
this._closePeerObjects(connection);
|
|
331
360
|
connection.state = "connecting";
|
|
332
361
|
const pc = new RTCPeerConnection();
|
|
333
362
|
const dc = pc.createDataChannel("pre-negotiated", {
|
|
334
|
-
negotiated: true,
|
|
363
|
+
negotiated: true,
|
|
364
|
+
id: 0,
|
|
365
|
+
ordered: false,
|
|
366
|
+
maxRetransmits: 0,
|
|
367
|
+
priority: "high"
|
|
335
368
|
});
|
|
336
369
|
connection.peerConnection = pc;
|
|
337
370
|
connection.dataChannel = dc;
|
|
@@ -343,20 +376,21 @@ export class RelayRtcTransport {
|
|
|
343
376
|
clearTimeout(connection.connectionTimeout);
|
|
344
377
|
connection.connectionTimeout = null;
|
|
345
378
|
}
|
|
346
|
-
|
|
347
|
-
|
|
379
|
+
this._flushBufferedPackets(connection);
|
|
380
|
+
this._startIceRttPolling(connection);
|
|
348
381
|
};
|
|
349
382
|
dc.onclose = () => {
|
|
350
|
-
if (connection.state !== "failed")
|
|
383
|
+
if (connection.state !== "failed") {
|
|
351
384
|
connection.state = "closed";
|
|
385
|
+
}
|
|
352
386
|
};
|
|
353
387
|
dc.onerror = () => {
|
|
354
388
|
connection.state = "failed";
|
|
355
389
|
};
|
|
356
|
-
dc.onmessage =
|
|
357
|
-
|
|
390
|
+
dc.onmessage = event => {
|
|
391
|
+
this._handleIncomingPacket(connection, event.data);
|
|
358
392
|
};
|
|
359
|
-
pc.onicecandidate =
|
|
393
|
+
pc.onicecandidate = event => {
|
|
360
394
|
if (event.candidate?.candidate && !connection.iceCandidate) {
|
|
361
395
|
connection.iceCandidate = event.candidate.candidate;
|
|
362
396
|
}
|
|
@@ -369,30 +403,39 @@ export class RelayRtcTransport {
|
|
|
369
403
|
connection.connectionTimeout = setTimeout(() => {
|
|
370
404
|
if (connection.state === "connecting") {
|
|
371
405
|
connection.state = "failed";
|
|
372
|
-
|
|
406
|
+
this._closePeerObjects(connection);
|
|
373
407
|
}
|
|
374
408
|
}, CONNECTION_TIMEOUT_MS);
|
|
375
409
|
const offer = await pc.createOffer();
|
|
376
410
|
await pc.setLocalDescription(offer);
|
|
377
411
|
const remoteSdp = buildRemoteRelayAnswer(offer.sdp ?? "", {
|
|
378
412
|
...connection.info,
|
|
379
|
-
port: getRtcConnectPort(connection.info)
|
|
413
|
+
port: getRtcConnectPort(connection.info)
|
|
414
|
+
});
|
|
415
|
+
await pc.setRemoteDescription({
|
|
416
|
+
type: "answer",
|
|
417
|
+
sdp: remoteSdp
|
|
380
418
|
});
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
if (connection.isReconnecting || !connection.hasNonStunPacketSent)
|
|
419
|
+
};
|
|
420
|
+
this._restartIce = async (connection) => {
|
|
421
|
+
if (connection.isReconnecting || !connection.hasNonStunPacketSent) {
|
|
385
422
|
return;
|
|
386
|
-
|
|
387
|
-
|
|
423
|
+
}
|
|
424
|
+
const { RTCPeerConnection } = await this._loadWrtc();
|
|
425
|
+
if (typeof RTCPeerConnection !== "function") {
|
|
388
426
|
return;
|
|
427
|
+
}
|
|
389
428
|
connection.isReconnecting = true;
|
|
390
429
|
try {
|
|
391
|
-
|
|
430
|
+
this._closePeerObjects(connection);
|
|
392
431
|
connection.state = "connecting";
|
|
393
432
|
const pc = new RTCPeerConnection();
|
|
394
433
|
const dc = pc.createDataChannel("pre-negotiated", {
|
|
395
|
-
negotiated: true,
|
|
434
|
+
negotiated: true,
|
|
435
|
+
id: 0,
|
|
436
|
+
ordered: false,
|
|
437
|
+
maxRetransmits: 0,
|
|
438
|
+
priority: "high"
|
|
396
439
|
});
|
|
397
440
|
connection.peerConnection = pc;
|
|
398
441
|
connection.dataChannel = dc;
|
|
@@ -400,33 +443,44 @@ export class RelayRtcTransport {
|
|
|
400
443
|
dc.onopen = () => {
|
|
401
444
|
connection.state = "open";
|
|
402
445
|
connection.isReconnecting = false;
|
|
403
|
-
|
|
404
|
-
|
|
446
|
+
this._flushBufferedPackets(connection);
|
|
447
|
+
this._startIceRttPolling(connection);
|
|
405
448
|
};
|
|
406
449
|
dc.onclose = () => {
|
|
407
|
-
if (connection.state !== "failed")
|
|
450
|
+
if (connection.state !== "failed") {
|
|
408
451
|
connection.state = "closed";
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
dc.onerror = () => {
|
|
455
|
+
connection.state = "failed";
|
|
409
456
|
};
|
|
410
|
-
dc.
|
|
411
|
-
|
|
412
|
-
__classPrivateFieldGet(this, _RelayRtcTransport_handleIncomingPacket, "f").call(this, connection, event.data);
|
|
457
|
+
dc.onmessage = event => {
|
|
458
|
+
this._handleIncomingPacket(connection, event.data);
|
|
413
459
|
};
|
|
414
|
-
pc.onicecandidate =
|
|
460
|
+
pc.onicecandidate = event => {
|
|
415
461
|
if (event.candidate?.candidate && !connection.iceCandidate) {
|
|
416
462
|
connection.iceCandidate = event.candidate.candidate;
|
|
417
463
|
}
|
|
418
464
|
};
|
|
419
|
-
const offer = await pc.createOffer({
|
|
465
|
+
const offer = await pc.createOffer({
|
|
466
|
+
iceRestart: false
|
|
467
|
+
});
|
|
420
468
|
let localSdp = offer.sdp ?? "";
|
|
421
469
|
if (connection.iceCandidate) {
|
|
422
470
|
localSdp = `${removeIceCandidates(localSdp)}a=${connection.iceCandidate}\r\na=end-of-candidates\r\n`;
|
|
423
471
|
}
|
|
424
|
-
await pc.setLocalDescription({
|
|
472
|
+
await pc.setLocalDescription({
|
|
473
|
+
type: "offer",
|
|
474
|
+
sdp: localSdp
|
|
475
|
+
});
|
|
425
476
|
const remoteSdp = buildRemoteRelayAnswer(localSdp, {
|
|
426
477
|
...connection.info,
|
|
427
|
-
port: getRtcConnectPort(connection.info)
|
|
478
|
+
port: getRtcConnectPort(connection.info)
|
|
479
|
+
});
|
|
480
|
+
await pc.setRemoteDescription({
|
|
481
|
+
type: "answer",
|
|
482
|
+
sdp: remoteSdp
|
|
428
483
|
});
|
|
429
|
-
await pc.setRemoteDescription({ type: "answer", sdp: remoteSdp });
|
|
430
484
|
}
|
|
431
485
|
catch {
|
|
432
486
|
connection.state = "failed";
|
|
@@ -434,34 +488,36 @@ export class RelayRtcTransport {
|
|
|
434
488
|
finally {
|
|
435
489
|
connection.isReconnecting = false;
|
|
436
490
|
}
|
|
437
|
-
}
|
|
438
|
-
|
|
491
|
+
};
|
|
492
|
+
this._handleIncomingPacket = (connection, raw) => {
|
|
439
493
|
const packet = toUint8Array(raw);
|
|
440
|
-
if (!packet)
|
|
494
|
+
if (!packet) {
|
|
441
495
|
return;
|
|
496
|
+
}
|
|
442
497
|
connection.stats.receivedPackets += 1;
|
|
443
498
|
connection.stats.receivedBytes += packet.byteLength;
|
|
444
499
|
connection.hasReceivedFirstPacket = true;
|
|
445
500
|
connection.lastRxPacketTime = Date.now();
|
|
446
|
-
|
|
447
|
-
|
|
501
|
+
this._totals.receivedPackets += 1;
|
|
502
|
+
this._totals.receivedBytes += packet.byteLength;
|
|
448
503
|
appendRelayPacketLog("recv", connection, packet);
|
|
449
504
|
this.config.onTransportMessage(packet, connection.info.ip, getVoipStackPort(connection.info));
|
|
450
|
-
}
|
|
451
|
-
|
|
505
|
+
};
|
|
506
|
+
this._flushBufferedPackets = connection => {
|
|
452
507
|
while (connection.state === "open" &&
|
|
453
508
|
connection.dataChannel?.readyState === "open" &&
|
|
454
509
|
connection.packetBuffer.length > 0) {
|
|
455
510
|
const packet = shiftPacket(connection);
|
|
456
|
-
if (!packet)
|
|
511
|
+
if (!packet) {
|
|
457
512
|
break;
|
|
458
|
-
|
|
513
|
+
}
|
|
514
|
+
if (!this._sendBufferedPacket(connection, packet)) {
|
|
459
515
|
connection.stats.droppedPackets += 1;
|
|
460
516
|
break;
|
|
461
517
|
}
|
|
462
518
|
}
|
|
463
|
-
}
|
|
464
|
-
|
|
519
|
+
};
|
|
520
|
+
this._sendBufferedPacket = (connection, packet) => {
|
|
465
521
|
try {
|
|
466
522
|
connection.dataChannel?.send(packet);
|
|
467
523
|
if (classifyRelayPacket(packet) === "non_stun") {
|
|
@@ -470,46 +526,53 @@ export class RelayRtcTransport {
|
|
|
470
526
|
}
|
|
471
527
|
connection.stats.sentPackets += 1;
|
|
472
528
|
connection.stats.sentBytes += packet.byteLength;
|
|
473
|
-
|
|
474
|
-
|
|
529
|
+
this._totals.sentPackets += 1;
|
|
530
|
+
this._totals.sentBytes += packet.byteLength;
|
|
475
531
|
appendRelayPacketLog("send", connection, packet);
|
|
476
532
|
return true;
|
|
477
533
|
}
|
|
478
534
|
catch {
|
|
479
535
|
return false;
|
|
480
536
|
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
|
|
537
|
+
};
|
|
538
|
+
this._startIceRttPolling = connection => {
|
|
539
|
+
this._stopIceRttPolling(connection);
|
|
484
540
|
const pc = connection.peerConnection;
|
|
485
|
-
if (!pc || typeof pc.getStats !== "function" || !this.config.onIceRtt)
|
|
541
|
+
if (!pc || typeof pc.getStats !== "function" || !this.config.onIceRtt) {
|
|
486
542
|
return;
|
|
543
|
+
}
|
|
487
544
|
const poll = async () => {
|
|
488
|
-
const rttMs = await
|
|
489
|
-
if (rttMs == null || connection.lastIceRttMs === rttMs)
|
|
545
|
+
const rttMs = await this._readCurrentRoundTripTimeMs(pc);
|
|
546
|
+
if (rttMs == null || connection.lastIceRttMs === rttMs) {
|
|
490
547
|
return;
|
|
548
|
+
}
|
|
491
549
|
connection.lastIceRttMs = rttMs;
|
|
492
550
|
this.config.onIceRtt?.(rttMs, connection.info.ip, connection.info.port);
|
|
493
551
|
};
|
|
494
552
|
void poll();
|
|
495
|
-
connection.iceStatsInterval = setInterval(() => {
|
|
553
|
+
connection.iceStatsInterval = setInterval(() => {
|
|
554
|
+
void poll();
|
|
555
|
+
}, ICE_RTT_POLL_MS);
|
|
496
556
|
connection.iceStatsInterval.unref?.();
|
|
497
|
-
}
|
|
498
|
-
|
|
557
|
+
};
|
|
558
|
+
this._stopIceRttPolling = connection => {
|
|
499
559
|
if (connection.iceStatsInterval) {
|
|
500
560
|
clearInterval(connection.iceStatsInterval);
|
|
501
561
|
connection.iceStatsInterval = null;
|
|
502
562
|
}
|
|
503
563
|
connection.lastIceRttMs = null;
|
|
504
|
-
}
|
|
505
|
-
|
|
564
|
+
};
|
|
565
|
+
this._readCurrentRoundTripTimeMs = async (pc) => {
|
|
506
566
|
try {
|
|
507
567
|
const stats = await pc.getStats();
|
|
508
568
|
const reports = stats && typeof stats.values === "function"
|
|
509
569
|
? Array.from(stats.values())
|
|
510
|
-
: Array.isArray(stats)
|
|
511
|
-
|
|
570
|
+
: Array.isArray(stats)
|
|
571
|
+
? stats
|
|
572
|
+
: Object.values(stats ?? {});
|
|
573
|
+
if (!reports.length) {
|
|
512
574
|
return null;
|
|
575
|
+
}
|
|
513
576
|
let selectedCandidatePairId = "";
|
|
514
577
|
for (const report of reports) {
|
|
515
578
|
if (report?.type === "transport" && typeof report.selectedCandidatePairId === "string") {
|
|
@@ -517,9 +580,9 @@ export class RelayRtcTransport {
|
|
|
517
580
|
break;
|
|
518
581
|
}
|
|
519
582
|
}
|
|
520
|
-
const candidatePairs = reports.filter(
|
|
521
|
-
const selectedPair =
|
|
522
|
-
candidatePairs.find(
|
|
583
|
+
const candidatePairs = reports.filter(report => report?.type === "candidate-pair");
|
|
584
|
+
const selectedPair = selectedCandidatePairId && candidatePairs.find(report => report?.id === selectedCandidatePairId) ||
|
|
585
|
+
candidatePairs.find(report => report?.selected || report?.nominated) ||
|
|
523
586
|
candidatePairs[0];
|
|
524
587
|
const rttSeconds = typeof selectedPair?.currentRoundTripTime === "number"
|
|
525
588
|
? selectedPair.currentRoundTripTime
|
|
@@ -528,30 +591,32 @@ export class RelayRtcTransport {
|
|
|
528
591
|
selectedPair.responsesReceived > 0
|
|
529
592
|
? selectedPair.totalRoundTripTime / selectedPair.responsesReceived
|
|
530
593
|
: null;
|
|
531
|
-
if (rttSeconds == null || !Number.isFinite(rttSeconds) || rttSeconds <= 0)
|
|
594
|
+
if (rttSeconds == null || !Number.isFinite(rttSeconds) || rttSeconds <= 0) {
|
|
532
595
|
return null;
|
|
596
|
+
}
|
|
533
597
|
return Math.max(1, Math.round(rttSeconds * 1000));
|
|
534
598
|
}
|
|
535
599
|
catch {
|
|
536
600
|
return null;
|
|
537
601
|
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
const connection =
|
|
541
|
-
if (!connection)
|
|
602
|
+
};
|
|
603
|
+
this._closeConnection = id => {
|
|
604
|
+
const connection = this._connections.get(id);
|
|
605
|
+
if (!connection) {
|
|
542
606
|
return;
|
|
607
|
+
}
|
|
543
608
|
if (connection.connectionTimeout) {
|
|
544
609
|
clearTimeout(connection.connectionTimeout);
|
|
545
610
|
connection.connectionTimeout = null;
|
|
546
611
|
}
|
|
547
|
-
|
|
612
|
+
this._closePeerObjects(connection);
|
|
548
613
|
connection.state = "closed";
|
|
549
614
|
connection.packetBuffer = [];
|
|
550
615
|
connection.bufferedBytes = 0;
|
|
551
|
-
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
|
|
616
|
+
this._connections.delete(id);
|
|
617
|
+
};
|
|
618
|
+
this._closePeerObjects = connection => {
|
|
619
|
+
this._stopIceRttPolling(connection);
|
|
555
620
|
try {
|
|
556
621
|
connection.dataChannel?.close?.();
|
|
557
622
|
}
|
|
@@ -562,15 +627,26 @@ export class RelayRtcTransport {
|
|
|
562
627
|
catch { }
|
|
563
628
|
connection.dataChannel = null;
|
|
564
629
|
connection.peerConnection = null;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
if (
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
630
|
+
};
|
|
631
|
+
this._loadWrtc = async () => {
|
|
632
|
+
if (this._wrtcPromise)
|
|
633
|
+
return this._wrtcPromise;
|
|
634
|
+
this._wrtcPromise = (async () => {
|
|
635
|
+
if (typeof globalThis.RTCPeerConnection !== 'undefined') {
|
|
636
|
+
return globalThis;
|
|
637
|
+
}
|
|
638
|
+
for (const pkg of ['@roamhq/wrtc', 'wrtc', '@koush/wrtc']) {
|
|
639
|
+
try {
|
|
640
|
+
const mod = await import(pkg);
|
|
641
|
+
return mod.default ?? mod;
|
|
642
|
+
}
|
|
643
|
+
catch { }
|
|
644
|
+
}
|
|
645
|
+
throw new Error('WebRTC support requires "@roamhq/wrtc" or "wrtc". Please install it via: npm install @roamhq/wrtc');
|
|
646
|
+
})();
|
|
647
|
+
return this._wrtcPromise;
|
|
648
|
+
};
|
|
573
649
|
}
|
|
574
650
|
}
|
|
575
|
-
|
|
651
|
+
export default RelayRtcTransport;
|
|
576
652
|
//# sourceMappingURL=relay-transport.js.map
|