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/known-hosts.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Read-only `~/.ssh/known_hosts` parsing and OpenSSH SHA256 fingerprints; hashed entries are counted, never guessed at.
|
|
3
|
-
*/
|
|
4
|
-
import { createHash } from 'node:crypto'
|
|
5
|
-
|
|
6
|
-
export function fingerprintFromBase64(keyBase64) {
|
|
7
|
-
const digest = createHash('sha256').update(Buffer.from(keyBase64, 'base64')).digest('base64')
|
|
8
|
-
return `SHA256:${digest.replace(/=+$/g, '')}`
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function fingerprintFromBlob(keyBlob) {
|
|
12
|
-
const digest = createHash('sha256').update(keyBlob).digest('base64')
|
|
13
|
-
return `SHA256:${digest.replace(/=+$/g, '')}`
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function parseKnownHosts(text) {
|
|
17
|
-
const entries = []
|
|
18
|
-
const malformed = []
|
|
19
|
-
let hashedCount = 0
|
|
20
|
-
let revoked = 0
|
|
21
|
-
let certAuthority = 0
|
|
22
|
-
|
|
23
|
-
const lines = String(text ?? '').split(/\r?\n/)
|
|
24
|
-
for (let index = 0; index < lines.length; index += 1) {
|
|
25
|
-
const lineNo = index + 1
|
|
26
|
-
const raw = lines[index].trim()
|
|
27
|
-
if (raw === '' || raw.startsWith('#')) continue
|
|
28
|
-
const parts = raw.split(/\s+/)
|
|
29
|
-
let marker = null
|
|
30
|
-
if (parts[0].startsWith('@')) marker = parts.shift() ?? null
|
|
31
|
-
if (parts.length < 3) {
|
|
32
|
-
malformed.push({ line: lineNo, text: raw })
|
|
33
|
-
continue
|
|
34
|
-
}
|
|
35
|
-
const [hostField, keyType, keyBase64] = parts
|
|
36
|
-
if (marker === '@revoked') {
|
|
37
|
-
revoked += 1
|
|
38
|
-
continue
|
|
39
|
-
}
|
|
40
|
-
if (marker === '@cert-authority') {
|
|
41
|
-
certAuthority += 1
|
|
42
|
-
continue
|
|
43
|
-
}
|
|
44
|
-
if (hostField.startsWith('|1|')) {
|
|
45
|
-
hashedCount += 1
|
|
46
|
-
continue
|
|
47
|
-
}
|
|
48
|
-
if (!/^[A-Za-z0-9+/=]+$/.test(keyBase64 ?? '')) {
|
|
49
|
-
malformed.push({ line: lineNo, text: raw })
|
|
50
|
-
continue
|
|
51
|
-
}
|
|
52
|
-
entries.push({
|
|
53
|
-
hosts: hostField.split(',').filter(Boolean),
|
|
54
|
-
keyType,
|
|
55
|
-
keyBase64,
|
|
56
|
-
fingerprint: fingerprintFromBase64(keyBase64),
|
|
57
|
-
marker,
|
|
58
|
-
line: lineNo,
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
return { entries, hashedCount, malformed, revoked, certAuthority }
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* OpenSSH `known_hosts` host-field matching: `*` matches any run of characters, `?` exactly
|
|
66
|
-
* one, everything else is literal and the comparison ignores case.
|
|
67
|
-
*
|
|
68
|
-
* Implemented without a regular expression on purpose: a host field is data, and building a
|
|
69
|
-
* `RegExp` from it invites injection and backtracking on remote-controlled text.
|
|
70
|
-
* @param {string} pattern - one host field.
|
|
71
|
-
* @param {string} value - the host field form being matched (plain or `[host]:port`).
|
|
72
|
-
* @returns {boolean} whether the pattern covers the value.
|
|
73
|
-
*/
|
|
74
|
-
function wildcardMatch(pattern, value) {
|
|
75
|
-
const wanted = String(pattern).toLowerCase()
|
|
76
|
-
const text = String(value).toLowerCase()
|
|
77
|
-
let patternAt = 0
|
|
78
|
-
let textAt = 0
|
|
79
|
-
let starAt = -1
|
|
80
|
-
let markAt = 0
|
|
81
|
-
while (textAt < text.length) {
|
|
82
|
-
if (patternAt < wanted.length && (wanted[patternAt] === '?' || wanted[patternAt] === text[textAt])) {
|
|
83
|
-
patternAt += 1
|
|
84
|
-
textAt += 1
|
|
85
|
-
} else if (patternAt < wanted.length && wanted[patternAt] === '*') {
|
|
86
|
-
starAt = patternAt
|
|
87
|
-
patternAt += 1
|
|
88
|
-
markAt = textAt
|
|
89
|
-
} else if (starAt >= 0) {
|
|
90
|
-
patternAt = starAt + 1
|
|
91
|
-
textAt = markAt + 1
|
|
92
|
-
markAt = textAt
|
|
93
|
-
} else {
|
|
94
|
-
return false
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
while (patternAt < wanted.length && wanted[patternAt] === '*') patternAt += 1
|
|
98
|
-
return patternAt === wanted.length
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Whether one `known_hosts` host field covers `host` on `port`.
|
|
103
|
-
*
|
|
104
|
-
* OpenSSH writes a plain `host` field only for port 22 and `[host]:port` for every other
|
|
105
|
-
* port, and it matches the same way: a plain entry never vouches for a service on 2222.
|
|
106
|
-
* @param {string} pattern - one (possibly wildcarded) host field.
|
|
107
|
-
* @param {string} host - the host being dialled.
|
|
108
|
-
* @param {number | string | null | undefined} port - the port being dialled.
|
|
109
|
-
* @returns {boolean} whether the field covers that destination.
|
|
110
|
-
*/
|
|
111
|
-
export function knownHostFieldMatches(pattern, host, port) {
|
|
112
|
-
const targetPort = Number(port) || 22
|
|
113
|
-
const target = targetPort === 22 ? String(host) : `[${host}]:${targetPort}`
|
|
114
|
-
return wildcardMatch(pattern, target)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function findKnownHostEntries(parsed, host, port) {
|
|
118
|
-
return parsed.entries.filter((entry) => entry.hosts.some((pattern) => knownHostFieldMatches(pattern, host, port)))
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function summarizeKnownHosts(parsed) {
|
|
122
|
-
const hosts = new Set()
|
|
123
|
-
for (const entry of parsed.entries) {
|
|
124
|
-
for (const host of entry.hosts) {
|
|
125
|
-
if (!host.startsWith('|1|')) hosts.add(host)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return {
|
|
129
|
-
entries: parsed.entries.length,
|
|
130
|
-
hashed: parsed.hashedCount,
|
|
131
|
-
hosts: [...hosts].sort().slice(0, 200),
|
|
132
|
-
}
|
|
133
|
-
}
|
package/src/layout.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remote directory layout: everything the plugin writes lives under `$DSH_HOME/remote-go`, so a host only gets `~/.dsh` plus that one subdirectory.
|
|
3
|
-
*/
|
|
4
|
-
import { REMOTE_DSH_DIR, REMOTE_STATE_DIR } from './constants.js'
|
|
5
|
-
import { errors } from './errors.js'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Build the remote layout for a known home directory.
|
|
9
|
-
*
|
|
10
|
-
* The home has to be a real absolute path: an unexpanded `~` is not expanded by a
|
|
11
|
-
* non-interactive shell, so a layout built without it would silently address a literal
|
|
12
|
-
* `~` directory (`kill -0` a `~`-path pid is a no-op that still exits 0). Refusing here
|
|
13
|
-
* keeps every caller honest instead of pretending such a command worked.
|
|
14
|
-
*
|
|
15
|
-
* @param {string} home - the remote user's absolute home directory.
|
|
16
|
-
* @returns {{ root: string, stateHome: string, dshHome: string, nodeDir: string,
|
|
17
|
-
* downloadDir: string, logsDir: string, logFile: string, runDir: string, runFile: string }}
|
|
18
|
-
*/
|
|
19
|
-
export function remoteLayout(home) {
|
|
20
|
-
const base = typeof home === 'string' ? home.trim().replace(/\/+$/, '') : ''
|
|
21
|
-
if (!base.startsWith('/')) {
|
|
22
|
-
throw errors.internal(
|
|
23
|
-
new Error(`the remote home directory is unknown (${JSON.stringify(home)}); refusing to build a "~" layout`),
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
const dshHome = `${base}/${REMOTE_DSH_DIR}`
|
|
27
|
-
const root = `${dshHome}/${REMOTE_STATE_DIR}`
|
|
28
|
-
return {
|
|
29
|
-
root,
|
|
30
|
-
// Kept for callers that resolve the remote DSH home separately from the
|
|
31
|
-
// plugin's own state directory.
|
|
32
|
-
stateHome: dshHome,
|
|
33
|
-
// The remote DSH runs with its real home, so its data is the user's own.
|
|
34
|
-
dshHome,
|
|
35
|
-
nodeDir: `${root}/node`,
|
|
36
|
-
downloadDir: `${root}/downloads`,
|
|
37
|
-
logsDir: `${root}/logs`,
|
|
38
|
-
logFile: `${root}/logs/web.log`,
|
|
39
|
-
runDir: `${root}/run`,
|
|
40
|
-
runFile: `${root}/run/web.json`,
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The layout a host runtime describes: the facts its live connection probed, or the home
|
|
46
|
-
* it remembered before those facts were cleared (keep-alive / idle policy).
|
|
47
|
-
* @param {{ facts?: { home?: string } | null, remoteHome?: string | null }} runtime
|
|
48
|
-
* @returns {ReturnType<typeof remoteLayout>}
|
|
49
|
-
*/
|
|
50
|
-
export function layoutForRuntime(runtime) {
|
|
51
|
-
const home = runtime?.facts?.home ?? runtime?.remoteHome ?? null
|
|
52
|
-
return remoteLayout(home)
|
|
53
|
-
}
|
package/src/local-download.js
DELETED
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local download writer. Every download (single file or folder) is streamed from
|
|
3
|
-
* SFTP directly to a user-selected local directory. Nothing is archived or held in
|
|
4
|
-
* memory, and there is no byte or entry cap. Existing target names are renamed with
|
|
5
|
-
* a ` (2)`, ` (3)` suffix so a download never overwrites local data.
|
|
6
|
-
*
|
|
7
|
-
* The destination is a **write primitive that a remote host chooses the contents of** (the
|
|
8
|
-
* file or folder name comes from the remote tree), so it is fenced like one: only folders
|
|
9
|
-
* under the local user's home, and never a system, autorun or credential folder. Without this
|
|
10
|
-
* fence a compromised remote could ask the GUI to save its tree into
|
|
11
|
-
* `…\Start Menu\Programs\Startup` (or `~/.ssh/authorized_keys`) and run code on this machine at
|
|
12
|
-
* the next logon.
|
|
13
|
-
*/
|
|
14
|
-
import path from 'node:path'
|
|
15
|
-
import { homedir } from 'node:os'
|
|
16
|
-
import { mkdir, open, rm, stat } from 'node:fs/promises'
|
|
17
|
-
import { errors, isRemoteGoError } from './errors.js'
|
|
18
|
-
import { remoteFileName, safeLocalSegment } from './remote-path.js'
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Path segments that execute something when the user logs in, or that authorize a login.
|
|
22
|
-
* Matched case-insensitively as a whole segment anywhere below the home directory, so both
|
|
23
|
-
* `…\Start Menu\Programs\Startup` and `~/.config/autostart` are covered. Shell startup files
|
|
24
|
-
* and the per-user containers that hold them (`~/.config`, `~/.local`, whose `bin` is on PATH in
|
|
25
|
-
* many distributions) are refused for the same reason, as are the macOS login-item folders
|
|
26
|
-
* (`~/Library/LaunchAgents`, `LaunchDaemons`, `StartupItems`) and the X session files.
|
|
27
|
-
*/
|
|
28
|
-
const FORBIDDEN_SEGMENTS = new Map([
|
|
29
|
-
['startup', 'autorun'],
|
|
30
|
-
['startupfolder', 'autorun'],
|
|
31
|
-
['startupitems', 'autorun'],
|
|
32
|
-
['autostart', 'autorun'],
|
|
33
|
-
['systemd', 'autorun'],
|
|
34
|
-
['launchagents', 'autorun'],
|
|
35
|
-
['launchdaemons', 'autorun'],
|
|
36
|
-
['.ssh', 'credentials'],
|
|
37
|
-
['.gnupg', 'credentials'],
|
|
38
|
-
['.config', 'login'],
|
|
39
|
-
['.local', 'login'],
|
|
40
|
-
['.bashrc', 'login'],
|
|
41
|
-
['.bash_profile', 'login'],
|
|
42
|
-
['.bash_login', 'login'],
|
|
43
|
-
['.bash_logout', 'login'],
|
|
44
|
-
['.profile', 'login'],
|
|
45
|
-
['.zshrc', 'login'],
|
|
46
|
-
['.zprofile', 'login'],
|
|
47
|
-
['.zshenv', 'login'],
|
|
48
|
-
['.zlogin', 'login'],
|
|
49
|
-
['.zlogout', 'login'],
|
|
50
|
-
['.kshrc', 'login'],
|
|
51
|
-
['.cshrc', 'login'],
|
|
52
|
-
['.tcshrc', 'login'],
|
|
53
|
-
['.login', 'login'],
|
|
54
|
-
['.logout', 'login'],
|
|
55
|
-
['.xsessionrc', 'login'],
|
|
56
|
-
['.xprofile', 'login'],
|
|
57
|
-
['.xinitrc', 'login'],
|
|
58
|
-
])
|
|
59
|
-
|
|
60
|
-
/** `shell:startup` / `shell:common startup` name an autorun folder without spelling a path. */
|
|
61
|
-
const SHELL_FOLDER_RE = /^\s*shell\s*:/i
|
|
62
|
-
|
|
63
|
-
/** Directories that belong to the OS; never a download target even if home somehow covers them. */
|
|
64
|
-
const SYSTEM_ROOTS = Object.freeze(
|
|
65
|
-
process.platform === 'win32'
|
|
66
|
-
? ['C:\\Windows', 'C:\\Program Files', 'C:\\Program Files (x86)', 'C:\\ProgramData']
|
|
67
|
-
: ['/etc', '/usr', '/bin', '/sbin', '/lib', '/lib64', '/boot', '/opt', '/var', '/dev', '/proc', '/sys'],
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
/** A stronger reason wins when several segments of one path match (most specific first). */
|
|
71
|
-
const REASON_PRIORITY = ['autorun', 'credentials', 'login', 'system']
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* One resolved path, comparable on this platform. Windows and macOS both have a
|
|
75
|
-
* case-insensitive default filesystem, so a case-insensitive compare is the one the OS itself
|
|
76
|
-
* would do (`C:\Users` vs `c:\users`, `/Users/Me` vs `/Users/me`).
|
|
77
|
-
*/
|
|
78
|
-
function comparable(value) {
|
|
79
|
-
return process.platform === 'win32' || process.platform === 'darwin' ? value.toLowerCase() : value
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function segmentsOf(resolved) {
|
|
83
|
-
return resolved.split(/[\\/]+/).filter((segment) => segment !== '')
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function isInside(root, target) {
|
|
87
|
-
const base = root.endsWith(path.sep) ? root : `${root}${path.sep}`
|
|
88
|
-
return target === root || target.startsWith(base)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Locations whose *contents* run or authenticate at logon. A download may neither land inside
|
|
93
|
-
* one of them nor create one of their ancestors: a remote folder named `Microsoft` under
|
|
94
|
-
* `~/AppData/Roaming` would otherwise put a whole `Windows\Start Menu\Programs\Startup` tree on
|
|
95
|
-
* disk without any single path segment looking suspicious (macOS has the same shape under
|
|
96
|
-
* `~/Library`).
|
|
97
|
-
* @param {string} home - resolved home directory.
|
|
98
|
-
* @returns {string[]} resolved sensitive roots that exist by definition (not on disk).
|
|
99
|
-
*/
|
|
100
|
-
function sensitiveRoots(home) {
|
|
101
|
-
const roots = ['.config', '.local', '.ssh', '.gnupg'].map((name) => path.join(home, name))
|
|
102
|
-
roots.push(path.join(home, 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu'))
|
|
103
|
-
for (const name of ['LaunchAgents', 'LaunchDaemons', 'StartupItems']) {
|
|
104
|
-
roots.push(path.join(home, 'Library', name))
|
|
105
|
-
}
|
|
106
|
-
const appData = typeof process.env.APPDATA === 'string' && process.env.APPDATA.trim() !== ''
|
|
107
|
-
if (appData) roots.push(path.join(process.env.APPDATA, 'Microsoft', 'Windows', 'Start Menu'))
|
|
108
|
-
return roots
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Whether a path is a sensitive location, inside one, or an ancestor of one.
|
|
113
|
-
* @param {string} target - resolved candidate path.
|
|
114
|
-
* @param {string} home - resolved home directory.
|
|
115
|
-
* @returns {boolean}
|
|
116
|
-
*/
|
|
117
|
-
function isProtectedLocation(target, home) {
|
|
118
|
-
const resolved = comparable(target)
|
|
119
|
-
for (const root of sensitiveRoots(home)) {
|
|
120
|
-
const sensitive = comparable(path.resolve(root))
|
|
121
|
-
if (isInside(sensitive, resolved) || isInside(resolved, sensitive)) return true
|
|
122
|
-
}
|
|
123
|
-
return false
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** The concrete home directory a policy call is judged against. */
|
|
127
|
-
function homeOf(options = {}) {
|
|
128
|
-
return path.resolve(options.home ?? homedir())
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Whether one local folder may receive a remote download.
|
|
133
|
-
* @param {unknown} destination - the folder the client asked to save into.
|
|
134
|
-
* @param {{ home?: string }} [options] - the local user's home (defaults to the OS home).
|
|
135
|
-
* @returns {'invalid'|'outside-home'|'autorun'|'credentials'|'login'|'system'|'protected'|null}
|
|
136
|
-
* a refusal reason, or null when the folder is acceptable. Pure: no filesystem access, so it
|
|
137
|
-
* is testable.
|
|
138
|
-
*/
|
|
139
|
-
export function destinationPolicy(destination, options = {}) {
|
|
140
|
-
if (typeof destination !== 'string' || destination.trim() === '' || destination.includes('\0')) return 'invalid'
|
|
141
|
-
if (SHELL_FOLDER_RE.test(destination)) return 'autorun'
|
|
142
|
-
const home = comparable(homeOf(options))
|
|
143
|
-
const target = comparable(path.resolve(destination))
|
|
144
|
-
if (!isInside(home, target)) return 'outside-home'
|
|
145
|
-
const segments = segmentsOf(target)
|
|
146
|
-
const reasons = new Set()
|
|
147
|
-
for (const segment of segments) {
|
|
148
|
-
const reason = FORBIDDEN_SEGMENTS.get(segment.toLowerCase())
|
|
149
|
-
if (reason !== undefined) reasons.add(reason)
|
|
150
|
-
}
|
|
151
|
-
// `…\Start Menu\Programs`: a two-segment location whose entries appear in the Start menu.
|
|
152
|
-
for (let at = 1; at < segments.length; at += 1) {
|
|
153
|
-
if (segments[at - 1].toLowerCase() === 'start menu' && segments[at].toLowerCase() === 'programs') {
|
|
154
|
-
reasons.add('autorun')
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
for (const root of SYSTEM_ROOTS) {
|
|
158
|
-
if (isInside(comparable(path.resolve(root)), target)) reasons.add('system')
|
|
159
|
-
}
|
|
160
|
-
for (const reason of REASON_PRIORITY) if (reasons.has(reason)) return reason
|
|
161
|
-
return null
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** The client-facing refusal for one destination policy result. */
|
|
165
|
-
function destinationRefusal(reason) {
|
|
166
|
-
if (reason === 'outside-home') {
|
|
167
|
-
return errors.invalidInput('destination', {
|
|
168
|
-
zh: '只能保存到本机用户主目录下的文件夹(防止写入系统或自启动位置)。',
|
|
169
|
-
en: 'Downloads must go to a folder under this machine’s home directory (system and autorun locations are refused).',
|
|
170
|
-
})
|
|
171
|
-
}
|
|
172
|
-
if (reason === 'autorun') {
|
|
173
|
-
return errors.invalidInput('destination', {
|
|
174
|
-
zh: '不能保存到自启动目录(例如 Startup / autostart / shell:startup)。',
|
|
175
|
-
en: 'An autorun folder (for example Startup / autostart / shell:startup) cannot be a download target.',
|
|
176
|
-
})
|
|
177
|
-
}
|
|
178
|
-
if (reason === 'credentials') {
|
|
179
|
-
return errors.invalidInput('destination', {
|
|
180
|
-
zh: '不能保存到 SSH / GPG 配置目录(例如 .ssh)。',
|
|
181
|
-
en: 'An SSH / GPG configuration folder (for example .ssh) cannot be a download target.',
|
|
182
|
-
})
|
|
183
|
-
}
|
|
184
|
-
if (reason === 'login') {
|
|
185
|
-
return errors.invalidInput('destination', {
|
|
186
|
-
zh: '不能保存到登录脚本或用户配置目录(例如 .bashrc / .profile / .config / .local)。',
|
|
187
|
-
en: 'A login script or user configuration folder (for example .bashrc / .profile / .config / .local) cannot be a download target.',
|
|
188
|
-
})
|
|
189
|
-
}
|
|
190
|
-
if (reason === 'protected') {
|
|
191
|
-
return errors.invalidInput('destination', {
|
|
192
|
-
zh: '不能保存到会在登录或启动时生效的位置(自启动、登录脚本或凭据目录及其上级目录)。',
|
|
193
|
-
en: 'A location that runs or authenticates at logon (autorun, login scripts, credentials, or one of their parent folders) cannot be a download target.',
|
|
194
|
-
})
|
|
195
|
-
}
|
|
196
|
-
if (reason === 'system') {
|
|
197
|
-
return errors.invalidInput('destination', {
|
|
198
|
-
zh: '不能保存到系统目录。',
|
|
199
|
-
en: 'A system directory cannot be a download target.',
|
|
200
|
-
})
|
|
201
|
-
}
|
|
202
|
-
return errors.invalidInput('destination', {
|
|
203
|
-
zh: '请选择本机的保存文件夹。',
|
|
204
|
-
en: 'Choose a destination folder on this machine.',
|
|
205
|
-
})
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Refuse a path the remote host named inside an already-accepted destination.
|
|
210
|
-
*
|
|
211
|
-
* The destination is chosen locally, but the *name* below it comes from the remote tree
|
|
212
|
-
* (`~/Downloads/…`, `~/.config` + a remote folder called `autostart`, `~` + a remote file called
|
|
213
|
-
* `.bash_login`). This is the check that turns those combinations into a refusal.
|
|
214
|
-
* @param {string} candidate - resolved folder or file path about to be created.
|
|
215
|
-
* @param {{ home?: string }} [options]
|
|
216
|
-
* @throws {import('./errors.js').RemoteGoError} `invalid-input` when the path is protected.
|
|
217
|
-
*/
|
|
218
|
-
function assertCandidateAllowed(candidate, options = {}) {
|
|
219
|
-
const refusal = destinationPolicy(candidate, options)
|
|
220
|
-
if (refusal !== null) throw destinationRefusal(refusal)
|
|
221
|
-
if (isProtectedLocation(candidate, homeOf(options))) throw destinationRefusal('protected')
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Resolve and check one download destination.
|
|
226
|
-
* @param {unknown} destination - absolute local folder.
|
|
227
|
-
* @param {{ home?: string }} [options] - the local user's home, for tests.
|
|
228
|
-
* @returns {Promise<string>} the resolved folder.
|
|
229
|
-
*/
|
|
230
|
-
async function requireDestination(destination, options = {}) {
|
|
231
|
-
const refusal = destinationPolicy(destination, options)
|
|
232
|
-
if (refusal !== null) throw destinationRefusal(refusal)
|
|
233
|
-
const base = path.resolve(destination)
|
|
234
|
-
try {
|
|
235
|
-
const info = await stat(base)
|
|
236
|
-
if (!info.isDirectory()) {
|
|
237
|
-
throw errors.invalidInput('destination', {
|
|
238
|
-
zh: '选择的保存位置不是文件夹。',
|
|
239
|
-
en: 'The selected destination is not a folder.',
|
|
240
|
-
})
|
|
241
|
-
}
|
|
242
|
-
} catch (error) {
|
|
243
|
-
if (isRemoteGoError(error)) throw error
|
|
244
|
-
throw errors.invalidInput('destination', {
|
|
245
|
-
zh: '选择的保存文件夹不存在或不可写。',
|
|
246
|
-
en: 'The selected destination folder does not exist or cannot be written.',
|
|
247
|
-
})
|
|
248
|
-
}
|
|
249
|
-
return base
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
async function createUniqueFolder(base, name, options = {}) {
|
|
253
|
-
const safeName = safeLocalSegment(name) || 'download'
|
|
254
|
-
for (let index = 1; index <= 999; index += 1) {
|
|
255
|
-
const candidate = path.join(base, index === 1 ? safeName : `${safeName} (${index})`)
|
|
256
|
-
// The root's name comes from the remote tree, so it is judged before anything is created:
|
|
257
|
-
// `~/.config` + a remote folder called `autostart` must fail here, with zero writes.
|
|
258
|
-
assertCandidateAllowed(candidate, options)
|
|
259
|
-
try {
|
|
260
|
-
await mkdir(candidate)
|
|
261
|
-
return candidate
|
|
262
|
-
} catch (error) {
|
|
263
|
-
if (/** @type {any} */ (error)?.code !== 'EEXIST') throw error
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
throw errors.invalidInput('destination', {
|
|
267
|
-
zh: '目标目录里已经有太多同名文件夹,请换一个目录。',
|
|
268
|
-
en: 'The destination already contains too many folders with this name; choose another directory.',
|
|
269
|
-
})
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
async function createUniqueFile(base, name, options = {}) {
|
|
273
|
-
const safeName = safeLocalSegment(name) || 'download'
|
|
274
|
-
const extension = path.extname(safeName)
|
|
275
|
-
const stem = path.basename(safeName, extension) || 'download'
|
|
276
|
-
for (let index = 1; index <= 999; index += 1) {
|
|
277
|
-
const fileName = index === 1 ? safeName : `${stem} (${index})${extension}`
|
|
278
|
-
const candidate = path.join(base, fileName)
|
|
279
|
-
// The remote names the file: `~` + `.bash_login` is a login script, not a download.
|
|
280
|
-
assertCandidateAllowed(candidate, options)
|
|
281
|
-
try {
|
|
282
|
-
const handle = await open(candidate, 'wx', 0o600)
|
|
283
|
-
await handle.close()
|
|
284
|
-
return candidate
|
|
285
|
-
} catch (error) {
|
|
286
|
-
if (/** @type {any} */ (error)?.code !== 'EEXIST') throw error
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
throw errors.invalidInput('destination', {
|
|
290
|
-
zh: '目标目录里已经有太多同名文件,请换一个目录。',
|
|
291
|
-
en: 'The destination already contains too many files with this name; choose another directory.',
|
|
292
|
-
})
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function toDownloadError(error) {
|
|
296
|
-
if (isRemoteGoError(error)) return error
|
|
297
|
-
return errors.remoteFileUnavailable(undefined, {
|
|
298
|
-
technical: String(/** @type {any} */ (error?.message ?? error ?? '')).slice(0, 300),
|
|
299
|
-
})
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Stream one remote file into `destination`, creating an auto-renamed local file.
|
|
304
|
-
* @param {object} runtime - connected host runtime.
|
|
305
|
-
* @param {string} remotePath - normalized absolute remote path.
|
|
306
|
-
* @param {unknown} destination - absolute local directory.
|
|
307
|
-
* @param {{ onProgress?: Function, isCancelled?: () => boolean, home?: string }} [options]
|
|
308
|
-
* @returns {Promise<{ path: string, kind: 'file', name: string, destination: string,
|
|
309
|
-
* totalBytes: number, files: number }>}
|
|
310
|
-
*/
|
|
311
|
-
export async function saveHostFile(runtime, remotePath, destination, options = {}) {
|
|
312
|
-
if (!runtime || runtime.status !== 'connected' || !runtime.ssh) {
|
|
313
|
-
throw errors.remoteFileUnavailable({
|
|
314
|
-
zh: '这台主机还没有连接,无法读取远端文件。',
|
|
315
|
-
en: 'This host is not connected, so the remote file cannot be read.',
|
|
316
|
-
})
|
|
317
|
-
}
|
|
318
|
-
if (typeof runtime.ssh.saveRemoteFile !== 'function') {
|
|
319
|
-
throw errors.internal('the SSH transport does not support file saves')
|
|
320
|
-
}
|
|
321
|
-
const base = await requireDestination(destination, options)
|
|
322
|
-
const name = remoteFileName(remotePath)
|
|
323
|
-
let localPath = null
|
|
324
|
-
try {
|
|
325
|
-
localPath = await createUniqueFile(base, name, options)
|
|
326
|
-
const result = await runtime.ssh.saveRemoteFile(remotePath, localPath, options)
|
|
327
|
-
return {
|
|
328
|
-
path: remotePath,
|
|
329
|
-
kind: 'file',
|
|
330
|
-
name: path.basename(localPath),
|
|
331
|
-
destination: localPath,
|
|
332
|
-
totalBytes: Number(result?.size ?? 0),
|
|
333
|
-
files: 1,
|
|
334
|
-
}
|
|
335
|
-
} catch (error) {
|
|
336
|
-
if (localPath) await rm(localPath, { force: true }).catch(() => {})
|
|
337
|
-
throw toDownloadError(error)
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Copy one remote folder into `destination` as individual local files.
|
|
343
|
-
* @param {object} runtime - connected host runtime.
|
|
344
|
-
* @param {string} remotePath - normalized absolute remote folder path.
|
|
345
|
-
* @param {unknown} destination - absolute local directory.
|
|
346
|
-
* @param {{ onInventory?: Function, onProgress?: Function, isCancelled?: () => boolean, home?: string }} [options]
|
|
347
|
-
* @returns {Promise<{ path: string, kind: 'folder', name: string, destination: string,
|
|
348
|
-
* totalBytes: number, files: number, cancelled?: boolean }>}
|
|
349
|
-
*/
|
|
350
|
-
export async function saveHostFolder(runtime, remotePath, destination, options = {}) {
|
|
351
|
-
if (!runtime || runtime.status !== 'connected' || !runtime.ssh) {
|
|
352
|
-
throw errors.remoteFileUnavailable({
|
|
353
|
-
zh: '这台主机还没有连接,无法读取远端文件夹。',
|
|
354
|
-
en: 'This host is not connected, so the remote folder cannot be read.',
|
|
355
|
-
})
|
|
356
|
-
}
|
|
357
|
-
if (typeof runtime.ssh.saveRemoteTree !== 'function') {
|
|
358
|
-
throw errors.internal('the SSH transport does not support folder saves')
|
|
359
|
-
}
|
|
360
|
-
const base = await requireDestination(destination, options)
|
|
361
|
-
const name = remoteFileName(remotePath)
|
|
362
|
-
let root = null
|
|
363
|
-
try {
|
|
364
|
-
root = await createUniqueFolder(base, name, options)
|
|
365
|
-
const result = await runtime.ssh.saveRemoteTree(remotePath, root, options)
|
|
366
|
-
return {
|
|
367
|
-
path: remotePath,
|
|
368
|
-
kind: 'folder',
|
|
369
|
-
name,
|
|
370
|
-
destination: result?.destination ?? root,
|
|
371
|
-
totalBytes: Number(result?.totalBytes ?? 0),
|
|
372
|
-
files: Number(result?.totalFiles ?? 0),
|
|
373
|
-
...(result?.cancelled === true ? { cancelled: true } : {}),
|
|
374
|
-
}
|
|
375
|
-
} catch (error) {
|
|
376
|
-
if (root) await rm(root, { recursive: true, force: true }).catch(() => {})
|
|
377
|
-
throw toDownloadError(error)
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
export { requireDestination }
|
package/src/local-ssh.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local SSH material (read-only): `~/.ssh/config` and `~/.ssh/known_hosts`; never writes, never prompts.
|
|
3
|
-
*/
|
|
4
|
-
import { existsSync } from 'node:fs'
|
|
5
|
-
import { readFile } from 'node:fs/promises'
|
|
6
|
-
import path from 'node:path'
|
|
7
|
-
|
|
8
|
-
export async function loadLocalSshMaterial(input) {
|
|
9
|
-
const sshDir = path.join(input.home, '.ssh')
|
|
10
|
-
const configPath = path.join(sshDir, 'config')
|
|
11
|
-
const knownHostsPath = path.join(sshDir, 'known_hosts')
|
|
12
|
-
const exists = input.exists ?? existsSync
|
|
13
|
-
const read = input.readFileImpl ?? readFile
|
|
14
|
-
let configText = ''
|
|
15
|
-
let knownHostsText = ''
|
|
16
|
-
try {
|
|
17
|
-
if (exists(configPath)) configText = await read(configPath, 'utf8')
|
|
18
|
-
} catch {
|
|
19
|
-
configText = ''
|
|
20
|
-
}
|
|
21
|
-
try {
|
|
22
|
-
if (exists(knownHostsPath)) knownHostsText = await read(knownHostsPath, 'utf8')
|
|
23
|
-
} catch {
|
|
24
|
-
knownHostsText = ''
|
|
25
|
-
}
|
|
26
|
-
return { sshDir, configText, knownHostsText, configPath, knownHostsPath }
|
|
27
|
-
}
|
package/src/local-version.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Detect the DSH version of the local installation; the remote runtime is pinned to this exact version, never to npm `latest`.
|
|
3
|
-
*/
|
|
4
|
-
import { existsSync } from 'node:fs'
|
|
5
|
-
import { readFile } from 'node:fs/promises'
|
|
6
|
-
import path from 'node:path'
|
|
7
|
-
import { normalizeVersion, parseVersion } from './version.js'
|
|
8
|
-
|
|
9
|
-
export async function detectLocalVersion(input = {}) {
|
|
10
|
-
const env = input.env ?? process.env
|
|
11
|
-
const explicit = env.DSH_VERSION || env.DSH_RUNTIME_VERSION
|
|
12
|
-
if (explicit && parseVersion(normalizeVersion(explicit))) {
|
|
13
|
-
return { version: normalizeVersion(explicit), source: 'env' }
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// The web client-module registry publishes the Host release identity, which
|
|
17
|
-
// releases share with the CLI package version.
|
|
18
|
-
try {
|
|
19
|
-
const clientModules = input.ctx?.get?.('clientModules')
|
|
20
|
-
const release = clientModules?.graph?.()?.release
|
|
21
|
-
if (release?.version && parseVersion(normalizeVersion(release.version))) {
|
|
22
|
-
return { version: normalizeVersion(release.version), source: 'client-modules' }
|
|
23
|
-
}
|
|
24
|
-
} catch {
|
|
25
|
-
/* service not present in this composition */
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
for (const candidate of candidatePackageJsons(input)) {
|
|
29
|
-
try {
|
|
30
|
-
if (!existsSync(candidate)) continue
|
|
31
|
-
const text = await readFile(candidate, 'utf8')
|
|
32
|
-
const parsed = JSON.parse(text)
|
|
33
|
-
if (parsed?.name === '@deepseek-ai/dsh' && parsed?.version && parseVersion(normalizeVersion(parsed.version))) {
|
|
34
|
-
return { version: normalizeVersion(parsed.version), source: `package.json:${candidate}` }
|
|
35
|
-
}
|
|
36
|
-
} catch {}
|
|
37
|
-
}
|
|
38
|
-
return { version: null, source: 'unknown' }
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The local DSH build's Node engine range (`engines.node`). It is the source of
|
|
43
|
-
* truth for the managed Node version the plugin installs on the remote: the
|
|
44
|
-
* remote runs this exact DSH version, so it must satisfy this declaration.
|
|
45
|
-
*
|
|
46
|
-
* Best-effort like the version probe: an unreadable or missing package yields
|
|
47
|
-
* `range: null` and the caller falls back to the plugin's own floor.
|
|
48
|
-
*/
|
|
49
|
-
export async function detectLocalEnginesNode(input = {}) {
|
|
50
|
-
for (const candidate of candidatePackageJsons(input)) {
|
|
51
|
-
try {
|
|
52
|
-
if (!existsSync(candidate)) continue
|
|
53
|
-
const parsed = JSON.parse(await readFile(candidate, 'utf8'))
|
|
54
|
-
const range = parsed?.engines?.node
|
|
55
|
-
if (typeof range === 'string' && range.trim() !== '') {
|
|
56
|
-
return { range: range.trim(), source: `package.json:${candidate}` }
|
|
57
|
-
}
|
|
58
|
-
} catch {}
|
|
59
|
-
}
|
|
60
|
-
return { range: null, source: 'unknown' }
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function candidatePackageJsons(input = {}) {
|
|
64
|
-
const out = []
|
|
65
|
-
const argv1 = input.argv1 ?? process.argv[1] ?? ''
|
|
66
|
-
const marker = `${path.sep}@deepseek-ai${path.sep}dsh${path.sep}`
|
|
67
|
-
const markerIndex = argv1.indexOf(marker)
|
|
68
|
-
if (markerIndex >= 0) {
|
|
69
|
-
out.push(path.join(argv1.slice(0, markerIndex + marker.length), 'package.json'))
|
|
70
|
-
}
|
|
71
|
-
const resourcesPath = input.resourcesPath ?? /** @type {any} */ (process).resourcesPath
|
|
72
|
-
if (typeof resourcesPath === 'string' && resourcesPath !== '') {
|
|
73
|
-
out.push(path.join(resourcesPath, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'package.json'))
|
|
74
|
-
out.push(
|
|
75
|
-
path.join(resourcesPath, 'app.asar.unpacked', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'package.json'),
|
|
76
|
-
)
|
|
77
|
-
out.push(path.join(resourcesPath, 'app', 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'package.json'))
|
|
78
|
-
}
|
|
79
|
-
// Walk up from the CLI entry looking for a nested package.
|
|
80
|
-
let dir = argv1 ? path.dirname(argv1) : (input.cwd ?? process.cwd())
|
|
81
|
-
for (let i = 0; i < 8; i += 1) {
|
|
82
|
-
out.push(path.join(dir, 'node_modules', '@deepseek-ai', 'dsh', 'package.json'))
|
|
83
|
-
const parent = path.dirname(dir)
|
|
84
|
-
if (parent === dir) break
|
|
85
|
-
dir = parent
|
|
86
|
-
}
|
|
87
|
-
return [...new Set(out)]
|
|
88
|
-
}
|