asyx 0.3.1 → 0.3.3
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/package.json +1 -1
- package/src/commands/mqtt.js +7 -7
- package/src/commands/sts.js +4 -4
- package/src/commands/tunnel.js +4 -4
- package/src/lib/device.js +15 -7
- package/src/tunnel/udp-tunnel.js +179 -141
package/package.json
CHANGED
package/src/commands/mqtt.js
CHANGED
|
@@ -2,11 +2,9 @@ import { constants as fsConstants } from 'node:fs';
|
|
|
2
2
|
import { access } from 'node:fs/promises';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { AsyxMqttClient, controlWildcard, controlOpen, controlClose, controlStatus } from "@asyx/mqtt-client";
|
|
5
|
-
import { loadOrCreateClientId } from "../lib/device.js";
|
|
5
|
+
import { loadOrCreateClientId, getDeviceJsonPath } from "../lib/device.js";
|
|
6
6
|
import { getSharedTransport } from "../lib/shared-transport.js";
|
|
7
7
|
|
|
8
|
-
const DEVICE_JSON_PATH = join(process.env.HOME ?? '', '.asyx', 'device.json');
|
|
9
|
-
|
|
10
8
|
export default async function mqtt() {
|
|
11
9
|
// Dev-only command gating
|
|
12
10
|
const isDev = process.env.NODE_ENV === 'dev';
|
|
@@ -15,8 +13,9 @@ export default async function mqtt() {
|
|
|
15
13
|
process.exit(2);
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
// Device credential guard
|
|
19
|
-
|
|
16
|
+
// Device credential guard - use same path resolution as device.js
|
|
17
|
+
const deviceJsonPath = getDeviceJsonPath();
|
|
18
|
+
await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
|
|
20
19
|
console.error('This command is for dev use only. Missing ~/.asyx/device.json');
|
|
21
20
|
process.exit(2);
|
|
22
21
|
});
|
|
@@ -138,8 +137,9 @@ async function connect() {
|
|
|
138
137
|
const { readFile } = await import('node:fs/promises');
|
|
139
138
|
const { join: pathJoin } = await import('node:path');
|
|
140
139
|
|
|
141
|
-
|
|
142
|
-
const
|
|
140
|
+
// Use same path resolution as device.js for consistency
|
|
141
|
+
const deviceJsonPath = getDeviceJsonPath();
|
|
142
|
+
const devJson = await readFile(deviceJsonPath, "utf8");
|
|
143
143
|
const dev = JSON.parse(devJson);
|
|
144
144
|
const clientId = dev.clientId;
|
|
145
145
|
const cli = new AsyxMqttClient(t, { clientId });
|
package/src/commands/sts.js
CHANGED
|
@@ -6,14 +6,14 @@ import fssync from 'node:fs';
|
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import os from 'node:os';
|
|
8
8
|
import { ensureKeyAndCsr } from '../lib/csr.js';
|
|
9
|
-
import { loadOrCreateClientId } from '../lib/device.js';
|
|
9
|
+
import { loadOrCreateClientId, getDeviceJsonPath } from '../lib/device.js';
|
|
10
10
|
|
|
11
11
|
const STS_URL = process.env.ASYX_STS_URL || 'https://www.asyx.ai/www/sts';
|
|
12
|
-
const DEVICE_JSON_PATH = join(process.env.HOME ?? '', '.asyx', 'device.json');
|
|
13
12
|
|
|
14
13
|
export default async function cmdSts(argv = process.argv.slice(2)) {
|
|
15
|
-
// Device credential guard
|
|
16
|
-
|
|
14
|
+
// Device credential guard - use same path resolution as device.js
|
|
15
|
+
const deviceJsonPath = getDeviceJsonPath();
|
|
16
|
+
await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
|
|
17
17
|
console.error('This command is for dev use only. Missing ~/.asyx/device.json');
|
|
18
18
|
process.exit(2);
|
|
19
19
|
});
|
package/src/commands/tunnel.js
CHANGED
|
@@ -9,8 +9,7 @@ import crypto from "node:crypto";
|
|
|
9
9
|
import logger from "../lib/logger.js";
|
|
10
10
|
import { dispatch as dispatchEvt } from "../mqtt/events/index.js";
|
|
11
11
|
import { MessageIngress } from "../mqtt/ingress.js";
|
|
12
|
-
|
|
13
|
-
const DEVICE_JSON_PATH = join(process.env.HOME ?? '', '.asyx', 'device.json');
|
|
12
|
+
import { getDeviceJsonPath } from "../lib/device.js";
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* QPA-CLI-09: Publish refresh request with ECONNRESET error handling.
|
|
@@ -78,8 +77,9 @@ export async function publishRefreshRequestWithErrorHandling(
|
|
|
78
77
|
}
|
|
79
78
|
|
|
80
79
|
export default async function tunnel(args = []) {
|
|
81
|
-
// Device credential guard
|
|
82
|
-
|
|
80
|
+
// Device credential guard - use same path resolution as device.js
|
|
81
|
+
const deviceJsonPath = getDeviceJsonPath();
|
|
82
|
+
await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
|
|
83
83
|
console.error('This command is for dev use only. Missing ~/.asyx/device.json');
|
|
84
84
|
process.exit(2);
|
|
85
85
|
});
|
package/src/lib/device.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as fs from "fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import crypto from "node:crypto";
|
|
@@ -26,18 +26,26 @@ const DIR = resolveConfigDir();
|
|
|
26
26
|
const FILE = join(DIR, "device.json");
|
|
27
27
|
|
|
28
28
|
export function loadOrCreateClientId() {
|
|
29
|
-
if (!existsSync(DIR)) mkdirSync(DIR, { recursive: true });
|
|
30
|
-
if (existsSync(FILE)) {
|
|
31
|
-
const raw = JSON.parse(readFileSync(FILE, "utf8"));
|
|
29
|
+
if (!fs.existsSync(DIR)) fs.mkdirSync(DIR, { recursive: true });
|
|
30
|
+
if (fs.existsSync(FILE)) {
|
|
31
|
+
const raw = JSON.parse(fs.readFileSync(FILE, "utf8"));
|
|
32
32
|
if (raw?.clientId) return raw.clientId;
|
|
33
33
|
}
|
|
34
34
|
const clientId = "cli_" + crypto.randomUUID().replace(/-/g, "").slice(0, 12).toUpperCase();
|
|
35
|
-
writeFileSync(FILE, JSON.stringify({ clientId }, null, 2));
|
|
35
|
+
fs.writeFileSync(FILE, JSON.stringify({ clientId }, null, 2));
|
|
36
36
|
return clientId;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export function resetClientId() {
|
|
40
|
-
if (existsSync(FILE)) {
|
|
41
|
-
writeFileSync(FILE, JSON.stringify({}, null, 2));
|
|
40
|
+
if (fs.existsSync(FILE)) {
|
|
41
|
+
fs.writeFileSync(FILE, JSON.stringify({}, null, 2));
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get the path to device.json using the same resolution logic as loadOrCreateClientId
|
|
47
|
+
* This ensures consistent path resolution across all commands (tunnel, setup, etc.)
|
|
48
|
+
*/
|
|
49
|
+
export function getDeviceJsonPath() {
|
|
50
|
+
return FILE;
|
|
51
|
+
}
|
package/src/tunnel/udp-tunnel.js
CHANGED
|
@@ -38,7 +38,7 @@ export class UdpTunnel {
|
|
|
38
38
|
this.relayId = relayId; // AC5.1: Store relayId for JWT validation
|
|
39
39
|
this.log = log;
|
|
40
40
|
this.ws = null;
|
|
41
|
-
|
|
41
|
+
// UDP-S14: Per-flow sockets managed via flowMap (no single udpSocket)
|
|
42
42
|
|
|
43
43
|
// Coordinator connection (not used for UDP refresh yet)
|
|
44
44
|
this.coordinatorCore = coordinatorCore;
|
|
@@ -51,10 +51,11 @@ export class UdpTunnel {
|
|
|
51
51
|
this.getForceReconnect = getForceReconnect;
|
|
52
52
|
this.controlPlaneTimeoutMs = Number(controlPlaneTimeoutMs) || 60000;
|
|
53
53
|
|
|
54
|
-
// UDP-
|
|
55
|
-
// flowId -> lastSeen
|
|
56
|
-
this.flowMap = new Map(); // flowId -> { lastSeen }
|
|
57
|
-
|
|
54
|
+
// UDP-S14: Per-flow socket mapping for correct return path routing
|
|
55
|
+
// flowId -> { socket, lastSeen } - one UDP socket per flowId, connected to targetHost:targetPort
|
|
56
|
+
this.flowMap = new Map(); // flowId -> { socket, lastSeen }
|
|
57
|
+
// Reverse mapping: socket -> flowId (for message handlers)
|
|
58
|
+
this.socketToFlowId = new WeakMap(); // socket -> flowId
|
|
58
59
|
|
|
59
60
|
// UDP-S1: Binary frame buffer for partial frames
|
|
60
61
|
this.frameBuffer = Buffer.alloc(0);
|
|
@@ -195,74 +196,10 @@ export class UdpTunnel {
|
|
|
195
196
|
// UDP-S1: Start flow cleanup timer
|
|
196
197
|
this.startFlowCleanup();
|
|
197
198
|
|
|
198
|
-
// UDP-
|
|
199
|
-
//
|
|
200
|
-
this.udpSocket = dgram.createSocket('udp4');
|
|
199
|
+
// UDP-S14: Per-flow sockets are created on-demand in handleUdpDatagram
|
|
200
|
+
// No global socket initialization needed - each flowId gets its own socket
|
|
201
201
|
|
|
202
|
-
|
|
203
|
-
// Use a flag to prevent double resolution/rejection
|
|
204
|
-
let bindResolved = false;
|
|
205
|
-
this.udpSocket.once('error', (err) => {
|
|
206
|
-
if (bindResolved) return;
|
|
207
|
-
bindResolved = true;
|
|
208
|
-
this.log.error({ error: err.message, code: err.code }, 'udp.socket.bind.failed');
|
|
209
|
-
const error = new Error(`Failed to create ephemeral UDP socket: ${err.message}`);
|
|
210
|
-
error.code = 'BIND_FAILED';
|
|
211
|
-
reject(error);
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// UDP-S13: Bind to 0.0.0.0:0 (ephemeral port, OS assigns)
|
|
215
|
-
// Note: bind callback doesn't receive error parameter - errors are emitted via 'error' event
|
|
216
|
-
this.udpSocket.bind(0, '0.0.0.0', () => {
|
|
217
|
-
// If bind error already occurred, don't proceed
|
|
218
|
-
if (bindResolved) return;
|
|
219
|
-
bindResolved = true;
|
|
220
|
-
|
|
221
|
-
const address = this.udpSocket.address();
|
|
222
|
-
this.log.debug({
|
|
223
|
-
localAddress: address.address,
|
|
224
|
-
localPort: address.port,
|
|
225
|
-
targetHost: this.targetHost,
|
|
226
|
-
targetPort: this.targetPort
|
|
227
|
-
}, 'udp.socket.bound.ephemeral');
|
|
228
|
-
|
|
229
|
-
// UDP-S13: Connect socket to targetHost:targetPort (app's listening port)
|
|
230
|
-
// This allows us to use send() without specifying address each time
|
|
231
|
-
// and ensures we receive responses from the app
|
|
232
|
-
try {
|
|
233
|
-
this.udpSocket.connect(this.targetPort, this.targetHost, () => {
|
|
234
|
-
this.log.debug({
|
|
235
|
-
targetHost: this.targetHost,
|
|
236
|
-
targetPort: this.targetPort
|
|
237
|
-
}, 'udp.socket.connected');
|
|
238
|
-
});
|
|
239
|
-
} catch (connectErr) {
|
|
240
|
-
this.log.error({
|
|
241
|
-
error: connectErr.message,
|
|
242
|
-
code: connectErr.code,
|
|
243
|
-
targetHost: this.targetHost,
|
|
244
|
-
targetPort: this.targetPort
|
|
245
|
-
}, 'udp.socket.connect.failed');
|
|
246
|
-
const error = new Error(`Failed to connect UDP socket to ${this.targetHost}:${this.targetPort}: ${connectErr.message}`);
|
|
247
|
-
error.code = 'CONNECT_FAILED';
|
|
248
|
-
reject(error);
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// UDP-S13: Set up read loop to capture responses from app and forward to relay via WS
|
|
253
|
-
// This is bidirectional: WS → app (via send) and app → WS (via message handler)
|
|
254
|
-
this.udpSocket.on('message', (msg, rinfo) => {
|
|
255
|
-
// UDP-S13: Forward app responses to relay via WebSocket
|
|
256
|
-
this.handleLocalDatagram(msg, rinfo);
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
// Set up error handler for runtime errors (after successful bind)
|
|
260
|
-
this.udpSocket.on('error', (err) => {
|
|
261
|
-
this.log.error({ error: err.message, code: err.code }, 'udp.socket.error');
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
resolve();
|
|
265
|
-
});
|
|
202
|
+
resolve();
|
|
266
203
|
});
|
|
267
204
|
|
|
268
205
|
this.ws.on('message', (data, isBinary) => {
|
|
@@ -294,7 +231,7 @@ export class UdpTunnel {
|
|
|
294
231
|
|
|
295
232
|
this.stopPingKeepalive();
|
|
296
233
|
this.stopFlowCleanup();
|
|
297
|
-
this.
|
|
234
|
+
this.closeAllFlowSockets();
|
|
298
235
|
|
|
299
236
|
// UDP-S1: Exit on WS close (no reconnect loop)
|
|
300
237
|
this.log.info('UDP tunnel exiting due to WebSocket close');
|
|
@@ -305,7 +242,7 @@ export class UdpTunnel {
|
|
|
305
242
|
this.log.error({ err: err.message, code: err.code }, 'udp.tunnel.ws.error');
|
|
306
243
|
this.stopPingKeepalive();
|
|
307
244
|
this.stopFlowCleanup();
|
|
308
|
-
this.
|
|
245
|
+
this.closeAllFlowSockets();
|
|
309
246
|
|
|
310
247
|
// UDP-S1: Exit on WS error (no reconnect loop)
|
|
311
248
|
const error = new Error(`UDP_RELAY_UNAVAILABLE: WebSocket connection failed: ${err.message}`);
|
|
@@ -407,7 +344,7 @@ export class UdpTunnel {
|
|
|
407
344
|
}, 'udp.protocol_violation_closing_tunnel');
|
|
408
345
|
|
|
409
346
|
// Close UDP socket
|
|
410
|
-
this.
|
|
347
|
+
this.closeAllFlowSockets();
|
|
411
348
|
|
|
412
349
|
// Close WebSocket connection
|
|
413
350
|
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
@@ -420,51 +357,147 @@ export class UdpTunnel {
|
|
|
420
357
|
|
|
421
358
|
// UDP-S1: Handle UDP_DGRAM frame (datagram from relay)
|
|
422
359
|
// UDP-S13: Forward to targetHost:targetPort (app's listening port)
|
|
360
|
+
// UDP-S14: Use per-flow socket for correct return path routing
|
|
423
361
|
handleUdpDatagram(frame) {
|
|
424
362
|
const flowId = frame.flowId;
|
|
363
|
+
const now = Date.now();
|
|
425
364
|
|
|
426
|
-
//
|
|
427
|
-
this.flowMap.
|
|
365
|
+
// UDP-S14: Lookup or create per-flow socket
|
|
366
|
+
let flowState = this.flowMap.get(flowId);
|
|
367
|
+
let socket = flowState?.socket;
|
|
428
368
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
369
|
+
if (!socket) {
|
|
370
|
+
// Create new per-flow socket for this flowId
|
|
371
|
+
socket = dgram.createSocket('udp4');
|
|
372
|
+
|
|
373
|
+
// UDP-S14: Store reverse mapping (socket -> flowId) for message handlers
|
|
374
|
+
this.socketToFlowId.set(socket, flowId);
|
|
375
|
+
|
|
376
|
+
// UDP-S14: Store socket in flowMap immediately (before bind completes)
|
|
377
|
+
flowState = { socket, lastSeen: now, connected: false, pendingPayload: frame.payload };
|
|
378
|
+
this.flowMap.set(flowId, flowState);
|
|
379
|
+
|
|
380
|
+
// UDP-S13: Bind to OS-assigned port (ephemeral)
|
|
381
|
+
socket.bind(0, '0.0.0.0', () => {
|
|
382
|
+
const address = socket.address();
|
|
383
|
+
this.log.debug({
|
|
384
|
+
flowId,
|
|
385
|
+
localAddress: address.address,
|
|
386
|
+
localPort: address.port,
|
|
387
|
+
targetHost: this.targetHost,
|
|
388
|
+
targetPort: this.targetPort
|
|
389
|
+
}, 'udp.flow.socket.bound');
|
|
390
|
+
|
|
391
|
+
// UDP-S13: Connect to targetHost:targetPort (allows send() without address/port)
|
|
392
|
+
socket.connect(this.targetPort, this.targetHost, () => {
|
|
393
|
+
this.log.debug({
|
|
394
|
+
flowId,
|
|
395
|
+
targetHost: this.targetHost,
|
|
396
|
+
targetPort: this.targetPort
|
|
397
|
+
}, 'udp.flow.socket.connected');
|
|
398
|
+
|
|
399
|
+
// UDP-S14: Mark socket as connected
|
|
400
|
+
flowState.connected = true;
|
|
401
|
+
|
|
402
|
+
// UDP-S13: Send any pending payload now that socket is connected
|
|
403
|
+
if (flowState.pendingPayload) {
|
|
404
|
+
this.sendDatagramToApp(socket, flowId, flowState.pendingPayload);
|
|
405
|
+
flowState.pendingPayload = null;
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// UDP-S14: Handle incoming datagrams from app on this flow's socket
|
|
410
|
+
socket.on('message', (msg, rinfo) => {
|
|
411
|
+
// We know the flowId from the socket itself
|
|
412
|
+
const socketFlowId = this.socketToFlowId.get(socket);
|
|
413
|
+
if (socketFlowId === flowId) {
|
|
414
|
+
this.handleLocalDatagram(msg, rinfo, flowId);
|
|
451
415
|
} else {
|
|
452
|
-
this.log.
|
|
416
|
+
this.log.error({
|
|
453
417
|
flowId,
|
|
454
|
-
|
|
418
|
+
socketFlowId,
|
|
419
|
+
note: 'FlowId mismatch in socket message handler'
|
|
420
|
+
}, 'udp.flow.socket.mismatch');
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
socket.on('error', (err) => {
|
|
425
|
+
this.log.error({
|
|
426
|
+
flowId,
|
|
427
|
+
error: err.message,
|
|
428
|
+
code: err.code
|
|
429
|
+
}, 'udp.flow.socket.error');
|
|
430
|
+
|
|
431
|
+
// UDP-S13: If target app isn't listening, log clearly
|
|
432
|
+
if (err.code === 'ECONNREFUSED' || err.code === 'EHOSTUNREACH') {
|
|
433
|
+
this.log.warn({
|
|
434
|
+
flowId,
|
|
455
435
|
targetHost: this.targetHost,
|
|
456
|
-
targetPort: this.targetPort
|
|
457
|
-
|
|
436
|
+
targetPort: this.targetPort,
|
|
437
|
+
note: 'Target app may not be listening on this port'
|
|
438
|
+
}, 'udp.target.unreachable');
|
|
458
439
|
}
|
|
459
|
-
}
|
|
460
|
-
);
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
} else {
|
|
443
|
+
// Update lastSeen for existing flow
|
|
444
|
+
flowState.lastSeen = now;
|
|
445
|
+
|
|
446
|
+
// UDP-S13: Send payload using existing socket
|
|
447
|
+
// If socket is not yet connected, queue the payload
|
|
448
|
+
if (flowState.connected) {
|
|
449
|
+
this.sendDatagramToApp(socket, flowId, frame.payload);
|
|
450
|
+
} else {
|
|
451
|
+
// Queue payload until socket is connected
|
|
452
|
+
flowState.pendingPayload = frame.payload;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// UDP-S13: Helper to send datagram to app on flow-specific socket
|
|
458
|
+
sendDatagramToApp(socket, flowId, payload) {
|
|
459
|
+
if (!socket || this.protocolViolation) {
|
|
460
|
+
return;
|
|
461
461
|
}
|
|
462
|
+
|
|
463
|
+
socket.send(
|
|
464
|
+
payload,
|
|
465
|
+
(err) => {
|
|
466
|
+
if (err) {
|
|
467
|
+
this.log.error({
|
|
468
|
+
flowId,
|
|
469
|
+
error: err.message,
|
|
470
|
+
code: err.code,
|
|
471
|
+
targetHost: this.targetHost,
|
|
472
|
+
targetPort: this.targetPort
|
|
473
|
+
}, 'udp.datagram.send_error');
|
|
474
|
+
} else {
|
|
475
|
+
// UDP-S14: Log with flowId for tracking
|
|
476
|
+
this.log.debug({
|
|
477
|
+
flowId,
|
|
478
|
+
size: payload.length,
|
|
479
|
+
targetHost: this.targetHost,
|
|
480
|
+
targetPort: this.targetPort
|
|
481
|
+
}, 'udp.datagram.delivered');
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
);
|
|
462
485
|
}
|
|
463
486
|
|
|
464
487
|
// UDP-S1: Handle UDP_CLOSE frame (flow shutdown hint)
|
|
488
|
+
// UDP-S14: Close per-flow socket
|
|
465
489
|
handleUdpClose(frame) {
|
|
466
490
|
const flowId = frame.flowId;
|
|
467
491
|
this.log.debug({ flowId }, 'udp.flow.close');
|
|
492
|
+
|
|
493
|
+
// UDP-S14: Close and remove per-flow socket
|
|
494
|
+
const flowState = this.flowMap.get(flowId);
|
|
495
|
+
if (flowState?.socket) {
|
|
496
|
+
flowState.socket.close();
|
|
497
|
+
this.socketToFlowId.delete(flowState.socket);
|
|
498
|
+
this.log.debug({ flowId }, 'udp.flow.socket.closed');
|
|
499
|
+
}
|
|
500
|
+
|
|
468
501
|
// Remove flow from mapping
|
|
469
502
|
this.flowMap.delete(flowId);
|
|
470
503
|
}
|
|
@@ -492,37 +525,25 @@ export class UdpTunnel {
|
|
|
492
525
|
this.lastPongMs = Date.now();
|
|
493
526
|
}
|
|
494
527
|
|
|
495
|
-
// UDP-S13: Handle incoming datagram from app (via
|
|
528
|
+
// UDP-S13: Handle incoming datagram from app (via per-flow socket)
|
|
496
529
|
// Forward app responses to relay via WebSocket
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
530
|
+
// UDP-S14: flowId is known from the socket itself (per-flow socket architecture)
|
|
531
|
+
handleLocalDatagram(msg, rinfo, flowId) {
|
|
532
|
+
// UDP-S14: flowId is passed as parameter (we know it from the socket)
|
|
533
|
+
if (!flowId) {
|
|
534
|
+
this.log.error({
|
|
535
|
+
note: 'flowId not provided to handleLocalDatagram - this should not happen'
|
|
536
|
+
}, 'udp.datagram.flow_missing');
|
|
537
|
+
return; // Drop datagram if flowId is unknown
|
|
538
|
+
}
|
|
500
539
|
|
|
501
|
-
//
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
if (flow.lastSeen > mostRecentTime) {
|
|
509
|
-
mostRecentTime = flow.lastSeen;
|
|
510
|
-
mostRecent = fid;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
if (mostRecent) {
|
|
514
|
-
flowId = mostRecent;
|
|
515
|
-
} else {
|
|
516
|
-
this.log.debug({
|
|
517
|
-
flowId: this.defaultFlowId,
|
|
518
|
-
note: 'Using default flowId - no active flows'
|
|
519
|
-
}, 'udp.datagram.unmapped_flow');
|
|
520
|
-
}
|
|
521
|
-
} else {
|
|
522
|
-
this.log.debug({
|
|
523
|
-
flowId: this.defaultFlowId,
|
|
524
|
-
note: 'Using default flowId - no flows seen yet'
|
|
525
|
-
}, 'udp.datagram.unmapped_flow');
|
|
540
|
+
// UDP-S14: Verify flowId is still active
|
|
541
|
+
if (!this.flowMap.has(flowId)) {
|
|
542
|
+
this.log.warn({
|
|
543
|
+
flowId,
|
|
544
|
+
note: 'Received datagram on closed/evicted flow'
|
|
545
|
+
}, 'udp.datagram.flow_closed');
|
|
546
|
+
return; // Drop datagram if flow is closed
|
|
526
547
|
}
|
|
527
548
|
|
|
528
549
|
// UDP-S1: Validate datagram size (max 64 KiB)
|
|
@@ -543,7 +564,13 @@ export class UdpTunnel {
|
|
|
543
564
|
payload: msg
|
|
544
565
|
});
|
|
545
566
|
this.ws.send(frame, { binary: true });
|
|
546
|
-
|
|
567
|
+
// UDP-S14: Log with flowId for tracking return path
|
|
568
|
+
// UDP-S14: Log with flowId (known from per-flow socket)
|
|
569
|
+
this.log.debug({
|
|
570
|
+
flowId,
|
|
571
|
+
size: msg.length,
|
|
572
|
+
note: 'App response forwarded with flowId (per-flow socket)'
|
|
573
|
+
}, 'udp.datagram.sent');
|
|
547
574
|
} catch (err) {
|
|
548
575
|
if (err instanceof UdpV1FramingError) {
|
|
549
576
|
this.log.error({ flowId, error: err.message }, 'udp.frame_encode_error');
|
|
@@ -614,6 +641,13 @@ export class UdpTunnel {
|
|
|
614
641
|
}
|
|
615
642
|
|
|
616
643
|
for (const flowId of expiredFlows) {
|
|
644
|
+
// UDP-S14: Close per-flow socket before removing
|
|
645
|
+
const flowState = this.flowMap.get(flowId);
|
|
646
|
+
if (flowState?.socket) {
|
|
647
|
+
flowState.socket.close();
|
|
648
|
+
this.socketToFlowId.delete(flowState.socket);
|
|
649
|
+
this.log.debug({ flowId }, 'udp.flow.socket.closed');
|
|
650
|
+
}
|
|
617
651
|
this.flowMap.delete(flowId);
|
|
618
652
|
this.log.debug({ flowId }, 'udp.flow.idle_evicted');
|
|
619
653
|
}
|
|
@@ -628,18 +662,22 @@ export class UdpTunnel {
|
|
|
628
662
|
}
|
|
629
663
|
}
|
|
630
664
|
|
|
631
|
-
// UDP-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
665
|
+
// UDP-S14: Close all per-flow sockets
|
|
666
|
+
closeAllFlowSockets() {
|
|
667
|
+
for (const [flowId, flowState] of this.flowMap.entries()) {
|
|
668
|
+
if (flowState?.socket) {
|
|
669
|
+
flowState.socket.close();
|
|
670
|
+
this.socketToFlowId.delete(flowState.socket);
|
|
671
|
+
this.log.debug({ flowId }, 'udp.flow.socket.closed');
|
|
672
|
+
}
|
|
636
673
|
}
|
|
674
|
+
this.flowMap.clear();
|
|
637
675
|
}
|
|
638
676
|
|
|
639
677
|
close() {
|
|
640
678
|
this.stopPingKeepalive();
|
|
641
679
|
this.stopFlowCleanup();
|
|
642
|
-
this.
|
|
680
|
+
this.closeAllFlowSockets();
|
|
643
681
|
|
|
644
682
|
if (this.ws) {
|
|
645
683
|
this.ws.close();
|