asyx 0.1.3 → 0.2.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 +121 -36
- package/package.json +5 -2
- package/src/commands/tunnel.js +578 -26
- package/src/control-plane/MqttControlPlaneClient.js +7 -2
- package/src/mqtt/events/handlers.js +72 -4
- package/src/state/assignment.js +8 -0
- package/src/tcp-v1/framing.js +203 -0
- package/src/tunnel/http-tunnel.js +27 -8
- package/src/tunnel/tcp-tunnel.js +896 -0
|
@@ -58,8 +58,29 @@ const registry = {
|
|
|
58
58
|
logger.info("qp.enabled", { group: quickpassSlug });
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
// Build relay URL
|
|
62
|
-
|
|
61
|
+
// Build relay URL with fallbacks for missing fields
|
|
62
|
+
// TCP-S4: Add fallbacks to prevent malformed URLs if TCP assignments omit fields
|
|
63
|
+
const proto = data.assignment?.proto || 'wss';
|
|
64
|
+
const fqdn = data.assignment?.fqdn || data.fqdn || 'relay.unknown';
|
|
65
|
+
// TCP-S4: Always use /tcp for TCP tunnels, /agent for HTTP tunnels
|
|
66
|
+
// Detect TCP by checking for publicPort (TCP-specific) or proto === 'tcp'
|
|
67
|
+
const isTcpTunnel = data.assignment?.publicPort !== undefined ||
|
|
68
|
+
data.assignment?.proto === 'tcp' ||
|
|
69
|
+
data.publicPort !== undefined;
|
|
70
|
+
// Always override path for TCP tunnels, regardless of what Lambda returns
|
|
71
|
+
const path = isTcpTunnel ? '/tcp' : (data.assignment?.path || '/agent');
|
|
72
|
+
const url = `${proto}://${fqdn}${path}`;
|
|
73
|
+
|
|
74
|
+
// Log warning if fallbacks were used
|
|
75
|
+
if (!data.assignment?.proto || !data.assignment?.fqdn || !data.assignment?.path) {
|
|
76
|
+
logger.warn('open_ack.relay_url_fallback', {
|
|
77
|
+
requestId: e.requestId,
|
|
78
|
+
proto: data.assignment?.proto || 'missing',
|
|
79
|
+
fqdn: data.assignment?.fqdn || 'missing',
|
|
80
|
+
path: data.assignment?.path || 'missing',
|
|
81
|
+
constructedUrl: url
|
|
82
|
+
});
|
|
83
|
+
}
|
|
63
84
|
|
|
64
85
|
// Store assignment in memory (never to disk)
|
|
65
86
|
const leaseId = e.correlationId || data.leaseId || data.assignment?.leaseId || null;
|
|
@@ -69,6 +90,15 @@ const registry = {
|
|
|
69
90
|
const existingAssignment = getAssignment();
|
|
70
91
|
const isRefresh = existingAssignment !== null;
|
|
71
92
|
|
|
93
|
+
// TCP-S3: Extract TCP-specific fields from assignment
|
|
94
|
+
// E3-S8: Extract SRV status for Minecraft tunnels
|
|
95
|
+
// Reuse fqdn from above (already declared for relayUrl construction)
|
|
96
|
+
const tcpAliasFqdn = data.assignment?.tcpAliasFqdn || data.tcpAliasFqdn || null;
|
|
97
|
+
const publicPort = data.assignment?.publicPort || data.publicPort || null;
|
|
98
|
+
const srvProvisioned = data.assignment?.srvProvisioned === true;
|
|
99
|
+
const srvFqdn = data.assignment?.srvFqdn || null;
|
|
100
|
+
const warnings = data.warnings || data.assignment?.warnings || [];
|
|
101
|
+
|
|
72
102
|
setAssignment({
|
|
73
103
|
requestId: e.requestId,
|
|
74
104
|
relayId: data.relayId,
|
|
@@ -76,10 +106,34 @@ const registry = {
|
|
|
76
106
|
jwt: data.assignment.jwt, // Stored in memory only
|
|
77
107
|
leaseMs: data.leaseMs,
|
|
78
108
|
leaseId,
|
|
79
|
-
quickpassSlug // QPA-CLI-11: Store QuickPass slug for vault UX messaging
|
|
109
|
+
quickpassSlug, // QPA-CLI-11: Store QuickPass slug for vault UX messaging
|
|
110
|
+
// TCP-S3: Store TCP-specific fields
|
|
111
|
+
tcpAliasFqdn,
|
|
112
|
+
publicPort,
|
|
113
|
+
fqdn,
|
|
114
|
+
// E3-S8: Store SRV provisioning status
|
|
115
|
+
srvProvisioned,
|
|
116
|
+
srvFqdn,
|
|
117
|
+
warnings
|
|
80
118
|
});
|
|
81
119
|
|
|
82
|
-
logger.debug("open_ack.assignment_stored", {
|
|
120
|
+
logger.debug("open_ack.assignment_stored", {
|
|
121
|
+
requestId: e.requestId,
|
|
122
|
+
isRefresh,
|
|
123
|
+
quickpassSlug,
|
|
124
|
+
tcpAliasFqdn,
|
|
125
|
+
publicPort,
|
|
126
|
+
hasWarnings: warnings.length > 0
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// TCP-S3: Handle TCP_ALIAS_UNAVAILABLE warning (warn-only, don't fail)
|
|
130
|
+
if (warnings.includes('TCP_ALIAS_UNAVAILABLE') || warnings.some(w => w.code === 'TCP_ALIAS_UNAVAILABLE')) {
|
|
131
|
+
logger.warn('tcp.alias.unavailable', {
|
|
132
|
+
requestId: e.requestId,
|
|
133
|
+
fqdn,
|
|
134
|
+
publicPort
|
|
135
|
+
});
|
|
136
|
+
}
|
|
83
137
|
|
|
84
138
|
// QPA-CLI-11: Suppress all intermediate messages (QP: enabled, Ack received)
|
|
85
139
|
// These will be consolidated into the final vault UX sequence
|
|
@@ -101,6 +155,20 @@ const registry = {
|
|
|
101
155
|
process.exit(1);
|
|
102
156
|
}
|
|
103
157
|
|
|
158
|
+
// TCP-S3: Handle TCP-specific error codes
|
|
159
|
+
if (d.reason === 'TCP_PORT_CAPACITY_EXHAUSTED' || d.reason === 'tcp_port_capacity_exhausted') {
|
|
160
|
+
process.stderr.write(`Relay TCP port capacity exhausted. Please retry later.\n`);
|
|
161
|
+
process.exit(1);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (d.reason === 'TENANT_TCP_LEASE_LIMIT_REACHED' || d.reason === 'tenant_tcp_lease_limit_reached') {
|
|
165
|
+
const relayId = d.relayId || 'unknown';
|
|
166
|
+
process.stderr.write(`Tenant TCP lease limit reached on relay ${relayId}\n`);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// TCP_ALIAS_UNAVAILABLE is handled as a warning in open_ack, not a rejection
|
|
171
|
+
|
|
104
172
|
process.stdout.write(
|
|
105
173
|
`❌ Connection rejected${reason}\n`
|
|
106
174
|
);
|
package/src/state/assignment.js
CHANGED
|
@@ -23,6 +23,14 @@ export function setAssignment(assignment) {
|
|
|
23
23
|
leaseMs: assignment.leaseMs,
|
|
24
24
|
leaseId: assignment.leaseId, // CLI-IMG-WSReconnect: Store leaseId for JWT refresh
|
|
25
25
|
quickpassSlug: assignment.quickpassSlug || null, // QPA-CLI-11: Store QuickPass slug for vault UX messaging
|
|
26
|
+
// TCP-S3: Store TCP-specific fields
|
|
27
|
+
tcpAliasFqdn: assignment.tcpAliasFqdn || null,
|
|
28
|
+
publicPort: assignment.publicPort || null,
|
|
29
|
+
fqdn: assignment.fqdn || null,
|
|
30
|
+
// E3-S8: Store SRV provisioning status
|
|
31
|
+
srvProvisioned: assignment.srvProvisioned === true,
|
|
32
|
+
srvFqdn: assignment.srvFqdn || null,
|
|
33
|
+
warnings: assignment.warnings || [],
|
|
26
34
|
timestamp: Date.now(),
|
|
27
35
|
lastRefreshMs: undefined,
|
|
28
36
|
lastRefreshIso: undefined,
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TCP-v1 Framing Protocol
|
|
3
|
+
*
|
|
4
|
+
* Binary frame format:
|
|
5
|
+
* [frameType:1 byte][streamId:4 bytes (big-endian)][length:4 bytes (big-endian)][payload:length bytes]
|
|
6
|
+
*
|
|
7
|
+
* Frame types:
|
|
8
|
+
* - 0x01: STREAM_OPEN
|
|
9
|
+
* - 0x02: STREAM_DATA
|
|
10
|
+
* - 0x03: STREAM_CLOSE
|
|
11
|
+
* - 0x04: STREAM_ERROR
|
|
12
|
+
* - 0x05: PING
|
|
13
|
+
* - 0x06: PONG
|
|
14
|
+
*
|
|
15
|
+
* Max payload size: 64 KiB (65535 bytes)
|
|
16
|
+
* StreamId: 24-bit value (stored as 32-bit, but only lower 24 bits used)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export const FrameType = {
|
|
20
|
+
STREAM_OPEN: 0x01,
|
|
21
|
+
STREAM_DATA: 0x02,
|
|
22
|
+
STREAM_CLOSE: 0x03,
|
|
23
|
+
STREAM_ERROR: 0x04,
|
|
24
|
+
PING: 0x05,
|
|
25
|
+
PONG: 0x06,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const MAX_FRAME_PAYLOAD = 64 * 1024; // 64 KiB
|
|
29
|
+
export const FRAME_HEADER_SIZE = 9; // 1 byte type + 4 bytes streamId + 4 bytes length
|
|
30
|
+
|
|
31
|
+
export class TcpV1FramingError extends Error {
|
|
32
|
+
constructor(message, code) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = 'TcpV1FramingError';
|
|
35
|
+
this.code = code;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Encode a frame to binary format
|
|
41
|
+
* @param {Object} frame - Frame object with type, streamId, and payload
|
|
42
|
+
* @param {number} frame.type - Frame type (FrameType constant)
|
|
43
|
+
* @param {number} frame.streamId - Stream ID (24-bit, 0 for PING/PONG)
|
|
44
|
+
* @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
|
|
45
|
+
* @returns {Buffer} Encoded frame
|
|
46
|
+
* @throws {TcpV1FramingError} If frame is invalid
|
|
47
|
+
*/
|
|
48
|
+
export function encodeFrame(frame) {
|
|
49
|
+
const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
|
|
50
|
+
|
|
51
|
+
// Validate frame type
|
|
52
|
+
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
53
|
+
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Convert payload to Buffer if needed
|
|
57
|
+
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
58
|
+
|
|
59
|
+
// Validate payload size
|
|
60
|
+
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
61
|
+
throw new TcpV1FramingError(
|
|
62
|
+
`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
63
|
+
'PAYLOAD_TOO_LARGE'
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// StreamID validation (PING/PONG can have 0)
|
|
68
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
69
|
+
if (streamId === 0) {
|
|
70
|
+
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
71
|
+
}
|
|
72
|
+
// Ensure streamId fits in 24 bits
|
|
73
|
+
if (streamId > 0x00FFFFFF) {
|
|
74
|
+
throw new TcpV1FramingError(`StreamID ${streamId} exceeds 24-bit limit`, 'INVALID_STREAM_ID');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Create frame buffer
|
|
79
|
+
const frameBuf = Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadBuf.length);
|
|
80
|
+
|
|
81
|
+
// Write frame type (1 byte)
|
|
82
|
+
frameBuf[0] = type;
|
|
83
|
+
|
|
84
|
+
// Write streamId (4 bytes, big-endian, only lower 24 bits used)
|
|
85
|
+
frameBuf.writeUInt32BE(streamId & 0x00FFFFFF, 1);
|
|
86
|
+
|
|
87
|
+
// Write payload length (4 bytes, big-endian)
|
|
88
|
+
frameBuf.writeUInt32BE(payloadBuf.length, 5);
|
|
89
|
+
|
|
90
|
+
// Write payload
|
|
91
|
+
if (payloadBuf.length > 0) {
|
|
92
|
+
payloadBuf.copy(frameBuf, FRAME_HEADER_SIZE);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return frameBuf;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Decode a frame from binary format
|
|
100
|
+
* @param {Buffer|Uint8Array} data - Binary frame data
|
|
101
|
+
* @param {number} [offset=0] - Offset to start reading from
|
|
102
|
+
* @returns {Object} Decoded frame object with type, streamId, payload, and consumed bytes
|
|
103
|
+
* @throws {TcpV1FramingError} If frame is invalid
|
|
104
|
+
*/
|
|
105
|
+
export function decodeFrame(data, offset = 0) {
|
|
106
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
107
|
+
|
|
108
|
+
// Check if we have enough data for header
|
|
109
|
+
if (buf.length - offset < FRAME_HEADER_SIZE) {
|
|
110
|
+
throw new TcpV1FramingError(
|
|
111
|
+
`Insufficient data for frame header: need ${FRAME_HEADER_SIZE} bytes, got ${buf.length - offset}`,
|
|
112
|
+
'INCOMPLETE_FRAME'
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// If offset is provided, create a subarray view
|
|
117
|
+
const view = offset > 0 ? buf.subarray(offset) : buf;
|
|
118
|
+
|
|
119
|
+
// Read frame type (1 byte)
|
|
120
|
+
const type = view[0];
|
|
121
|
+
|
|
122
|
+
// Validate frame type
|
|
123
|
+
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
124
|
+
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Read streamId (4 bytes, big-endian, only lower 24 bits used)
|
|
128
|
+
const streamId = view.readUInt32BE(1) & 0x00FFFFFF;
|
|
129
|
+
|
|
130
|
+
// Read payload length (4 bytes, big-endian)
|
|
131
|
+
const payloadLength = view.readUInt32BE(5);
|
|
132
|
+
|
|
133
|
+
// Validate payload length
|
|
134
|
+
if (payloadLength > MAX_FRAME_PAYLOAD) {
|
|
135
|
+
throw new TcpV1FramingError(
|
|
136
|
+
`Payload length ${payloadLength} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
137
|
+
'PAYLOAD_TOO_LARGE'
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Check if we have enough data for payload
|
|
142
|
+
const totalFrameSize = FRAME_HEADER_SIZE + payloadLength;
|
|
143
|
+
if (view.length < totalFrameSize) {
|
|
144
|
+
throw new TcpV1FramingError(
|
|
145
|
+
`Insufficient data for frame payload: need ${totalFrameSize} bytes, got ${view.length}`,
|
|
146
|
+
'INCOMPLETE_FRAME'
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// StreamID validation (PING/PONG can have 0)
|
|
151
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
152
|
+
if (streamId === 0) {
|
|
153
|
+
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Read payload
|
|
158
|
+
const payload = payloadLength > 0
|
|
159
|
+
? view.subarray(FRAME_HEADER_SIZE, FRAME_HEADER_SIZE + payloadLength)
|
|
160
|
+
: Buffer.alloc(0);
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
type,
|
|
164
|
+
streamId,
|
|
165
|
+
payload,
|
|
166
|
+
consumed: totalFrameSize,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Validate a frame for protocol violations
|
|
172
|
+
* @param {Object} frame - Frame object to validate
|
|
173
|
+
* @returns {void}
|
|
174
|
+
* @throws {TcpV1FramingError} If frame is invalid
|
|
175
|
+
*/
|
|
176
|
+
export function validateFrame(frame) {
|
|
177
|
+
const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
|
|
178
|
+
|
|
179
|
+
// Validate frame type
|
|
180
|
+
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
181
|
+
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Validate payload size
|
|
185
|
+
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
186
|
+
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
187
|
+
throw new TcpV1FramingError(
|
|
188
|
+
`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
189
|
+
'PAYLOAD_TOO_LARGE'
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// StreamID validation (PING/PONG can have 0)
|
|
194
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
195
|
+
if (streamId === 0) {
|
|
196
|
+
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
197
|
+
}
|
|
198
|
+
if (streamId > 0x00FFFFFF) {
|
|
199
|
+
throw new TcpV1FramingError(`StreamID ${streamId} exceeds 24-bit limit`, 'INVALID_STREAM_ID');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
@@ -545,7 +545,14 @@ export class HttpTunnel {
|
|
|
545
545
|
});
|
|
546
546
|
|
|
547
547
|
this.ws.on('close', (code, reason) => {
|
|
548
|
-
|
|
548
|
+
// QPA-CLI-10: Normal closures (code 1000) are debug-level, not warnings
|
|
549
|
+
const reasonStr = reason?.toString() || '';
|
|
550
|
+
const isNormalClose = code === 1000 && (reasonStr === 'closing' || reasonStr === '');
|
|
551
|
+
if (isNormalClose) {
|
|
552
|
+
this.log.debug({ code, reason: reasonStr }, 'Tunnel WS closed');
|
|
553
|
+
} else {
|
|
554
|
+
this.log.warn({ code, reason: reasonStr }, 'Tunnel WS closed');
|
|
555
|
+
}
|
|
549
556
|
// CRT-Relay-E: Stop heartbeat on close
|
|
550
557
|
this.stopHeartbeat();
|
|
551
558
|
// CLI-REAUTH: Cleanup control frame waiters on socket close
|
|
@@ -606,13 +613,25 @@ export class HttpTunnel {
|
|
|
606
613
|
|
|
607
614
|
this.retryCount++;
|
|
608
615
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
+
// QPA-CLI-10: Normal closures (code 1000) are debug-level, not warnings
|
|
617
|
+
const isNormalClose = code === 1000 && (reasonStr === 'closing' || reasonStr === '');
|
|
618
|
+
if (isNormalClose) {
|
|
619
|
+
this.log.debug({
|
|
620
|
+
delay,
|
|
621
|
+
retryCount: this.retryCount,
|
|
622
|
+
maxRetries: this.maxRetries,
|
|
623
|
+
code,
|
|
624
|
+
reason: reasonStr
|
|
625
|
+
}, 'Tunnel WS reconnect scheduled');
|
|
626
|
+
} else {
|
|
627
|
+
this.log.warn({
|
|
628
|
+
delay,
|
|
629
|
+
retryCount: this.retryCount,
|
|
630
|
+
maxRetries: this.maxRetries,
|
|
631
|
+
code,
|
|
632
|
+
reason: reasonStr
|
|
633
|
+
}, 'Tunnel WS reconnect scheduled');
|
|
634
|
+
}
|
|
616
635
|
|
|
617
636
|
// Schedule reconnect attempt
|
|
618
637
|
this.reconnectTimer = setTimeout(() => {
|