cmux-ssh-here 0.3.0 → 0.4.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -2
  3. package/bin.js +96 -12
  4. package/package.json +9 -1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Viktor Silakov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -15,7 +15,9 @@ Output:
15
15
  https://cmux.com/deeplink/ssh?host=192.168.1.42&port=52968&user=wudUKicRFRw3&host-key-policy=accept-new&title=your-mac
16
16
  ```
17
17
 
18
- Open the link — cmux connects on its own. `Ctrl-C` in the terminal stops the server.
18
+ Open the link — cmux connects on its own. The server terminal shows a live dashboard: the current link, a countdown until it regenerates, and the list of connected sessions. `Ctrl-C` stops the server.
19
+
20
+ The link (and its token) is valid for **3 minutes**, then a fresh one is generated — already-connected sessions stay alive; only new connections need the new link.
19
21
 
20
22
  ## How it works
21
23
 
@@ -40,4 +42,5 @@ Open the link — cmux connects on its own. `Ctrl-C` in the terminal stops the s
40
42
  ## Options
41
43
 
42
44
  - `PORT=2222 npx cmux-ssh-here` — fixed port (random free port by default).
43
- - `CMUX_SSH_DEBUG=1 npx cmux-ssh-here` — log incoming auth/env/exec/shell requests to stderr (troubleshooting).
45
+ - `CMUX_SSH_TTL=600 npx cmux-ssh-here` — link/token lifetime in seconds before regeneration (default 180).
46
+ - `CMUX_SSH_DEBUG=1 npx cmux-ssh-here` — log incoming auth/env/exec/shell requests to stderr (disables the live dashboard).
package/bin.js CHANGED
@@ -31,7 +31,18 @@ const { default: pty } = await import("node-pty");
31
31
 
32
32
  // ponytail: hex (not base64url) — cmux rejects a user that starts with "-",
33
33
  // and an ssh client treats a leading-dash username as a flag. Hex is URL/ssh-safe.
34
- const token = randomBytes(12).toString("hex"); // secret carried in user=<token>
34
+ const TTL_SECONDS = Number(process.env.CMUX_SSH_TTL) || 180; // link/token lifetime before regeneration
35
+ let token; // secret carried in user=<token>; rotated every TTL_SECONDS
36
+ let remaining; // seconds until the next regeneration
37
+ const regenerateToken = () => {
38
+ token = randomBytes(12).toString("hex");
39
+ remaining = TTL_SECONDS;
40
+ };
41
+ regenerateToken();
42
+
43
+ // Active authenticated connections, shown live in the server terminal.
44
+ const sessions = new Map();
45
+ let nextSid = 0;
35
46
  const { privateKey } = generateKeyPairSync("rsa", {
36
47
  // ponytail: rsa PEM parses cleanly in ssh2 (ed25519 PKCS8 is hit-or-miss)
37
48
  modulusLength: 2048,
@@ -233,11 +244,21 @@ function attachSFTP(accept) {
233
244
 
234
245
  const serverCfg = { hostKeys: [privateKey] };
235
246
  if (process.env.CMUX_SSH2_DEBUG) serverCfg.debug = (m) => { if (/SFTP|CHANNEL|EOF|CLOSE/.test(m)) console.error("[ssh2]", m); };
236
- const server = new Server(serverCfg, (client) => {
247
+ let render = () => {}; // assigned once the server is listening (knows ip/port)
248
+ const server = new Server(serverCfg, (client, info) => {
237
249
  client.on("authentication", (ctx) => {
238
250
  debug("auth", ctx.method, ctx.username);
239
251
  return ctx.username === token ? ctx.accept() : ctx.reject();
240
252
  });
253
+ client.on("ready", () => {
254
+ const id = nextSid++;
255
+ sessions.set(id, { ip: info?.ip || "?", since: Date.now() });
256
+ render();
257
+ client.on("close", () => {
258
+ sessions.delete(id);
259
+ render();
260
+ });
261
+ });
241
262
  client.on("session", (accept) => {
242
263
  const session = accept();
243
264
  const term = { term: "xterm-256color", cols: 80, rows: 24, env: {} };
@@ -289,14 +310,77 @@ server.listen(PORT, "0.0.0.0", function () {
289
310
  process.exit(1);
290
311
  }
291
312
  const port = this.address().port;
292
- const params = new URLSearchParams({
293
- host: ip,
294
- port: String(port),
295
- user: token,
296
- "host-key-policy": "accept-new",
297
- title: os.hostname(),
298
- });
299
- console.log(`\n Connect to this machine via cmux over the LAN`);
300
- console.log(` (shell as ${os.userInfo().username}; Ctrl-C here to stop):\n`);
301
- console.log(` https://cmux.com/deeplink/ssh?${params}\n`);
313
+ const user = os.userInfo().username;
314
+ const buildLink = () => {
315
+ const params = new URLSearchParams({
316
+ host: ip,
317
+ port: String(port),
318
+ user: token,
319
+ "host-key-policy": "accept-new",
320
+ title: os.hostname(),
321
+ });
322
+ return `https://cmux.com/deeplink/ssh?${params}`;
323
+ };
324
+
325
+ // ponytail: in debug mode skip the dashboard so logs stay readable.
326
+ const liveUI = !process.env.CMUX_SSH_DEBUG;
327
+ const ago = (since) => `${Math.floor((Date.now() - since) / 1000)}s`;
328
+
329
+ // ponytail: never clear the whole screen (that wipes the user's selection and
330
+ // makes the link impossible to copy). Repaint the static block only on real
331
+ // events (link rotation, session add/remove); the per-second countdown rewrites
332
+ // just its own line via "\r", leaving the link above untouched and selectable.
333
+ const countdownLine = () => ` ↻ Link regenerates in ${remaining}s — copy it above. Ctrl-C to stop.`;
334
+
335
+ // Collapse the raw SSH connections (cmux opens several from one machine:
336
+ // ControlMaster, the daemon channel, short-lived probes) into one row per IP.
337
+ const sessionRows = () => {
338
+ const byIp = new Map();
339
+ for (const s of sessions.values()) {
340
+ const e = byIp.get(s.ip);
341
+ if (e) { e.count++; e.since = Math.min(e.since, s.since); }
342
+ else byIp.set(s.ip, { count: 1, since: s.since });
343
+ }
344
+ return [...byIp.entries()].map(
345
+ ([ipAddr, e]) => ` • ${ipAddr} connected ${ago(e.since)} ago${e.count > 1 ? ` (${e.count} connections)` : ""}`
346
+ );
347
+ };
348
+
349
+ const block = () => {
350
+ const lines = [
351
+ "",
352
+ ` cmux-ssh-here — shell as ${user} over the LAN`,
353
+ "",
354
+ " Open in cmux:",
355
+ ` ${buildLink()}`,
356
+ "",
357
+ ];
358
+ const rows = sessionRows();
359
+ if (rows.length) lines.push(` Connected (${rows.length}):`, ...rows);
360
+ else lines.push(" No active sessions yet.");
361
+ lines.push("");
362
+ return lines.join("\n");
363
+ };
364
+
365
+ // Repaint the block, then leave the cursor parked on the countdown line.
366
+ render = () => {
367
+ if (!liveUI) return;
368
+ process.stdout.write(`\n${block()}\n${countdownLine()}`);
369
+ };
370
+
371
+ if (liveUI) render();
372
+ else console.log(`\n Open in cmux (regenerates in ${remaining}s):\n ${buildLink()}\n`);
373
+
374
+ setInterval(() => {
375
+ remaining--;
376
+ if (remaining <= 0) {
377
+ regenerateToken();
378
+ // Link changed: repaint the whole block (rare — once per TTL).
379
+ if (liveUI) render();
380
+ else console.log(`\n [link regenerated]\n ${buildLink()}\n`);
381
+ return;
382
+ }
383
+ // Common case: rewrite only the countdown line in place — no screen churn.
384
+ if (liveUI) process.stdout.write(`\r\x1b[2K${countdownLine()}`);
385
+ }, 1000);
302
386
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cmux-ssh-here",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Spin up a token-auth SSH server and print a cmux deep link to connect over LAN",
5
5
  "type": "module",
6
6
  "bin": {
@@ -18,6 +18,14 @@
18
18
  "remote"
19
19
  ],
20
20
  "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/viktor-silakov/cmux-ssh-here.git"
24
+ },
25
+ "homepage": "https://github.com/viktor-silakov/cmux-ssh-here#readme",
26
+ "bugs": {
27
+ "url": "https://github.com/viktor-silakov/cmux-ssh-here/issues"
28
+ },
21
29
  "engines": {
22
30
  "node": ">=18"
23
31
  },