asyx 0.5.9 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -9
- package/SECURITY.md +5 -3
- package/package.json +13 -23
- package/src/commands/name.js +23 -0
- package/src/commands/reject-messages.js +54 -0
- package/src/commands/setup.js +98 -102
- package/src/commands/tunnel.js +279 -2425
- package/src/lib/naming.js +81 -0
- package/src/main.js +9 -1
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +260 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +1088 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.d.ts +79 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.js +138 -0
- package/vendor/tunnel-core/dist/control-plane/session.d.ts +50 -0
- package/vendor/tunnel-core/dist/control-plane/session.js +115 -0
- package/vendor/tunnel-core/dist/errors.d.ts +49 -0
- package/vendor/tunnel-core/dist/errors.js +53 -0
- package/vendor/tunnel-core/dist/flows/http.d.ts +50 -0
- package/vendor/tunnel-core/dist/flows/http.js +585 -0
- package/vendor/tunnel-core/dist/flows/tcp.d.ts +60 -0
- package/vendor/tunnel-core/dist/flows/tcp.js +546 -0
- package/vendor/tunnel-core/dist/flows/udp.d.ts +41 -0
- package/vendor/tunnel-core/dist/flows/udp.js +376 -0
- package/vendor/tunnel-core/dist/lib/adapt-logger.d.ts +2 -0
- package/vendor/tunnel-core/dist/lib/adapt-logger.js +6 -0
- package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +13 -0
- package/vendor/tunnel-core/dist/lib/fingerprint.js +95 -0
- package/vendor/tunnel-core/dist/lib/jwt-verifier.d.ts +27 -0
- package/vendor/tunnel-core/dist/lib/jwt-verifier.js +230 -0
- package/vendor/tunnel-core/dist/lib/logger.d.ts +9 -0
- package/vendor/tunnel-core/dist/lib/logger.js +58 -0
- package/vendor/tunnel-core/dist/lib/refresh-helpers.d.ts +16 -0
- package/vendor/tunnel-core/dist/lib/refresh-helpers.js +66 -0
- package/vendor/tunnel-core/dist/lifecycle.d.ts +24 -0
- package/vendor/tunnel-core/dist/lifecycle.js +29 -0
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.d.ts +56 -0
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.js +227 -0
- package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +58 -0
- package/vendor/tunnel-core/dist/mqtt/events/handlers.js +250 -0
- package/vendor/tunnel-core/dist/mqtt/events/index.d.ts +1 -0
- package/vendor/tunnel-core/dist/mqtt/events/index.js +191 -0
- package/vendor/tunnel-core/dist/mqtt/events/schemas.d.ts +498 -0
- package/vendor/tunnel-core/dist/mqtt/events/schemas.js +51 -0
- package/vendor/tunnel-core/dist/mqtt/ingress.d.ts +63 -0
- package/vendor/tunnel-core/dist/mqtt/ingress.js +170 -0
- package/vendor/tunnel-core/dist/state/assignment.d.ts +122 -0
- package/vendor/tunnel-core/dist/state/assignment.js +322 -0
- package/vendor/tunnel-core/dist/state/instance-registry.d.ts +88 -0
- package/{src → vendor/tunnel-core/dist}/state/instance-registry.js +38 -53
- package/vendor/tunnel-core/dist/tcp-v1/framing.d.ts +43 -0
- package/vendor/tunnel-core/dist/tcp-v1/framing.js +157 -0
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +137 -0
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +1591 -0
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.d.ts +13 -0
- package/{src → vendor/tunnel-core/dist}/tunnel/httpws-constants.js +5 -7
- package/vendor/tunnel-core/dist/tunnel/reconnect.d.ts +76 -0
- package/vendor/tunnel-core/dist/tunnel/reconnect.js +183 -0
- package/vendor/tunnel-core/dist/tunnel/request-sink.d.ts +40 -0
- package/vendor/tunnel-core/dist/tunnel/request-sink.js +39 -0
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +89 -0
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +838 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +90 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +737 -0
- package/vendor/tunnel-core/dist/tunnel/ws-control.d.ts +22 -0
- package/vendor/tunnel-core/dist/tunnel/ws-control.js +124 -0
- package/vendor/tunnel-core/dist/types.d.ts +24 -0
- package/vendor/tunnel-core/dist/types.js +2 -0
- package/vendor/tunnel-core/dist/udp-v1/framing.d.ts +41 -0
- package/vendor/tunnel-core/dist/udp-v1/framing.js +158 -0
- package/vendor/tunnel-core/package.json +34 -0
- package/src/control-plane/MqttControlPlaneClient.js +0 -1196
- package/src/control-plane/MqttControlPlaneTransport.js +0 -150
- package/src/lib/fingerprint.js +0 -115
- package/src/lib/jwt-verifier.js +0 -260
- package/src/lib/logger.js +0 -62
- package/src/mqtt/awaiter-map.js +0 -247
- package/src/mqtt/events/handlers.js +0 -300
- package/src/mqtt/events/index.js +0 -222
- package/src/mqtt/events/schemas.js +0 -58
- package/src/mqtt/ingress.js +0 -182
- package/src/state/assignment.js +0 -333
- package/src/tcp-v1/framing.js +0 -203
- package/src/tunnel/http-tunnel.js +0 -1738
- package/src/tunnel/reconnect.js +0 -182
- package/src/tunnel/tcp-tunnel.js +0 -944
- package/src/tunnel/udp-tunnel.js +0 -813
- package/src/tunnel/ws-control.js +0 -135
- package/src/udp-v1/framing.js +0 -203
package/src/tunnel/reconnect.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
// Story 11.2: reconnect for TCP and UDP tunnels.
|
|
2
|
-
//
|
|
3
|
-
// When the relay connection of an established TCP or UDP tunnel drops, the tunnel
|
|
4
|
-
// reconnects to the same relay and lease with the newest assignment token instead
|
|
5
|
-
// of ending the CLI. Everything bound to the old connection (TCP streams and their
|
|
6
|
-
// local sockets, UDP flows and their sockets, partial frames) is discarded first,
|
|
7
|
-
// so traffic from before the drop can never be delivered on the new connection.
|
|
8
|
-
//
|
|
9
|
-
// Settings (environment):
|
|
10
|
-
// ASYX_TUNNEL_RECONNECT=0 disable (the tunnel ends on a drop, as before)
|
|
11
|
-
// ASYX_TUNNEL_RECONNECT_MAX_MS give up after this long without a connection (default 15 min)
|
|
12
|
-
// ASYX_TUNNEL_RECONNECT_MAX_DELAY_MS longest wait between attempts (default 5000)
|
|
13
|
-
|
|
14
|
-
export const DEFAULT_INITIAL_DELAY_MS = 250;
|
|
15
|
-
export const DEFAULT_MAX_DELAY_MS = 5000;
|
|
16
|
-
export const DEFAULT_MAX_OUTAGE_MS = 15 * 60 * 1000;
|
|
17
|
-
export const DEFAULT_MAX_AUTH_FAILURES = 3;
|
|
18
|
-
|
|
19
|
-
export function reconnectEnabled(env = process.env) {
|
|
20
|
-
return env.ASYX_TUNNEL_RECONNECT !== '0';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function envInt(env, name, fallback) {
|
|
24
|
-
const v = parseInt(env[name] ?? '', 10);
|
|
25
|
-
return Number.isFinite(v) && v > 0 ? v : fallback;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Decide what an established connection's close means.
|
|
30
|
-
* Close code 4000 is the relay's application code: "lease_closed" (the control plane
|
|
31
|
-
* ended the lease) and "lease_reconnected" (another connection took the lease over)
|
|
32
|
-
* must not be fought; anything else (relay restart, network loss, 1006, 1001, 1011,
|
|
33
|
-
* protocol errors) is retried.
|
|
34
|
-
*/
|
|
35
|
-
export function classifyDrop(code, reason) {
|
|
36
|
-
const r = String(reason || '');
|
|
37
|
-
if (code === 4000 && r === 'lease_closed') return { fatal: true, reason: 'lease_closed' };
|
|
38
|
-
if (code === 4000 && r === 'lease_reconnected') return { fatal: true, reason: 'replaced_by_another_connection' };
|
|
39
|
-
return { fatal: false, reason: `ws_close_${code}` };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** HTTP status from a ws handshake error ("Unexpected server response: 403"). */
|
|
43
|
-
export function httpStatusOf(err) {
|
|
44
|
-
if (!err) return null;
|
|
45
|
-
if (Number.isInteger(err.httpStatus)) return err.httpStatus;
|
|
46
|
-
const m = /Unexpected server response: (\d{3})/.exec(err.message || '');
|
|
47
|
-
return m ? Number(m[1]) : null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Decide what a failed reconnect attempt means.
|
|
52
|
-
* 401/403 and token verification failures are counted as authorization failures
|
|
53
|
-
* (the token expired or the lease was closed); a few in a row end the tunnel.
|
|
54
|
-
* 409 means the relay still holds the old connection for the lease; retry.
|
|
55
|
-
* 400 means the request itself is malformed; retrying cannot help.
|
|
56
|
-
*/
|
|
57
|
-
export function classifyFailure(err) {
|
|
58
|
-
const status = httpStatusOf(err);
|
|
59
|
-
if (status === 400) return { fatal: true, auth: false, reason: 'bad_request' };
|
|
60
|
-
if (status === 401 || status === 403) return { fatal: false, auth: true, reason: `http_${status}` };
|
|
61
|
-
if (err && err.code === 'UDP_RELAY_UNAVAILABLE' && /JWT verification failed/.test(err.message || '')) {
|
|
62
|
-
// Failing to fetch the JWKS (network) is not a token problem: keep retrying
|
|
63
|
-
const cause = err.cause || {};
|
|
64
|
-
const fetchProblem = /JWKS|fetch|timeout|ECONN|ENOTFOUND|EAI_AGAIN|network/i.test(`${cause.code || ''} ${cause.message || ''}`)
|
|
65
|
-
&& !/expired|signature|claim/i.test(`${cause.code || ''} ${cause.message || ''}`);
|
|
66
|
-
if (fetchProblem) return { fatal: false, auth: false, reason: 'jwks_unavailable' };
|
|
67
|
-
return { fatal: false, auth: true, reason: 'token_verification_failed' };
|
|
68
|
-
}
|
|
69
|
-
return { fatal: false, auth: false, reason: status ? `http_${status}` : (err?.code || 'connect_failed') };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Seconds-since-epoch exp claim of a JWT, or null. */
|
|
73
|
-
export function jwtExp(jwt) {
|
|
74
|
-
try {
|
|
75
|
-
const part = String(jwt).split('.')[1];
|
|
76
|
-
const payload = JSON.parse(Buffer.from(part.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'));
|
|
77
|
-
return typeof payload.exp === 'number' ? payload.exp : null;
|
|
78
|
-
} catch {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Runs the reconnect loop for one tunnel.
|
|
85
|
-
* connect(): Promise resolving when a new connection is open, rejecting otherwise.
|
|
86
|
-
* onGiveUp(reason): called once if the tunnel cannot be reconnected.
|
|
87
|
-
* onAuthFailure(): optional (Story 10.9). Called on the first authorization failure of a
|
|
88
|
-
* streak; it may fetch a new assignment (for example after the tenant moved to another
|
|
89
|
-
* relay, where the old token's relay no longer matches). If it returns true, the streak
|
|
90
|
-
* is reset and the next attempt uses the new assignment at once.
|
|
91
|
-
*/
|
|
92
|
-
export class Reconnector {
|
|
93
|
-
constructor({
|
|
94
|
-
kind,
|
|
95
|
-
log,
|
|
96
|
-
connect,
|
|
97
|
-
onGiveUp,
|
|
98
|
-
env = process.env,
|
|
99
|
-
initialDelayMs = DEFAULT_INITIAL_DELAY_MS,
|
|
100
|
-
maxDelayMs = envInt(env, 'ASYX_TUNNEL_RECONNECT_MAX_DELAY_MS', DEFAULT_MAX_DELAY_MS),
|
|
101
|
-
maxOutageMs = envInt(env, 'ASYX_TUNNEL_RECONNECT_MAX_MS', DEFAULT_MAX_OUTAGE_MS),
|
|
102
|
-
maxAuthFailures = DEFAULT_MAX_AUTH_FAILURES,
|
|
103
|
-
onAuthFailure = null,
|
|
104
|
-
now = () => Date.now(),
|
|
105
|
-
sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
106
|
-
}) {
|
|
107
|
-
Object.assign(this, { kind, log, connect, onGiveUp, initialDelayMs, maxDelayMs, maxOutageMs, maxAuthFailures, onAuthFailure, now, sleep });
|
|
108
|
-
this.running = false;
|
|
109
|
-
this.stopped = false;
|
|
110
|
-
this.reconnects = 0;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
stop() {
|
|
114
|
-
this.stopped = true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
delayFor(attempt) {
|
|
118
|
-
return Math.min(this.maxDelayMs, this.initialDelayMs * 2 ** attempt);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Start reconnecting after a drop. Resolves when connected, given up or stopped. */
|
|
122
|
-
async run(dropReason) {
|
|
123
|
-
if (this.running || this.stopped) return;
|
|
124
|
-
this.running = true;
|
|
125
|
-
const started = this.now();
|
|
126
|
-
let authFailures = 0;
|
|
127
|
-
let reassigned = false;
|
|
128
|
-
this.log.warn({ kind: this.kind, reason: dropReason }, `${this.kind}.reconnect.start`);
|
|
129
|
-
try {
|
|
130
|
-
for (let attempt = 0; ; attempt++) {
|
|
131
|
-
if (!reassigned) await this.sleep(this.delayFor(attempt));
|
|
132
|
-
reassigned = false;
|
|
133
|
-
if (this.stopped) return;
|
|
134
|
-
const outageMs = this.now() - started;
|
|
135
|
-
if (outageMs > this.maxOutageMs) {
|
|
136
|
-
this.giveUp('outage_limit', { outageMs, attempts: attempt });
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
try {
|
|
140
|
-
await this.connect();
|
|
141
|
-
this.reconnects += 1;
|
|
142
|
-
this.log.info({ kind: this.kind, attempts: attempt + 1, outageMs: this.now() - started, reconnects: this.reconnects },
|
|
143
|
-
`${this.kind}.reconnect.ok`);
|
|
144
|
-
return;
|
|
145
|
-
} catch (err) {
|
|
146
|
-
if (this.stopped) return;
|
|
147
|
-
const verdict = classifyFailure(err);
|
|
148
|
-
this.log.warn({ kind: this.kind, attempt: attempt + 1, reason: verdict.reason, error: err?.message },
|
|
149
|
-
`${this.kind}.reconnect.attempt_failed`);
|
|
150
|
-
if (verdict.fatal) {
|
|
151
|
-
this.giveUp(verdict.reason, { attempts: attempt + 1 });
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
authFailures = verdict.auth ? authFailures + 1 : 0;
|
|
155
|
-
if (authFailures === 1 && this.onAuthFailure) {
|
|
156
|
-
let got = false;
|
|
157
|
-
try { got = await this.onAuthFailure(); } catch (e) {
|
|
158
|
-
this.log.warn({ kind: this.kind, error: e?.message }, `${this.kind}.reconnect.reassign_failed`);
|
|
159
|
-
}
|
|
160
|
-
if (got) {
|
|
161
|
-
this.log.info({ kind: this.kind, attempt: attempt + 1 }, `${this.kind}.reconnect.reassigned`);
|
|
162
|
-
authFailures = 0;
|
|
163
|
-
reassigned = true;
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (authFailures >= this.maxAuthFailures) {
|
|
168
|
-
this.giveUp(`${verdict.reason}_repeated`, { attempts: attempt + 1 });
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
} finally {
|
|
174
|
-
this.running = false;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
giveUp(reason, extra = {}) {
|
|
179
|
-
this.log.error({ kind: this.kind, reason, ...extra }, `${this.kind}.reconnect.gave_up`);
|
|
180
|
-
this.onGiveUp(reason);
|
|
181
|
-
}
|
|
182
|
-
}
|