asyx 0.5.7 → 0.5.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/README.md +33 -9
- package/package.json +4 -1
- package/src/commands/tunnel.js +153 -19
- package/src/main.js +16 -6
- package/src/mqtt/events/handlers.js +1 -1
- package/src/tunnel/http-tunnel.js +154 -13
- package/src/tunnel/reconnect.js +182 -0
- package/src/tunnel/tcp-tunnel.js +135 -55
- package/src/tunnel/udp-tunnel.js +182 -55
package/README.md
CHANGED
|
@@ -114,7 +114,8 @@ asyx tunnel --http --port 3000
|
|
|
114
114
|
|
|
115
115
|
- Forwards to `127.0.0.1:3000` (loopback only).
|
|
116
116
|
- Prints your stable `https://<subdomain>.tunnel.asyx.ai` URL.
|
|
117
|
-
- TLS and
|
|
117
|
+
- TLS and token refresh handled automatically; your app keeps its port.
|
|
118
|
+
- If the connection drops (network loss, laptop wake, Asyx server maintenance), the CLI reconnects on its own and the same URL comes back.
|
|
118
119
|
|
|
119
120
|
### Private demos (Quickpass)
|
|
120
121
|
|
|
@@ -152,11 +153,21 @@ asyx tunnel --minecraft [--port 25565] [--name <alias>]
|
|
|
152
153
|
|
|
153
154
|
After a tunnel opens, the CLI prints the public `fqdn` (and port for TCP/UDP) plus the local target (`tcp://127.0.0.1:<port>` or `udp://127.0.0.1:<port>`).
|
|
154
155
|
|
|
156
|
+
If the connection of a TCP or UDP tunnel drops (network loss, Asyx server maintenance, or Asyx moving your tunnel to another server), the CLI reconnects on its own and keeps the same hostname and public port. Visitor connections that were open at the moment of the drop end; new ones work as soon as the tunnel is back, usually within seconds. The CLI ends instead if the tunnel was closed by the service or opened elsewhere, or if it cannot reconnect for 15 minutes.
|
|
157
|
+
|
|
158
|
+
| Variable | Effect |
|
|
159
|
+
| --- | --- |
|
|
160
|
+
| `ASYX_TUNNEL_RECONNECT=0` | Turn reconnecting off (the tunnel ends on a drop) |
|
|
161
|
+
| `ASYX_TUNNEL_RECONNECT_MAX_MS` | Give up after this long without a connection (default 15 minutes) |
|
|
162
|
+
| `ASYX_TUNNEL_RECONNECT_MAX_DELAY_MS` | Longest wait between attempts (default 5 seconds) |
|
|
163
|
+
|
|
155
164
|
---
|
|
156
165
|
|
|
157
166
|
## Command quick reference
|
|
158
167
|
|
|
159
168
|
```bash
|
|
169
|
+
asyx help # commands; asyx tunnel --help for tunnel options
|
|
170
|
+
asyx --version
|
|
160
171
|
asyx setup
|
|
161
172
|
asyx tunnel --http --port <n> [--secgroup <slug>]
|
|
162
173
|
asyx tunnel --tcp --port <n> [--name <alias>] [--external-port <p>]
|
|
@@ -166,15 +177,27 @@ asyx tunnel --minecraft [--port <n>] [--name <alias>]
|
|
|
166
177
|
|
|
167
178
|
---
|
|
168
179
|
|
|
169
|
-
## What's new in 0.5.
|
|
180
|
+
## What's new in 0.5.9
|
|
181
|
+
|
|
182
|
+
**TCP and UDP tunnels reconnect on their own** — after a network drop or Asyx server maintenance, TCP and UDP tunnels come back with the same hostname and public port, without rerunning the CLI. Previously they ended and had to be started again. See [Other tunnel types](#other-tunnel-types) for the settings.
|
|
183
|
+
|
|
184
|
+
**Tunnels follow when Asyx moves them** — Asyx can move your tunnel to another server (to balance load or during maintenance). HTTP, TCP and UDP tunnels reconnect to the new server by themselves; your URL, hostname and ports stay the same.
|
|
185
|
+
|
|
186
|
+
**HTTP tunnels ride out server restarts** — an HTTP tunnel no longer crashes or waits 30 seconds when its server restarts; it reconnects within seconds. Very large responses that cannot be relayed now fail only that request (502) instead of closing the whole tunnel.
|
|
187
|
+
|
|
188
|
+
**Clean installs** — every package the CLI loads is now declared, so installs with strict package managers (and nested npm installs) work.
|
|
189
|
+
|
|
190
|
+
**Clearer help** — `asyx help` / `asyx --help` and `asyx tunnel --help` list the commands and options with examples.
|
|
191
|
+
|
|
192
|
+
## Earlier in 0.5.x
|
|
170
193
|
|
|
171
|
-
**DNS outage keeps the tunnel up (0.5.7)** — if the
|
|
194
|
+
**DNS outage keeps the tunnel up (0.5.7)** — if the service hostname fails to resolve, the CLI waits and retries instead of exiting.
|
|
172
195
|
|
|
173
|
-
**Refresh after a control
|
|
196
|
+
**Refresh after a control reconnect (0.5.6)** — if the control connection drops and reconnects, the next token refresh keeps the tunnel up instead of exiting.
|
|
174
197
|
|
|
175
|
-
**Reconnect after a DNS blip (0.5.5)** — if the network drops before the
|
|
198
|
+
**Reconnect after a DNS blip (0.5.5)** — if the network drops before the secure control connection is established, the CLI stays up and retries.
|
|
176
199
|
|
|
177
|
-
**Tunnel-only CLI (0.5.0)** — npm ships tunnel commands only: `setup
|
|
200
|
+
**Tunnel-only CLI (0.5.0)** — npm ships the tunnel commands only: `setup` and `tunnel`. Focused on secure localhost exposure (HTTP, TCP, UDP). Docs at https://www.asyx.ai/docs
|
|
178
201
|
|
|
179
202
|
**Reliable global install (0.5.2)** — `asyx setup` uses Node's built-in Web Crypto. `npm install -g asyx` works on Node 18+ without module-resolution errors.
|
|
180
203
|
|
|
@@ -224,16 +247,17 @@ Levels: `error` | `warn` | `info` | `debug` (default `info`).
|
|
|
224
247
|
| `asyx: command not found` | `npm install -g asyx` or `npx asyx@latest` |
|
|
225
248
|
| Setup browser did not open | Run `asyx setup` again; open the printed URL manually |
|
|
226
249
|
| Tunnel URL 502 / unreachable | Dev server not running on the same port |
|
|
227
|
-
| Link dies overnight | Expected—tunnel
|
|
250
|
+
| Link dies overnight | Expected—the tunnel lives only while the CLI runs and the machine is awake |
|
|
251
|
+
| TCP/UDP tunnel ended after an outage | It retries for 15 minutes, then exits; run the same command again (raise `ASYX_TUNNEL_RECONNECT_MAX_MS` for longer outages) |
|
|
228
252
|
| External port rejected | `--external-port` must be inside your assigned 10-port block |
|
|
229
|
-
| Windows
|
|
253
|
+
| Windows: secure connection fails at startup | Re-run `asyx setup` to regenerate the device key |
|
|
230
254
|
|
|
231
255
|
---
|
|
232
256
|
|
|
233
257
|
## Security
|
|
234
258
|
|
|
235
259
|
- Device-bound identity per host; hardware fingerprint hashed locally.
|
|
236
|
-
- Mutual TLS
|
|
260
|
+
- Mutual TLS with a per-device certificate; short-lived signed tunnel tokens.
|
|
237
261
|
- Forward-only proxy—your app stays isolated; no direct inbound to your host.
|
|
238
262
|
- Report issues: [security@asyx.ai](mailto:security@asyx.ai)
|
|
239
263
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "asyx",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.9",
|
|
4
4
|
"description": "Asyx tunnel CLI — secure localhost exposure (HTTP, TCP, UDP)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"src/tcp-v1/framing.js",
|
|
59
59
|
"src/tunnel/http-tunnel.js",
|
|
60
60
|
"src/tunnel/httpws-constants.js",
|
|
61
|
+
"src/tunnel/reconnect.js",
|
|
61
62
|
"src/tunnel/tcp-tunnel.js",
|
|
62
63
|
"src/tunnel/udp-tunnel.js",
|
|
63
64
|
"src/tunnel/ws-control.js",
|
|
@@ -69,10 +70,12 @@
|
|
|
69
70
|
"dependencies": {
|
|
70
71
|
"@asyx/mqtt-broker-aws": "^0.1.0",
|
|
71
72
|
"@asyx/mqtt-client": "^0.1.0",
|
|
73
|
+
"@asyx/mqtt-core": "^0.1.0",
|
|
72
74
|
"@peculiar/x509": "^1.14.0",
|
|
73
75
|
"chalk": "^5.3.0",
|
|
74
76
|
"hw-fingerprint": "^3.2.0",
|
|
75
77
|
"jose": "^5.10.0",
|
|
78
|
+
"mqtt": "^5.3.4",
|
|
76
79
|
"open": "^9.1.0",
|
|
77
80
|
"ulid": "^2.4.0",
|
|
78
81
|
"ws": "^8.14.2",
|
package/src/commands/tunnel.js
CHANGED
|
@@ -78,9 +78,11 @@ export async function publishRefreshRequestWithErrorHandling(
|
|
|
78
78
|
|
|
79
79
|
export default async function tunnel(args = []) {
|
|
80
80
|
// Device credential guard - use same path resolution as device.js
|
|
81
|
+
// (help works before enrollment)
|
|
81
82
|
const deviceJsonPath = getDeviceJsonPath();
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
const wantsHelp = args.includes('--help') || args.includes('-h');
|
|
84
|
+
if (!wantsHelp) await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
|
|
85
|
+
console.error('This machine is not set up yet. Run "asyx setup" first (it opens your browser once), then run this command again.');
|
|
84
86
|
process.exit(2);
|
|
85
87
|
});
|
|
86
88
|
|
|
@@ -89,15 +91,25 @@ export default async function tunnel(args = []) {
|
|
|
89
91
|
|
|
90
92
|
if (parsed.help || args.length === 0) {
|
|
91
93
|
logger.info("cli.help", {});
|
|
92
|
-
process.stdout.write(`
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
process.stdout.write(`Usage:
|
|
95
|
+
asyx tunnel --http --port <n> [--secgroup <slug>]
|
|
96
|
+
HTTP tunnel: a public HTTPS URL for a web app on localhost:<n>.
|
|
97
|
+
--secgroup <slug> QuickPass security group; only its members can open the URL
|
|
98
|
+
|
|
99
|
+
asyx tunnel --tcp --port <n> [--name <alias>] [--external-port <p>]
|
|
100
|
+
TCP tunnel: a public TCP port for a service on localhost:<n> (databases, SSH, games).
|
|
101
|
+
|
|
102
|
+
asyx tunnel --udp --port <n> [--external-port <p>]
|
|
103
|
+
UDP tunnel: a public UDP port for a service on localhost:<n> (games, realtime apps).
|
|
104
|
+
|
|
105
|
+
asyx tunnel --minecraft [--port <n>] [--name <alias>]
|
|
106
|
+
Minecraft server (default port 25565); players connect with the hostname only.
|
|
107
|
+
|
|
108
|
+
--external-port <p> choose the public port inside your assigned 10-port block
|
|
109
|
+
|
|
110
|
+
Tunnels forward to 127.0.0.1 only and reconnect on their own after a network or
|
|
111
|
+
server interruption. Press Ctrl+C to stop. Docs: https://www.asyx.ai/docs
|
|
112
|
+
`);
|
|
101
113
|
return;
|
|
102
114
|
}
|
|
103
115
|
|
|
@@ -2100,6 +2112,23 @@ async function tcpTunnel(localPort, serviceName, kind = null, requestedExternalP
|
|
|
2100
2112
|
});
|
|
2101
2113
|
}
|
|
2102
2114
|
|
|
2115
|
+
|
|
2116
|
+
// Story 10.9: if the relay refuses this assignment on a reconnect (the tenant was moved to
|
|
2117
|
+
// another relay, so the token's relay no longer matches), open again for the same public
|
|
2118
|
+
// port; the control plane places the new lease on the tenant's current relay.
|
|
2119
|
+
const reassignSamePort = async () => {
|
|
2120
|
+
const { setAssignment: setA, getAssignment: getA } = await import("../state/assignment.js");
|
|
2121
|
+
const cur = getA(instanceIdRef.instanceId) || assignment;
|
|
2122
|
+
const rid = crypto.randomUUID();
|
|
2123
|
+
const again = { ...openPayload, requestId: rid, opts: { ...(openPayload.opts || {}), requestedExternalPort: cur.publicPort ?? assignment.publicPort } };
|
|
2124
|
+
setExpectedRequestId(instanceIdRef.instanceId, rid);
|
|
2125
|
+
await core.publish({ topic: cmdOpenTopic, payload: JSON.stringify(again), qos: 1, retain: false });
|
|
2126
|
+
const next = await waitForAssignment(rid, ACK_TIMEOUT_MS);
|
|
2127
|
+
setA(instanceIdRef.instanceId, next);
|
|
2128
|
+
logger.info("tcp.reassign.ok", { leaseId: next.leaseId, relayId: next.relayId, publicPort: next.publicPort });
|
|
2129
|
+
return { relayUrl: next.relayUrl, jwt: next.jwt, leaseId: next.leaseId, relayId: next.relayId };
|
|
2130
|
+
};
|
|
2131
|
+
|
|
2103
2132
|
// TCP-S4: Connect to relay using TcpTunnel for data-plane readiness
|
|
2104
2133
|
await connectToRelayTcp(
|
|
2105
2134
|
assignment,
|
|
@@ -2109,7 +2138,8 @@ async function tcpTunnel(localPort, serviceName, kind = null, requestedExternalP
|
|
|
2109
2138
|
ingress,
|
|
2110
2139
|
{ isControlPlaneConnected, waitForControlPlane, getLastMqttError, getForceReconnect, controlPlaneTimeoutMs: CONTROL_PLANE_TIMEOUT_MS },
|
|
2111
2140
|
tunnelInstanceRef, // QPA-CLI-01: Shared reference - tunnel will be stored here
|
|
2112
|
-
mqttClient // QPA-CLI-05: Pass mqttClient for refresh scheduling (not used for TCP yet)
|
|
2141
|
+
mqttClient, // QPA-CLI-05: Pass mqttClient for refresh scheduling (not used for TCP yet)
|
|
2142
|
+
reassignSamePort
|
|
2113
2143
|
);
|
|
2114
2144
|
|
|
2115
2145
|
} catch (error) {
|
|
@@ -2193,14 +2223,19 @@ export async function connectToRelayTcp(
|
|
|
2193
2223
|
coordinatorIngress,
|
|
2194
2224
|
controlPlaneHelpers,
|
|
2195
2225
|
tunnelInstanceRef, // QPA-CLI-01: Pass reference to store tunnel instance
|
|
2196
|
-
mqttClient // QPA-CLI-05: Pass mqttClient for refresh scheduling (not used for TCP yet)
|
|
2226
|
+
mqttClient, // QPA-CLI-05: Pass mqttClient for refresh scheduling (not used for TCP yet)
|
|
2227
|
+
reassign = null // Story 10.9: new assignment after a relay refuses the old one
|
|
2197
2228
|
) {
|
|
2198
2229
|
// Import TCP tunnel logic
|
|
2199
2230
|
const { TcpTunnel } = await import("../tunnel/tcp-tunnel.js");
|
|
2231
|
+
const { getAssignment: getStoredAssignment } = await import("../state/assignment.js");
|
|
2200
2232
|
|
|
2201
2233
|
const tunnel = new TcpTunnel({
|
|
2202
2234
|
relayUrl: assignment.relayUrl,
|
|
2203
2235
|
jwt: assignment.jwt, // Use JWT as-is (no validation)
|
|
2236
|
+
// Story 11.2: reconnects use the newest token (kept fresh by the TPB-17 refresh)
|
|
2237
|
+
getJwt: () => (mqttClient ? getStoredAssignment(mqttClient.getClientId())?.jwt : null) || null,
|
|
2238
|
+
reassign,
|
|
2204
2239
|
deviceId,
|
|
2205
2240
|
requestId: assignment.requestId,
|
|
2206
2241
|
localPort,
|
|
@@ -2494,11 +2529,66 @@ async function udpTunnel(targetPort, requestedExternalPort = null, targetHost =
|
|
|
2494
2529
|
process.stderr.write(`❌ Fatal control-plane error: ${reason}\n`);
|
|
2495
2530
|
process.exit(1);
|
|
2496
2531
|
},
|
|
2497
|
-
//
|
|
2532
|
+
// Story 11.2: UDP refresh (control plane UDP-S3), so a reconnect after the first
|
|
2533
|
+
// token expires still has a valid token. Same flow as TCP (TPB-17); failures are
|
|
2534
|
+
// non-fatal: the open tunnel keeps running.
|
|
2498
2535
|
onRefreshLease: async () => {
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2536
|
+
if (!coordinatorCoreRef || !coordinatorIngressRef) {
|
|
2537
|
+
throw new Error("Coordinator not available for refresh");
|
|
2538
|
+
}
|
|
2539
|
+
const refreshInstanceId = mqttClient.getClientId();
|
|
2540
|
+
const { getAssignment } = await import('../state/assignment.js');
|
|
2541
|
+
const { getFpHash } = await import('../lib/fingerprint.js');
|
|
2542
|
+
const currentAssignment = getAssignment(refreshInstanceId);
|
|
2543
|
+
if (!currentAssignment?.leaseId) {
|
|
2544
|
+
throw new Error(`No current assignment for refresh (instanceId: ${refreshInstanceId})`);
|
|
2545
|
+
}
|
|
2546
|
+
const refreshRequestId = `refresh-${Date.now()}`;
|
|
2547
|
+
const fingerprint = await getFpHash(deviceIdRef);
|
|
2548
|
+
const refreshPayload = {
|
|
2549
|
+
requestId: refreshRequestId,
|
|
2550
|
+
instanceId: refreshInstanceId,
|
|
2551
|
+
refreshOf: currentAssignment.leaseId,
|
|
2552
|
+
proto: 'udp',
|
|
2553
|
+
protocol: 'udp',
|
|
2554
|
+
tunnelMode: 'udpWs-v1',
|
|
2555
|
+
targetHost: targetHostRef,
|
|
2556
|
+
targetPort: targetPortRef,
|
|
2557
|
+
clientId: deviceIdRef,
|
|
2558
|
+
...fingerprint
|
|
2559
|
+
};
|
|
2560
|
+
const cmdOpenTopic = `asyx/tunnel/clients/${deviceIdRef}/cmd/open`;
|
|
2561
|
+
const ACK_TIMEOUT_MS = parseInt(process.env.ASYX_CONTROL_PLANE_ACK_TIMEOUT_MS || "20000", 10);
|
|
2562
|
+
const ackPromise = coordinatorIngressRef.registerAwaiter((message) => {
|
|
2563
|
+
const parsed = message.payload;
|
|
2564
|
+
if (parsed && parsed.requestId === refreshRequestId && parsed.type === 'open_reject') {
|
|
2565
|
+
const rejectError = new Error(`Refresh rejected: ${parsed.data?.reason || 'unknown'}`);
|
|
2566
|
+
rejectError.code = 'REFRESH_REJECTED';
|
|
2567
|
+
throw rejectError;
|
|
2568
|
+
}
|
|
2569
|
+
return !!parsed && parsed.type === 'open_ack' && parsed.requestId === refreshRequestId;
|
|
2570
|
+
}, ACK_TIMEOUT_MS);
|
|
2571
|
+
const publishSuccess = await publishRefreshRequestWithErrorHandling(
|
|
2572
|
+
coordinatorCoreRef, cmdOpenTopic, refreshPayload, refreshRequestId, logger);
|
|
2573
|
+
if (publishSuccess === false) {
|
|
2574
|
+
const bufferedError = new Error('Connection closed during refresh publish');
|
|
2575
|
+
bufferedError.code = 'ECONNRESET';
|
|
2576
|
+
throw bufferedError;
|
|
2577
|
+
}
|
|
2578
|
+
try {
|
|
2579
|
+
await ackPromise;
|
|
2580
|
+
} catch (error) {
|
|
2581
|
+
logger.warn('udp.refresh.failed', { error: error.message, requestId: refreshRequestId, action: 'non_fatal_continue' });
|
|
2582
|
+
return null;
|
|
2583
|
+
}
|
|
2584
|
+
// The open_ack handler stored the new assignment for this instance
|
|
2585
|
+
const exp = jwtExpOf(getAssignment(refreshInstanceId)?.jwt);
|
|
2586
|
+
if (!exp) {
|
|
2587
|
+
logger.warn('udp.refresh.no_exp', { requestId: refreshRequestId });
|
|
2588
|
+
return null;
|
|
2589
|
+
}
|
|
2590
|
+
logger.debug('udp.refresh.success', { leaseId: currentAssignment.leaseId, expMs: exp * 1000, requestId: refreshRequestId });
|
|
2591
|
+
return exp * 1000;
|
|
2502
2592
|
},
|
|
2503
2593
|
});
|
|
2504
2594
|
|
|
@@ -2740,6 +2830,23 @@ async function udpTunnel(targetPort, requestedExternalPort = null, targetHost =
|
|
|
2740
2830
|
clearExpectedRequestId(instanceId);
|
|
2741
2831
|
} catch {}
|
|
2742
2832
|
|
|
2833
|
+
|
|
2834
|
+
// Story 10.9: if the relay refuses this assignment on a reconnect (the tenant was moved to
|
|
2835
|
+
// another relay, so the token's relay no longer matches), open again for the same public
|
|
2836
|
+
// port; the control plane places the new lease on the tenant's current relay.
|
|
2837
|
+
const reassignSamePort = async () => {
|
|
2838
|
+
const { setAssignment: setA, getAssignment: getA } = await import("../state/assignment.js");
|
|
2839
|
+
const cur = getA(instanceIdRef.instanceId) || assignment;
|
|
2840
|
+
const rid = crypto.randomUUID();
|
|
2841
|
+
const again = { ...openPayload, requestId: rid, externalPort: cur.publicPort ?? assignment.publicPort };
|
|
2842
|
+
setExpectedRequestId(instanceIdRef.instanceId, rid);
|
|
2843
|
+
await core.publish({ topic: cmdOpenTopic, payload: JSON.stringify(again), qos: 1, retain: false });
|
|
2844
|
+
const next = await waitForAssignment(rid, ACK_TIMEOUT_MS);
|
|
2845
|
+
setA(instanceIdRef.instanceId, next);
|
|
2846
|
+
logger.info("udp.reassign.ok", { leaseId: next.leaseId, relayId: next.relayId, publicPort: next.publicPort });
|
|
2847
|
+
return { relayUrl: next.relayUrl, jwt: next.jwt, leaseId: next.leaseId, relayId: next.relayId };
|
|
2848
|
+
};
|
|
2849
|
+
|
|
2743
2850
|
// UDP-S1: Connect to relay using UdpTunnel
|
|
2744
2851
|
// UDP-S13: Pass targetHost and targetPort (not localPort that we bind to)
|
|
2745
2852
|
await connectToRelayUdp(
|
|
@@ -2752,7 +2859,8 @@ async function udpTunnel(targetPort, requestedExternalPort = null, targetHost =
|
|
|
2752
2859
|
{ isControlPlaneConnected, waitForControlPlane, getLastMqttError, getForceReconnect, controlPlaneTimeoutMs: CONTROL_PLANE_TIMEOUT_MS },
|
|
2753
2860
|
tunnelInstanceRef,
|
|
2754
2861
|
mqttClient,
|
|
2755
|
-
instanceId
|
|
2862
|
+
instanceId,
|
|
2863
|
+
reassignSamePort
|
|
2756
2864
|
);
|
|
2757
2865
|
|
|
2758
2866
|
} catch (error) {
|
|
@@ -2788,14 +2896,22 @@ export async function connectToRelayUdp(
|
|
|
2788
2896
|
controlPlaneHelpers,
|
|
2789
2897
|
tunnelInstanceRef,
|
|
2790
2898
|
mqttClient,
|
|
2791
|
-
instanceId = null
|
|
2899
|
+
instanceId = null,
|
|
2900
|
+
reassign = null // Story 10.9: new assignment after a relay refuses the old one
|
|
2792
2901
|
) {
|
|
2793
2902
|
// Import UDP tunnel logic
|
|
2794
2903
|
const { UdpTunnel } = await import("../tunnel/udp-tunnel.js");
|
|
2904
|
+
const { getAssignment: getStoredAssignment } = await import("../state/assignment.js");
|
|
2795
2905
|
|
|
2796
2906
|
const tunnel = new UdpTunnel({
|
|
2907
|
+
reassign,
|
|
2797
2908
|
relayUrl: assignment.relayUrl,
|
|
2798
2909
|
jwt: assignment.assignmentJwt || assignment.jwt, // UDP-S1: Use assignmentJwt if available
|
|
2910
|
+
// Story 11.2: reconnects use the newest token (kept fresh by the UDP refresh)
|
|
2911
|
+
getJwt: () => {
|
|
2912
|
+
const stored = instanceId ? getStoredAssignment(instanceId) : null;
|
|
2913
|
+
return stored?.assignmentJwt || stored?.jwt || null;
|
|
2914
|
+
},
|
|
2799
2915
|
deviceId,
|
|
2800
2916
|
requestId: assignment.requestId,
|
|
2801
2917
|
targetHost, // UDP-S13: Target host (must be loopback)
|
|
@@ -2836,6 +2952,13 @@ export async function connectToRelayUdp(
|
|
|
2836
2952
|
|
|
2837
2953
|
try {
|
|
2838
2954
|
await tunnel.open();
|
|
2955
|
+
|
|
2956
|
+
// Story 11.2: schedule the first proactive token refresh (as TCP, TPB-17)
|
|
2957
|
+
const firstExp = jwtExpOf(assignment.assignmentJwt || assignment.jwt);
|
|
2958
|
+
if (mqttClient && firstExp && assignment.leaseId) {
|
|
2959
|
+
mqttClient.scheduleRefresh(firstExp * 1000, assignment.leaseId);
|
|
2960
|
+
logger.debug('udp.refresh.initial_scheduled', { expMs: firstExp * 1000, leaseId: assignment.leaseId });
|
|
2961
|
+
}
|
|
2839
2962
|
|
|
2840
2963
|
// UDP-S1: Log tunnel active
|
|
2841
2964
|
// UDP-S13: Use targetHost and targetPort (not localPort)
|
|
@@ -2929,3 +3052,14 @@ export async function connectToRelayUdp(
|
|
|
2929
3052
|
}
|
|
2930
3053
|
|
|
2931
3054
|
function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
|
|
3055
|
+
|
|
3056
|
+
// Story 11.2: exp claim (seconds) of an assignment token, or null
|
|
3057
|
+
function jwtExpOf(jwt) {
|
|
3058
|
+
try {
|
|
3059
|
+
const part = String(jwt).split('.')[1];
|
|
3060
|
+
const payload = JSON.parse(Buffer.from(part.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'));
|
|
3061
|
+
return typeof payload.exp === 'number' ? payload.exp : null;
|
|
3062
|
+
} catch {
|
|
3063
|
+
return null;
|
|
3064
|
+
}
|
|
3065
|
+
}
|
package/src/main.js
CHANGED
|
@@ -22,14 +22,24 @@ const handleFailure = (label) => (err) => {
|
|
|
22
22
|
|
|
23
23
|
async function run() {
|
|
24
24
|
const cmd = process.argv[2];
|
|
25
|
-
if (!cmd || cmd === "help") {
|
|
26
|
-
console.log(`asyx
|
|
25
|
+
if (!cmd || cmd === "help" || cmd === "--help" || cmd === "-h") {
|
|
26
|
+
console.log(`asyx ${CLI_VERSION}
|
|
27
|
+
|
|
28
|
+
Usage: asyx <command>
|
|
27
29
|
|
|
28
30
|
Commands:
|
|
31
|
+
setup Enroll this machine (opens your browser once)
|
|
32
|
+
tunnel Open an HTTP, TCP or UDP tunnel to a local port
|
|
29
33
|
help Show this help
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
|
|
35
|
+
Examples:
|
|
36
|
+
asyx setup
|
|
37
|
+
asyx tunnel --http --port 3000
|
|
38
|
+
asyx tunnel --tcp --port 5432
|
|
39
|
+
asyx tunnel --udp --port 27015
|
|
40
|
+
|
|
41
|
+
Options for each tunnel type: asyx tunnel --help
|
|
42
|
+
Documentation: https://www.asyx.ai/docs
|
|
33
43
|
`);
|
|
34
44
|
process.exit(0);
|
|
35
45
|
}
|
|
@@ -48,7 +58,7 @@ async function run() {
|
|
|
48
58
|
const { default: tunnelCommand } = await import("./commands/tunnel.js");
|
|
49
59
|
tunnelCommand(process.argv.slice(3)).catch(handleFailure('Tunnel'));
|
|
50
60
|
} else {
|
|
51
|
-
console.log(`Unknown command: ${cmd}
|
|
61
|
+
console.log(`Unknown command: ${cmd}\nRun "asyx help" for the list of commands.`);
|
|
52
62
|
process.exit(1);
|
|
53
63
|
}
|
|
54
64
|
}
|
|
@@ -92,7 +92,7 @@ const registry = {
|
|
|
92
92
|
const isUdpTunnel = data.assignment?.tunnelMode === 'udpWs-v1' ||
|
|
93
93
|
data.assignment?.protocol === 'udp' ||
|
|
94
94
|
instanceContext.tunnelType === 'udp';
|
|
95
|
-
// Always override path for TCP/UDP tunnels, regardless of what
|
|
95
|
+
// Always override path for TCP/UDP tunnels, regardless of what the control plane returns
|
|
96
96
|
let path;
|
|
97
97
|
if (isUdpTunnel) {
|
|
98
98
|
path = '/udp';
|