dsh-remote-go 0.1.0 → 0.2.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 +56 -95
- package/client.js +3121 -330
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +12 -11
- package/src/admission.js +43 -0
- package/src/api.js +219 -23
- package/src/atomic-json.js +19 -2
- package/src/bootstrap.js +14 -1
- package/src/connect-log.js +145 -8
- package/src/connect-pipeline.js +1545 -108
- package/src/connect-progress.js +28 -8
- package/src/constants.js +65 -3
- package/src/diagnostics.js +3 -0
- package/src/download-jobs.js +17 -0
- package/src/errors.js +98 -2
- package/src/host-lifecycle.js +434 -17
- package/src/host-registry.js +78 -6
- package/src/host-runtime.js +74 -0
- package/src/host-view.js +28 -4
- package/src/index.js +47 -3
- package/src/key-files.js +6 -1
- package/src/known-hosts.js +50 -11
- package/src/layout.js +28 -10
- package/src/local-download.js +232 -13
- package/src/probe.js +13 -1
- package/src/prompts.js +114 -17
- package/src/redact.js +17 -3
- package/src/remote-files.js +2 -2
- package/src/remote-path.js +64 -0
- package/src/remote-proc.js +135 -0
- package/src/remote-proxy.js +815 -0
- package/src/remote-web.js +479 -28
- package/src/request-trust.js +89 -0
- package/src/secrets.js +38 -8
- package/src/service.js +222 -13
- package/src/settings.js +30 -2
- package/src/ssh.js +108 -29
- package/src/store.js +58 -8
- package/src/tools.js +386 -0
- package/src/tunnel.js +60 -1
- package/src/webapp.js +15 -0
- package/vendor/THIRD_PARTY_NOTICES.md +27 -1
- package/vendor/ssh2-entry.mjs +0 -12
package/src/remote-proc.js
CHANGED
|
@@ -121,6 +121,141 @@ echo "STOPPED"
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
/**
|
|
125
|
+
* TCP ports a pid is listening on, read from its own `/proc` network tables and
|
|
126
|
+
* matched against the socket inodes it holds. Used after a remote restart: the
|
|
127
|
+
* process may be the same pid with a new random port, or a new pid entirely.
|
|
128
|
+
* @param {object} ssh - SSH transport.
|
|
129
|
+
* @param {number} pid - remote process id.
|
|
130
|
+
* @returns {Promise<number[]>} listening ports, ascending and unique.
|
|
131
|
+
*/
|
|
132
|
+
export async function remoteListenPorts(ssh, pid) {
|
|
133
|
+
if (!isPid(pid)) return []
|
|
134
|
+
const script = `set -u
|
|
135
|
+
PID=${pid}
|
|
136
|
+
[ -d "/proc/$PID" ] || exit 0
|
|
137
|
+
ls -l "/proc/$PID/fd" 2>/dev/null | sed -n 's/.*socket:\\[\\([0-9][0-9]*\\)\\].*/INODE:\\1/p' | sort -u
|
|
138
|
+
for NET in "/proc/$PID/net/tcp" "/proc/$PID/net/tcp6"; do
|
|
139
|
+
[ -r "$NET" ] || continue
|
|
140
|
+
awk 'NR > 1 && $4 == "0A" { print "LISTEN:" $2 ":" $10 }' "$NET" 2>/dev/null
|
|
141
|
+
done
|
|
142
|
+
`
|
|
143
|
+
try {
|
|
144
|
+
const result = await ssh.exec(script, { timeoutMs: 15_000 })
|
|
145
|
+
return parseListenPorts(result.stdout)
|
|
146
|
+
} catch {
|
|
147
|
+
// swallow-ok: an unreadable /proc directory yields no candidate ports
|
|
148
|
+
return []
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Decode the `INODE:` / `LISTEN:<addr-hex>:<port-hex>:<inode>` lines of {@link remoteListenPorts}. */
|
|
153
|
+
export function parseListenPorts(stdout) {
|
|
154
|
+
const inodes = new Set()
|
|
155
|
+
const ports = []
|
|
156
|
+
for (const line of String(stdout ?? '').split(/\r?\n/)) {
|
|
157
|
+
const inode = /^INODE:(\d+)$/.exec(line)
|
|
158
|
+
if (inode) {
|
|
159
|
+
inodes.add(inode[1])
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
const listen = /^LISTEN:[0-9A-Fa-f]+:([0-9A-Fa-f]{1,4}):(\d+)$/.exec(line)
|
|
163
|
+
if (!listen) continue
|
|
164
|
+
const port = Number.parseInt(listen[1], 16)
|
|
165
|
+
if (isPid(port) && port <= 65535) ports.push({ port, inode: listen[2] })
|
|
166
|
+
}
|
|
167
|
+
return [...new Set(ports.filter((entry) => inodes.has(entry.inode)).map((entry) => entry.port))].sort(
|
|
168
|
+
(left, right) => left - right,
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The pid behind one listening loopback port, found by matching the port's socket
|
|
174
|
+
* inode against every `/proc/<pid>/fd`. Needed when the remote restarted into a new
|
|
175
|
+
* pid that the run registry does not know yet.
|
|
176
|
+
* @param {object} ssh - SSH transport.
|
|
177
|
+
* @param {number} port - TCP port on the remote's loopback.
|
|
178
|
+
* @returns {Promise<number | null>} the owning pid, or null when it cannot be read.
|
|
179
|
+
*/
|
|
180
|
+
export async function remotePortOwnerPid(ssh, port) {
|
|
181
|
+
if (!isPid(port) || port > 65535) return null
|
|
182
|
+
const script = `set -u
|
|
183
|
+
PORT_HEX=$(printf '%04X' ${port})
|
|
184
|
+
INODE=$(awk -v p=":$PORT_HEX" '$2 ~ p"$" && $4 == "0A" { print $10; exit }' /proc/net/tcp 2>/dev/null)
|
|
185
|
+
[ -n "\${INODE:-}" ] || exit 0
|
|
186
|
+
for D in /proc/[0-9]*; do
|
|
187
|
+
if ls -l "$D/fd" 2>/dev/null | grep -q "socket:\\[\${INODE}\\]"; then
|
|
188
|
+
echo "PID:\${D#/proc/}"
|
|
189
|
+
exit 0
|
|
190
|
+
fi
|
|
191
|
+
done
|
|
192
|
+
`
|
|
193
|
+
try {
|
|
194
|
+
const result = await ssh.exec(script, { timeoutMs: 20_000 })
|
|
195
|
+
const match = /^PID:(\d+)$/m.exec(String(result.stdout ?? ''))
|
|
196
|
+
const pid = match ? Number(match[1]) : 0
|
|
197
|
+
return isPid(pid) ? pid : null
|
|
198
|
+
} catch {
|
|
199
|
+
// swallow-ok: an unreadable /proc scan means the pid is unknown, the caller falls back
|
|
200
|
+
return null
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The identity of one remote process, read from `/proc` before the plugin ever signals it.
|
|
206
|
+
* The kernel start time is stable across `exec` (so an in-place DSH restart keeps it) and
|
|
207
|
+
* changes when a pid is reused by another process - which is exactly what a kill decision
|
|
208
|
+
* must rule out. The command line and `DSH_HOME` prove the process is the DSH the plugin
|
|
209
|
+
* thinks it is.
|
|
210
|
+
*
|
|
211
|
+
* @param {object} ssh - SSH transport.
|
|
212
|
+
* @param {number} pid - remote process id.
|
|
213
|
+
* @returns {Promise<{ ok: true, startTime: string, command: string, dshHome: string } |
|
|
214
|
+
* { ok: false, reason: 'invalid-pid' | 'missing' | 'unreadable' }>}
|
|
215
|
+
*/
|
|
216
|
+
export async function remoteProcessIdentity(ssh, pid) {
|
|
217
|
+
if (!isPid(pid)) return { ok: false, reason: 'invalid-pid' }
|
|
218
|
+
const script = `set -u
|
|
219
|
+
PID=${pid}
|
|
220
|
+
[ -d "/proc/$PID" ] || { echo "MISSING"; exit 0; }
|
|
221
|
+
RAW=$(cat "/proc/$PID/stat" 2>/dev/null || true)
|
|
222
|
+
[ -n "$RAW" ] || { echo "NO-STAT"; exit 0; }
|
|
223
|
+
REST=$(printf '%s' "$RAW" | sed 's/^.*) //')
|
|
224
|
+
set -- $REST
|
|
225
|
+
printf 'START:%s\\n' "\${20}"
|
|
226
|
+
CMD=$(tr '\\0' ' ' < "/proc/$PID/cmdline" 2>/dev/null || true)
|
|
227
|
+
printf 'CMD:%s\\n' "$CMD"
|
|
228
|
+
HOME_LINE=$(tr '\\0' '\\n' < "/proc/$PID/environ" 2>/dev/null | sed -n 's/^DSH_HOME=//p' | tail -n 1)
|
|
229
|
+
printf 'DSH_HOME:%s\\n' "\${HOME_LINE:-}"
|
|
230
|
+
`
|
|
231
|
+
try {
|
|
232
|
+
const result = await ssh.exec(script, { timeoutMs: 10_000, maxBytes: 32_000 })
|
|
233
|
+
return parseProcessIdentity(result.stdout)
|
|
234
|
+
} catch {
|
|
235
|
+
// swallow-ok: an unreadable /proc means the pid cannot be proven, and callers fail closed
|
|
236
|
+
return { ok: false, reason: 'unreadable' }
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Decode the `START:` / `CMD:` / `DSH_HOME:` lines of {@link remoteProcessIdentity}. */
|
|
241
|
+
export function parseProcessIdentity(stdout) {
|
|
242
|
+
const text = String(stdout ?? '')
|
|
243
|
+
// The markers below are matched anywhere in the output, deliberately: a command line that
|
|
244
|
+
// happens to contain MISSING/NO-STAT must not be mistaken for a readable identity (fail closed).
|
|
245
|
+
if (text.includes('MISSING')) return { ok: false, reason: 'missing' }
|
|
246
|
+
if (text.includes('NO-STAT')) return { ok: false, reason: 'unreadable' }
|
|
247
|
+
const start = /^START:(\d+)$/m.exec(text)
|
|
248
|
+
const command = /^CMD:(.*)$/m.exec(text)
|
|
249
|
+
if (!start || !command) return { ok: false, reason: 'unreadable' }
|
|
250
|
+
const home = /^DSH_HOME:(.*)$/m.exec(text)
|
|
251
|
+
return {
|
|
252
|
+
ok: true,
|
|
253
|
+
startTime: start[1],
|
|
254
|
+
command: command[1].trim(),
|
|
255
|
+
dshHome: home === null ? '' : home[1].trim(),
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
124
259
|
export async function remoteHttpStatus(ssh, port) {
|
|
125
260
|
if (!isPid(port)) return null
|
|
126
261
|
const script = `URL="http://127.0.0.1:${port}/"
|