asyx 0.6.1 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -8
- package/package.json +2 -1
- package/src/commands/in-use-messages.js +64 -0
- package/src/commands/reject-messages.js +56 -0
- package/src/commands/tunnel.js +59 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +13 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +21 -0
- package/vendor/tunnel-core/dist/control-plane/session.d.ts +5 -0
- package/vendor/tunnel-core/dist/control-plane/session.js +2 -1
- package/vendor/tunnel-core/dist/flows/goodbye.d.ts +10 -0
- package/vendor/tunnel-core/dist/flows/goodbye.js +27 -0
- package/vendor/tunnel-core/dist/flows/http.d.ts +11 -0
- package/vendor/tunnel-core/dist/flows/http.js +84 -10
- package/vendor/tunnel-core/dist/flows/in-use.d.ts +27 -0
- package/vendor/tunnel-core/dist/flows/in-use.js +42 -0
- package/vendor/tunnel-core/dist/flows/reopen.d.ts +9 -0
- package/vendor/tunnel-core/dist/flows/reopen.js +27 -0
- package/vendor/tunnel-core/dist/flows/tcp.d.ts +11 -0
- package/vendor/tunnel-core/dist/flows/tcp.js +70 -11
- package/vendor/tunnel-core/dist/flows/udp.d.ts +11 -0
- package/vendor/tunnel-core/dist/flows/udp.js +70 -10
- package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +132 -0
- package/vendor/tunnel-core/dist/lib/fingerprint.js +24 -1
- package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +2 -0
- package/vendor/tunnel-core/dist/mqtt/events/handlers.js +37 -0
- package/vendor/tunnel-core/dist/mqtt/events/index.js +2 -1
- package/vendor/tunnel-core/dist/state/assignment.d.ts +7 -0
- package/vendor/tunnel-core/dist/state/assignment.js +17 -0
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +4 -1
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +60 -12
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +3 -0
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +18 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +3 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +18 -0
|
@@ -1,10 +1,142 @@
|
|
|
1
1
|
export function getFpHash(clientId: any): Promise<{
|
|
2
|
+
replace: boolean;
|
|
3
|
+
host: string;
|
|
4
|
+
to: string;
|
|
2
5
|
clientVersion: string;
|
|
3
6
|
fpv: string;
|
|
4
7
|
fph: string;
|
|
5
8
|
alg: string;
|
|
6
9
|
lib: string;
|
|
7
10
|
} | {
|
|
11
|
+
replace?: undefined;
|
|
12
|
+
host: string;
|
|
13
|
+
to: string;
|
|
14
|
+
clientVersion: string;
|
|
15
|
+
fpv: string;
|
|
16
|
+
fph: string;
|
|
17
|
+
alg: string;
|
|
18
|
+
lib: string;
|
|
19
|
+
} | {
|
|
20
|
+
replace: boolean;
|
|
21
|
+
host?: undefined;
|
|
22
|
+
to: string;
|
|
23
|
+
clientVersion: string;
|
|
24
|
+
fpv: string;
|
|
25
|
+
fph: string;
|
|
26
|
+
alg: string;
|
|
27
|
+
lib: string;
|
|
28
|
+
} | {
|
|
29
|
+
replace?: undefined;
|
|
30
|
+
host?: undefined;
|
|
31
|
+
to: string;
|
|
32
|
+
clientVersion: string;
|
|
33
|
+
fpv: string;
|
|
34
|
+
fph: string;
|
|
35
|
+
alg: string;
|
|
36
|
+
lib: string;
|
|
37
|
+
} | {
|
|
38
|
+
replace: boolean;
|
|
39
|
+
host: string;
|
|
40
|
+
to?: undefined;
|
|
41
|
+
clientVersion: string;
|
|
42
|
+
fpv: string;
|
|
43
|
+
fph: string;
|
|
44
|
+
alg: string;
|
|
45
|
+
lib: string;
|
|
46
|
+
} | {
|
|
47
|
+
replace?: undefined;
|
|
48
|
+
host: string;
|
|
49
|
+
to?: undefined;
|
|
50
|
+
clientVersion: string;
|
|
51
|
+
fpv: string;
|
|
52
|
+
fph: string;
|
|
53
|
+
alg: string;
|
|
54
|
+
lib: string;
|
|
55
|
+
} | {
|
|
56
|
+
replace: boolean;
|
|
57
|
+
host?: undefined;
|
|
58
|
+
to?: undefined;
|
|
59
|
+
clientVersion: string;
|
|
60
|
+
fpv: string;
|
|
61
|
+
fph: string;
|
|
62
|
+
alg: string;
|
|
63
|
+
lib: string;
|
|
64
|
+
} | {
|
|
65
|
+
replace?: undefined;
|
|
66
|
+
host?: undefined;
|
|
67
|
+
to?: undefined;
|
|
68
|
+
clientVersion: string;
|
|
69
|
+
fpv: string;
|
|
70
|
+
fph: string;
|
|
71
|
+
alg: string;
|
|
72
|
+
lib: string;
|
|
73
|
+
} | {
|
|
74
|
+
replace: boolean;
|
|
75
|
+
host: string;
|
|
76
|
+
to: string;
|
|
77
|
+
clientVersion?: undefined;
|
|
78
|
+
fpv: string;
|
|
79
|
+
fph: string;
|
|
80
|
+
alg: string;
|
|
81
|
+
lib: string;
|
|
82
|
+
} | {
|
|
83
|
+
replace?: undefined;
|
|
84
|
+
host: string;
|
|
85
|
+
to: string;
|
|
86
|
+
clientVersion?: undefined;
|
|
87
|
+
fpv: string;
|
|
88
|
+
fph: string;
|
|
89
|
+
alg: string;
|
|
90
|
+
lib: string;
|
|
91
|
+
} | {
|
|
92
|
+
replace: boolean;
|
|
93
|
+
host?: undefined;
|
|
94
|
+
to: string;
|
|
95
|
+
clientVersion?: undefined;
|
|
96
|
+
fpv: string;
|
|
97
|
+
fph: string;
|
|
98
|
+
alg: string;
|
|
99
|
+
lib: string;
|
|
100
|
+
} | {
|
|
101
|
+
replace?: undefined;
|
|
102
|
+
host?: undefined;
|
|
103
|
+
to: string;
|
|
104
|
+
clientVersion?: undefined;
|
|
105
|
+
fpv: string;
|
|
106
|
+
fph: string;
|
|
107
|
+
alg: string;
|
|
108
|
+
lib: string;
|
|
109
|
+
} | {
|
|
110
|
+
replace: boolean;
|
|
111
|
+
host: string;
|
|
112
|
+
to?: undefined;
|
|
113
|
+
clientVersion?: undefined;
|
|
114
|
+
fpv: string;
|
|
115
|
+
fph: string;
|
|
116
|
+
alg: string;
|
|
117
|
+
lib: string;
|
|
118
|
+
} | {
|
|
119
|
+
replace?: undefined;
|
|
120
|
+
host: string;
|
|
121
|
+
to?: undefined;
|
|
122
|
+
clientVersion?: undefined;
|
|
123
|
+
fpv: string;
|
|
124
|
+
fph: string;
|
|
125
|
+
alg: string;
|
|
126
|
+
lib: string;
|
|
127
|
+
} | {
|
|
128
|
+
replace: boolean;
|
|
129
|
+
host?: undefined;
|
|
130
|
+
to?: undefined;
|
|
131
|
+
clientVersion?: undefined;
|
|
132
|
+
fpv: string;
|
|
133
|
+
fph: string;
|
|
134
|
+
alg: string;
|
|
135
|
+
lib: string;
|
|
136
|
+
} | {
|
|
137
|
+
replace?: undefined;
|
|
138
|
+
host?: undefined;
|
|
139
|
+
to?: undefined;
|
|
8
140
|
clientVersion?: undefined;
|
|
9
141
|
fpv: string;
|
|
10
142
|
fph: string;
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { getFingerprintingInfo } from 'hw-fingerprint';
|
|
8
8
|
import { createHash } from 'node:crypto';
|
|
9
|
+
import os from 'node:os';
|
|
9
10
|
/**
|
|
10
11
|
* Safe hardware attributes that don't trigger macOS privacy warnings.
|
|
11
12
|
* Based on analysis of hw-fingerprint library behavior.
|
|
@@ -69,8 +70,30 @@ function versionField() {
|
|
|
69
70
|
const v = (process.env.ASYX_CLIENT_VERSION || '').trim();
|
|
70
71
|
return /^[0-9A-Za-z.@+-]{1,40}$/.test(v) ? { clientVersion: v } : {};
|
|
71
72
|
}
|
|
73
|
+
// Story 4.10 r3: the address the tunnel serves (`asyx tunnel --to <hostname>`), sent on every
|
|
74
|
+
// open/refresh/reconnect so the coordinator can refuse a tunnel for an address this device does not serve.
|
|
75
|
+
function targetField() {
|
|
76
|
+
const v = (process.env.ASYX_TUNNEL_TO || '').trim().toLowerCase();
|
|
77
|
+
return /^[a-z0-9.-]{3,253}$/.test(v) ? { to: v } : {};
|
|
78
|
+
}
|
|
79
|
+
// Story 1.9: the computer's name (so a refusal can say where the running tunnel is; never an IP
|
|
80
|
+
// address) and whether this open may take over a live tunnel of the same kind (`--replace`).
|
|
81
|
+
function hostField() {
|
|
82
|
+
let h = '';
|
|
83
|
+
try {
|
|
84
|
+
h = (process.env.ASYX_HOST_NAME || os.hostname() || '').trim();
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
h = '';
|
|
88
|
+
}
|
|
89
|
+
h = h.replace(/\.local$/i, '').replace(/[^\w .'()-]/g, '').slice(0, 64);
|
|
90
|
+
return h ? { host: h } : {};
|
|
91
|
+
}
|
|
92
|
+
function replaceField() {
|
|
93
|
+
return process.env.ASYX_TUNNEL_REPLACE === '1' ? { replace: true } : {};
|
|
94
|
+
}
|
|
72
95
|
export async function getFpHash(clientId) {
|
|
73
|
-
return { ...(await fingerprintFields(clientId)), ...versionField() };
|
|
96
|
+
return { ...(await fingerprintFields(clientId)), ...versionField(), ...targetField(), ...hostField(), ...replaceField() };
|
|
74
97
|
}
|
|
75
98
|
async function fingerprintFields(clientId) {
|
|
76
99
|
try {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** Story 1.9: how "this tunnel was taken over by a newer one" is reported (the CLI prints and exits 75). */
|
|
2
|
+
export function setReplacedReporter(fn: any): void;
|
|
1
3
|
/** Register how open rejections are reported to the user (the CLI does; the SDK uses errors). */
|
|
2
4
|
export function setOpenRejectReporter(fn: any): void;
|
|
3
5
|
export function getHandler(type: any): any;
|
|
@@ -9,6 +9,9 @@ export const FATAL_REJECT_CODES = new Set([
|
|
|
9
9
|
'tcp_port_block_exhausted', 'tcp_port_capacity_exhausted', 'tenant_tcp_lease_limit_reached',
|
|
10
10
|
]);
|
|
11
11
|
let openRejectReporter = null;
|
|
12
|
+
let replacedReporter = null;
|
|
13
|
+
/** Story 1.9: how "this tunnel was taken over by a newer one" is reported (the CLI prints and exits 75). */
|
|
14
|
+
export function setReplacedReporter(fn) { replacedReporter = fn; }
|
|
12
15
|
/** Register how open rejections are reported to the user (the CLI does; the SDK uses errors). */
|
|
13
16
|
export function setOpenRejectReporter(fn) { openRejectReporter = fn; }
|
|
14
17
|
/**
|
|
@@ -218,6 +221,22 @@ const registry = {
|
|
|
218
221
|
}
|
|
219
222
|
}
|
|
220
223
|
const code = String(d.reason || 'refused').toLowerCase();
|
|
224
|
+
// Story 1.9: a live tunnel of this kind already serves the address. The open that asked fails with
|
|
225
|
+
// a RefusedError carrying the details; the flow decides whether to wait and ask again.
|
|
226
|
+
if (code === 'address_in_use' && e.requestId) {
|
|
227
|
+
const { rejectAssignment } = await import('../../state/assignment.js');
|
|
228
|
+
if (rejectAssignment(e.requestId, new RefusedError('address_in_use', 'a live tunnel of this kind already serves this address', { details: d })))
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
// Story 3.13: a renewal (or reopen) refused because the lease is closed fails the request waiting
|
|
232
|
+
// for it with LEASE_CLOSED, so the tunnel reopens on the same address instead of printing a refusal.
|
|
233
|
+
if (code === 'lease_closed' && e.requestId) {
|
|
234
|
+
const { rejectAssignment } = await import('../../state/assignment.js');
|
|
235
|
+
const err = new RefusedError('lease_closed', 'the lease was closed', { details: d });
|
|
236
|
+
/** @type {any} */ (err).code = 'LEASE_CLOSED';
|
|
237
|
+
if (rejectAssignment(e.requestId, err))
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
221
240
|
const report = { code, reason: d.reason, fatal: FATAL_REJECT_CODES.has(code), data: d, instanceId };
|
|
222
241
|
if (openRejectReporter) {
|
|
223
242
|
await openRejectReporter(report);
|
|
@@ -226,6 +245,24 @@ const registry = {
|
|
|
226
245
|
if (report.fatal)
|
|
227
246
|
throw new RefusedError(code, `coordinator refused the open: ${d.reason}`, { details: d });
|
|
228
247
|
},
|
|
248
|
+
// Story 1.9: a newer tunnel took this address over with --replace. Only the tunnel whose lease was
|
|
249
|
+
// replaced reacts (routed by instanceId/leaseId); it stops and does not reconnect.
|
|
250
|
+
async replaced(e) {
|
|
251
|
+
const d = e.data || {};
|
|
252
|
+
const ctx = e._instanceContext;
|
|
253
|
+
const mine = ctx?.getAssignment?.(e._instanceId);
|
|
254
|
+
if (!d.leaseId || mine?.leaseId !== d.leaseId)
|
|
255
|
+
return; // only the tunnel holding that lease (a new one has none yet)
|
|
256
|
+
const report = { code: 'replaced', at: d.at || null, by: d.by || null, kind: d.kind || null, hostname: d.hostname || null, instanceId: e._instanceId || null };
|
|
257
|
+
if (replacedReporter) {
|
|
258
|
+
await replacedReporter(report);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const err = /** @type {Error & { code?: string, details?: unknown }} */ (new Error('this tunnel was taken over by a newer tunnel for the same address'));
|
|
262
|
+
err.code = 'ERROR_REPLACED';
|
|
263
|
+
err.details = report;
|
|
264
|
+
throw err;
|
|
265
|
+
},
|
|
229
266
|
async error(e) {
|
|
230
267
|
const d = e.data || {};
|
|
231
268
|
const err = /** @type {Error & { code?: string, cause?: unknown, closeCode?: number, originalError?: unknown }} */ (new Error(d.msg || "Command failed"));
|
|
@@ -74,7 +74,8 @@ export async function dispatch(parsedMessage) {
|
|
|
74
74
|
// their renewal ids collided.
|
|
75
75
|
// Only log/drop if all fallbacks failed
|
|
76
76
|
if (!targetInstanceId) {
|
|
77
|
-
|
|
77
|
+
// 0.7.0: debug, not warn — with several tunnels on one computer every tunnel sees the others' replies.
|
|
78
|
+
logger.debug('evt.instance.unknown', {
|
|
78
79
|
instanceId,
|
|
79
80
|
leaseId,
|
|
80
81
|
eventType: evt.type,
|
|
@@ -105,6 +105,13 @@ export function hasAssignment(instanceId: string): boolean;
|
|
|
105
105
|
* @returns {Promise<Object>} resolves with the assignment snapshot
|
|
106
106
|
*/
|
|
107
107
|
export function waitForAssignment(requestId: string, timeoutMs?: number): Promise<any>;
|
|
108
|
+
/**
|
|
109
|
+
* Story 1.9: fail the open waiting for requestId with `error` (e.g. address_in_use), so the flow can
|
|
110
|
+
* decide to wait and ask again instead of timing out. Returns true when someone was waiting.
|
|
111
|
+
* @param {string} requestId
|
|
112
|
+
* @param {Error} error
|
|
113
|
+
*/
|
|
114
|
+
export function rejectAssignment(requestId: string, error: Error): boolean;
|
|
108
115
|
/**
|
|
109
116
|
* Get assignment metadata (without JWT for logging)
|
|
110
117
|
* TPB-12: Per-instance metadata retrieval
|
|
@@ -264,6 +264,23 @@ export function waitForAssignment(requestId, timeoutMs = 10000) {
|
|
|
264
264
|
}
|
|
265
265
|
});
|
|
266
266
|
}
|
|
267
|
+
/**
|
|
268
|
+
* Story 1.9: fail the open waiting for requestId with `error` (e.g. address_in_use), so the flow can
|
|
269
|
+
* decide to wait and ask again instead of timing out. Returns true when someone was waiting.
|
|
270
|
+
* @param {string} requestId
|
|
271
|
+
* @param {Error} error
|
|
272
|
+
*/
|
|
273
|
+
export function rejectAssignment(requestId, error) {
|
|
274
|
+
const queue = assignmentWaiters.get(requestId);
|
|
275
|
+
if (!queue || queue.length === 0)
|
|
276
|
+
return false;
|
|
277
|
+
assignmentWaiters.delete(requestId);
|
|
278
|
+
for (const entry of queue) {
|
|
279
|
+
clearTimeout(entry.timer);
|
|
280
|
+
entry.reject(error);
|
|
281
|
+
}
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
267
284
|
/**
|
|
268
285
|
* Get assignment metadata (without JWT for logging)
|
|
269
286
|
* TPB-12: Per-instance metadata retrieval
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const LOCAL_UNAVAILABLE_PAGE: "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Service not reachable</title></head><body style=\"font-family:system-ui,sans-serif;max-width:32rem;margin:4rem auto;padding:0 1rem;color:#334155\"><h1 style=\"font-size:1.4rem;color:#0f172a\">This service is not reachable right now</h1><p>The tunnel is up, but the app behind it is not answering. It may be restarting; try again in a moment.</p></body></html>";
|
|
2
2
|
export class HttpTunnel {
|
|
3
|
-
constructor({ relayUrl, jwt, deviceId, requestId, localPort, log, coordinatorCore, coordinatorIngress, isControlPlaneConnected, waitForControlPlane, getLastMqttError, getForceReconnect, controlPlaneTimeoutMs, secGroupSlug, instanceId, onFatal, onNotice, requestSink, }: {
|
|
3
|
+
constructor({ relayUrl, jwt, deviceId, requestId, localPort, log, coordinatorCore, coordinatorIngress, isControlPlaneConnected, waitForControlPlane, getLastMqttError, getForceReconnect, controlPlaneTimeoutMs, secGroupSlug, instanceId, onFatal, onNotice, onLeaseClosed, requestSink, }: {
|
|
4
4
|
relayUrl: any;
|
|
5
5
|
jwt: any;
|
|
6
6
|
deviceId: any;
|
|
@@ -18,10 +18,12 @@ export class HttpTunnel {
|
|
|
18
18
|
instanceId?: any;
|
|
19
19
|
onFatal?: any;
|
|
20
20
|
onNotice?: any;
|
|
21
|
+
onLeaseClosed?: any;
|
|
21
22
|
requestSink?: any;
|
|
22
23
|
});
|
|
23
24
|
requestSink: any;
|
|
24
25
|
onFatal: any;
|
|
26
|
+
onLeaseClosed: any;
|
|
25
27
|
onNotice: any;
|
|
26
28
|
relayUrl: any;
|
|
27
29
|
jwt: any;
|
|
@@ -109,6 +111,7 @@ export class HttpTunnel {
|
|
|
109
111
|
open(): Promise<any>;
|
|
110
112
|
_attemptConnect(): Promise<any>;
|
|
111
113
|
_helloAttempts: any;
|
|
114
|
+
lastInboundMs: number;
|
|
112
115
|
scheduleReconnect(code: any, reason: any): any;
|
|
113
116
|
attemptReconnect(): Promise<void>;
|
|
114
117
|
_scheduleRefreshRetry(reason: any): any;
|
|
@@ -80,11 +80,12 @@ export class HttpTunnel {
|
|
|
80
80
|
instanceId = null,
|
|
81
81
|
// Story 12.1 (c2): the core never exits or prints. onFatal(code) is called where the CLI used to
|
|
82
82
|
// exit(1); onNotice(code, details) where it used to print. Without onFatal, a fatal condition throws.
|
|
83
|
-
onFatal = null, onNotice = null,
|
|
83
|
+
onFatal = null, onNotice = null, onLeaseClosed = null, // Story 3.13: reopen after the relay closed this lease (4000 lease_closed)
|
|
84
84
|
// Story 12.1 c3: where received requests go; defaults to the local service on localPort
|
|
85
85
|
requestSink = null, }) {
|
|
86
86
|
this.requestSink = requestSink || new LocalPortSink(localPort);
|
|
87
87
|
this.onFatal = onFatal;
|
|
88
|
+
this.onLeaseClosed = onLeaseClosed;
|
|
88
89
|
this.onNotice = onNotice;
|
|
89
90
|
this.relayUrl = relayUrl;
|
|
90
91
|
this.jwt = jwt; // Accept JWT as-is (no validation)
|
|
@@ -562,9 +563,13 @@ export class HttpTunnel {
|
|
|
562
563
|
this.startHeartbeat();
|
|
563
564
|
resolve();
|
|
564
565
|
});
|
|
566
|
+
// Story 3.11: anything from the relay, including the pong the WebSocket protocol
|
|
567
|
+
// requires in answer to a ping, proves the connection is alive
|
|
568
|
+
this.ws.on('pong', () => { this.lastInboundMs = Date.now(); });
|
|
565
569
|
this.ws.on('message', (data, isBinary) => {
|
|
566
570
|
// CRT-Relay-E: Update activity on any message
|
|
567
571
|
this.lastActivityMs = Date.now();
|
|
572
|
+
this.lastInboundMs = this.lastActivityMs;
|
|
568
573
|
if (!isBinary) {
|
|
569
574
|
try {
|
|
570
575
|
const msg = JSON.parse(data.toString());
|
|
@@ -627,6 +632,13 @@ export class HttpTunnel {
|
|
|
627
632
|
clearAllWaiters();
|
|
628
633
|
// Clear single-flight guard
|
|
629
634
|
this.reauthInFlight = null;
|
|
635
|
+
// Story 3.13: the relay closed this lease (the cloud closed it): reconnecting with the same token
|
|
636
|
+
// would be refused every time; reopen on the same address instead, or end saying why.
|
|
637
|
+
if (code === 4000 && reasonStr === 'lease_closed' && this.onLeaseClosed && this.shouldReconnect) {
|
|
638
|
+
this.onLeaseClosed().then((ok) => { if (!ok)
|
|
639
|
+
this._fatal('lease_closed_reopen_refused'); }).catch(() => this._fatal('lease_closed_reopen_refused'));
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
630
642
|
// CLI-IMG-WSReconnect: Schedule reconnect instead of exiting
|
|
631
643
|
this.scheduleReconnect(code, reason);
|
|
632
644
|
});
|
|
@@ -852,12 +864,14 @@ export class HttpTunnel {
|
|
|
852
864
|
const ackPromise = this.coordinatorIngress.registerAwaiter((message) => {
|
|
853
865
|
try {
|
|
854
866
|
const parsed = message.payload;
|
|
855
|
-
|
|
867
|
+
// Story 3.13: an open_reject for this renewal (lease_closed) is an answer too, not a timeout
|
|
868
|
+
return parsed && (parsed.type === 'open_ack' || parsed.type === 'open_reject') && parsed.requestId === refreshRequestId;
|
|
856
869
|
}
|
|
857
870
|
catch (e) {
|
|
858
871
|
return false;
|
|
859
872
|
}
|
|
860
873
|
}, 10000);
|
|
874
|
+
let ackMsg = null; // Story 3.13: the renewal's answer (open_ack, or open_reject such as lease_closed)
|
|
861
875
|
// CLI-IMG-MQTT-Resilience: Publish with retry logic and fallback
|
|
862
876
|
try {
|
|
863
877
|
// Publish the refresh request
|
|
@@ -870,9 +884,17 @@ export class HttpTunnel {
|
|
|
870
884
|
this.log.info('jwt.refresh.request.sent', { refreshRequestId, retryCount: this.refreshAttempts });
|
|
871
885
|
// Wait for new assignment with timeout handling
|
|
872
886
|
try {
|
|
873
|
-
await ackPromise;
|
|
887
|
+
ackMsg = await ackPromise;
|
|
874
888
|
}
|
|
875
889
|
catch (timeoutError) {
|
|
890
|
+
// Story 3.13: the coordinator says this lease is closed: reopen on the same address, or end saying why
|
|
891
|
+
if ((timeoutError?.code === 'LEASE_CLOSED' || timeoutError?.reason === 'lease_closed') && this.onLeaseClosed) {
|
|
892
|
+
this.log.warn('jwt.refresh.lease_closed', { retryCount: this.refreshAttempts });
|
|
893
|
+
const ok = await this.onLeaseClosed().catch(() => false);
|
|
894
|
+
if (!ok)
|
|
895
|
+
return this._fatal('lease_closed_reopen_refused');
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
876
898
|
this.log.warn('jwt.refresh.timeout', { retryCount: this.refreshAttempts, error: timeoutError.message });
|
|
877
899
|
// CLI-IMG-MQTT-Resilience: Fallback to fresh tunnel when awaiter times out
|
|
878
900
|
await this._fallbackToFreshTunnel();
|
|
@@ -886,6 +908,20 @@ export class HttpTunnel {
|
|
|
886
908
|
await this._fallbackToFreshTunnel();
|
|
887
909
|
return;
|
|
888
910
|
}
|
|
911
|
+
// Story 3.13: the coordinator refused the renewal: a closed lease reopens on the same address
|
|
912
|
+
const rejected = ackMsg?.payload?.type === 'open_reject' ? ackMsg.payload : null;
|
|
913
|
+
if (rejected) {
|
|
914
|
+
const why = String(rejected.data?.reason || rejected.reason || '').toLowerCase();
|
|
915
|
+
if (why === 'lease_closed' && this.onLeaseClosed) {
|
|
916
|
+
this.log.warn('jwt.refresh.lease_closed', { retryCount: this.refreshAttempts });
|
|
917
|
+
const ok = await this.onLeaseClosed().catch(() => false);
|
|
918
|
+
if (!ok)
|
|
919
|
+
return this._fatal('lease_closed_reopen_refused');
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
this.log.error('jwt.refresh.refused', { reason: why });
|
|
923
|
+
return this._fatal('refresh_refused', { reason: why });
|
|
924
|
+
}
|
|
889
925
|
// TPB-12: Get the new assignment for this specific instance
|
|
890
926
|
const newAssignment = getAssignment(this.instanceId);
|
|
891
927
|
if (!newAssignment) {
|
|
@@ -1549,18 +1585,24 @@ export class HttpTunnel {
|
|
|
1549
1585
|
this.ws.close();
|
|
1550
1586
|
}
|
|
1551
1587
|
}
|
|
1552
|
-
// CRT-Relay-E: Heartbeat methods
|
|
1588
|
+
// CRT-Relay-E / Story 3.11: Heartbeat methods. Every interval (30 s by default) the tunnel
|
|
1589
|
+
// sends a WebSocket protocol ping, which the relay's WebSocket library answers by itself, so
|
|
1590
|
+
// relays in the field need no change. Nothing received for three intervals means the
|
|
1591
|
+
// connection died without a close: terminate it so the close handler reconnects (as the
|
|
1592
|
+
// TCP and UDP tunnels do, Story 11.2), instead of waiting minutes for a write to fail.
|
|
1553
1593
|
getHeartbeatInterval() {
|
|
1554
|
-
const interval = process.env.ASYX_TUNNEL_HEARTBEAT_SEC || '
|
|
1594
|
+
const interval = process.env.ASYX_TUNNEL_HEARTBEAT_SEC || '30';
|
|
1555
1595
|
const intervalMs = Number(interval) * 1000;
|
|
1556
|
-
return intervalMs
|
|
1596
|
+
return !(intervalMs > 0) ? Infinity : intervalMs;
|
|
1557
1597
|
}
|
|
1558
1598
|
startHeartbeat() {
|
|
1599
|
+
this.stopHeartbeat();
|
|
1559
1600
|
const interval = this.getHeartbeatInterval();
|
|
1560
1601
|
if (interval === Infinity) {
|
|
1561
1602
|
this.log.debug('Heartbeat disabled (ASYX_TUNNEL_HEARTBEAT_SEC <= 0)');
|
|
1562
1603
|
return;
|
|
1563
1604
|
}
|
|
1605
|
+
this.lastInboundMs = Date.now();
|
|
1564
1606
|
this.heartbeatTimer = setInterval(() => {
|
|
1565
1607
|
this.sendHeartbeat();
|
|
1566
1608
|
}, interval);
|
|
@@ -1571,15 +1613,21 @@ export class HttpTunnel {
|
|
|
1571
1613
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
1572
1614
|
return;
|
|
1573
1615
|
}
|
|
1574
|
-
// Check if we should send heartbeat (respect real traffic)
|
|
1575
|
-
const now = Date.now();
|
|
1576
|
-
const timeSinceActivity = now - this.lastActivityMs;
|
|
1577
1616
|
const interval = this.getHeartbeatInterval();
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
this.
|
|
1617
|
+
const silentMs = Date.now() - (this.lastInboundMs || 0);
|
|
1618
|
+
if (silentMs > 3 * interval) {
|
|
1619
|
+
this.log.warn({ silentMs }, 'http.ws.liveness_timeout');
|
|
1620
|
+
this.stopHeartbeat();
|
|
1621
|
+
this.ws.terminate();
|
|
1622
|
+
return;
|
|
1623
|
+
}
|
|
1624
|
+
try {
|
|
1625
|
+
this.ws.ping();
|
|
1581
1626
|
this.log.debug({ interval }, 'tunnel heartbeat ping sent');
|
|
1582
1627
|
}
|
|
1628
|
+
catch (err) {
|
|
1629
|
+
this.log.debug({ err: err?.message }, 'tunnel heartbeat ping failed');
|
|
1630
|
+
}
|
|
1583
1631
|
}
|
|
1584
1632
|
stopHeartbeat() {
|
|
1585
1633
|
if (this.heartbeatTimer) {
|
|
@@ -84,6 +84,9 @@ export class TcpTunnel {
|
|
|
84
84
|
startHeartbeat(): void;
|
|
85
85
|
sendHeartbeat(): void;
|
|
86
86
|
stopHeartbeat(): void;
|
|
87
|
+
/** Story 3.13: the coordinator said this lease is closed (at renewal): reopen exactly as on a relay
|
|
88
|
+
* close 4000 lease_closed (same port, new lease, then reconnect); resolves false when refused. */
|
|
89
|
+
reopenAfterLeaseClosed(): Promise<boolean>;
|
|
87
90
|
}
|
|
88
91
|
import WebSocket from 'ws';
|
|
89
92
|
import { Reconnector } from './reconnect.js';
|
|
@@ -835,4 +835,22 @@ export class TcpTunnel {
|
|
|
835
835
|
this.heartbeatTimer = null;
|
|
836
836
|
}
|
|
837
837
|
}
|
|
838
|
+
/** Story 3.13: the coordinator said this lease is closed (at renewal): reopen exactly as on a relay
|
|
839
|
+
* close 4000 lease_closed (same port, new lease, then reconnect); resolves false when refused. */
|
|
840
|
+
async reopenAfterLeaseClosed() {
|
|
841
|
+
if (this.closing || !this.reassign)
|
|
842
|
+
return false;
|
|
843
|
+
let got = false;
|
|
844
|
+
try {
|
|
845
|
+
got = await this.reconnector.onAuthFailure();
|
|
846
|
+
}
|
|
847
|
+
catch {
|
|
848
|
+
got = false;
|
|
849
|
+
}
|
|
850
|
+
if (got && !this.closing) {
|
|
851
|
+
this.reconnector.run('lease_closed_reassigned');
|
|
852
|
+
return true;
|
|
853
|
+
}
|
|
854
|
+
return false;
|
|
855
|
+
}
|
|
838
856
|
}
|
|
@@ -85,6 +85,9 @@ export class UdpTunnel {
|
|
|
85
85
|
stopFlowCleanup(): void;
|
|
86
86
|
closeAllFlowSockets(): void;
|
|
87
87
|
close(): void;
|
|
88
|
+
/** Story 3.13: the coordinator said this lease is closed (at renewal): reopen exactly as on a relay
|
|
89
|
+
* close 4000 lease_closed (same port, new lease, then reconnect); resolves false when refused. */
|
|
90
|
+
reopenAfterLeaseClosed(): Promise<boolean>;
|
|
88
91
|
}
|
|
89
92
|
import WebSocket from 'ws';
|
|
90
93
|
import { Reconnector } from './reconnect.js';
|
|
@@ -734,4 +734,22 @@ export class UdpTunnel {
|
|
|
734
734
|
this.ws.close();
|
|
735
735
|
}
|
|
736
736
|
}
|
|
737
|
+
/** Story 3.13: the coordinator said this lease is closed (at renewal): reopen exactly as on a relay
|
|
738
|
+
* close 4000 lease_closed (same port, new lease, then reconnect); resolves false when refused. */
|
|
739
|
+
async reopenAfterLeaseClosed() {
|
|
740
|
+
if (this.closing || !this.reassign)
|
|
741
|
+
return false;
|
|
742
|
+
let got = false;
|
|
743
|
+
try {
|
|
744
|
+
got = await this.reconnector.onAuthFailure();
|
|
745
|
+
}
|
|
746
|
+
catch {
|
|
747
|
+
got = false;
|
|
748
|
+
}
|
|
749
|
+
if (got && !this.closing) {
|
|
750
|
+
this.reconnector.run('lease_closed_reassigned');
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
737
755
|
}
|