runlocal 0.6.0 → 0.8.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/lib.js +111 -11
- package/package.json +2 -2
package/lib.js
CHANGED
|
@@ -31,7 +31,7 @@ function parseArgs(argv) {
|
|
|
31
31
|
let apiKey = process.env.RUNLATER_API_KEY || readApiKeyFile();
|
|
32
32
|
let subdomain = null;
|
|
33
33
|
for (let i = 0; i < argv.length; i++) {
|
|
34
|
-
if (argv[i] === "--host" && argv[i + 1]) {
|
|
34
|
+
if ((argv[i] === "--server" || argv[i] === "--host") && argv[i + 1]) {
|
|
35
35
|
host = argv[++i];
|
|
36
36
|
} else if (argv[i] === "--api-key" && argv[i + 1]) {
|
|
37
37
|
apiKey = argv[++i];
|
|
@@ -40,26 +40,27 @@ function parseArgs(argv) {
|
|
|
40
40
|
} else if (argv[i] === "--help" || argv[i] === "-h") {
|
|
41
41
|
console.log("Usage: runlocal <port> [options]");
|
|
42
42
|
console.log("");
|
|
43
|
-
console.log(" Expose localhost to the internet
|
|
43
|
+
console.log(" Expose localhost to the internet. Works with runlocal.eu");
|
|
44
|
+
console.log(" or any self-hosted runlocal server.");
|
|
44
45
|
console.log("");
|
|
45
46
|
console.log("Options:");
|
|
46
|
-
console.log(" --
|
|
47
|
+
console.log(" --server <url> Server URL (default: wss://runlocal.eu)");
|
|
47
48
|
console.log(" --api-key <key> Runlater API key for stable subdomain");
|
|
48
|
-
console.log(" --subdomain <name>
|
|
49
|
+
console.log(" --subdomain <name> Request a specific subdomain");
|
|
49
50
|
console.log(" --help, -h Show this help");
|
|
50
51
|
console.log("");
|
|
51
|
-
console.log("
|
|
52
|
-
console.log("
|
|
53
|
-
console.log("
|
|
54
|
-
console.log(" 3. ~/.runlater/api-key file");
|
|
52
|
+
console.log("Environment variables:");
|
|
53
|
+
console.log(" RUNLOCAL_HOST Server URL (same as --server)");
|
|
54
|
+
console.log(" RUNLATER_API_KEY API key (same as --api-key)");
|
|
55
55
|
console.log("");
|
|
56
56
|
console.log("Examples:");
|
|
57
57
|
console.log(" npx runlocal 3000 Random subdomain");
|
|
58
58
|
console.log(" npx runlocal 3000 --api-key pk_xxx Stable subdomain");
|
|
59
|
-
console.log(" npx runlocal 3000 --subdomain my-api Custom subdomain
|
|
59
|
+
console.log(" npx runlocal 3000 --subdomain my-api Custom subdomain");
|
|
60
|
+
console.log(" npx runlocal 3000 --server wss://tunnel.example.com Self-hosted");
|
|
60
61
|
console.log("");
|
|
61
|
-
console.log("
|
|
62
|
-
console.log("
|
|
62
|
+
console.log("Self-hosting: https://github.com/runlater-eu/runlocal-server");
|
|
63
|
+
console.log("Hosted version: https://runlocal.eu");
|
|
63
64
|
process.exit(0);
|
|
64
65
|
} else if (!argv[i].startsWith("-")) {
|
|
65
66
|
port = parseInt(argv[i], 10);
|
|
@@ -192,6 +193,7 @@ function createConnection(options) {
|
|
|
192
193
|
const ws = new WebSocket(wsUrl);
|
|
193
194
|
let heartbeatTimer = null;
|
|
194
195
|
let joinRef = null;
|
|
196
|
+
const activeWsConnections = new Map();
|
|
195
197
|
|
|
196
198
|
ws.on("open", () => {
|
|
197
199
|
const displayHost = host.replace(/^wss?:\/\//, "");
|
|
@@ -270,6 +272,21 @@ function createConnection(options) {
|
|
|
270
272
|
return;
|
|
271
273
|
}
|
|
272
274
|
|
|
275
|
+
if (event === "ws_upgrade") {
|
|
276
|
+
handleWsUpgrade(ws, joinRef, topic, payload, port, nextRef, log, activeWsConnections, WebSocket);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (event === "ws_client_frame") {
|
|
281
|
+
handleWsClientFrame(payload, activeWsConnections);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (event === "ws_close") {
|
|
286
|
+
handleWsClose(payload, activeWsConnections);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
273
290
|
if (event === "phx_close") {
|
|
274
291
|
log(`${YELLOW}Tunnel closed by server${RESET}`);
|
|
275
292
|
if (onClose) {
|
|
@@ -282,6 +299,10 @@ function createConnection(options) {
|
|
|
282
299
|
|
|
283
300
|
ws.on("close", () => {
|
|
284
301
|
clearInterval(heartbeatTimer);
|
|
302
|
+
for (const [, localWs] of activeWsConnections) {
|
|
303
|
+
try { localWs.close(); } catch {}
|
|
304
|
+
}
|
|
305
|
+
activeWsConnections.clear();
|
|
285
306
|
log(`${YELLOW}Disconnected. Reconnecting in 3s...${RESET}`);
|
|
286
307
|
const reconnectTimer = setTimeout(() => createConnection(options), 3000);
|
|
287
308
|
reconnectTimer.unref();
|
|
@@ -301,6 +322,85 @@ function createConnection(options) {
|
|
|
301
322
|
return { ws, getJoinRef: () => joinRef, nextRef };
|
|
302
323
|
}
|
|
303
324
|
|
|
325
|
+
function handleWsUpgrade(ws, joinRef, topic, payload, port, nextRef, log, activeWsConnections, WebSocket) {
|
|
326
|
+
const { ws_id, path: wsPath, query_string, headers } = payload;
|
|
327
|
+
const fullPath = query_string ? `${wsPath}?${query_string}` : wsPath;
|
|
328
|
+
const timestamp = new Date().toLocaleTimeString();
|
|
329
|
+
|
|
330
|
+
log(`${DIM}${timestamp}${RESET} ${BOLD}WS${RESET} ${fullPath}`);
|
|
331
|
+
|
|
332
|
+
const localWsUrl = `ws://127.0.0.1:${port}${fullPath}`;
|
|
333
|
+
|
|
334
|
+
const reqHeaders = {};
|
|
335
|
+
if (headers) {
|
|
336
|
+
for (const [k, v] of headers) {
|
|
337
|
+
const lower = k.toLowerCase();
|
|
338
|
+
if (lower !== "host" && lower !== "upgrade" && lower !== "connection" &&
|
|
339
|
+
lower !== "sec-websocket-key" && lower !== "sec-websocket-version" &&
|
|
340
|
+
lower !== "sec-websocket-extensions") {
|
|
341
|
+
reqHeaders[k] = v;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let localWs;
|
|
347
|
+
try {
|
|
348
|
+
localWs = new WebSocket(localWsUrl, { headers: reqHeaders });
|
|
349
|
+
} catch (err) {
|
|
350
|
+
log(`${DIM}${timestamp}${RESET} ${RED}WS ERR${RESET} ${fullPath} — ${err.message}`);
|
|
351
|
+
ws.send(JSON.stringify([joinRef, nextRef(), topic, "ws_close", { ws_id }]));
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
activeWsConnections.set(ws_id, localWs);
|
|
356
|
+
|
|
357
|
+
localWs.on("message", (data, isBinary) => {
|
|
358
|
+
const opcode = isBinary ? "binary" : "text";
|
|
359
|
+
const frameData = isBinary ? Buffer.from(data).toString("base64") : data.toString();
|
|
360
|
+
|
|
361
|
+
ws.send(JSON.stringify([
|
|
362
|
+
joinRef,
|
|
363
|
+
nextRef(),
|
|
364
|
+
topic,
|
|
365
|
+
"ws_frame",
|
|
366
|
+
{ ws_id, data: frameData, opcode },
|
|
367
|
+
]));
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
localWs.on("close", () => {
|
|
371
|
+
activeWsConnections.delete(ws_id);
|
|
372
|
+
log(`${DIM}${timestamp}${RESET} ${DIM}WS closed${RESET} ${fullPath}`);
|
|
373
|
+
ws.send(JSON.stringify([joinRef, nextRef(), topic, "ws_close", { ws_id }]));
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
localWs.on("error", (err) => {
|
|
377
|
+
log(`${DIM}${timestamp}${RESET} ${RED}WS ERR${RESET} ${fullPath} — ${err.message}`);
|
|
378
|
+
activeWsConnections.delete(ws_id);
|
|
379
|
+
ws.send(JSON.stringify([joinRef, nextRef(), topic, "ws_close", { ws_id }]));
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function handleWsClientFrame(payload, activeWsConnections) {
|
|
384
|
+
const { ws_id, data, opcode } = payload;
|
|
385
|
+
const localWs = activeWsConnections.get(ws_id);
|
|
386
|
+
if (!localWs || localWs.readyState !== 1) return;
|
|
387
|
+
|
|
388
|
+
if (opcode === "binary") {
|
|
389
|
+
localWs.send(Buffer.from(data, "base64"));
|
|
390
|
+
} else {
|
|
391
|
+
localWs.send(data);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function handleWsClose(payload, activeWsConnections) {
|
|
396
|
+
const { ws_id } = payload;
|
|
397
|
+
const localWs = activeWsConnections.get(ws_id);
|
|
398
|
+
if (localWs) {
|
|
399
|
+
activeWsConnections.delete(ws_id);
|
|
400
|
+
try { localWs.close(); } catch {}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
304
404
|
module.exports = {
|
|
305
405
|
parseArgs,
|
|
306
406
|
filterHeaders,
|
package/package.json
CHANGED