asyx 0.2.3 → 0.3.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 +101 -1
- package/package.json +10 -3
- package/src/commands/tunnel.js +1281 -48
- package/src/commands/validate-target-host.js +68 -0
- package/src/lib/jwt-verifier.js +260 -0
- package/src/mqtt/events/handlers.js +103 -13
- package/src/mqtt/events/index.js +203 -0
- package/src/state/assignment.js +139 -35
- package/src/state/instance-registry.js +130 -0
- package/src/tunnel/http-tunnel.js +20 -15
- package/src/tunnel/tcp-tunnel.js +27 -30
- package/src/tunnel/udp-tunnel.js +648 -0
- package/src/udp-v1/framing.js +203 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UDP-v1 (udpWs-v1) Framing Protocol
|
|
3
|
+
*
|
|
4
|
+
* Binary frame format (same as tcpWs-v1):
|
|
5
|
+
* [frameType:1 byte][flowId:4 bytes (big-endian)][length:4 bytes (big-endian)][payload:length bytes]
|
|
6
|
+
*
|
|
7
|
+
* Frame types:
|
|
8
|
+
* - 0x10: UDP_DGRAM
|
|
9
|
+
* - 0x11: UDP_CLOSE
|
|
10
|
+
* - 0x05: PING
|
|
11
|
+
* - 0x06: PONG
|
|
12
|
+
*
|
|
13
|
+
* Max payload size: 64 KiB (65536 bytes)
|
|
14
|
+
* FlowId: 32-bit value (big-endian)
|
|
15
|
+
*
|
|
16
|
+
* Reference: docs/v4.1/v4.1_tcpWs_udpWs_protocol_v1.md
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export const FrameType = {
|
|
20
|
+
UDP_DGRAM: 0x10,
|
|
21
|
+
UDP_CLOSE: 0x11,
|
|
22
|
+
PING: 0x05,
|
|
23
|
+
PONG: 0x06,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const MAX_FRAME_PAYLOAD = 64 * 1024; // 64 KiB
|
|
27
|
+
export const FRAME_HEADER_SIZE = 9; // 1 byte type + 4 bytes flowId + 4 bytes length
|
|
28
|
+
|
|
29
|
+
export class UdpV1FramingError extends Error {
|
|
30
|
+
constructor(message, code) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = 'UdpV1FramingError';
|
|
33
|
+
this.code = code;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Encode a frame to binary format
|
|
39
|
+
* @param {Object} frame - Frame object with type, flowId, and payload
|
|
40
|
+
* @param {number} frame.type - Frame type (FrameType constant)
|
|
41
|
+
* @param {number} frame.flowId - Flow ID (32-bit, 0 for PING/PONG)
|
|
42
|
+
* @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
|
|
43
|
+
* @returns {Buffer} Encoded frame
|
|
44
|
+
* @throws {UdpV1FramingError} If frame is invalid
|
|
45
|
+
*/
|
|
46
|
+
export function encodeFrame(frame) {
|
|
47
|
+
const { type, flowId = 0, payload = Buffer.alloc(0) } = frame;
|
|
48
|
+
|
|
49
|
+
// Validate frame type
|
|
50
|
+
const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
|
|
51
|
+
if (!validTypes.includes(type)) {
|
|
52
|
+
throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Convert payload to Buffer if needed
|
|
56
|
+
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
57
|
+
|
|
58
|
+
// Validate payload size
|
|
59
|
+
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
60
|
+
throw new UdpV1FramingError(
|
|
61
|
+
`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
62
|
+
'PAYLOAD_TOO_LARGE'
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// FlowID validation (PING/PONG can have 0)
|
|
67
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
68
|
+
if (flowId === 0) {
|
|
69
|
+
throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_ID');
|
|
70
|
+
}
|
|
71
|
+
// Ensure flowId fits in 32 bits
|
|
72
|
+
if (flowId > 0xFFFFFFFF) {
|
|
73
|
+
throw new UdpV1FramingError(`FlowID ${flowId} exceeds 32-bit limit`, 'INVALID_FLOW_ID');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Create frame buffer
|
|
78
|
+
const frameBuf = Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadBuf.length);
|
|
79
|
+
|
|
80
|
+
// Write frame type (1 byte)
|
|
81
|
+
frameBuf[0] = type;
|
|
82
|
+
|
|
83
|
+
// Write flowId (4 bytes, big-endian)
|
|
84
|
+
frameBuf.writeUInt32BE(flowId, 1);
|
|
85
|
+
|
|
86
|
+
// Write payload length (4 bytes, big-endian)
|
|
87
|
+
frameBuf.writeUInt32BE(payloadBuf.length, 5);
|
|
88
|
+
|
|
89
|
+
// Write payload
|
|
90
|
+
if (payloadBuf.length > 0) {
|
|
91
|
+
payloadBuf.copy(frameBuf, FRAME_HEADER_SIZE);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return frameBuf;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Decode a frame from binary format
|
|
99
|
+
* @param {Buffer|Uint8Array} data - Binary frame data
|
|
100
|
+
* @param {number} [offset=0] - Offset to start reading from
|
|
101
|
+
* @returns {Object} Decoded frame object with type, flowId, payload, and consumed bytes
|
|
102
|
+
* @throws {UdpV1FramingError} If frame is invalid
|
|
103
|
+
*/
|
|
104
|
+
export function decodeFrame(data, offset = 0) {
|
|
105
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
106
|
+
|
|
107
|
+
// Check if we have enough data for header
|
|
108
|
+
if (buf.length - offset < FRAME_HEADER_SIZE) {
|
|
109
|
+
throw new UdpV1FramingError(
|
|
110
|
+
`Insufficient data for frame header: need ${FRAME_HEADER_SIZE} bytes, got ${buf.length - offset}`,
|
|
111
|
+
'INCOMPLETE_FRAME'
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// If offset is provided, create a subarray view
|
|
116
|
+
const view = offset > 0 ? buf.subarray(offset) : buf;
|
|
117
|
+
|
|
118
|
+
// Read frame type (1 byte)
|
|
119
|
+
const type = view[0];
|
|
120
|
+
|
|
121
|
+
// Validate frame type
|
|
122
|
+
const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
|
|
123
|
+
if (!validTypes.includes(type)) {
|
|
124
|
+
throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Read flowId (4 bytes, big-endian)
|
|
128
|
+
const flowId = view.readUInt32BE(1);
|
|
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 UdpV1FramingError(
|
|
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 UdpV1FramingError(
|
|
145
|
+
`Insufficient data for frame payload: need ${totalFrameSize} bytes, got ${view.length}`,
|
|
146
|
+
'INCOMPLETE_FRAME'
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// FlowID validation (PING/PONG can have 0)
|
|
151
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
152
|
+
if (flowId === 0) {
|
|
153
|
+
throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_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
|
+
flowId,
|
|
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 {UdpV1FramingError} If frame is invalid
|
|
175
|
+
*/
|
|
176
|
+
export function validateFrame(frame) {
|
|
177
|
+
const { type, flowId = 0, payload = Buffer.alloc(0) } = frame;
|
|
178
|
+
|
|
179
|
+
// Validate frame type
|
|
180
|
+
const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
|
|
181
|
+
if (!validTypes.includes(type)) {
|
|
182
|
+
throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Validate payload size
|
|
186
|
+
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
187
|
+
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
188
|
+
throw new UdpV1FramingError(
|
|
189
|
+
`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
190
|
+
'PAYLOAD_TOO_LARGE'
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// FlowID validation (PING/PONG can have 0)
|
|
195
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
196
|
+
if (flowId === 0) {
|
|
197
|
+
throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_ID');
|
|
198
|
+
}
|
|
199
|
+
if (flowId > 0xFFFFFFFF) {
|
|
200
|
+
throw new UdpV1FramingError(`FlowID ${flowId} exceeds 32-bit limit`, 'INVALID_FLOW_ID');
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|