vyomi-tunnel 1.0.0 → 1.1.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 +15 -0
- package/bin/vyomi-tunnel.mjs +177 -16
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -54,6 +54,21 @@ localStorage.setItem("nano_local_ws", "ws://127.0.0.1:9000/register");
|
|
|
54
54
|
localStorage.setItem("nano_local_ext", "http://127.0.0.1:9000");
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
+
## SSH into Nano VMs (1.1.0+)
|
|
58
|
+
|
|
59
|
+
With in-browser VM compute on in Nano, every EC2 instance, Compute Engine
|
|
60
|
+
instance and Azure VM is a real Linux VM running in your browser tab. The tunnel
|
|
61
|
+
gives each one a TCP port on this machine (127.0.0.1:22000-22999, shown in the
|
|
62
|
+
instance's Connect tab) and bridges it to the VM's SSH server, so a normal
|
|
63
|
+
`ssh` works from your terminal while the Nano tab is open:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
ssh -i ~/.ssh/vyomi-<instance>.pem -p 22000 ec2-user@127.0.0.1
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`GET /health` lists `"vm-tcp"` in `features` and the bridged VMs under `vms`.
|
|
70
|
+
Port range: `VM_PORT_MIN` / `VM_PORT_MAX`.
|
|
71
|
+
|
|
57
72
|
## Notes
|
|
58
73
|
|
|
59
74
|
- **Safari**: connecting an `https://` page to `ws://localhost` can be blocked;
|
package/bin/vyomi-tunnel.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// GENERATED — canonical source: wasm/relay/local-relay.mjs
|
|
3
|
-
// Re-sync:
|
|
3
|
+
// Re-sync: see packaging/tunnel/bin/vyomi-tunnel.mjs header
|
|
4
4
|
/* Vyomi-Nano local relay — the local, zero-cloud reverse-tunnel. The
|
|
5
5
|
* install-once counterpart to the Cloudflare Worker (worker.js): same WS
|
|
6
6
|
* protocol, same external HTTP surface, but everything stays on your machine
|
|
@@ -13,28 +13,51 @@
|
|
|
13
13
|
* Nano tab (Pyodide + conformance cores)
|
|
14
14
|
*
|
|
15
15
|
* The tab connects out to ws://host:PORT/register and is held here; external
|
|
16
|
-
* HTTP requests are forwarded over that WS and correlated by id.
|
|
17
|
-
*
|
|
16
|
+
* HTTP requests are forwarded over that WS and correlated by id. MULTIPLE tabs may
|
|
17
|
+
* register (shared worker + standalone endpoint + extra views); we keep them all,
|
|
18
|
+
* prune dead ones via ping/pong liveness, and forward to the freshest OPEN one —
|
|
19
|
+
* their stores are synced (nano_registry + IndexedDB) so any live tab answers
|
|
20
|
+
* consistently. The Cloudflare version keys this per-session via a Durable Object.
|
|
18
21
|
*
|
|
19
22
|
* A GET /health endpoint (CORS + Private-Network-Access enabled) lets the Nano
|
|
20
23
|
* bundle AUTO-DETECT this relay from an HTTPS tab and prefer it over the cloud
|
|
21
24
|
* tunnel — and detect it the moment it's installed/started, with no reload.
|
|
22
25
|
*
|
|
26
|
+
* Since 1.1.0 it also bridges TCP to Nano's in-browser VMs (EC2 / Compute Engine /
|
|
27
|
+
* Azure VMs, wasm/vm): each VM worker connects to /vm, gets a host port in
|
|
28
|
+
* VM_PORT_MIN..VM_PORT_MAX (default 22000-22999), and `ssh -p <port> user@127.0.0.1`
|
|
29
|
+
* from this machine reaches the VM's sshd (see "In-browser VM TCP bridge" below).
|
|
30
|
+
*
|
|
23
31
|
* Run: vyomi-tunnel (brew) — or: node local-relay.mjs
|
|
24
|
-
* env: RELAY_PORT (default 8090), RELAY_HOST (default 127.0.0.1; set 0.0.0.0 for LAN)
|
|
32
|
+
* env: RELAY_PORT (default 8090), RELAY_HOST (default 127.0.0.1; set 0.0.0.0 for LAN),
|
|
33
|
+
* VM_PORT_MIN / VM_PORT_MAX (ssh ports for in-browser VMs)
|
|
25
34
|
*/
|
|
26
35
|
import http from "node:http";
|
|
36
|
+
import net from "node:net";
|
|
27
37
|
import crypto from "node:crypto";
|
|
28
38
|
import { createRequire } from "node:module";
|
|
29
39
|
// CommonJS require honors NODE_PATH (unlike ESM import), so the `ws` package
|
|
30
40
|
// resolves wherever it's installed (e.g. NODE_PATH=/tmp/node_modules for tests).
|
|
31
41
|
const { WebSocketServer } = createRequire(import.meta.url)(process.env.WS_PKG || "ws");
|
|
32
42
|
|
|
33
|
-
const VERSION = "1.
|
|
43
|
+
const VERSION = "1.1.0";
|
|
34
44
|
const PORT = Number(process.env.RELAY_PORT || 8090);
|
|
35
45
|
const HOST = process.env.RELAY_HOST || "127.0.0.1"; // loopback-only by default; 0.0.0.0 to expose on LAN
|
|
36
46
|
const pending = new Map(); // id -> {resolve}
|
|
37
|
-
|
|
47
|
+
// All registered tab sockets (shared worker, standalone endpoint, extra views).
|
|
48
|
+
// Previously a single `tab` var was OVERWRITTEN on each /register, so N contexts
|
|
49
|
+
// fought — requests routed to whoever registered last (often a mid-boot/empty or
|
|
50
|
+
// orphaned socket) and the losers churned with code-1006. Now we keep every tab,
|
|
51
|
+
// prune dead ones via ping/pong liveness, and forward to the freshest OPEN one.
|
|
52
|
+
// Store state is synced across contexts (nano_registry + IndexedDB), so ANY live
|
|
53
|
+
// tab answers consistently — no need to force-close others (which would war with
|
|
54
|
+
// their auto-reconnect).
|
|
55
|
+
const tabs = new Set(); // live registrant sockets, insertion-ordered
|
|
56
|
+
function pickTab() { // most-recently-registered OPEN socket, or null
|
|
57
|
+
let chosen = null;
|
|
58
|
+
for (const ws of tabs) if (ws.readyState === ws.OPEN) chosen = ws;
|
|
59
|
+
return chosen;
|
|
60
|
+
}
|
|
38
61
|
|
|
39
62
|
// CORS + Private-Network-Access headers so an HTTPS Nano tab (a "public" origin)
|
|
40
63
|
// may probe/reach this loopback ("private") server. Chrome ≥104 gates public→
|
|
@@ -60,17 +83,22 @@ const server = http.createServer((req, res) => {
|
|
|
60
83
|
cors(res, 204); return res.end();
|
|
61
84
|
}
|
|
62
85
|
if (url.pathname === "/health") {
|
|
86
|
+
let open = 0; for (const ws of tabs) if (ws.readyState === ws.OPEN) open++;
|
|
63
87
|
cors(res, 200, { "content-type": "application/json" });
|
|
64
88
|
return res.end(JSON.stringify({
|
|
65
89
|
ok: true, relay: "vyomi-local", version: VERSION,
|
|
66
|
-
tab:
|
|
90
|
+
tab: open > 0, tabs: open,
|
|
91
|
+
// vm-tcp: this tunnel bridges host TCP ports to in-browser VMs (ssh to Nano EC2/GCE/Azure VMs).
|
|
92
|
+
features: ["http", "vm-tcp"],
|
|
93
|
+
vms: [...vmHosts.values()].map((v) => ({ vm: v.vm, port: v.port, host_port: v.hostPort })),
|
|
67
94
|
}));
|
|
68
95
|
}
|
|
69
96
|
// Collect the external request body, forward to the tab, await the response.
|
|
70
97
|
const chunks = [];
|
|
71
98
|
req.on("data", (c) => chunks.push(c));
|
|
72
99
|
req.on("end", () => {
|
|
73
|
-
|
|
100
|
+
const tab = pickTab();
|
|
101
|
+
if (!tab) {
|
|
74
102
|
cors(res, 503, { "content-type": "text/plain" });
|
|
75
103
|
return res.end("no Nano tab registered");
|
|
76
104
|
}
|
|
@@ -87,13 +115,18 @@ const server = http.createServer((req, res) => {
|
|
|
87
115
|
// the relay is authoritative for the actual byte count (a duplicate
|
|
88
116
|
// Content-Length makes undici/fetch reject the response).
|
|
89
117
|
const headers = { "access-control-allow-origin": "*" };
|
|
118
|
+
let declared = null;
|
|
90
119
|
for (const [k, v] of Object.entries(resp.headers || {})) {
|
|
91
|
-
if (k.toLowerCase() === "content-length") continue;
|
|
120
|
+
if (k.toLowerCase() === "content-length") { declared = v; continue; }
|
|
92
121
|
headers[k.toLowerCase()] = v;
|
|
93
122
|
}
|
|
94
|
-
|
|
123
|
+
// A HEAD answer has no body but declares the size of what GET would return (S3's
|
|
124
|
+
// object size): keep the handler's value there. Clients size downloads from it —
|
|
125
|
+
// aws-cli 2.3x writes nothing for an object a HEAD says is 0 bytes.
|
|
126
|
+
const head = req.method === "HEAD";
|
|
127
|
+
headers["content-length"] = head && declared != null ? String(declared) : String(buf.length);
|
|
95
128
|
res.writeHead(resp.status || 200, headers);
|
|
96
|
-
res.end(buf);
|
|
129
|
+
res.end(head ? undefined : buf);
|
|
97
130
|
});
|
|
98
131
|
tab.send(JSON.stringify({
|
|
99
132
|
id, method: req.method, path: url.pathname, query,
|
|
@@ -102,18 +135,145 @@ const server = http.createServer((req, res) => {
|
|
|
102
135
|
});
|
|
103
136
|
});
|
|
104
137
|
|
|
105
|
-
|
|
138
|
+
// Two WebSocket endpoints share the HTTP server, routed by path on upgrade:
|
|
139
|
+
// /register the Nano tab's HTTP-relay socket (above)
|
|
140
|
+
// /vm one socket per in-browser VM: the tunnel opens a host TCP port
|
|
141
|
+
// for it and bridges each accepted connection to the guest port
|
|
142
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
143
|
+
const vmWss = new WebSocketServer({ noServer: true });
|
|
144
|
+
server.on("upgrade", (req, socket, head) => {
|
|
145
|
+
const p = new URL(req.url, "http://x").pathname;
|
|
146
|
+
const target = p === "/register" ? wss : p === "/vm" ? vmWss : null;
|
|
147
|
+
if (!target) { socket.destroy(); return; }
|
|
148
|
+
target.handleUpgrade(req, socket, head, (ws) => target.emit("connection", ws, req));
|
|
149
|
+
});
|
|
106
150
|
wss.on("connection", (ws) => {
|
|
107
|
-
|
|
108
|
-
|
|
151
|
+
ws.isAlive = true;
|
|
152
|
+
tabs.add(ws);
|
|
153
|
+
console.log(`[relay] tab registered (${tabs.size} active)`);
|
|
109
154
|
ws.on("message", (data) => {
|
|
110
155
|
let msg; try { msg = JSON.parse(data.toString()); } catch { return; }
|
|
111
|
-
if (msg.type === "pong") return;
|
|
156
|
+
if (msg.type === "pong") { ws.isAlive = true; return; }
|
|
112
157
|
const done = pending.get(msg.id);
|
|
113
158
|
if (done) { pending.delete(msg.id); done(msg); }
|
|
114
159
|
});
|
|
115
|
-
ws.on("close", () => {
|
|
160
|
+
ws.on("close", () => { tabs.delete(ws); console.log(`[relay] tab gone (${tabs.size} active)`); });
|
|
161
|
+
ws.on("error", () => { try { ws.terminate(); } catch (_) {} tabs.delete(ws); });
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const PING_MS = 10000;
|
|
165
|
+
// ── In-browser VM TCP bridge (ssh to Nano EC2 / Compute Engine / Azure VMs) ──
|
|
166
|
+
// A VM worker connects to /vm?vm=<id>&port=22&prefer=<host port>. We listen on
|
|
167
|
+
// HOST:<host port> (the preferred one if free, else the next free in VM_PORT_MIN..MAX)
|
|
168
|
+
// and tell it {type:"vm-listening", host_port}. Each accepted TCP connection becomes a
|
|
169
|
+
// stream: {type:"tcp-open", sid, port} → the VM dials its guest port and answers
|
|
170
|
+
// {type:"tcp-opened", sid}; bytes flow as binary frames [u32 sid][payload] both ways;
|
|
171
|
+
// either side ends it with {type:"tcp-close", sid}.
|
|
172
|
+
const VM_PORT_MIN = Number(process.env.VM_PORT_MIN || 22000);
|
|
173
|
+
const VM_PORT_MAX = Number(process.env.VM_PORT_MAX || 22999);
|
|
174
|
+
const vmHosts = new Map(); // vm id -> {vm, port, hostPort, ws, server, streams}
|
|
175
|
+
let sidSeq = 1;
|
|
176
|
+
function listenFree(start) {
|
|
177
|
+
return new Promise((resolve, reject) => {
|
|
178
|
+
const span = VM_PORT_MAX - VM_PORT_MIN + 1;
|
|
179
|
+
const first = start >= VM_PORT_MIN && start <= VM_PORT_MAX ? start : VM_PORT_MIN;
|
|
180
|
+
let tries = 0;
|
|
181
|
+
const attempt = (p) => {
|
|
182
|
+
const srv = net.createServer();
|
|
183
|
+
srv.once("error", () => {
|
|
184
|
+
if (++tries >= span) return reject(new Error("no free VM port"));
|
|
185
|
+
attempt(VM_PORT_MIN + ((p - VM_PORT_MIN + 1) % span));
|
|
186
|
+
});
|
|
187
|
+
srv.listen(p, HOST, () => resolve(srv));
|
|
188
|
+
};
|
|
189
|
+
attempt(first);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
vmWss.on("connection", async (ws, req) => {
|
|
193
|
+
const q = new URL(req.url, "http://x").searchParams;
|
|
194
|
+
const vm = q.get("vm") || "", guestPort = Number(q.get("port") || 22);
|
|
195
|
+
if (!/^[A-Za-z0-9._-]{1,80}$/.test(vm)) { ws.close(1008, "bad vm id"); return; }
|
|
196
|
+
const old = vmHosts.get(vm);
|
|
197
|
+
// A VM that reconnects (worker restart, tab reload) keeps its host port.
|
|
198
|
+
const prefer = old ? old.hostPort : Number(q.get("prefer") || 0);
|
|
199
|
+
if (old) { try { old.server.close(); } catch (_) {} try { old.ws.terminate(); } catch (_) {} vmHosts.delete(vm); }
|
|
200
|
+
let srv;
|
|
201
|
+
try { srv = await listenFree(prefer); } catch (e) { ws.close(1013, String(e.message)); return; }
|
|
202
|
+
const hostPort = srv.address().port;
|
|
203
|
+
const entry = { vm, port: guestPort, hostPort, ws, server: srv, streams: new Map() };
|
|
204
|
+
vmHosts.set(vm, entry);
|
|
205
|
+
ws.isAlive = true;
|
|
206
|
+
console.log(`[relay] vm ${vm} :${guestPort} ⇄ ${HOST}:${hostPort}`);
|
|
207
|
+
ws.send(JSON.stringify({ type: "vm-listening", vm, port: guestPort, host_port: hostPort }));
|
|
208
|
+
srv.on("connection", (sock) => {
|
|
209
|
+
const sid = sidSeq++;
|
|
210
|
+
const st = { sock, open: false, queue: [] };
|
|
211
|
+
entry.streams.set(sid, st);
|
|
212
|
+
sock.setNoDelay(true);
|
|
213
|
+
sock.on("data", (d) => {
|
|
214
|
+
const frame = Buffer.allocUnsafe(4 + d.length); frame.writeUInt32BE(sid, 0); d.copy(frame, 4);
|
|
215
|
+
if (st.open) ws.send(frame); else st.queue.push(frame);
|
|
216
|
+
});
|
|
217
|
+
const end = () => {
|
|
218
|
+
if (!entry.streams.delete(sid)) return;
|
|
219
|
+
try { ws.send(JSON.stringify({ type: "tcp-close", sid })); } catch (_) {}
|
|
220
|
+
};
|
|
221
|
+
sock.on("close", end); sock.on("error", end);
|
|
222
|
+
ws.send(JSON.stringify({ type: "tcp-open", sid, port: guestPort }));
|
|
223
|
+
// A VM that never answers (not running / port closed) must not hang the client.
|
|
224
|
+
st.timer = setTimeout(() => { if (!st.open) { entry.streams.delete(sid); sock.destroy(); } }, 20000);
|
|
225
|
+
});
|
|
226
|
+
ws.on("message", (data, isBinary) => {
|
|
227
|
+
if (isBinary) {
|
|
228
|
+
const sid = data.readUInt32BE(0); const st = entry.streams.get(sid);
|
|
229
|
+
if (st) st.sock.write(data.subarray(4));
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
let m; try { m = JSON.parse(data.toString()); } catch { return; }
|
|
233
|
+
if (m.type === "pong") { ws.isAlive = true; return; }
|
|
234
|
+
const st = entry.streams.get(m.sid);
|
|
235
|
+
if (!st) return;
|
|
236
|
+
if (m.type === "tcp-opened") {
|
|
237
|
+
st.open = true; clearTimeout(st.timer);
|
|
238
|
+
for (const f of st.queue) ws.send(f); st.queue = [];
|
|
239
|
+
} else if (m.type === "tcp-close") {
|
|
240
|
+
entry.streams.delete(m.sid); clearTimeout(st.timer); st.sock.end();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
const gone = () => {
|
|
244
|
+
for (const st of entry.streams.values()) { try { st.sock.destroy(); } catch (_) {} }
|
|
245
|
+
entry.streams.clear();
|
|
246
|
+
if (vmHosts.get(vm) === entry) {
|
|
247
|
+
vmHosts.delete(vm);
|
|
248
|
+
// keep the port briefly reserved? no: close it so a stopped VM refuses ssh
|
|
249
|
+
try { srv.close(); } catch (_) {}
|
|
250
|
+
console.log(`[relay] vm ${vm} gone (port ${hostPort} closed)`);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
ws.on("close", gone); ws.on("error", () => { try { ws.terminate(); } catch (_) {} gone(); });
|
|
116
254
|
});
|
|
255
|
+
const vmLiveness = setInterval(() => {
|
|
256
|
+
for (const e of vmHosts.values()) {
|
|
257
|
+
const ws = e.ws;
|
|
258
|
+
if (ws.isAlive === false) { try { ws.terminate(); } catch (_) {} continue; }
|
|
259
|
+
ws.isAlive = false;
|
|
260
|
+
try { ws.send(JSON.stringify({ type: "ping" })); } catch (_) {}
|
|
261
|
+
}
|
|
262
|
+
}, PING_MS);
|
|
263
|
+
|
|
264
|
+
// Liveness: ping every tab on an interval and drop any that didn't pong since the
|
|
265
|
+
// last tick (dead/zombie sockets). The tab side already answers {type:"ping"} with
|
|
266
|
+
// a pong, so this is protocol-compatible with existing endpoints. This is what
|
|
267
|
+
// clears the orphaned sockets that used to linger and misroute requests.
|
|
268
|
+
const liveness = setInterval(() => {
|
|
269
|
+
for (const ws of tabs) {
|
|
270
|
+
if (ws.readyState !== ws.OPEN) { tabs.delete(ws); continue; }
|
|
271
|
+
if (ws.isAlive === false) { try { ws.terminate(); } catch (_) {} tabs.delete(ws); continue; }
|
|
272
|
+
ws.isAlive = false;
|
|
273
|
+
try { ws.send(JSON.stringify({ type: "ping" })); } catch (_) {}
|
|
274
|
+
}
|
|
275
|
+
}, PING_MS);
|
|
276
|
+
wss.on("close", () => { clearInterval(liveness); clearInterval(vmLiveness); });
|
|
117
277
|
|
|
118
278
|
server.listen(PORT, HOST, () => {
|
|
119
279
|
const shown = HOST === "0.0.0.0" ? "<this-machine-ip>" : HOST;
|
|
@@ -121,4 +281,5 @@ server.listen(PORT, HOST, () => {
|
|
|
121
281
|
console.log(` health http://${shown}:${PORT}/health`);
|
|
122
282
|
console.log(` tab regs ws://${shown}:${PORT}/register (the Nano tab auto-detects this)`);
|
|
123
283
|
console.log(` apps → point --endpoint-url / SDK endpoint at http://${shown}:${PORT}`);
|
|
284
|
+
console.log(` VMs → ssh to in-browser instances on ${shown}:${VM_PORT_MIN}-${VM_PORT_MAX} (shown in each Connect tab)`);
|
|
124
285
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vyomi-tunnel",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Vyomi-Nano local reverse-tunnel — reach the in-browser cloud simulator from any local SDK/CLI. The install-once, zero-cloud counterpart to the hosted Cloudflare relay; the Nano tab auto-detects it and prefers it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"homepage": "https://vyomi.cloud",
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
23
|
-
"url": "https://github.com/vyomi-cloud/appliance.git",
|
|
23
|
+
"url": "git+https://github.com/vyomi-cloud/appliance.git",
|
|
24
24
|
"directory": "packaging/tunnel"
|
|
25
25
|
}
|
|
26
26
|
}
|