dsh-remote-go 0.2.0 → 0.3.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 +11 -3
- package/{client.js → dist/client.js} +7710 -8182
- package/dist/index.js +4 -0
- package/dist/src/admission.js +46 -0
- package/dist/src/api.js +442 -0
- package/dist/src/atomic-json.js +235 -0
- package/dist/src/bootstrap.js +106 -0
- package/dist/src/connect/attach.js +43 -0
- package/dist/src/connect/bootstrap.js +14 -0
- package/dist/src/connect/failures.js +44 -0
- package/dist/src/connect/guards.js +31 -0
- package/dist/src/connect/launch.js +80 -0
- package/dist/src/connect/links.js +70 -0
- package/dist/src/connect/ownership.js +220 -0
- package/dist/src/connect/pipeline.js +473 -0
- package/dist/src/connect/reattach.js +215 -0
- package/dist/src/connect/registry.js +11 -0
- package/dist/src/connect/repair.js +126 -0
- package/dist/src/connect/restart.js +316 -0
- package/dist/src/connect/stop.js +198 -0
- package/dist/src/connect/transport.js +109 -0
- package/dist/src/connect/verify.js +39 -0
- package/dist/src/connect/watchdog.js +76 -0
- package/dist/src/connect-log.js +162 -0
- package/dist/src/connect-progress.js +142 -0
- package/{src → dist/src}/constants.js +30 -56
- package/dist/src/context.js +117 -0
- package/dist/src/diagnostics.js +102 -0
- package/dist/src/download-jobs.js +230 -0
- package/dist/src/errors.js +243 -0
- package/dist/src/events.js +390 -0
- package/dist/src/host-lifecycle.js +504 -0
- package/dist/src/host-registry.js +232 -0
- package/dist/src/host-runtime.js +86 -0
- package/dist/src/host-view.js +75 -0
- package/dist/src/index.js +216 -0
- package/dist/src/kernel-adapter.js +434 -0
- package/dist/src/key-files.js +52 -0
- package/dist/src/known-hosts.js +125 -0
- package/dist/src/layout.js +43 -0
- package/dist/src/local-download.js +380 -0
- package/dist/src/local-ssh.js +30 -0
- package/dist/src/local-version.js +101 -0
- package/dist/src/node/download.js +160 -0
- package/dist/src/node-runtime.js +346 -0
- package/dist/src/probe.js +96 -0
- package/dist/src/prompts.js +270 -0
- package/dist/src/proxy/cookies.js +136 -0
- package/dist/src/proxy/headers.js +262 -0
- package/dist/src/proxy/http.js +142 -0
- package/dist/src/proxy/index.js +135 -0
- package/dist/src/proxy/limits.js +8 -0
- package/dist/src/proxy/routes.js +39 -0
- package/dist/src/proxy/upgrade.js +175 -0
- package/dist/src/redact.js +104 -0
- package/dist/src/remote-files.js +72 -0
- package/dist/src/remote-path.js +123 -0
- package/dist/src/remote-proc.js +339 -0
- package/dist/src/remote-web/registry.js +147 -0
- package/dist/src/remote-web/resolve.js +310 -0
- package/dist/src/remote-web/start.js +227 -0
- package/dist/src/remote-web/stop.js +93 -0
- package/dist/src/request-trust.js +87 -0
- package/dist/src/runtime/dispatch.js +310 -0
- package/dist/src/runtime/effects.js +91 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +120 -0
- package/dist/src/runtime/rules.js +565 -0
- package/dist/src/secrets.js +142 -0
- package/dist/src/service.js +406 -0
- package/dist/src/settings.js +144 -0
- package/dist/src/ssh-config.js +276 -0
- package/dist/src/ssh-failures.js +122 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +297 -0
- package/dist/src/ssh.js +477 -0
- package/dist/src/store.js +191 -0
- package/dist/src/tool-shared.js +181 -0
- package/dist/src/tools-exec-policy.js +167 -0
- package/dist/src/tools-exec.js +371 -0
- package/dist/src/tools-report.js +424 -0
- package/dist/src/tools.js +411 -0
- package/dist/src/tunnel.js +129 -0
- package/dist/src/util.js +149 -0
- package/dist/src/version.js +114 -0
- package/dist/src/watchdog-bus.js +29 -0
- package/dist/src/watchdog.js +156 -0
- package/index.js +5 -4
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +37 -19
- package/src/admission.js +0 -43
- package/src/api.js +0 -536
- package/src/atomic-json.js +0 -225
- package/src/bootstrap.js +0 -113
- package/src/connect-log.js +0 -176
- package/src/connect-pipeline.js +0 -1900
- package/src/connect-progress.js +0 -155
- package/src/diagnostics.js +0 -160
- package/src/download-jobs.js +0 -234
- package/src/errors.js +0 -412
- package/src/host-lifecycle.js +0 -604
- package/src/host-registry.js +0 -217
- package/src/host-runtime.js +0 -127
- package/src/host-view.js +0 -75
- package/src/index.js +0 -206
- package/src/key-files.js +0 -54
- package/src/known-hosts.js +0 -133
- package/src/layout.js +0 -53
- package/src/local-download.js +0 -381
- package/src/local-ssh.js +0 -27
- package/src/local-version.js +0 -88
- package/src/node-runtime.js +0 -521
- package/src/probe.js +0 -101
- package/src/prompts.js +0 -229
- package/src/redact.js +0 -68
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -137
- package/src/remote-proc.js +0 -343
- package/src/remote-proxy.js +0 -815
- package/src/remote-web.js +0 -751
- package/src/request-trust.js +0 -89
- package/src/secrets.js +0 -109
- package/src/service.js +0 -459
- package/src/settings.js +0 -163
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -731
- package/src/store.js +0 -193
- package/src/tools.js +0 -386
- package/src/tunnel.js +0 -132
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -43
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
package/src/remote-proc.js
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remote process and reachability primitives: find, stop and probe DSH processes on the remote, plus the one HTTP round-trip that proves the remote UI answers.
|
|
3
|
-
*/
|
|
4
|
-
import http from 'node:http'
|
|
5
|
-
import { TUNNEL_PROBE_TIMEOUT_MS } from './constants.js'
|
|
6
|
-
import { isPid, messageOf } from './util.js'
|
|
7
|
-
|
|
8
|
-
/** A listener answering 200, 302 or 401 is up: 401 without the launch cookie still proves it serves. */
|
|
9
|
-
export function isUiUp(status) {
|
|
10
|
-
return status === 200 || status === 302 || status === 401
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function buildForeignDshCommand(ignorePids = []) {
|
|
14
|
-
const ignore = (Array.isArray(ignorePids) ? ignorePids : [])
|
|
15
|
-
.map((pid) => Number(pid))
|
|
16
|
-
.filter((pid) => isPid(pid))
|
|
17
|
-
.join(' ')
|
|
18
|
-
return `set -u
|
|
19
|
-
IGNORE=" ${ignore} "
|
|
20
|
-
PS_OUT=$(ps -eww -o pid=,comm=,args= 2>/dev/null || ps -eo pid=,comm=,args= 2>/dev/null || true)
|
|
21
|
-
printf '%s\\n' "$PS_OUT" | while IFS= read -r LINE; do
|
|
22
|
-
[ -z "$LINE" ] && continue
|
|
23
|
-
set -- $LINE
|
|
24
|
-
PID=$1
|
|
25
|
-
COMM=$2
|
|
26
|
-
shift 2
|
|
27
|
-
ARGS="$*"
|
|
28
|
-
[ -z "$PID" ] && continue
|
|
29
|
-
case "$IGNORE" in *" $PID "*) continue ;; esac
|
|
30
|
-
case "$COMM" in node|dsh|npm|npx) ;; *) continue ;; esac
|
|
31
|
-
case "$ARGS" in
|
|
32
|
-
*"@deepseek-ai/dsh"*|*"/bin/dsh "*|*" dsh "*) ;;
|
|
33
|
-
*) continue ;;
|
|
34
|
-
esac
|
|
35
|
-
case "$ARGS" in
|
|
36
|
-
*"ps -eww"*|*"PS_OUT"*|*"IGNORE"*) continue ;;
|
|
37
|
-
esac
|
|
38
|
-
ENV_HOME=$(tr '\\0' '\\n' < "/proc/$PID/environ" 2>/dev/null | sed -n 's/^DSH_HOME=//p' | tail -n 1)
|
|
39
|
-
if [ -n "\${ENV_HOME:-}" ] && [ "$ENV_HOME" != "$HOME/.dsh" ]; then continue; fi
|
|
40
|
-
printf '%s\\t%s\\n' "$PID" "$ARGS"
|
|
41
|
-
done`
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function parseForeignDshOutput(stdout) {
|
|
45
|
-
const processes = []
|
|
46
|
-
for (const rawLine of String(stdout ?? '').split(/\r?\n/)) {
|
|
47
|
-
const tab = rawLine.indexOf('\t')
|
|
48
|
-
if (tab <= 0) continue
|
|
49
|
-
const pid = Number(rawLine.slice(0, tab).trim())
|
|
50
|
-
const command = rawLine.slice(tab + 1).trim()
|
|
51
|
-
if (!isPid(pid) || command === '') continue
|
|
52
|
-
processes.push({ pid, command })
|
|
53
|
-
}
|
|
54
|
-
return processes
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Find DSH processes on the remote that this plugin did not start.
|
|
59
|
-
*
|
|
60
|
-
* Never throws: a failed scan is reported as `{ ok: false }`, and the caller
|
|
61
|
-
* proceeds without a conflict prompt (a broken scan must not block connecting).
|
|
62
|
-
*/
|
|
63
|
-
export async function findForeignDshProcesses(ssh, options = {}) {
|
|
64
|
-
try {
|
|
65
|
-
const result = await ssh.exec(buildForeignDshCommand(options.ignorePids ?? []), {
|
|
66
|
-
timeoutMs: options.timeoutMs ?? 20_000,
|
|
67
|
-
maxBytes: 200_000,
|
|
68
|
-
})
|
|
69
|
-
if (result.code !== 0) {
|
|
70
|
-
return { ok: false, processes: [], reason: 'exec-failed', technical: String(result.stderr ?? '').slice(0, 500) }
|
|
71
|
-
}
|
|
72
|
-
return { ok: true, processes: parseForeignDshOutput(result.stdout) }
|
|
73
|
-
} catch (error) {
|
|
74
|
-
// swallow-ok: a scan failure is surfaced as ok:false and the caller proceeds without a prompt
|
|
75
|
-
return {
|
|
76
|
-
ok: false,
|
|
77
|
-
processes: [],
|
|
78
|
-
reason: 'exec-failed',
|
|
79
|
-
technical: messageOf(error),
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export async function stopForeignDshProcesses(ssh, processes, options = {}) {
|
|
85
|
-
const pids = [
|
|
86
|
-
...new Set(
|
|
87
|
-
(Array.isArray(processes) ? processes : []).map((process) => Number(process?.pid)).filter((pid) => isPid(pid)),
|
|
88
|
-
),
|
|
89
|
-
]
|
|
90
|
-
if (pids.length === 0) return { ok: true, alive: [], output: '' }
|
|
91
|
-
const script = `set -u
|
|
92
|
-
PIDS="${pids.join(' ')}"
|
|
93
|
-
for PID in $PIDS; do kill -TERM "$PID" 2>/dev/null || true; done
|
|
94
|
-
i=0
|
|
95
|
-
while [ "$i" -lt 20 ]; do
|
|
96
|
-
ALIVE=0
|
|
97
|
-
for PID in $PIDS; do
|
|
98
|
-
if kill -0 "$PID" 2>/dev/null; then ALIVE=1; fi
|
|
99
|
-
done
|
|
100
|
-
if [ "$ALIVE" -eq 0 ]; then break; fi
|
|
101
|
-
i=$((i+1))
|
|
102
|
-
sleep 0.5
|
|
103
|
-
done
|
|
104
|
-
for PID in $PIDS; do
|
|
105
|
-
if kill -0 "$PID" 2>/dev/null; then kill -KILL "$PID" 2>/dev/null || true; fi
|
|
106
|
-
done
|
|
107
|
-
sleep 0.2
|
|
108
|
-
for PID in $PIDS; do
|
|
109
|
-
if kill -0 "$PID" 2>/dev/null; then echo "ALIVE:$PID"; fi
|
|
110
|
-
done
|
|
111
|
-
echo "STOPPED"
|
|
112
|
-
`
|
|
113
|
-
try {
|
|
114
|
-
const result = await ssh.exec(script, { timeoutMs: options.timeoutMs ?? 30_000 })
|
|
115
|
-
const output = String(result.stdout ?? '')
|
|
116
|
-
const alive = [...output.matchAll(/ALIVE:(\d+)/g)].map((match) => Number(match[1]))
|
|
117
|
-
return { ok: result.code === 0 && alive.length === 0, alive, output }
|
|
118
|
-
} catch (error) {
|
|
119
|
-
// swallow-ok: a stop failure is returned to the caller with the original message
|
|
120
|
-
return { ok: false, alive: pids, output: messageOf(error) }
|
|
121
|
-
}
|
|
122
|
-
}
|
|
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
|
-
|
|
259
|
-
export async function remoteHttpStatus(ssh, port) {
|
|
260
|
-
if (!isPid(port)) return null
|
|
261
|
-
const script = `URL="http://127.0.0.1:${port}/"
|
|
262
|
-
if command -v curl >/dev/null 2>&1; then
|
|
263
|
-
curl -s -o /dev/null -w '%{http_code}' --max-time 5 "$URL" 2>/dev/null || true
|
|
264
|
-
elif command -v wget >/dev/null 2>&1; then
|
|
265
|
-
wget -S -O /dev/null --max-time 5 "$URL" 2>&1 | sed -n 's/.*HTTP\\/[0-9.]* \\([0-9][0-9][0-9]\\).*/\\1/p' | head -n 1
|
|
266
|
-
fi
|
|
267
|
-
`
|
|
268
|
-
try {
|
|
269
|
-
const result = await ssh.exec(script, { timeoutMs: 15_000 })
|
|
270
|
-
const code = Number(String(result.stdout ?? '').trim())
|
|
271
|
-
return Number.isInteger(code) && code > 0 ? code : null
|
|
272
|
-
} catch {
|
|
273
|
-
return null
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export async function waitForRemoteUi(ssh, port, options = {}) {
|
|
278
|
-
const attempts = options.attempts ?? 20
|
|
279
|
-
const intervalMs = options.intervalMs ?? 500
|
|
280
|
-
let last = null
|
|
281
|
-
for (let i = 0; i < attempts; i += 1) {
|
|
282
|
-
last = await remoteHttpStatus(ssh, port)
|
|
283
|
-
if (isUiUp(last)) return { ok: true, status: last }
|
|
284
|
-
await new Promise((resolve) => setTimeout(resolve, intervalMs))
|
|
285
|
-
}
|
|
286
|
-
return { ok: false, status: last }
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export async function exchangeTokenOverTunnel(localPort, token, options = {}) {
|
|
290
|
-
const timeoutMs = options.timeoutMs ?? TUNNEL_PROBE_TIMEOUT_MS
|
|
291
|
-
const basePath = options.path ?? '/'
|
|
292
|
-
const tokenPath = `/?token=${encodeURIComponent(token)}`
|
|
293
|
-
const first = await httpRequest({
|
|
294
|
-
port: localPort,
|
|
295
|
-
path: basePath === '/' ? tokenPath : `${basePath}?token=${encodeURIComponent(token)}`,
|
|
296
|
-
timeoutMs,
|
|
297
|
-
})
|
|
298
|
-
const setCookies = first.headers['set-cookie'] ?? []
|
|
299
|
-
const cookieHeader = setCookies.map((cookie) => String(cookie).split(';')[0]).join('; ')
|
|
300
|
-
const location = first.headers.location ?? null
|
|
301
|
-
const second = await httpRequest({
|
|
302
|
-
port: localPort,
|
|
303
|
-
path: location && location.startsWith('/') ? location : '/',
|
|
304
|
-
timeoutMs,
|
|
305
|
-
headers: cookieHeader ? { cookie: cookieHeader } : {},
|
|
306
|
-
})
|
|
307
|
-
const titleMatch = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(second.body)
|
|
308
|
-
return {
|
|
309
|
-
ok: second.status === 200 && second.body.length > 0,
|
|
310
|
-
firstStatus: first.status,
|
|
311
|
-
status: second.status,
|
|
312
|
-
title: titleMatch ? titleMatch[1].trim().slice(0, 200) : null,
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function httpRequest(options) {
|
|
317
|
-
return new Promise((resolve, reject) => {
|
|
318
|
-
const request = http.request(
|
|
319
|
-
{
|
|
320
|
-
host: '127.0.0.1',
|
|
321
|
-
port: options.port,
|
|
322
|
-
path: options.path,
|
|
323
|
-
method: 'GET',
|
|
324
|
-
headers: { 'user-agent': 'dsh-remote-go', ...options.headers },
|
|
325
|
-
timeout: options.timeoutMs,
|
|
326
|
-
},
|
|
327
|
-
(response) => {
|
|
328
|
-
const chunks = []
|
|
329
|
-
response.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
|
|
330
|
-
response.on('end', () => {
|
|
331
|
-
resolve({
|
|
332
|
-
status: response.statusCode ?? 0,
|
|
333
|
-
headers: response.headers,
|
|
334
|
-
body: Buffer.concat(chunks).toString('utf8'),
|
|
335
|
-
})
|
|
336
|
-
})
|
|
337
|
-
},
|
|
338
|
-
)
|
|
339
|
-
request.on('timeout', () => request.destroy(new Error('local tunnel probe timed out')))
|
|
340
|
-
request.on('error', reject)
|
|
341
|
-
request.end()
|
|
342
|
-
})
|
|
343
|
-
}
|