cmux-ssh-here 0.2.1 → 0.4.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/LICENSE +21 -0
- package/README.md +6 -2
- package/bin.js +262 -38
- 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`
|
|
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
|
|
|
@@ -23,6 +25,7 @@ Open the link — cmux connects on its own. `Ctrl-C` in the terminal stops the s
|
|
|
23
25
|
- The token is passed in the deep link's `user=`; the server only accepts the correct token.
|
|
24
26
|
- cmux deep links deliberately carry no passwords or keys, so the secret is the token itself.
|
|
25
27
|
- Full PTY shell via `node-pty`, with window-resize support.
|
|
28
|
+
- Real exec channel (raw pipes) and a filesystem-backed SFTP server, so `scp`/`sftp` work — this is what lets `cmux ssh` upload and run its remote helper. A shell-only server is not enough for cmux.
|
|
26
29
|
- When `tmux` is available, the interactive shell runs inside it: sessions persist and are shared across connections, and the session list (`choose-tree`) is shown on connect. Without `tmux`, it falls back to a plain login shell.
|
|
27
30
|
|
|
28
31
|
## Requirements
|
|
@@ -39,4 +42,5 @@ Open the link — cmux connects on its own. `Ctrl-C` in the terminal stops the s
|
|
|
39
42
|
## Options
|
|
40
43
|
|
|
41
44
|
- `PORT=2222 npx cmux-ssh-here` — fixed port (random free port by default).
|
|
42
|
-
- `
|
|
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
|
@@ -3,13 +3,21 @@
|
|
|
3
3
|
// Spins up an ephemeral token-auth SSH server and prints a cmux deep link.
|
|
4
4
|
// Anyone on the LAN who opens the link lands in a shell as the current user.
|
|
5
5
|
// ponytail: the token is a bearer secret. Trusted LAN only, not the internet.
|
|
6
|
+
//
|
|
7
|
+
// This is a near-full SSH server (auth + pty/shell + exec + SFTP) on purpose:
|
|
8
|
+
// cmux's `cmux ssh` flow scp-uploads a helper daemon (cmuxd-remote) and then
|
|
9
|
+
// talks a binary protocol to it over an exec channel. A shell-only server is
|
|
10
|
+
// not enough; SFTP (for scp) and raw-pipe exec (binary-clean) are required.
|
|
6
11
|
import ssh2 from "ssh2"; // ponytail: ssh2 is CJS, no named exports
|
|
7
12
|
const { Server } = ssh2;
|
|
13
|
+
const { OPEN_MODE, STATUS_CODE } = ssh2.utils.sftp;
|
|
8
14
|
import { generateKeyPairSync, randomBytes } from "node:crypto";
|
|
9
15
|
import os from "node:os";
|
|
16
|
+
import fs from "node:fs";
|
|
17
|
+
import { spawn } from "node:child_process";
|
|
10
18
|
import { chmodSync } from "node:fs";
|
|
11
19
|
import { createRequire } from "node:module";
|
|
12
|
-
import { dirname, join } from "node:path";
|
|
20
|
+
import { dirname, join, resolve } from "node:path";
|
|
13
21
|
|
|
14
22
|
// ponytail: node-pty's prebuilt spawn-helper sometimes unpacks without +x
|
|
15
23
|
// (packaging bug) -> "posix_spawnp failed". Fix it before importing.
|
|
@@ -23,7 +31,18 @@ const { default: pty } = await import("node-pty");
|
|
|
23
31
|
|
|
24
32
|
// ponytail: hex (not base64url) — cmux rejects a user that starts with "-",
|
|
25
33
|
// and an ssh client treats a leading-dash username as a flag. Hex is URL/ssh-safe.
|
|
26
|
-
const
|
|
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;
|
|
27
46
|
const { privateKey } = generateKeyPairSync("rsa", {
|
|
28
47
|
// ponytail: rsa PEM parses cleanly in ssh2 (ed25519 PKCS8 is hit-or-miss)
|
|
29
48
|
modulusLength: 2048,
|
|
@@ -32,15 +51,24 @@ const { privateKey } = generateKeyPairSync("rsa", {
|
|
|
32
51
|
});
|
|
33
52
|
|
|
34
53
|
const shellPath = process.env.SHELL || "/bin/zsh";
|
|
54
|
+
const debug = process.env.CMUX_SSH_DEBUG ? (...a) => console.error("[debug]", ...a) : () => {};
|
|
35
55
|
|
|
36
56
|
const lanIP = () =>
|
|
37
57
|
Object.values(os.networkInterfaces())
|
|
38
58
|
.flat()
|
|
39
59
|
.find((i) => i?.family === "IPv4" && !i.internal && !i.address.startsWith("169.254"))?.address;
|
|
40
60
|
|
|
41
|
-
//
|
|
42
|
-
function
|
|
43
|
-
|
|
61
|
+
// Interactive shell over a PTY (plain `ssh user@host` with no remote command).
|
|
62
|
+
function startShell(stream, term) {
|
|
63
|
+
// ponytail: route the interactive shell through tmux so sessions persist and
|
|
64
|
+
// are shared across LAN connections; show the session list on connect.
|
|
65
|
+
// Falls back to a plain login shell when tmux is absent.
|
|
66
|
+
const start =
|
|
67
|
+
'if command -v tmux >/dev/null 2>&1; then ' +
|
|
68
|
+
'exec tmux new-session -A -s main \\; choose-tree -Zs; ' +
|
|
69
|
+
'else printf "[cmux-ssh-here] tmux not found; opening a plain shell\\n"; ' +
|
|
70
|
+
'exec "$SHELL" -l; fi';
|
|
71
|
+
const child = pty.spawn(shellPath, ["-lc", start], {
|
|
44
72
|
name: term.term,
|
|
45
73
|
cols: term.cols,
|
|
46
74
|
rows: term.rows,
|
|
@@ -50,24 +78,189 @@ function bridge(stream, args, term) {
|
|
|
50
78
|
child.onData((d) => stream.write(d));
|
|
51
79
|
stream.on("data", (d) => child.write(d.toString()));
|
|
52
80
|
child.onExit(({ exitCode }) => {
|
|
53
|
-
try {
|
|
54
|
-
stream.exit(exitCode ?? 0);
|
|
55
|
-
} catch {}
|
|
81
|
+
try { stream.exit(exitCode ?? 0); } catch {}
|
|
56
82
|
stream.end();
|
|
57
83
|
});
|
|
58
84
|
return child;
|
|
59
85
|
}
|
|
60
86
|
|
|
61
|
-
|
|
87
|
+
// exec channel: run a command with RAW pipes (no PTY) so binary protocols
|
|
88
|
+
// (cmux's daemon stdio) stay byte-exact. Faithful to how sshd runs exec.
|
|
89
|
+
function startExec(stream, command, env) {
|
|
90
|
+
const child = spawn(shellPath, ["-c", command], { cwd: os.homedir(), env: { ...process.env, ...env } });
|
|
91
|
+
child.stdout.on("data", (d) => stream.write(d));
|
|
92
|
+
child.stderr.on("data", (d) => stream.stderr.write(d));
|
|
93
|
+
stream.on("data", (d) => child.stdin.write(d));
|
|
94
|
+
stream.on("end", () => child.stdin.end());
|
|
95
|
+
child.on("close", (code) => {
|
|
96
|
+
try { stream.exit(code ?? 0); } catch {}
|
|
97
|
+
stream.end();
|
|
98
|
+
});
|
|
99
|
+
child.on("error", () => {
|
|
100
|
+
try { stream.exit(127); } catch {}
|
|
101
|
+
stream.end();
|
|
102
|
+
});
|
|
103
|
+
return child;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Minimal real-filesystem SFTP server so `scp`/`sftp` work (cmux uploads its
|
|
107
|
+
// daemon via scp, which uses the SFTP subsystem on modern OpenSSH).
|
|
108
|
+
function attachSFTP(accept) {
|
|
109
|
+
const sftp = accept();
|
|
110
|
+
// ponytail: client half-closes (EOF) after its last request. A real sshd sends
|
|
111
|
+
// exit-status 0 then EOF/close for the sftp subsystem; without it scp/sftp
|
|
112
|
+
// report failure ("Exit status -1") even though the transfer succeeded.
|
|
113
|
+
// These are ssh2 internals (no public exit() on the SFTP wrapper), but stable.
|
|
114
|
+
sftp.on("end", () => {
|
|
115
|
+
try {
|
|
116
|
+
sftp._protocol.exitStatus(sftp.outgoing.id, 0);
|
|
117
|
+
sftp._protocol.channelEOF(sftp.outgoing.id);
|
|
118
|
+
} catch {}
|
|
119
|
+
sftp.end();
|
|
120
|
+
});
|
|
121
|
+
const handles = new Map();
|
|
122
|
+
let next = 0;
|
|
123
|
+
const open = (obj) => {
|
|
124
|
+
const id = next++;
|
|
125
|
+
handles.set(id, obj);
|
|
126
|
+
const b = Buffer.alloc(4);
|
|
127
|
+
b.writeUInt32BE(id, 0);
|
|
128
|
+
return b;
|
|
129
|
+
};
|
|
130
|
+
const get = (h) => (h.length === 4 ? handles.get(h.readUInt32BE(0)) : undefined);
|
|
131
|
+
const fail = (reqid, err) =>
|
|
132
|
+
sftp.status(
|
|
133
|
+
reqid,
|
|
134
|
+
err?.code === "ENOENT"
|
|
135
|
+
? STATUS_CODE.NO_SUCH_FILE
|
|
136
|
+
: err?.code === "EACCES" || err?.code === "EPERM"
|
|
137
|
+
? STATUS_CODE.PERMISSION_DENIED
|
|
138
|
+
: STATUS_CODE.FAILURE
|
|
139
|
+
);
|
|
140
|
+
const toAttrs = (st) => ({
|
|
141
|
+
mode: st.mode,
|
|
142
|
+
uid: st.uid,
|
|
143
|
+
gid: st.gid,
|
|
144
|
+
size: st.size,
|
|
145
|
+
atime: Math.floor(st.atimeMs / 1000),
|
|
146
|
+
mtime: Math.floor(st.mtimeMs / 1000),
|
|
147
|
+
});
|
|
148
|
+
const fsFlags = (flags) => {
|
|
149
|
+
const C = fs.constants;
|
|
150
|
+
let f = flags & OPEN_MODE.READ && flags & OPEN_MODE.WRITE ? C.O_RDWR : flags & OPEN_MODE.WRITE ? C.O_WRONLY : C.O_RDONLY;
|
|
151
|
+
if (flags & OPEN_MODE.APPEND) f |= C.O_APPEND;
|
|
152
|
+
if (flags & OPEN_MODE.CREAT) f |= C.O_CREAT;
|
|
153
|
+
if (flags & OPEN_MODE.TRUNC) f |= C.O_TRUNC;
|
|
154
|
+
if (flags & OPEN_MODE.EXCL) f |= C.O_EXCL;
|
|
155
|
+
return f;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
sftp.on("REALPATH", (reqid, p) => {
|
|
159
|
+
let r;
|
|
160
|
+
try {
|
|
161
|
+
r = fs.realpathSync(p === "" ? "." : p);
|
|
162
|
+
} catch {
|
|
163
|
+
r = p.startsWith("/") ? resolve(p) : join(os.homedir(), p === "." ? "" : p);
|
|
164
|
+
}
|
|
165
|
+
sftp.name(reqid, [{ filename: r, longname: r, attrs: {} }]);
|
|
166
|
+
});
|
|
167
|
+
sftp.on("OPEN", (reqid, filename, flags, attrs) => {
|
|
168
|
+
fs.open(filename, fsFlags(flags), attrs?.mode ?? 0o644, (err, fd) => {
|
|
169
|
+
if (err) return fail(reqid, err);
|
|
170
|
+
sftp.handle(reqid, open({ fd, path: filename }));
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
sftp.on("WRITE", (reqid, handle, offset, data) => {
|
|
174
|
+
const h = get(handle);
|
|
175
|
+
if (!h || h.fd == null) return sftp.status(reqid, STATUS_CODE.FAILURE);
|
|
176
|
+
fs.write(h.fd, data, 0, data.length, offset, (err) => (err ? fail(reqid, err) : sftp.status(reqid, STATUS_CODE.OK)));
|
|
177
|
+
});
|
|
178
|
+
sftp.on("READ", (reqid, handle, offset, length) => {
|
|
179
|
+
const h = get(handle);
|
|
180
|
+
if (!h || h.fd == null) return sftp.status(reqid, STATUS_CODE.FAILURE);
|
|
181
|
+
const buf = Buffer.alloc(length);
|
|
182
|
+
fs.read(h.fd, buf, 0, length, offset, (err, bytes) => {
|
|
183
|
+
if (err) return fail(reqid, err);
|
|
184
|
+
if (!bytes) return sftp.status(reqid, STATUS_CODE.EOF);
|
|
185
|
+
sftp.data(reqid, buf.subarray(0, bytes));
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
sftp.on("FSTAT", (reqid, handle) => {
|
|
189
|
+
const h = get(handle);
|
|
190
|
+
if (!h || h.fd == null) return sftp.status(reqid, STATUS_CODE.FAILURE);
|
|
191
|
+
fs.fstat(h.fd, (err, st) => (err ? fail(reqid, err) : sftp.attrs(reqid, toAttrs(st))));
|
|
192
|
+
});
|
|
193
|
+
sftp.on("FSETSTAT", (reqid, handle, attrs) => {
|
|
194
|
+
const h = get(handle);
|
|
195
|
+
if (!h || h.fd == null) return sftp.status(reqid, STATUS_CODE.FAILURE);
|
|
196
|
+
if (attrs?.mode != null) {
|
|
197
|
+
try { fs.fchmodSync(h.fd, attrs.mode); } catch (e) { return fail(reqid, e); }
|
|
198
|
+
}
|
|
199
|
+
sftp.status(reqid, STATUS_CODE.OK);
|
|
200
|
+
});
|
|
201
|
+
sftp.on("CLOSE", (reqid, handle) => {
|
|
202
|
+
const h = get(handle);
|
|
203
|
+
if (h?.fd != null) try { fs.closeSync(h.fd); } catch {}
|
|
204
|
+
if (h) handles.delete(handle.readUInt32BE(0));
|
|
205
|
+
sftp.status(reqid, STATUS_CODE.OK);
|
|
206
|
+
});
|
|
207
|
+
const onStat = (reqid, p) => fs.stat(p, (err, st) => (err ? fail(reqid, err) : sftp.attrs(reqid, toAttrs(st))));
|
|
208
|
+
sftp.on("STAT", onStat);
|
|
209
|
+
sftp.on("LSTAT", (reqid, p) => fs.lstat(p, (err, st) => (err ? fail(reqid, err) : sftp.attrs(reqid, toAttrs(st)))));
|
|
210
|
+
sftp.on("SETSTAT", (reqid, p, attrs) => {
|
|
211
|
+
if (attrs?.mode != null) {
|
|
212
|
+
try { fs.chmodSync(p, attrs.mode); } catch (e) { return fail(reqid, e); }
|
|
213
|
+
}
|
|
214
|
+
sftp.status(reqid, STATUS_CODE.OK);
|
|
215
|
+
});
|
|
216
|
+
sftp.on("MKDIR", (reqid, p, attrs) =>
|
|
217
|
+
fs.mkdir(p, { mode: attrs?.mode ?? 0o755 }, (err) => (err ? fail(reqid, err) : sftp.status(reqid, STATUS_CODE.OK)))
|
|
218
|
+
);
|
|
219
|
+
sftp.on("RMDIR", (reqid, p) => fs.rmdir(p, (err) => (err ? fail(reqid, err) : sftp.status(reqid, STATUS_CODE.OK))));
|
|
220
|
+
sftp.on("REMOVE", (reqid, p) => fs.unlink(p, (err) => (err ? fail(reqid, err) : sftp.status(reqid, STATUS_CODE.OK))));
|
|
221
|
+
sftp.on("RENAME", (reqid, from, to) =>
|
|
222
|
+
fs.rename(from, to, (err) => (err ? fail(reqid, err) : sftp.status(reqid, STATUS_CODE.OK)))
|
|
223
|
+
);
|
|
224
|
+
sftp.on("OPENDIR", (reqid, p) => {
|
|
225
|
+
try {
|
|
226
|
+
sftp.handle(reqid, open({ dir: p, entries: fs.readdirSync(p), idx: 0 }));
|
|
227
|
+
} catch (e) {
|
|
228
|
+
fail(reqid, e);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
sftp.on("READDIR", (reqid, handle) => {
|
|
232
|
+
const h = get(handle);
|
|
233
|
+
if (!h || !h.entries) return sftp.status(reqid, STATUS_CODE.FAILURE);
|
|
234
|
+
if (h.idx >= h.entries.length) return sftp.status(reqid, STATUS_CODE.EOF);
|
|
235
|
+
const names = h.entries.slice(h.idx).map((name) => {
|
|
236
|
+
let st;
|
|
237
|
+
try { st = fs.lstatSync(join(h.dir, name)); } catch {}
|
|
238
|
+
return { filename: name, longname: name, attrs: st ? toAttrs(st) : {} };
|
|
239
|
+
});
|
|
240
|
+
h.idx = h.entries.length;
|
|
241
|
+
sftp.name(reqid, names);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
62
244
|
|
|
63
|
-
const
|
|
245
|
+
const serverCfg = { hostKeys: [privateKey] };
|
|
246
|
+
if (process.env.CMUX_SSH2_DEBUG) serverCfg.debug = (m) => { if (/SFTP|CHANNEL|EOF|CLOSE/.test(m)) console.error("[ssh2]", m); };
|
|
247
|
+
let render = () => {}; // assigned once the server is listening (knows ip/port)
|
|
248
|
+
const server = new Server(serverCfg, (client, info) => {
|
|
64
249
|
client.on("authentication", (ctx) => {
|
|
65
250
|
debug("auth", ctx.method, ctx.username);
|
|
66
251
|
return ctx.username === token ? ctx.accept() : ctx.reject();
|
|
67
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
|
+
});
|
|
68
262
|
client.on("session", (accept) => {
|
|
69
263
|
const session = accept();
|
|
70
|
-
// Defaults; overwritten by pty/env requests before shell/exec.
|
|
71
264
|
const term = { term: "xterm-256color", cols: 80, rows: 24, env: {} };
|
|
72
265
|
let child;
|
|
73
266
|
|
|
@@ -84,32 +277,23 @@ const server = new Server({ hostKeys: [privateKey] }, (client) => {
|
|
|
84
277
|
a?.();
|
|
85
278
|
});
|
|
86
279
|
session.on("window-change", (a, _r, info) => {
|
|
87
|
-
child?.resize(info.cols, info.rows);
|
|
280
|
+
child?.resize?.(info.cols, info.rows);
|
|
88
281
|
a?.();
|
|
89
282
|
});
|
|
90
283
|
session.on("shell", (acc) => {
|
|
91
284
|
debug("shell");
|
|
92
|
-
|
|
93
|
-
// and are shared across LAN connections; show the session list on connect.
|
|
94
|
-
// Falls back to a plain login shell when tmux is absent.
|
|
95
|
-
const start =
|
|
96
|
-
'if command -v tmux >/dev/null 2>&1; then ' +
|
|
97
|
-
'exec tmux new-session -A -s main \\; choose-tree -Zs; ' +
|
|
98
|
-
'else printf "[cmux-ssh-here] tmux not found; opening a plain shell (no server-side sessions)\\n"; ' +
|
|
99
|
-
'exec "$SHELL" -l; fi';
|
|
100
|
-
child = bridge(acc(), ["-lc", start], term);
|
|
285
|
+
child = startShell(acc(), term);
|
|
101
286
|
});
|
|
102
287
|
session.on("exec", (acc, _r, info) => {
|
|
103
288
|
debug("exec", info.command);
|
|
104
|
-
|
|
105
|
-
// cmux runs remote commands (tmux probes, tmux -CC) over this channel.
|
|
106
|
-
child = bridge(acc(), ["-lc", info.command], term);
|
|
289
|
+
child = startExec(acc(), info.command, term.env);
|
|
107
290
|
});
|
|
108
|
-
session.on("
|
|
109
|
-
debug("
|
|
110
|
-
|
|
111
|
-
rej?.();
|
|
291
|
+
session.on("sftp", (acc) => {
|
|
292
|
+
debug("sftp");
|
|
293
|
+
attachSFTP(acc);
|
|
112
294
|
});
|
|
295
|
+
// ponytail: no 'subsystem' handler — ssh2 auto-rejects non-sftp subsystems,
|
|
296
|
+
// and a handler here also intercepts the sftp request and kills the channel.
|
|
113
297
|
});
|
|
114
298
|
});
|
|
115
299
|
|
|
@@ -126,14 +310,54 @@ server.listen(PORT, "0.0.0.0", function () {
|
|
|
126
310
|
process.exit(1);
|
|
127
311
|
}
|
|
128
312
|
const port = this.address().port;
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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 screen-clearing UI so logs stay readable;
|
|
326
|
+
// just print the link whenever it rotates.
|
|
327
|
+
const liveUI = !process.env.CMUX_SSH_DEBUG;
|
|
328
|
+
const ago = (since) => `${Math.floor((Date.now() - since) / 1000)}s`;
|
|
329
|
+
|
|
330
|
+
render = () => {
|
|
331
|
+
if (!liveUI) return;
|
|
332
|
+
const lines = [
|
|
333
|
+
"",
|
|
334
|
+
` cmux-ssh-here — shell as ${user} over the LAN`,
|
|
335
|
+
"",
|
|
336
|
+
` Open in cmux (regenerates in ${remaining}s):`,
|
|
337
|
+
` ${buildLink()}`,
|
|
338
|
+
"",
|
|
339
|
+
];
|
|
340
|
+
if (sessions.size) {
|
|
341
|
+
lines.push(` Connected sessions (${sessions.size}):`);
|
|
342
|
+
for (const s of sessions.values()) lines.push(` • ${s.ip} connected ${ago(s.since)} ago`);
|
|
343
|
+
} else {
|
|
344
|
+
lines.push(" No active sessions yet.");
|
|
345
|
+
}
|
|
346
|
+
lines.push("", " Ctrl-C to stop.", "");
|
|
347
|
+
// Clear screen + home, then redraw.
|
|
348
|
+
process.stdout.write(`\x1b[2J\x1b[3J\x1b[H${lines.join("\n")}\n`);
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
if (liveUI) render();
|
|
352
|
+
else console.log(`\n Open in cmux (regenerates in ${remaining}s):\n ${buildLink()}\n`);
|
|
353
|
+
|
|
354
|
+
// Tick once a second: count down, regenerate on expiry, refresh the UI.
|
|
355
|
+
setInterval(() => {
|
|
356
|
+
remaining--;
|
|
357
|
+
if (remaining <= 0) {
|
|
358
|
+
regenerateToken();
|
|
359
|
+
if (!liveUI) console.log(`\n [link regenerated]\n ${buildLink()}\n`);
|
|
360
|
+
}
|
|
361
|
+
render();
|
|
362
|
+
}, 1000);
|
|
139
363
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cmux-ssh-here",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
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
|
},
|