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/mqtt/awaiter-map.js
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AwaiterMap - Manages request/response correlation for MQTT messages
|
|
3
|
-
*
|
|
4
|
-
* Provides a bounded map of awaiters that can be resolved by incoming messages.
|
|
5
|
-
* Each awaiter has a criteria function and timeout, and will be automatically
|
|
6
|
-
* cleaned up on resolve, reject, or timeout.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export class AwaiterMap {
|
|
10
|
-
constructor(maxSize = 100, logger = null) {
|
|
11
|
-
this.maxSize = maxSize;
|
|
12
|
-
this.awaiters = new Map();
|
|
13
|
-
this.nextId = 1;
|
|
14
|
-
this.logger = logger; // QPA-CLI-10: Optional logger for quiet mode
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Register an awaiter that will be resolved when a message matches the criteria
|
|
19
|
-
* @param {Function} criteria - Function that returns true if message matches
|
|
20
|
-
* @param {number} timeoutMs - Timeout in milliseconds
|
|
21
|
-
* @returns {Promise} Promise that resolves with the matching message
|
|
22
|
-
*/
|
|
23
|
-
register(criteria, timeoutMs = 5000) {
|
|
24
|
-
if (this.awaiters.size >= this.maxSize) {
|
|
25
|
-
throw new Error(`AwaiterMap is at capacity (${this.maxSize}). Cannot register more awaiters.`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const id = this.nextId++;
|
|
29
|
-
const awaiter = {
|
|
30
|
-
id,
|
|
31
|
-
criteria,
|
|
32
|
-
timeoutMs,
|
|
33
|
-
resolve: null,
|
|
34
|
-
reject: null,
|
|
35
|
-
timeoutId: null,
|
|
36
|
-
createdAt: Date.now()
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const promise = new Promise((resolve, reject) => {
|
|
40
|
-
awaiter.resolve = resolve;
|
|
41
|
-
awaiter.reject = reject;
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// Set up timeout
|
|
45
|
-
awaiter.timeoutId = setTimeout(() => {
|
|
46
|
-
this.awaiters.delete(id);
|
|
47
|
-
awaiter.reject(new Error(`Awaiter ${id} timed out after ${timeoutMs}ms`));
|
|
48
|
-
}, timeoutMs);
|
|
49
|
-
|
|
50
|
-
this.awaiters.set(id, awaiter);
|
|
51
|
-
return promise;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Process an incoming message against all registered awaiters
|
|
56
|
-
* @param {Object} message - Message object with topic and payload
|
|
57
|
-
* @returns {boolean} True if any awaiter was resolved
|
|
58
|
-
*/
|
|
59
|
-
processMessage(message) {
|
|
60
|
-
let resolved = false;
|
|
61
|
-
|
|
62
|
-
// Log open_ack messages for debugging awaiter matching
|
|
63
|
-
const isOpenAck = message.payload?.type === 'open_ack';
|
|
64
|
-
const requestId = message.payload?.requestId;
|
|
65
|
-
|
|
66
|
-
if (isOpenAck && requestId) {
|
|
67
|
-
// QPA-CLI-10: Use logger at debug level for quiet mode
|
|
68
|
-
if (this.logger) {
|
|
69
|
-
this.logger.debug('awaiter.open_ack.received', {
|
|
70
|
-
requestId,
|
|
71
|
-
topic: message.topic,
|
|
72
|
-
pendingAwaiters: this.awaiters.size,
|
|
73
|
-
awaiterIds: Array.from(this.awaiters.keys())
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
for (const [id, awaiter] of this.awaiters) {
|
|
79
|
-
try {
|
|
80
|
-
const matched = awaiter.criteria(message);
|
|
81
|
-
|
|
82
|
-
if (isOpenAck && requestId) {
|
|
83
|
-
// QPA-CLI-10: Use logger at debug level for quiet mode
|
|
84
|
-
if (this.logger) {
|
|
85
|
-
this.logger.debug('awaiter.open_ack.checked', {
|
|
86
|
-
requestId,
|
|
87
|
-
awaiterId: id,
|
|
88
|
-
matched,
|
|
89
|
-
topic: message.topic
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (matched) {
|
|
95
|
-
// Clear timeout and resolve
|
|
96
|
-
clearTimeout(awaiter.timeoutId);
|
|
97
|
-
this.awaiters.delete(id);
|
|
98
|
-
awaiter.resolve(message);
|
|
99
|
-
resolved = true;
|
|
100
|
-
|
|
101
|
-
if (isOpenAck && requestId) {
|
|
102
|
-
// QPA-CLI-10: Use logger at debug level for quiet mode
|
|
103
|
-
if (this.logger) {
|
|
104
|
-
this.logger.debug('awaiter.open_ack.resolved', {
|
|
105
|
-
requestId,
|
|
106
|
-
awaiterId: id,
|
|
107
|
-
topic: message.topic
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
} catch (error) {
|
|
113
|
-
// Criteria function threw an error - reject and clean up
|
|
114
|
-
clearTimeout(awaiter.timeoutId);
|
|
115
|
-
this.awaiters.delete(id);
|
|
116
|
-
awaiter.reject(error);
|
|
117
|
-
resolved = true;
|
|
118
|
-
|
|
119
|
-
if (isOpenAck && requestId) {
|
|
120
|
-
// QPA-CLI-10: Use logger at debug level for quiet mode
|
|
121
|
-
if (this.logger) {
|
|
122
|
-
this.logger.debug('awaiter.open_ack.criteria_error', {
|
|
123
|
-
requestId,
|
|
124
|
-
awaiterId: id,
|
|
125
|
-
error: error.message,
|
|
126
|
-
topic: message.topic
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (isOpenAck && requestId && !resolved) {
|
|
134
|
-
// QPA-CLI-10: Use logger at debug level for quiet mode (no_match is expected when awaiter already resolved)
|
|
135
|
-
if (this.logger) {
|
|
136
|
-
this.logger.debug('awaiter.open_ack.no_match', {
|
|
137
|
-
requestId,
|
|
138
|
-
topic: message.topic,
|
|
139
|
-
pendingAwaiters: this.awaiters.size,
|
|
140
|
-
payloadType: message.payload?.type,
|
|
141
|
-
payloadRequestId: message.payload?.requestId
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return resolved;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Get current statistics about the awaiter map
|
|
151
|
-
* @returns {Object} Statistics object
|
|
152
|
-
*/
|
|
153
|
-
getStats() {
|
|
154
|
-
const now = Date.now();
|
|
155
|
-
const ages = Array.from(this.awaiters.values()).map(a => now - a.createdAt);
|
|
156
|
-
|
|
157
|
-
return {
|
|
158
|
-
count: this.awaiters.size,
|
|
159
|
-
maxSize: this.maxSize,
|
|
160
|
-
utilization: this.awaiters.size / this.maxSize,
|
|
161
|
-
oldestAge: ages.length > 0 ? Math.max(...ages) : 0,
|
|
162
|
-
averageAge: ages.length > 0 ? ages.reduce((a, b) => a + b, 0) / ages.length : 0
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Cancel awaiters that match a test criteria
|
|
168
|
-
* @param {Function} testCriteria - Function that returns true if an awaiter should be cancelled
|
|
169
|
-
* @returns {number} Number of awaiters cancelled
|
|
170
|
-
*/
|
|
171
|
-
cancelByCriteria(testCriteria) {
|
|
172
|
-
let cancelled = 0;
|
|
173
|
-
for (const [id, awaiter] of this.awaiters) {
|
|
174
|
-
try {
|
|
175
|
-
if (testCriteria(awaiter)) {
|
|
176
|
-
clearTimeout(awaiter.timeoutId);
|
|
177
|
-
this.awaiters.delete(id);
|
|
178
|
-
awaiter.reject(new Error(`Awaiter ${id} cancelled by criteria`));
|
|
179
|
-
cancelled++;
|
|
180
|
-
}
|
|
181
|
-
} catch (error) {
|
|
182
|
-
// Test criteria threw an error - skip this awaiter
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
return cancelled;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Clear all awaiters (for cleanup)
|
|
191
|
-
*/
|
|
192
|
-
clear() {
|
|
193
|
-
for (const [id, awaiter] of this.awaiters) {
|
|
194
|
-
clearTimeout(awaiter.timeoutId);
|
|
195
|
-
awaiter.reject(new Error(`AwaiterMap cleared - awaiter ${id} cancelled`));
|
|
196
|
-
}
|
|
197
|
-
this.awaiters.clear();
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Create a criteria function for session ID matching (backward compatibility)
|
|
202
|
-
* @param {string} sessionId - Session ID to match
|
|
203
|
-
* @param {string} expectedTopic - Expected topic pattern
|
|
204
|
-
* @returns {Function} Criteria function
|
|
205
|
-
*/
|
|
206
|
-
static createSessionIdCriteria(sessionId, expectedTopic) {
|
|
207
|
-
return (message) => {
|
|
208
|
-
if (message.topic !== expectedTopic) {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
try {
|
|
213
|
-
const raw = message.payload;
|
|
214
|
-
const payload = typeof raw === 'object' && raw !== null && !Buffer.isBuffer(raw)
|
|
215
|
-
? raw
|
|
216
|
-
: JSON.parse(Buffer.isBuffer(raw) ? raw.toString() : String(raw));
|
|
217
|
-
return payload.sessionId === sessionId;
|
|
218
|
-
} catch {
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Create a criteria function for correlation ID matching (future use)
|
|
226
|
-
* @param {string} correlationId - Correlation ID to match
|
|
227
|
-
* @param {string} expectedTopic - Expected topic pattern
|
|
228
|
-
* @returns {Function} Criteria function
|
|
229
|
-
*/
|
|
230
|
-
static createCorrelationIdCriteria(correlationId, expectedTopic) {
|
|
231
|
-
return (message) => {
|
|
232
|
-
if (message.topic !== expectedTopic) {
|
|
233
|
-
return false;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
try {
|
|
237
|
-
const raw = message.payload;
|
|
238
|
-
const payload = typeof raw === 'object' && raw !== null && !Buffer.isBuffer(raw)
|
|
239
|
-
? raw
|
|
240
|
-
: JSON.parse(Buffer.isBuffer(raw) ? raw.toString() : String(raw));
|
|
241
|
-
return payload.correlationId === correlationId;
|
|
242
|
-
} catch {
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
}
|
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
import { AnyEvt as AnyEvtSchema } from "./schemas.js";
|
|
2
|
-
import { setAssignment, getAssignment } from "../../state/assignment.js";
|
|
3
|
-
import logger from "../../lib/logger.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Format milliseconds to human-readable hours:minutes
|
|
7
|
-
* @param {number} ms - Milliseconds
|
|
8
|
-
* @returns {string} Formatted string like "1h 30m" or "45m"
|
|
9
|
-
*/
|
|
10
|
-
function formatLeaseDuration(ms) {
|
|
11
|
-
const totalMinutes = Math.floor(ms / 60000);
|
|
12
|
-
const hours = Math.floor(totalMinutes / 60);
|
|
13
|
-
const minutes = totalMinutes % 60;
|
|
14
|
-
|
|
15
|
-
if (hours > 0 && minutes > 0) {
|
|
16
|
-
return `${hours}h ${minutes}m`;
|
|
17
|
-
} else if (hours > 0) {
|
|
18
|
-
return `${hours}h`;
|
|
19
|
-
} else {
|
|
20
|
-
return `${minutes}m`;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const registry = {
|
|
25
|
-
async setup_required(e) {
|
|
26
|
-
// Minimal user-facing message; CLI should exit(2)
|
|
27
|
-
// Do not add logs; leave control to caller via thrown Error with code.
|
|
28
|
-
const reason = e.data?.reason ? ` (${e.data.reason})` : "";
|
|
29
|
-
const err = new Error(`Please run "asyx setup"${reason}`);
|
|
30
|
-
err.code = "SETUP_REQUIRED";
|
|
31
|
-
throw err;
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
async open_ack(e) {
|
|
35
|
-
// TPB-12: Route by instanceId - get instance context from dispatcher
|
|
36
|
-
const instanceId = e._instanceId;
|
|
37
|
-
const instanceContext = e._instanceContext;
|
|
38
|
-
|
|
39
|
-
if (!instanceId || !instanceContext) {
|
|
40
|
-
logger.warn('open_ack.unknown_instance', {
|
|
41
|
-
requestId: e.requestId,
|
|
42
|
-
instanceId: e.instanceId || e.data?.instanceId || null,
|
|
43
|
-
leaseId: e.correlationId || e.data?.leaseId || null,
|
|
44
|
-
note: 'Event dropped - no matching instance context'
|
|
45
|
-
});
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Normalize legacy format to canonical format
|
|
50
|
-
let data = e.data;
|
|
51
|
-
|
|
52
|
-
// Legacy format: { data: { jwt, relayId, leaseMs } }
|
|
53
|
-
// Canonical format: { data: { relayId, leaseMs, assignment: { jwt, fqdn, proto, path, ... } } }
|
|
54
|
-
if (data.jwt && !data.assignment) {
|
|
55
|
-
// Transform legacy to canonical
|
|
56
|
-
data = {
|
|
57
|
-
relayId: data.relayId,
|
|
58
|
-
leaseMs: data.leaseMs,
|
|
59
|
-
assignment: {
|
|
60
|
-
jwt: data.jwt,
|
|
61
|
-
fqdn: 'relay.customer.example', // Default fallback
|
|
62
|
-
proto: 'wss',
|
|
63
|
-
path: '/agent'
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// QPA-CLI-11: Store QuickPass info in assignment for vault UX messaging
|
|
69
|
-
// Suppress QP: enabled and Ack received messages - they'll be consolidated in final tunnel.active message
|
|
70
|
-
const quickpassSlug = data.quickpass?.slug || null;
|
|
71
|
-
if (quickpassSlug) {
|
|
72
|
-
logger.info("qp.enabled", {
|
|
73
|
-
group: quickpassSlug,
|
|
74
|
-
instanceId,
|
|
75
|
-
tunnelType: instanceContext.tunnelType
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Build relay URL with fallbacks for missing fields
|
|
80
|
-
// TCP-S4: Add fallbacks to prevent malformed URLs if TCP assignments omit fields
|
|
81
|
-
// UDP-S1: Add UDP support
|
|
82
|
-
const proto = data.assignment?.proto || 'wss';
|
|
83
|
-
const fqdn = data.assignment?.fqdn || data.fqdn || 'relay.unknown';
|
|
84
|
-
// TCP-S4: Always use /tcp for TCP tunnels, /agent for HTTP tunnels
|
|
85
|
-
// UDP-S1: Always use /udp for UDP tunnels
|
|
86
|
-
// Detect tunnel type by checking for publicPort (TCP/UDP-specific) or proto/tunnelMode
|
|
87
|
-
const isTcpTunnel = (data.assignment?.publicPort !== undefined ||
|
|
88
|
-
data.assignment?.proto === 'tcp' ||
|
|
89
|
-
data.publicPort !== undefined) &&
|
|
90
|
-
data.assignment?.tunnelMode !== 'udpWs-v1' &&
|
|
91
|
-
data.assignment?.protocol !== 'udp';
|
|
92
|
-
const isUdpTunnel = data.assignment?.tunnelMode === 'udpWs-v1' ||
|
|
93
|
-
data.assignment?.protocol === 'udp' ||
|
|
94
|
-
instanceContext.tunnelType === 'udp';
|
|
95
|
-
// Always override path for TCP/UDP tunnels, regardless of what the control plane returns
|
|
96
|
-
let path;
|
|
97
|
-
if (isUdpTunnel) {
|
|
98
|
-
path = '/udp';
|
|
99
|
-
} else if (isTcpTunnel) {
|
|
100
|
-
path = '/tcp';
|
|
101
|
-
} else {
|
|
102
|
-
path = data.assignment?.path || '/agent';
|
|
103
|
-
}
|
|
104
|
-
const url = `${proto}://${fqdn}${path}`;
|
|
105
|
-
|
|
106
|
-
// Log warning if fallbacks were used
|
|
107
|
-
if (!data.assignment?.proto || !data.assignment?.fqdn || !data.assignment?.path) {
|
|
108
|
-
logger.warn('open_ack.relay_url_fallback', {
|
|
109
|
-
requestId: e.requestId,
|
|
110
|
-
instanceId,
|
|
111
|
-
tunnelType: instanceContext.tunnelType,
|
|
112
|
-
proto: data.assignment?.proto || 'missing',
|
|
113
|
-
fqdn: data.assignment?.fqdn || 'missing',
|
|
114
|
-
path: data.assignment?.path || 'missing',
|
|
115
|
-
constructedUrl: url
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Store assignment in memory (never to disk)
|
|
120
|
-
const leaseId = e.correlationId || data.leaseId || data.assignment?.leaseId || null;
|
|
121
|
-
logger.debug("open_ack.lease_id", {
|
|
122
|
-
requestId: e.requestId,
|
|
123
|
-
instanceId,
|
|
124
|
-
leaseId,
|
|
125
|
-
tunnelType: instanceContext.tunnelType
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// Check if this is a refresh (existing assignment) or initial connection
|
|
129
|
-
const existingAssignment = instanceContext.getAssignment(instanceId);
|
|
130
|
-
const isRefresh = existingAssignment !== null;
|
|
131
|
-
|
|
132
|
-
// TCP-S3: Extract TCP-specific fields from assignment
|
|
133
|
-
// E3-S8: Extract SRV status for Minecraft tunnels
|
|
134
|
-
// Reuse fqdn from above (already declared for relayUrl construction)
|
|
135
|
-
const tcpAliasFqdn = data.assignment?.tcpAliasFqdn || data.tcpAliasFqdn || null;
|
|
136
|
-
const publicPort = data.assignment?.publicPort || data.publicPort || null;
|
|
137
|
-
const srvProvisioned = data.assignment?.srvProvisioned === true;
|
|
138
|
-
const srvFqdn = data.assignment?.srvFqdn || null;
|
|
139
|
-
const warnings = data.warnings || data.assignment?.warnings || [];
|
|
140
|
-
|
|
141
|
-
// Extract JWT with null safety (required for tunnel authentication)
|
|
142
|
-
const jwt = data.assignment?.jwt || data.jwt || null;
|
|
143
|
-
if (!jwt) {
|
|
144
|
-
logger.error('open_ack.missing_jwt', {
|
|
145
|
-
requestId: e.requestId,
|
|
146
|
-
instanceId,
|
|
147
|
-
tunnelType: instanceContext.tunnelType,
|
|
148
|
-
hasAssignment: !!data.assignment,
|
|
149
|
-
hasDataJwt: !!data.jwt
|
|
150
|
-
});
|
|
151
|
-
throw new Error('Missing JWT in assignment');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// UDP-S1: Extract UDP-specific fields from assignment
|
|
155
|
-
const tunnelMode = data.assignment?.tunnelMode || data.tunnelMode || null;
|
|
156
|
-
const protocol = data.assignment?.protocol || data.protocol || null;
|
|
157
|
-
const assignmentJwt = data.assignment?.assignmentJwt || data.assignmentJwt || jwt; // UDP-S1: Use assignmentJwt if available
|
|
158
|
-
|
|
159
|
-
// TPB-12: Use per-instance setAssignment
|
|
160
|
-
instanceContext.setAssignment(instanceId, {
|
|
161
|
-
requestId: e.requestId,
|
|
162
|
-
relayId: data.relayId,
|
|
163
|
-
relayUrl: url,
|
|
164
|
-
jwt: assignmentJwt, // UDP-S1: Use assignmentJwt if available, fallback to jwt
|
|
165
|
-
assignmentJwt: assignmentJwt, // UDP-S1: Store assignmentJwt separately
|
|
166
|
-
leaseMs: data.leaseMs,
|
|
167
|
-
leaseId,
|
|
168
|
-
quickpassSlug, // QPA-CLI-11: Store QuickPass slug for vault UX messaging
|
|
169
|
-
// TCP-S3: Store TCP-specific fields
|
|
170
|
-
tcpAliasFqdn,
|
|
171
|
-
publicPort,
|
|
172
|
-
fqdn,
|
|
173
|
-
// E3-S8: Store SRV provisioning status
|
|
174
|
-
srvProvisioned,
|
|
175
|
-
srvFqdn,
|
|
176
|
-
warnings,
|
|
177
|
-
// UDP-S1: Store UDP-specific fields
|
|
178
|
-
tunnelMode,
|
|
179
|
-
protocol
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
logger.debug("open_ack.assignment_stored", {
|
|
183
|
-
requestId: e.requestId,
|
|
184
|
-
instanceId,
|
|
185
|
-
leaseId,
|
|
186
|
-
tunnelType: instanceContext.tunnelType,
|
|
187
|
-
isRefresh,
|
|
188
|
-
quickpassSlug,
|
|
189
|
-
tcpAliasFqdn,
|
|
190
|
-
publicPort,
|
|
191
|
-
hasWarnings: warnings.length > 0
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// TCP-S3: Handle TCP_ALIAS_UNAVAILABLE warning (warn-only, don't fail)
|
|
195
|
-
if (warnings.includes('TCP_ALIAS_UNAVAILABLE') || warnings.some(w => w.code === 'TCP_ALIAS_UNAVAILABLE')) {
|
|
196
|
-
logger.warn('tcp.alias.unavailable', {
|
|
197
|
-
requestId: e.requestId,
|
|
198
|
-
instanceId,
|
|
199
|
-
tunnelType: instanceContext.tunnelType,
|
|
200
|
-
fqdn,
|
|
201
|
-
publicPort
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// QPA-CLI-11: Suppress all intermediate messages (QP: enabled, Ack received)
|
|
206
|
-
// These will be consolidated into the final vault UX sequence
|
|
207
|
-
},
|
|
208
|
-
|
|
209
|
-
async open_reject(e) {
|
|
210
|
-
// Print rejection reason and keep process alive.
|
|
211
|
-
const d = e.data;
|
|
212
|
-
const reason = d.reason ? ` (${d.reason})` : "";
|
|
213
|
-
|
|
214
|
-
// TPB-12: Clean up expected requestId on rejection (prevents map leak)
|
|
215
|
-
const instanceId = e._instanceId || null;
|
|
216
|
-
if (instanceId) {
|
|
217
|
-
try {
|
|
218
|
-
const { clearExpectedRequestId } = await import("../../state/instance-registry.js");
|
|
219
|
-
clearExpectedRequestId(instanceId);
|
|
220
|
-
} catch (err) {
|
|
221
|
-
// Ignore cleanup errors (module may not be available in all contexts)
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// QPA-04: Handle invalid_secgroup rejection
|
|
226
|
-
if (d.reason === 'invalid_secgroup' || d.reason === 'INVALID_SECGROUP') {
|
|
227
|
-
// Extract slug from requestId context or data if available
|
|
228
|
-
// Note: The slug was sent in the cmd/open payload, but we don't have direct access here
|
|
229
|
-
// The coordinator should include it in the rejection message, but for now we'll use a generic message
|
|
230
|
-
const slug = d.slug || d.secGroupSlug || 'unknown';
|
|
231
|
-
process.stderr.write(`QuickPass group "${slug}" not found. Re-run \`asyx tunnel\` with a valid group.\n`);
|
|
232
|
-
// Exit with code 1 before opening tunnel
|
|
233
|
-
process.exit(1);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// TCP-S3: Handle TCP-specific error codes
|
|
237
|
-
// TPB-03B: Handle new port block error codes
|
|
238
|
-
if (d.reason === 'TCP_PORT_OUT_OF_BLOCK' || d.reason === 'tcp_port_out_of_block') {
|
|
239
|
-
const requestedPort = d.requestedPort || d.requestedExternalPort || 'X';
|
|
240
|
-
const blockStart = d.blockStart || 'A';
|
|
241
|
-
const blockEnd = d.blockEnd || 'B';
|
|
242
|
-
process.stderr.write(`Requested external port ${requestedPort} is outside your assigned block ${blockStart}–${blockEnd}.\n`);
|
|
243
|
-
process.exit(1);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (d.reason === 'TCP_PORT_ALREADY_IN_USE' || d.reason === 'tcp_port_already_in_use') {
|
|
247
|
-
const requestedPort = d.requestedPort || d.requestedExternalPort || 'X';
|
|
248
|
-
process.stderr.write(`External port ${requestedPort} is already in use by another active tunnel.\n`);
|
|
249
|
-
process.exit(1);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if (d.reason === 'TCP_PORT_BLOCK_EXHAUSTED_FOR_TENANT' || d.reason === 'tcp_port_block_exhausted_for_tenant' ||
|
|
253
|
-
d.reason === 'TCP_PORT_BLOCK_EXHAUSTED' || d.reason === 'tcp_port_block_exhausted') {
|
|
254
|
-
const blockStart = d.blockStart || 'A';
|
|
255
|
-
const blockEnd = d.blockEnd || 'B';
|
|
256
|
-
process.stderr.write(`All 10 ports in your block ${blockStart}–${blockEnd} are currently in use.\n`);
|
|
257
|
-
process.exit(1);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (d.reason === 'TCP_PORT_CAPACITY_EXHAUSTED' || d.reason === 'tcp_port_capacity_exhausted') {
|
|
261
|
-
process.stderr.write(`Relay TCP port capacity exhausted. Please retry later.\n`);
|
|
262
|
-
process.exit(1);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (d.reason === 'TENANT_TCP_LEASE_LIMIT_REACHED' || d.reason === 'tenant_tcp_lease_limit_reached') {
|
|
266
|
-
const relayId = d.relayId || 'unknown';
|
|
267
|
-
process.stderr.write(`Tenant TCP lease limit reached on relay ${relayId}\n`);
|
|
268
|
-
process.exit(1);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// TCP_ALIAS_UNAVAILABLE is handled as a warning in open_ack, not a rejection
|
|
272
|
-
|
|
273
|
-
process.stdout.write(
|
|
274
|
-
`❌ Connection rejected${reason}\n`
|
|
275
|
-
);
|
|
276
|
-
},
|
|
277
|
-
|
|
278
|
-
async error(e) {
|
|
279
|
-
const d = e.data || {};
|
|
280
|
-
const err = new Error(d.msg || "Command failed");
|
|
281
|
-
err.code = `ERROR_${String(d.code || "UNKNOWN").toUpperCase()}`;
|
|
282
|
-
throw err;
|
|
283
|
-
},
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
export function getHandler(type) {
|
|
287
|
-
return registry[type];
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// Safe parse helper the caller can reuse if needed
|
|
291
|
-
export function parseAny(raw) {
|
|
292
|
-
try {
|
|
293
|
-
const txt = typeof raw === "string" ? raw : new TextDecoder().decode(raw);
|
|
294
|
-
const json = JSON.parse(txt);
|
|
295
|
-
const parsed = AnyEvtSchema.safeParse(json);
|
|
296
|
-
return parsed.success ? parsed.data : null;
|
|
297
|
-
} catch (e) {
|
|
298
|
-
return null;
|
|
299
|
-
}
|
|
300
|
-
}
|