dsh-remote-go 0.2.0 → 0.3.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.
- package/README.md +11 -3
- package/{client.js → dist/client.js} +7144 -8367
- package/dist/index.js +4 -0
- package/dist/src/admission.js +21 -0
- package/dist/src/api.js +380 -0
- package/dist/src/atomic-json.js +201 -0
- package/dist/src/bootstrap.js +96 -0
- package/dist/src/connect/attach.js +21 -0
- package/dist/src/connect/bootstrap.js +8 -0
- package/dist/src/connect/failures.js +29 -0
- package/dist/src/connect/guards.js +36 -0
- package/dist/src/connect/launch.js +70 -0
- package/dist/src/connect/links.js +54 -0
- package/dist/src/connect/ownership.js +139 -0
- package/dist/src/connect/pipeline.js +376 -0
- package/dist/src/connect/reattach.js +191 -0
- package/dist/src/connect/registry.js +10 -0
- package/dist/src/connect/repair.js +97 -0
- package/dist/src/connect/restart.js +255 -0
- package/dist/src/connect/stop.js +164 -0
- package/dist/src/connect/transport.js +101 -0
- package/dist/src/connect/verify.js +30 -0
- package/dist/src/connect/watchdog.js +44 -0
- package/dist/src/connect-log.js +137 -0
- package/dist/src/connect-progress.js +110 -0
- package/dist/src/constants.js +29 -0
- package/dist/src/context.js +96 -0
- package/dist/src/diagnostics.js +133 -0
- package/dist/src/download-jobs.js +221 -0
- package/dist/src/errors.js +228 -0
- package/dist/src/events.js +326 -0
- package/dist/src/host-lifecycle.js +368 -0
- package/dist/src/host-registry.js +181 -0
- package/dist/src/host-runtime.js +77 -0
- package/dist/src/host-view.js +52 -0
- package/dist/src/index.js +196 -0
- package/dist/src/kernel-adapter.js +388 -0
- package/dist/src/key-files.js +39 -0
- package/dist/src/known-hosts.js +104 -0
- package/dist/src/layout.js +26 -0
- package/dist/src/local-download.js +311 -0
- package/dist/src/local-ssh.js +23 -0
- package/dist/src/local-version.js +87 -0
- package/dist/src/node/download.js +157 -0
- package/dist/src/node-runtime.js +327 -0
- package/dist/src/probe.js +95 -0
- package/dist/src/prompts.js +198 -0
- package/dist/src/proxy/cookies.js +102 -0
- package/dist/src/proxy/headers.js +167 -0
- package/dist/src/proxy/http.js +134 -0
- package/dist/src/proxy/index.js +121 -0
- package/dist/src/proxy/limits.js +6 -0
- package/dist/src/proxy/routes.js +32 -0
- package/dist/src/proxy/upgrade.js +160 -0
- package/dist/src/redact.js +85 -0
- package/dist/src/remote-files.js +60 -0
- package/dist/src/remote-path.js +82 -0
- package/dist/src/remote-proc.js +271 -0
- package/dist/src/remote-web/registry.js +107 -0
- package/dist/src/remote-web/resolve.js +229 -0
- package/dist/src/remote-web/start.js +190 -0
- package/dist/src/remote-web/stop.js +74 -0
- package/dist/src/request-trust.js +70 -0
- package/dist/src/runtime/dispatch.js +200 -0
- package/dist/src/runtime/effects.js +81 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +70 -0
- package/dist/src/runtime/rules.js +480 -0
- package/dist/src/secrets.js +119 -0
- package/dist/src/service.js +300 -0
- package/dist/src/settings.js +143 -0
- package/dist/src/ssh-config.js +257 -0
- package/dist/src/ssh-failures.js +71 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +258 -0
- package/dist/src/ssh.js +395 -0
- package/dist/src/store.js +174 -0
- package/dist/src/tool-shared.js +146 -0
- package/dist/src/tools-exec-policy.js +121 -0
- package/dist/src/tools-exec.js +307 -0
- package/dist/src/tools-report.js +357 -0
- package/dist/src/tools.js +337 -0
- package/dist/src/tunnel.js +111 -0
- package/dist/src/util.js +112 -0
- package/dist/src/version.js +108 -0
- package/dist/src/watchdog-bus.js +21 -0
- package/dist/src/watchdog.js +116 -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/constants.js +0 -93
- 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
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
// Local download writer: streams one remote file or folder from SFTP into a user-selected local directory, never
|
|
2
|
+
// overwriting (a taken name gets a ` (2)` suffix). The destination is a **write primitive whose contents a remote host
|
|
3
|
+
// chooses**, so it is fenced to folders under the local user's home — otherwise a compromised remote could save into
|
|
4
|
+
// `…\Start Menu\Programs\Startup` or `~/.ssh/authorized_keys` and run code at the next logon.
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { mkdir, open, rm, stat } from 'node:fs/promises';
|
|
8
|
+
import { errors, isRemoteGoError } from './errors.js';
|
|
9
|
+
import { remoteFileName, safeLocalSegment } from './remote-path.js';
|
|
10
|
+
// Segments that execute something at logon or authorize one, matched case-insensitively as a whole segment anywhere
|
|
11
|
+
// below home (so `…\Start Menu\Programs\Startup` and `~/.config/autostart` hit). The login group covers the shell
|
|
12
|
+
// startup files, `~/.config`/`~/.local` (whose `bin` is on PATH in many distributions), the macOS login items and X.
|
|
13
|
+
const FORBIDDEN_SEGMENTS = new Map([
|
|
14
|
+
['startup', 'autorun'],
|
|
15
|
+
['startupfolder', 'autorun'],
|
|
16
|
+
['startupitems', 'autorun'],
|
|
17
|
+
['autostart', 'autorun'],
|
|
18
|
+
['systemd', 'autorun'],
|
|
19
|
+
['launchagents', 'autorun'],
|
|
20
|
+
['launchdaemons', 'autorun'],
|
|
21
|
+
['.ssh', 'credentials'],
|
|
22
|
+
['.gnupg', 'credentials'],
|
|
23
|
+
['.config', 'login'],
|
|
24
|
+
['.local', 'login'],
|
|
25
|
+
['.bashrc', 'login'],
|
|
26
|
+
['.bash_profile', 'login'],
|
|
27
|
+
['.bash_login', 'login'],
|
|
28
|
+
['.bash_logout', 'login'],
|
|
29
|
+
['.profile', 'login'],
|
|
30
|
+
['.zshrc', 'login'],
|
|
31
|
+
['.zprofile', 'login'],
|
|
32
|
+
['.zshenv', 'login'],
|
|
33
|
+
['.zlogin', 'login'],
|
|
34
|
+
['.zlogout', 'login'],
|
|
35
|
+
['.kshrc', 'login'],
|
|
36
|
+
['.cshrc', 'login'],
|
|
37
|
+
['.tcshrc', 'login'],
|
|
38
|
+
['.login', 'login'],
|
|
39
|
+
['.logout', 'login'],
|
|
40
|
+
['.xsessionrc', 'login'],
|
|
41
|
+
['.xprofile', 'login'],
|
|
42
|
+
['.xinitrc', 'login'],
|
|
43
|
+
]);
|
|
44
|
+
/** `shell:startup` / `shell:common startup` name an autorun folder without spelling a path. */
|
|
45
|
+
const SHELL_FOLDER_RE = /^\s*shell\s*:/i;
|
|
46
|
+
/** Directories that belong to the OS; never a download target even if home somehow covers them. */
|
|
47
|
+
const SYSTEM_ROOTS = Object.freeze(process.platform === 'win32'
|
|
48
|
+
? ['C:\\Windows', 'C:\\Program Files', 'C:\\Program Files (x86)', 'C:\\ProgramData']
|
|
49
|
+
: ['/etc', '/usr', '/bin', '/sbin', '/lib', '/lib64', '/boot', '/opt', '/var', '/dev', '/proc', '/sys']);
|
|
50
|
+
/** A stronger reason wins when several segments of one path match (most specific first). */
|
|
51
|
+
const REASON_PRIORITY = ['autorun', 'credentials', 'login', 'system'];
|
|
52
|
+
/** The client-facing text of each refusal; `invalid` also covers "no destination at all". */
|
|
53
|
+
const REFUSAL_TEXT = {
|
|
54
|
+
invalid: { zh: '请选择本机的保存文件夹。', en: 'Choose a destination folder on this machine.' },
|
|
55
|
+
'outside-home': {
|
|
56
|
+
zh: '只能保存到本机用户主目录下的文件夹(防止写入系统或自启动位置)。',
|
|
57
|
+
en: 'Downloads must go to a folder under this machine’s home directory (system and autorun locations are refused).',
|
|
58
|
+
},
|
|
59
|
+
autorun: {
|
|
60
|
+
zh: '不能保存到自启动目录(例如 Startup / autostart / shell:startup)。',
|
|
61
|
+
en: 'An autorun folder (for example Startup / autostart / shell:startup) cannot be a download target.',
|
|
62
|
+
},
|
|
63
|
+
credentials: {
|
|
64
|
+
zh: '不能保存到 SSH / GPG 配置目录(例如 .ssh)。',
|
|
65
|
+
en: 'An SSH / GPG configuration folder (for example .ssh) cannot be a download target.',
|
|
66
|
+
},
|
|
67
|
+
login: {
|
|
68
|
+
zh: '不能保存到登录脚本或用户配置目录(例如 .bashrc / .profile / .config / .local)。',
|
|
69
|
+
en: 'A login script or user configuration folder (for example .bashrc / .profile / .config / .local) cannot be a download target.',
|
|
70
|
+
},
|
|
71
|
+
protected: {
|
|
72
|
+
zh: '不能保存到会在登录或启动时生效的位置(自启动、登录脚本或凭据目录及其上级目录)。',
|
|
73
|
+
en: 'A location that runs or authenticates at logon (autorun, login scripts, credentials, or one of their parent folders) cannot be a download target.',
|
|
74
|
+
},
|
|
75
|
+
system: {
|
|
76
|
+
zh: '不能保存到系统目录。',
|
|
77
|
+
en: 'A system directory cannot be a download target.',
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
/** The remaining client-facing texts of this module. */
|
|
81
|
+
const TEXT = {
|
|
82
|
+
notFolder: { zh: '选择的保存位置不是文件夹。', en: 'The selected destination is not a folder.' },
|
|
83
|
+
destinationUnusable: {
|
|
84
|
+
zh: '选择的保存文件夹不存在或不可写。',
|
|
85
|
+
en: 'The selected destination folder does not exist or cannot be written.',
|
|
86
|
+
},
|
|
87
|
+
tooManyFolders: {
|
|
88
|
+
zh: '目标目录里已经有太多同名文件夹,请换一个目录。',
|
|
89
|
+
en: 'The destination already contains too many folders with this name; choose another directory.',
|
|
90
|
+
},
|
|
91
|
+
tooManyFiles: {
|
|
92
|
+
zh: '目标目录里已经有太多同名文件,请换一个目录。',
|
|
93
|
+
en: 'The destination already contains too many files with this name; choose another directory.',
|
|
94
|
+
},
|
|
95
|
+
fileNotConnected: {
|
|
96
|
+
zh: '这台主机还没有连接,无法读取远端文件。',
|
|
97
|
+
en: 'This host is not connected, so the remote file cannot be read.',
|
|
98
|
+
},
|
|
99
|
+
folderNotConnected: {
|
|
100
|
+
zh: '这台主机还没有连接,无法读取远端文件夹。',
|
|
101
|
+
en: 'This host is not connected, so the remote folder cannot be read.',
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
// Windows and macOS default to a case-insensitive filesystem, so compare the way the OS does.
|
|
105
|
+
function comparable(value) {
|
|
106
|
+
return process.platform === 'win32' || process.platform === 'darwin' ? value.toLowerCase() : value;
|
|
107
|
+
}
|
|
108
|
+
function segmentsOf(resolved) {
|
|
109
|
+
return resolved.split(/[\\/]+/).filter((segment) => segment !== '');
|
|
110
|
+
}
|
|
111
|
+
function isInside(root, target) {
|
|
112
|
+
const base = root.endsWith(path.sep) ? root : `${root}${path.sep}`;
|
|
113
|
+
return target === root || target.startsWith(base);
|
|
114
|
+
}
|
|
115
|
+
// Locations whose *contents* run or authenticate at logon: a download may neither land inside one nor create an
|
|
116
|
+
// ancestor of one, or `~/AppData/Roaming` plus a remote folder named `Microsoft` would put a Startup tree on disk.
|
|
117
|
+
function sensitiveRoots(home) {
|
|
118
|
+
const roots = ['.config', '.local', '.ssh', '.gnupg'].map((name) => path.join(home, name));
|
|
119
|
+
roots.push(path.join(home, 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu'));
|
|
120
|
+
for (const name of ['LaunchAgents', 'LaunchDaemons', 'StartupItems'])
|
|
121
|
+
roots.push(path.join(home, 'Library', name));
|
|
122
|
+
const appData = typeof process.env.APPDATA === 'string' && process.env.APPDATA.trim() !== '';
|
|
123
|
+
if (appData)
|
|
124
|
+
roots.push(path.join(process.env.APPDATA, 'Microsoft', 'Windows', 'Start Menu'));
|
|
125
|
+
return roots;
|
|
126
|
+
}
|
|
127
|
+
/** Whether a path is a sensitive location, inside one, or an ancestor of one. */
|
|
128
|
+
function isProtectedLocation(target, home) {
|
|
129
|
+
const resolved = comparable(target);
|
|
130
|
+
for (const root of sensitiveRoots(home)) {
|
|
131
|
+
const sensitive = comparable(path.resolve(root));
|
|
132
|
+
if (isInside(sensitive, resolved) || isInside(resolved, sensitive))
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
function homeOf(options = {}) {
|
|
138
|
+
return path.resolve(options.home ?? homedir());
|
|
139
|
+
}
|
|
140
|
+
// Pure: no filesystem access, so a caller can ask before creating anything and a test can ask about
|
|
141
|
+
// a folder that does not exist.
|
|
142
|
+
export function destinationPolicy(destination, options = {}) {
|
|
143
|
+
if (typeof destination !== 'string' || destination.trim() === '' || destination.includes('\0'))
|
|
144
|
+
return 'invalid';
|
|
145
|
+
if (SHELL_FOLDER_RE.test(destination))
|
|
146
|
+
return 'autorun';
|
|
147
|
+
const home = comparable(homeOf(options));
|
|
148
|
+
const target = comparable(path.resolve(destination));
|
|
149
|
+
if (!isInside(home, target))
|
|
150
|
+
return 'outside-home';
|
|
151
|
+
const segments = segmentsOf(target);
|
|
152
|
+
const reasons = new Set();
|
|
153
|
+
for (const segment of segments) {
|
|
154
|
+
const reason = FORBIDDEN_SEGMENTS.get(segment.toLowerCase());
|
|
155
|
+
if (reason !== undefined)
|
|
156
|
+
reasons.add(reason);
|
|
157
|
+
}
|
|
158
|
+
// `…\Start Menu\Programs`: a two-segment location whose entries appear in the Start menu.
|
|
159
|
+
for (let at = 1; at < segments.length; at += 1) {
|
|
160
|
+
if (segments[at - 1].toLowerCase() === 'start menu' && segments[at].toLowerCase() === 'programs') {
|
|
161
|
+
reasons.add('autorun');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (const root of SYSTEM_ROOTS) {
|
|
165
|
+
if (isInside(comparable(path.resolve(root)), target))
|
|
166
|
+
reasons.add('system');
|
|
167
|
+
}
|
|
168
|
+
for (const reason of REASON_PRIORITY)
|
|
169
|
+
if (reasons.has(reason))
|
|
170
|
+
return reason;
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
function destinationRefusal(reason) {
|
|
174
|
+
return errors.invalidInput('destination', REFUSAL_TEXT[reason ?? 'invalid']);
|
|
175
|
+
}
|
|
176
|
+
// The destination is chosen locally, but the *name* below it comes from the remote tree (`~/.config` + a remote folder
|
|
177
|
+
// called `autostart`, `~` + a remote file called `.bash_login`); this refuses those combinations before any write.
|
|
178
|
+
function assertCandidateAllowed(candidate, options = {}) {
|
|
179
|
+
const refusal = destinationPolicy(candidate, options);
|
|
180
|
+
if (refusal !== null)
|
|
181
|
+
throw destinationRefusal(refusal);
|
|
182
|
+
if (isProtectedLocation(candidate, homeOf(options)))
|
|
183
|
+
throw destinationRefusal('protected');
|
|
184
|
+
}
|
|
185
|
+
export async function requireDestination(destination, options = {}) {
|
|
186
|
+
const refusal = destinationPolicy(destination, options);
|
|
187
|
+
if (refusal !== null)
|
|
188
|
+
throw destinationRefusal(refusal);
|
|
189
|
+
// `destinationPolicy` accepts only a non-empty string path, so by here the unknown is a string;
|
|
190
|
+
// the `as` restates that narrowing instead of adding a second runtime check.
|
|
191
|
+
const base = path.resolve(destination);
|
|
192
|
+
try {
|
|
193
|
+
const info = await stat(base);
|
|
194
|
+
if (!info.isDirectory())
|
|
195
|
+
throw errors.invalidInput('destination', TEXT.notFolder);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (isRemoteGoError(error))
|
|
199
|
+
throw error;
|
|
200
|
+
throw errors.invalidInput('destination', TEXT.destinationUnusable);
|
|
201
|
+
}
|
|
202
|
+
return base;
|
|
203
|
+
}
|
|
204
|
+
/** Only EEXIST means "this name is taken, try the next suffix"; every other code is a real failure. */
|
|
205
|
+
function isTaken(error) {
|
|
206
|
+
return error?.code === 'EEXIST';
|
|
207
|
+
}
|
|
208
|
+
async function createUniqueFolder(base, name, options = {}) {
|
|
209
|
+
const safeName = safeLocalSegment(name) || 'download';
|
|
210
|
+
for (let index = 1; index <= 999; index += 1) {
|
|
211
|
+
const candidate = path.join(base, index === 1 ? safeName : `${safeName} (${index})`);
|
|
212
|
+
// The root's name comes from the remote tree, so it is judged before anything is created:
|
|
213
|
+
// `~/.config` + a remote folder called `autostart` must fail here, with zero writes.
|
|
214
|
+
assertCandidateAllowed(candidate, options);
|
|
215
|
+
try {
|
|
216
|
+
await mkdir(candidate);
|
|
217
|
+
return candidate;
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
if (!isTaken(error))
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
throw errors.invalidInput('destination', TEXT.tooManyFolders);
|
|
225
|
+
}
|
|
226
|
+
async function createUniqueFile(base, name, options = {}) {
|
|
227
|
+
const safeName = safeLocalSegment(name) || 'download';
|
|
228
|
+
const extension = path.extname(safeName);
|
|
229
|
+
const stem = path.basename(safeName, extension) || 'download';
|
|
230
|
+
for (let index = 1; index <= 999; index += 1) {
|
|
231
|
+
const fileName = index === 1 ? safeName : `${stem} (${index})${extension}`;
|
|
232
|
+
const candidate = path.join(base, fileName);
|
|
233
|
+
// The remote names the file: `~` + `.bash_login` is a login script, not a download.
|
|
234
|
+
assertCandidateAllowed(candidate, options);
|
|
235
|
+
try {
|
|
236
|
+
const handle = await open(candidate, 'wx', 0o600);
|
|
237
|
+
await handle.close();
|
|
238
|
+
return candidate;
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
if (!isTaken(error))
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
throw errors.invalidInput('destination', TEXT.tooManyFiles);
|
|
246
|
+
}
|
|
247
|
+
function toDownloadError(error) {
|
|
248
|
+
// `isRemoteGoError` only declares a boolean (it lives in the P1a set); the `as` restates
|
|
249
|
+
// the narrowing its `instanceof` proves at runtime.
|
|
250
|
+
if (isRemoteGoError(error))
|
|
251
|
+
return error;
|
|
252
|
+
return errors.remoteFileUnavailable(undefined, {
|
|
253
|
+
technical: String(error?.message ?? error ?? '').slice(0, 300),
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
function requireTransport(runtime, notConnected) {
|
|
257
|
+
if (!runtime || runtime.status !== 'connected' || !runtime.ssh)
|
|
258
|
+
throw errors.remoteFileUnavailable(notConnected);
|
|
259
|
+
return runtime.ssh;
|
|
260
|
+
}
|
|
261
|
+
export async function saveHostFile(runtime, remotePath, destination, options = {}) {
|
|
262
|
+
const ssh = requireTransport(runtime, TEXT.fileNotConnected);
|
|
263
|
+
if (typeof ssh.saveRemoteFile !== 'function')
|
|
264
|
+
throw errors.internal('the SSH transport does not support file saves');
|
|
265
|
+
const base = await requireDestination(destination, options);
|
|
266
|
+
const name = remoteFileName(remotePath);
|
|
267
|
+
let localPath = null;
|
|
268
|
+
try {
|
|
269
|
+
localPath = await createUniqueFile(base, name, options);
|
|
270
|
+
const result = await ssh.saveRemoteFile(remotePath, localPath, options);
|
|
271
|
+
return {
|
|
272
|
+
path: remotePath,
|
|
273
|
+
kind: 'file',
|
|
274
|
+
name: path.basename(localPath),
|
|
275
|
+
destination: localPath,
|
|
276
|
+
totalBytes: Number(result?.size ?? 0),
|
|
277
|
+
files: 1,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
if (localPath)
|
|
282
|
+
await rm(localPath, { force: true }).catch(() => { });
|
|
283
|
+
throw toDownloadError(error);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
export async function saveHostFolder(runtime, remotePath, destination, options = {}) {
|
|
287
|
+
const ssh = requireTransport(runtime, TEXT.folderNotConnected);
|
|
288
|
+
if (typeof ssh.saveRemoteTree !== 'function')
|
|
289
|
+
throw errors.internal('the SSH transport does not support folder saves');
|
|
290
|
+
const base = await requireDestination(destination, options);
|
|
291
|
+
const name = remoteFileName(remotePath);
|
|
292
|
+
let root = null;
|
|
293
|
+
try {
|
|
294
|
+
root = await createUniqueFolder(base, name, options);
|
|
295
|
+
const result = await ssh.saveRemoteTree(remotePath, root, options);
|
|
296
|
+
return {
|
|
297
|
+
path: remotePath,
|
|
298
|
+
kind: 'folder',
|
|
299
|
+
name,
|
|
300
|
+
destination: result?.destination ?? root,
|
|
301
|
+
totalBytes: Number(result?.totalBytes ?? 0),
|
|
302
|
+
files: Number(result?.totalFiles ?? 0),
|
|
303
|
+
...(result?.cancelled === true ? { cancelled: true } : {}),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
if (root)
|
|
308
|
+
await rm(root, { recursive: true, force: true }).catch(() => { });
|
|
309
|
+
throw toDownloadError(error);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Local SSH material (read-only): `~/.ssh/config` and `~/.ssh/known_hosts`; never writes, never prompts. */
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
export async function loadLocalSshMaterial(input) {
|
|
6
|
+
const sshDir = path.join(input.home, '.ssh');
|
|
7
|
+
const configPath = path.join(sshDir, 'config');
|
|
8
|
+
const knownHostsPath = path.join(sshDir, 'known_hosts');
|
|
9
|
+
const exists = input.exists ?? existsSync;
|
|
10
|
+
const read = input.readFileImpl ?? readFile;
|
|
11
|
+
// A missing or unreadable file reads as empty: the wizard has nothing to offer either way.
|
|
12
|
+
const readIf = async (target) => {
|
|
13
|
+
try {
|
|
14
|
+
return exists(target) ? await read(target, 'utf8') : '';
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return '';
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const configText = await readIf(configPath);
|
|
21
|
+
const knownHostsText = await readIf(knownHostsPath);
|
|
22
|
+
return { sshDir, configText, knownHostsText, configPath, knownHostsPath };
|
|
23
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Detect the DSH version of the local installation; the remote runtime is pinned to this exact version, never to npm `latest`. */
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { createKernelAdapter } from './kernel-adapter.js';
|
|
6
|
+
import { isPlainObject } from './util.js';
|
|
7
|
+
import { normalizeVersion, parseVersion } from './version.js';
|
|
8
|
+
/** The parsed `package.json` at `candidate`, or null when it is absent or not a JSON object. */
|
|
9
|
+
async function readPackageJson(candidate) {
|
|
10
|
+
try {
|
|
11
|
+
if (!existsSync(candidate))
|
|
12
|
+
return null;
|
|
13
|
+
// A package.json on disk is untrusted input: narrow it before reading fields.
|
|
14
|
+
const parsed = JSON.parse(await readFile(candidate, 'utf8'));
|
|
15
|
+
return isPlainObject(parsed) ? parsed : null;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// swallow-ok: a candidate that is absent or unparsable is skipped by design
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function detectLocalVersion(input = {}) {
|
|
23
|
+
const env = input.env ?? process.env;
|
|
24
|
+
const explicit = env.DSH_VERSION || env.DSH_RUNTIME_VERSION;
|
|
25
|
+
if (explicit && parseVersion(normalizeVersion(explicit)))
|
|
26
|
+
return { version: normalizeVersion(explicit), source: 'env' };
|
|
27
|
+
// The web client-module registry publishes the Host release identity, which releases share with
|
|
28
|
+
// the CLI package version.
|
|
29
|
+
const release = createKernelAdapter(input.ctx).clientModuleRelease();
|
|
30
|
+
if (release.state === 'ok' && release.value !== null) {
|
|
31
|
+
const version = release.value.version;
|
|
32
|
+
if (version !== null && parseVersion(normalizeVersion(version))) {
|
|
33
|
+
return { version: normalizeVersion(version), source: 'client-modules' };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (release.state === 'changed') {
|
|
37
|
+
// A moved release identity is a kernel this probe does not recognize: keep the package.json
|
|
38
|
+
// fallback, but say so — a silently wrong pin breaks every connect that follows.
|
|
39
|
+
input.logger?.warn?.('[dsh-remote-go] the client-module release identity changed shape; falling back to package.json:', release.detail);
|
|
40
|
+
}
|
|
41
|
+
for (const candidate of candidatePackageJsons(input)) {
|
|
42
|
+
const manifest = await readPackageJson(candidate);
|
|
43
|
+
if (manifest?.name !== '@deepseek-ai/dsh' || typeof manifest.version !== 'string')
|
|
44
|
+
continue;
|
|
45
|
+
if (!parseVersion(normalizeVersion(manifest.version)))
|
|
46
|
+
continue;
|
|
47
|
+
return { version: normalizeVersion(manifest.version), source: `package.json:${candidate}` };
|
|
48
|
+
}
|
|
49
|
+
return { version: null, source: 'unknown' };
|
|
50
|
+
}
|
|
51
|
+
/** The local DSH build's Node engine range (`engines.node`), the source of truth for the managed Node on the remote: it
|
|
52
|
+
* must satisfy this declaration. Best-effort: a missing package yields `range: null` and the plugin's own floor. */
|
|
53
|
+
export async function detectLocalEnginesNode(input = {}) {
|
|
54
|
+
for (const candidate of candidatePackageJsons(input)) {
|
|
55
|
+
const manifest = await readPackageJson(candidate);
|
|
56
|
+
const engines = manifest !== null && isPlainObject(manifest.engines) ? manifest.engines : null;
|
|
57
|
+
const range = engines?.node;
|
|
58
|
+
if (typeof range === 'string' && range.trim() !== '')
|
|
59
|
+
return { range: range.trim(), source: `package.json:${candidate}` };
|
|
60
|
+
}
|
|
61
|
+
return { range: null, source: 'unknown' };
|
|
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
|
+
// Electron's `resourcesPath` is not on the standard Process type; it is read duck-typed.
|
|
71
|
+
const resourcesPath = input.resourcesPath ?? process.resourcesPath;
|
|
72
|
+
if (typeof resourcesPath === 'string' && resourcesPath !== '') {
|
|
73
|
+
for (const dir of ['app.asar', 'app.asar.unpacked', 'app']) {
|
|
74
|
+
out.push(path.join(resourcesPath, dir, 'dsh', 'node_modules', '@deepseek-ai', 'dsh', 'package.json'));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// Walk up from the CLI entry looking for a nested package.
|
|
78
|
+
let dir = argv1 ? path.dirname(argv1) : (input.cwd ?? process.cwd());
|
|
79
|
+
for (let i = 0; i < 8; i += 1) {
|
|
80
|
+
out.push(path.join(dir, 'node_modules', '@deepseek-ai', 'dsh', 'package.json'));
|
|
81
|
+
const parent = path.dirname(dir);
|
|
82
|
+
if (parent === dir)
|
|
83
|
+
break;
|
|
84
|
+
dir = parent;
|
|
85
|
+
}
|
|
86
|
+
return [...new Set(out)];
|
|
87
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { shellQuote } from '../util.js';
|
|
2
|
+
export const NODE_DIST_BASES = Object.freeze([
|
|
3
|
+
'https://nodejs.org/dist',
|
|
4
|
+
'https://npmmirror.com/mirrors/node',
|
|
5
|
+
]);
|
|
6
|
+
/** The downloader both install scripts share: curl when present, else wget; exit 9 is the "neither tool" contract the caller maps to `no-fetch`. */
|
|
7
|
+
const FETCH_PROLOGUE = `if command -v curl >/dev/null 2>&1; then
|
|
8
|
+
FETCH=curl
|
|
9
|
+
elif command -v wget >/dev/null 2>&1; then
|
|
10
|
+
FETCH=wget
|
|
11
|
+
else
|
|
12
|
+
printf 'FAILED\\tno-fetch\\n'
|
|
13
|
+
exit 9
|
|
14
|
+
fi`;
|
|
15
|
+
export function buildNodeChecksumScript(input) {
|
|
16
|
+
const bases = (input.bases ?? NODE_DIST_BASES).map((base) => shellQuote(base)).join(' ');
|
|
17
|
+
return `set -u
|
|
18
|
+
DL=${shellQuote(input.downloadDir)}
|
|
19
|
+
VERSION=${shellQuote(input.version)}
|
|
20
|
+
FILE="node-v$VERSION-linux-${input.arch}.tar.gz"
|
|
21
|
+
mkdir -p "$DL" || exit 3
|
|
22
|
+
${FETCH_PROLOGUE}
|
|
23
|
+
fetch() {
|
|
24
|
+
URL=$1
|
|
25
|
+
OUT=$2
|
|
26
|
+
if [ "$FETCH" = curl ]; then
|
|
27
|
+
curl -fsSL --connect-timeout 10 --max-time 60 -o "$OUT" "$URL" 2>/dev/null
|
|
28
|
+
else
|
|
29
|
+
wget -q -T 30 -t 1 -O "$OUT" "$URL" 2>/dev/null
|
|
30
|
+
fi
|
|
31
|
+
}
|
|
32
|
+
for BASE in ${bases}; do
|
|
33
|
+
TMP="$DL/.shasums.$$"
|
|
34
|
+
if fetch "$BASE/v$VERSION/SHASUMS256.txt" "$TMP"; then
|
|
35
|
+
LINE=$(grep -F " $FILE" "$TMP" | head -n 1 || true)
|
|
36
|
+
if [ -n "$LINE" ]; then
|
|
37
|
+
SHA=$(printf '%s\\n' "$LINE" | awk '{print $1}')
|
|
38
|
+
if [ "\${#SHA}" -eq 64 ]; then
|
|
39
|
+
printf 'CHECKSUM\\t%s\\t%s\\n' "$BASE" "$SHA"
|
|
40
|
+
rm -f "$TMP"
|
|
41
|
+
exit 0
|
|
42
|
+
fi
|
|
43
|
+
fi
|
|
44
|
+
fi
|
|
45
|
+
rm -f "$TMP"
|
|
46
|
+
done
|
|
47
|
+
exit 5
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
export function parseNodeChecksumOutput(stdout, input) {
|
|
51
|
+
const file = `node-v${input.version}-linux-${input.arch}.tar.gz`;
|
|
52
|
+
const allowed = input.bases ?? NODE_DIST_BASES;
|
|
53
|
+
for (const rawLine of String(stdout ?? '').split(/\r?\n/)) {
|
|
54
|
+
const parts = rawLine.split('\t');
|
|
55
|
+
if (parts[0] !== 'CHECKSUM' || parts.length < 3)
|
|
56
|
+
continue;
|
|
57
|
+
const [, base, sha256] = parts;
|
|
58
|
+
if (!allowed.includes(base))
|
|
59
|
+
continue;
|
|
60
|
+
if (!/^[0-9a-f]{64}$/.test(sha256))
|
|
61
|
+
continue;
|
|
62
|
+
return { base, version: input.version, sha256, file };
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
export function buildNodeInstallScript(input) {
|
|
67
|
+
const bases = (input.bases ?? NODE_DIST_BASES).map((base) => shellQuote(base)).join(' ');
|
|
68
|
+
const { release } = input;
|
|
69
|
+
return `set -eu
|
|
70
|
+
NODE_DIR=${shellQuote(input.layout.nodeDir)}
|
|
71
|
+
DL=${shellQuote(input.layout.downloadDir)}
|
|
72
|
+
VERSION=${shellQuote(release.version)}
|
|
73
|
+
SHA=${shellQuote(release.sha256)}
|
|
74
|
+
FILE=${shellQuote(release.file)}
|
|
75
|
+
BINDIR="$NODE_DIR/v$VERSION"
|
|
76
|
+
mkdir -p "$NODE_DIR" "$DL" || exit 3
|
|
77
|
+
if [ -x "$BINDIR/bin/node" ] && [ "$("$BINDIR/bin/node" -v 2>/dev/null || true)" = "v$VERSION" ]; then
|
|
78
|
+
printf 'READY\\t%s\\n' "$BINDIR"
|
|
79
|
+
exit 0
|
|
80
|
+
fi
|
|
81
|
+
${FETCH_PROLOGUE}
|
|
82
|
+
verify() {
|
|
83
|
+
TARGET=$1
|
|
84
|
+
if command -v sha256sum >/dev/null 2>&1; then
|
|
85
|
+
printf '%s %s\\n' "$SHA" "$TARGET" | sha256sum -c - >/dev/null 2>&1
|
|
86
|
+
elif command -v shasum >/dev/null 2>&1; then
|
|
87
|
+
printf '%s %s\\n' "$SHA" "$TARGET" | shasum -a 256 -c - >/dev/null 2>&1
|
|
88
|
+
elif command -v openssl >/dev/null 2>&1; then
|
|
89
|
+
GOT=$(openssl dgst -sha256 "$TARGET" 2>/dev/null | sed 's/.*= //')
|
|
90
|
+
[ "$GOT" = "$SHA" ]
|
|
91
|
+
else
|
|
92
|
+
return 127
|
|
93
|
+
fi
|
|
94
|
+
}
|
|
95
|
+
TARBALL="$DL/$FILE"
|
|
96
|
+
if [ -f "$TARBALL" ] && verify "$TARBALL"; then
|
|
97
|
+
echo "STEP\\treuse"
|
|
98
|
+
else
|
|
99
|
+
OK=0
|
|
100
|
+
for BASE in ${bases}; do
|
|
101
|
+
PART="$TARBALL.part.$$"
|
|
102
|
+
rm -f "$PART"
|
|
103
|
+
printf 'STEP\\tdownload\\t%s\\n' "$BASE"
|
|
104
|
+
if [ "$FETCH" = curl ]; then
|
|
105
|
+
curl -fsSL --connect-timeout 15 --max-time 600 -o "$PART" "$BASE/v$VERSION/$FILE" &
|
|
106
|
+
else
|
|
107
|
+
wget -q -T 60 -t 3 -O "$PART" "$BASE/v$VERSION/$FILE" &
|
|
108
|
+
fi
|
|
109
|
+
PID=$!
|
|
110
|
+
while kill -0 "$PID" 2>/dev/null; do
|
|
111
|
+
printf 'BYTES\\t%s\\n' "$(wc -c < "$PART" 2>/dev/null || echo 0)"
|
|
112
|
+
sleep 2
|
|
113
|
+
done
|
|
114
|
+
if wait "$PID" && verify "$PART"; then
|
|
115
|
+
mv "$PART" "$TARBALL"
|
|
116
|
+
OK=1
|
|
117
|
+
break
|
|
118
|
+
fi
|
|
119
|
+
printf 'FAILED\\tchecksum\\t%s\\n' "$BASE"
|
|
120
|
+
rm -f "$PART"
|
|
121
|
+
done
|
|
122
|
+
if [ "$OK" -ne 1 ]; then
|
|
123
|
+
echo "FAILED\\tdownload"
|
|
124
|
+
exit 6
|
|
125
|
+
fi
|
|
126
|
+
fi
|
|
127
|
+
echo "STEP\\textract"
|
|
128
|
+
TMPX="$NODE_DIR/.extract.$$"
|
|
129
|
+
rm -rf "$TMPX"
|
|
130
|
+
mkdir -p "$TMPX"
|
|
131
|
+
if ! tar -xzf "$TARBALL" -C "$TMPX" --strip-components=1; then
|
|
132
|
+
echo "FAILED\\textract"
|
|
133
|
+
rm -rf "$TMPX"
|
|
134
|
+
exit 7
|
|
135
|
+
fi
|
|
136
|
+
if [ ! -x "$TMPX/bin/node" ]; then
|
|
137
|
+
echo "FAILED\\textract"
|
|
138
|
+
rm -rf "$TMPX"
|
|
139
|
+
exit 7
|
|
140
|
+
fi
|
|
141
|
+
GOT=$("$TMPX/bin/node" -v 2>/dev/null || true)
|
|
142
|
+
if [ "$GOT" != "v$VERSION" ]; then
|
|
143
|
+
echo "FAILED\\tverify"
|
|
144
|
+
rm -rf "$TMPX"
|
|
145
|
+
exit 8
|
|
146
|
+
fi
|
|
147
|
+
rm -rf "$BINDIR"
|
|
148
|
+
mv "$TMPX" "$BINDIR"
|
|
149
|
+
rm -f "$TARBALL"
|
|
150
|
+
for OLD in "$NODE_DIR"/v*; do
|
|
151
|
+
[ -d "$OLD" ] || continue
|
|
152
|
+
[ "$OLD" = "$BINDIR" ] && continue
|
|
153
|
+
rm -rf "$OLD"
|
|
154
|
+
done
|
|
155
|
+
printf 'READY\\t%s\\n' "$BINDIR"
|
|
156
|
+
`;
|
|
157
|
+
}
|