dsh-remote-go 0.3.0 → 0.3.2
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 +1 -0
- package/dist/client.js +1348 -2095
- package/dist/src/admission.js +3 -28
- package/dist/src/api.js +37 -99
- package/dist/src/atomic-json.js +32 -66
- package/dist/src/bootstrap.js +17 -27
- package/dist/src/connect/attach.js +3 -25
- package/dist/src/connect/bootstrap.js +2 -8
- package/dist/src/connect/failures.js +3 -18
- package/dist/src/connect/guards.js +13 -8
- package/dist/src/connect/launch.js +11 -21
- package/dist/src/connect/links.js +11 -27
- package/dist/src/connect/ownership.js +33 -114
- package/dist/src/connect/pipeline.js +62 -159
- package/dist/src/connect/reattach.js +26 -44
- package/dist/src/connect/registry.js +0 -1
- package/dist/src/connect/repair.js +8 -37
- package/dist/src/connect/restart.js +36 -97
- package/dist/src/connect/stop.js +16 -50
- package/dist/src/connect/transport.js +10 -18
- package/dist/src/connect/verify.js +3 -12
- package/dist/src/connect/watchdog.js +6 -38
- package/dist/src/connect-log.js +16 -41
- package/dist/src/connect-progress.js +13 -45
- package/dist/src/constants.js +18 -43
- package/dist/src/context.js +13 -32
- package/dist/src/diagnostics.js +56 -25
- package/dist/src/download-jobs.js +11 -20
- package/dist/src/errors.js +175 -190
- package/dist/src/events.js +20 -84
- package/dist/src/host-lifecycle.js +69 -177
- package/dist/src/host-registry.js +27 -78
- package/dist/src/host-runtime.js +0 -9
- package/dist/src/host-view.js +6 -29
- package/dist/src/index.js +15 -35
- package/dist/src/kernel-adapter.js +34 -80
- package/dist/src/key-files.js +10 -23
- package/dist/src/known-hosts.js +13 -34
- package/dist/src/layout.js +3 -20
- package/dist/src/local-download.js +94 -163
- package/dist/src/local-ssh.js +12 -19
- package/dist/src/local-version.js +35 -49
- package/dist/src/node/download.js +1 -4
- package/dist/src/node-runtime.js +19 -38
- package/dist/src/probe.js +6 -7
- package/dist/src/prompts.js +56 -128
- package/dist/src/proxy/cookies.js +26 -60
- package/dist/src/proxy/headers.js +30 -125
- package/dist/src/proxy/http.js +33 -41
- package/dist/src/proxy/index.js +20 -34
- package/dist/src/proxy/limits.js +4 -6
- package/dist/src/proxy/routes.js +10 -17
- package/dist/src/proxy/upgrade.js +27 -42
- package/dist/src/redact.js +14 -33
- package/dist/src/remote-files.js +9 -21
- package/dist/src/remote-path.js +21 -62
- package/dist/src/remote-proc.js +35 -103
- package/dist/src/remote-web/registry.js +10 -50
- package/dist/src/remote-web/resolve.js +23 -104
- package/dist/src/remote-web/start.js +27 -64
- package/dist/src/remote-web/stop.js +6 -25
- package/dist/src/request-trust.js +15 -32
- package/dist/src/runtime/dispatch.js +33 -143
- package/dist/src/runtime/effects.js +7 -17
- package/dist/src/runtime/events.js +1 -1
- package/dist/src/runtime/fsm.js +11 -61
- package/dist/src/runtime/rules.js +121 -199
- package/dist/src/secrets.js +15 -38
- package/dist/src/service.js +22 -128
- package/dist/src/settings.js +41 -42
- package/dist/src/ssh-config.js +22 -41
- package/dist/src/ssh-failures.js +23 -74
- package/dist/src/ssh-sftp.js +47 -86
- package/dist/src/ssh.js +99 -179
- package/dist/src/store.js +31 -48
- package/dist/src/tool-shared.js +17 -52
- package/dist/src/tools-exec-policy.js +33 -79
- package/dist/src/tools-exec.js +87 -151
- package/dist/src/tools-report.js +100 -167
- package/dist/src/tools.js +59 -133
- package/dist/src/tunnel.js +9 -27
- package/dist/src/util.js +19 -56
- package/dist/src/version.js +6 -12
- package/dist/src/watchdog-bus.js +0 -8
- package/dist/src/watchdog.js +7 -47
- package/package.json +3 -3
|
@@ -1,29 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
*/
|
|
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.
|
|
14
5
|
import path from 'node:path';
|
|
15
6
|
import { homedir } from 'node:os';
|
|
16
7
|
import { mkdir, open, rm, stat } from 'node:fs/promises';
|
|
17
8
|
import { errors, isRemoteGoError } from './errors.js';
|
|
18
9
|
import { remoteFileName, safeLocalSegment } from './remote-path.js';
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
* `…\Start Menu\Programs\Startup` and `~/.config/autostart` are covered. Shell startup files
|
|
23
|
-
* and the per-user containers that hold them (`~/.config`, `~/.local`, whose `bin` is on PATH in
|
|
24
|
-
* many distributions) are refused for the same reason, as are the macOS login-item folders
|
|
25
|
-
* (`~/Library/LaunchAgents`, `LaunchDaemons`, `StartupItems`) and the X session files.
|
|
26
|
-
*/
|
|
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.
|
|
27
13
|
const FORBIDDEN_SEGMENTS = new Map([
|
|
28
14
|
['startup', 'autorun'],
|
|
29
15
|
['startupfolder', 'autorun'],
|
|
@@ -63,11 +49,59 @@ const SYSTEM_ROOTS = Object.freeze(process.platform === 'win32'
|
|
|
63
49
|
: ['/etc', '/usr', '/bin', '/sbin', '/lib', '/lib64', '/boot', '/opt', '/var', '/dev', '/proc', '/sys']);
|
|
64
50
|
/** A stronger reason wins when several segments of one path match (most specific first). */
|
|
65
51
|
const REASON_PRIORITY = ['autorun', 'credentials', 'login', 'system'];
|
|
66
|
-
/**
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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.
|
|
71
105
|
function comparable(value) {
|
|
72
106
|
return process.platform === 'win32' || process.platform === 'darwin' ? value.toLowerCase() : value;
|
|
73
107
|
}
|
|
@@ -78,31 +112,19 @@ function isInside(root, target) {
|
|
|
78
112
|
const base = root.endsWith(path.sep) ? root : `${root}${path.sep}`;
|
|
79
113
|
return target === root || target.startsWith(base);
|
|
80
114
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
* one of them nor create one of their ancestors: a remote folder named `Microsoft` under
|
|
84
|
-
* `~/AppData/Roaming` would otherwise put a whole `Windows\Start Menu\Programs\Startup` tree on
|
|
85
|
-
* disk without any single path segment looking suspicious (macOS has the same shape under
|
|
86
|
-
* `~/Library`).
|
|
87
|
-
* @param home - resolved home directory.
|
|
88
|
-
* @returns resolved sensitive roots that exist by definition (not on disk).
|
|
89
|
-
*/
|
|
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.
|
|
90
117
|
function sensitiveRoots(home) {
|
|
91
118
|
const roots = ['.config', '.local', '.ssh', '.gnupg'].map((name) => path.join(home, name));
|
|
92
119
|
roots.push(path.join(home, 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu'));
|
|
93
|
-
for (const name of ['LaunchAgents', 'LaunchDaemons', 'StartupItems'])
|
|
120
|
+
for (const name of ['LaunchAgents', 'LaunchDaemons', 'StartupItems'])
|
|
94
121
|
roots.push(path.join(home, 'Library', name));
|
|
95
|
-
}
|
|
96
122
|
const appData = typeof process.env.APPDATA === 'string' && process.env.APPDATA.trim() !== '';
|
|
97
123
|
if (appData)
|
|
98
124
|
roots.push(path.join(process.env.APPDATA, 'Microsoft', 'Windows', 'Start Menu'));
|
|
99
125
|
return roots;
|
|
100
126
|
}
|
|
101
|
-
/**
|
|
102
|
-
* Whether a path is a sensitive location, inside one, or an ancestor of one.
|
|
103
|
-
* @param target - resolved candidate path.
|
|
104
|
-
* @param home - resolved home directory.
|
|
105
|
-
*/
|
|
127
|
+
/** Whether a path is a sensitive location, inside one, or an ancestor of one. */
|
|
106
128
|
function isProtectedLocation(target, home) {
|
|
107
129
|
const resolved = comparable(target);
|
|
108
130
|
for (const root of sensitiveRoots(home)) {
|
|
@@ -112,17 +134,11 @@ function isProtectedLocation(target, home) {
|
|
|
112
134
|
}
|
|
113
135
|
return false;
|
|
114
136
|
}
|
|
115
|
-
/** The concrete home directory a policy call is judged against. */
|
|
116
137
|
function homeOf(options = {}) {
|
|
117
138
|
return path.resolve(options.home ?? homedir());
|
|
118
139
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
* @param destination - the folder the client asked to save into.
|
|
122
|
-
* @param options - the local user's home (defaults to the OS home).
|
|
123
|
-
* @returns a refusal reason, or null when the folder is acceptable. Pure: no filesystem
|
|
124
|
-
* access, so it is testable.
|
|
125
|
-
*/
|
|
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.
|
|
126
142
|
export function destinationPolicy(destination, options = {}) {
|
|
127
143
|
if (typeof destination !== 'string' || destination.trim() === '' || destination.includes('\0'))
|
|
128
144
|
return 'invalid';
|
|
@@ -154,58 +170,11 @@ export function destinationPolicy(destination, options = {}) {
|
|
|
154
170
|
return reason;
|
|
155
171
|
return null;
|
|
156
172
|
}
|
|
157
|
-
/** The client-facing refusal for one destination policy result. */
|
|
158
173
|
function destinationRefusal(reason) {
|
|
159
|
-
|
|
160
|
-
return errors.invalidInput('destination', {
|
|
161
|
-
zh: '只能保存到本机用户主目录下的文件夹(防止写入系统或自启动位置)。',
|
|
162
|
-
en: 'Downloads must go to a folder under this machine’s home directory (system and autorun locations are refused).',
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
if (reason === 'autorun') {
|
|
166
|
-
return errors.invalidInput('destination', {
|
|
167
|
-
zh: '不能保存到自启动目录(例如 Startup / autostart / shell:startup)。',
|
|
168
|
-
en: 'An autorun folder (for example Startup / autostart / shell:startup) cannot be a download target.',
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
if (reason === 'credentials') {
|
|
172
|
-
return errors.invalidInput('destination', {
|
|
173
|
-
zh: '不能保存到 SSH / GPG 配置目录(例如 .ssh)。',
|
|
174
|
-
en: 'An SSH / GPG configuration folder (for example .ssh) cannot be a download target.',
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
if (reason === 'login') {
|
|
178
|
-
return errors.invalidInput('destination', {
|
|
179
|
-
zh: '不能保存到登录脚本或用户配置目录(例如 .bashrc / .profile / .config / .local)。',
|
|
180
|
-
en: 'A login script or user configuration folder (for example .bashrc / .profile / .config / .local) cannot be a download target.',
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
if (reason === 'protected') {
|
|
184
|
-
return errors.invalidInput('destination', {
|
|
185
|
-
zh: '不能保存到会在登录或启动时生效的位置(自启动、登录脚本或凭据目录及其上级目录)。',
|
|
186
|
-
en: 'A location that runs or authenticates at logon (autorun, login scripts, credentials, or one of their parent folders) cannot be a download target.',
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
if (reason === 'system') {
|
|
190
|
-
return errors.invalidInput('destination', {
|
|
191
|
-
zh: '不能保存到系统目录。',
|
|
192
|
-
en: 'A system directory cannot be a download target.',
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
return errors.invalidInput('destination', {
|
|
196
|
-
zh: '请选择本机的保存文件夹。',
|
|
197
|
-
en: 'Choose a destination folder on this machine.',
|
|
198
|
-
});
|
|
174
|
+
return errors.invalidInput('destination', REFUSAL_TEXT[reason ?? 'invalid']);
|
|
199
175
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
*
|
|
203
|
-
* The destination is chosen locally, but the *name* below it comes from the remote tree
|
|
204
|
-
* (`~/Downloads/…`, `~/.config` + a remote folder called `autostart`, `~` + a remote file called
|
|
205
|
-
* `.bash_login`). This is the check that turns those combinations into a refusal.
|
|
206
|
-
* @param candidate - resolved folder or file path about to be created.
|
|
207
|
-
* @throws `invalid-input` when the path is protected.
|
|
208
|
-
*/
|
|
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.
|
|
209
178
|
function assertCandidateAllowed(candidate, options = {}) {
|
|
210
179
|
const refusal = destinationPolicy(candidate, options);
|
|
211
180
|
if (refusal !== null)
|
|
@@ -213,38 +182,29 @@ function assertCandidateAllowed(candidate, options = {}) {
|
|
|
213
182
|
if (isProtectedLocation(candidate, homeOf(options)))
|
|
214
183
|
throw destinationRefusal('protected');
|
|
215
184
|
}
|
|
216
|
-
|
|
217
|
-
* Resolve and check one download destination.
|
|
218
|
-
* @param destination - absolute local folder.
|
|
219
|
-
* @param options - the local user's home, for tests.
|
|
220
|
-
* @returns the resolved folder.
|
|
221
|
-
*/
|
|
222
|
-
async function requireDestination(destination, options = {}) {
|
|
185
|
+
export async function requireDestination(destination, options = {}) {
|
|
223
186
|
const refusal = destinationPolicy(destination, options);
|
|
224
187
|
if (refusal !== null)
|
|
225
188
|
throw destinationRefusal(refusal);
|
|
226
|
-
// `destinationPolicy` accepts only a non-empty string path, so by here the unknown is a
|
|
227
|
-
//
|
|
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.
|
|
228
191
|
const base = path.resolve(destination);
|
|
229
192
|
try {
|
|
230
193
|
const info = await stat(base);
|
|
231
|
-
if (!info.isDirectory())
|
|
232
|
-
throw errors.invalidInput('destination',
|
|
233
|
-
zh: '选择的保存位置不是文件夹。',
|
|
234
|
-
en: 'The selected destination is not a folder.',
|
|
235
|
-
});
|
|
236
|
-
}
|
|
194
|
+
if (!info.isDirectory())
|
|
195
|
+
throw errors.invalidInput('destination', TEXT.notFolder);
|
|
237
196
|
}
|
|
238
197
|
catch (error) {
|
|
239
198
|
if (isRemoteGoError(error))
|
|
240
199
|
throw error;
|
|
241
|
-
throw errors.invalidInput('destination',
|
|
242
|
-
zh: '选择的保存文件夹不存在或不可写。',
|
|
243
|
-
en: 'The selected destination folder does not exist or cannot be written.',
|
|
244
|
-
});
|
|
200
|
+
throw errors.invalidInput('destination', TEXT.destinationUnusable);
|
|
245
201
|
}
|
|
246
202
|
return base;
|
|
247
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
|
+
}
|
|
248
208
|
async function createUniqueFolder(base, name, options = {}) {
|
|
249
209
|
const safeName = safeLocalSegment(name) || 'download';
|
|
250
210
|
for (let index = 1; index <= 999; index += 1) {
|
|
@@ -257,16 +217,11 @@ async function createUniqueFolder(base, name, options = {}) {
|
|
|
257
217
|
return candidate;
|
|
258
218
|
}
|
|
259
219
|
catch (error) {
|
|
260
|
-
|
|
261
|
-
// `unknown`, and the `as` restates the one field this branch reads.
|
|
262
|
-
if (error?.code !== 'EEXIST')
|
|
220
|
+
if (!isTaken(error))
|
|
263
221
|
throw error;
|
|
264
222
|
}
|
|
265
223
|
}
|
|
266
|
-
throw errors.invalidInput('destination',
|
|
267
|
-
zh: '目标目录里已经有太多同名文件夹,请换一个目录。',
|
|
268
|
-
en: 'The destination already contains too many folders with this name; choose another directory.',
|
|
269
|
-
});
|
|
224
|
+
throw errors.invalidInput('destination', TEXT.tooManyFolders);
|
|
270
225
|
}
|
|
271
226
|
async function createUniqueFile(base, name, options = {}) {
|
|
272
227
|
const safeName = safeLocalSegment(name) || 'download';
|
|
@@ -283,15 +238,11 @@ async function createUniqueFile(base, name, options = {}) {
|
|
|
283
238
|
return candidate;
|
|
284
239
|
}
|
|
285
240
|
catch (error) {
|
|
286
|
-
|
|
287
|
-
if (error?.code !== 'EEXIST')
|
|
241
|
+
if (!isTaken(error))
|
|
288
242
|
throw error;
|
|
289
243
|
}
|
|
290
244
|
}
|
|
291
|
-
throw errors.invalidInput('destination',
|
|
292
|
-
zh: '目标目录里已经有太多同名文件,请换一个目录。',
|
|
293
|
-
en: 'The destination already contains too many files with this name; choose another directory.',
|
|
294
|
-
});
|
|
245
|
+
throw errors.invalidInput('destination', TEXT.tooManyFiles);
|
|
295
246
|
}
|
|
296
247
|
function toDownloadError(error) {
|
|
297
248
|
// `isRemoteGoError` only declares a boolean (it lives in the P1a set); the `as` restates
|
|
@@ -302,28 +253,21 @@ function toDownloadError(error) {
|
|
|
302
253
|
technical: String(error?.message ?? error ?? '').slice(0, 300),
|
|
303
254
|
});
|
|
304
255
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
*/
|
|
256
|
+
function requireTransport(runtime, notConnected) {
|
|
257
|
+
if (!runtime || runtime.status !== 'connected' || !runtime.ssh)
|
|
258
|
+
throw errors.remoteFileUnavailable(notConnected);
|
|
259
|
+
return runtime.ssh;
|
|
260
|
+
}
|
|
311
261
|
export async function saveHostFile(runtime, remotePath, destination, options = {}) {
|
|
312
|
-
|
|
313
|
-
|
|
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') {
|
|
262
|
+
const ssh = requireTransport(runtime, TEXT.fileNotConnected);
|
|
263
|
+
if (typeof ssh.saveRemoteFile !== 'function')
|
|
319
264
|
throw errors.internal('the SSH transport does not support file saves');
|
|
320
|
-
}
|
|
321
265
|
const base = await requireDestination(destination, options);
|
|
322
266
|
const name = remoteFileName(remotePath);
|
|
323
267
|
let localPath = null;
|
|
324
268
|
try {
|
|
325
269
|
localPath = await createUniqueFile(base, name, options);
|
|
326
|
-
const result = await
|
|
270
|
+
const result = await ssh.saveRemoteFile(remotePath, localPath, options);
|
|
327
271
|
return {
|
|
328
272
|
path: remotePath,
|
|
329
273
|
kind: 'file',
|
|
@@ -339,28 +283,16 @@ export async function saveHostFile(runtime, remotePath, destination, options = {
|
|
|
339
283
|
throw toDownloadError(error);
|
|
340
284
|
}
|
|
341
285
|
}
|
|
342
|
-
/**
|
|
343
|
-
* Copy one remote folder into `destination` as individual local files.
|
|
344
|
-
* @param runtime - connected host runtime.
|
|
345
|
-
* @param remotePath - normalized absolute remote folder path.
|
|
346
|
-
* @param destination - absolute local directory.
|
|
347
|
-
*/
|
|
348
286
|
export async function saveHostFolder(runtime, remotePath, destination, options = {}) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
zh: '这台主机还没有连接,无法读取远端文件夹。',
|
|
352
|
-
en: 'This host is not connected, so the remote folder cannot be read.',
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
if (typeof runtime.ssh.saveRemoteTree !== 'function') {
|
|
287
|
+
const ssh = requireTransport(runtime, TEXT.folderNotConnected);
|
|
288
|
+
if (typeof ssh.saveRemoteTree !== 'function')
|
|
356
289
|
throw errors.internal('the SSH transport does not support folder saves');
|
|
357
|
-
}
|
|
358
290
|
const base = await requireDestination(destination, options);
|
|
359
291
|
const name = remoteFileName(remotePath);
|
|
360
292
|
let root = null;
|
|
361
293
|
try {
|
|
362
294
|
root = await createUniqueFolder(base, name, options);
|
|
363
|
-
const result = await
|
|
295
|
+
const result = await ssh.saveRemoteTree(remotePath, root, options);
|
|
364
296
|
return {
|
|
365
297
|
path: remotePath,
|
|
366
298
|
kind: 'folder',
|
|
@@ -377,4 +309,3 @@ export async function saveHostFolder(runtime, remotePath, destination, options =
|
|
|
377
309
|
throw toDownloadError(error);
|
|
378
310
|
}
|
|
379
311
|
}
|
|
380
|
-
export { requireDestination };
|
package/dist/src/local-ssh.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local SSH material (read-only): `~/.ssh/config` and `~/.ssh/known_hosts`; never writes, never prompts.
|
|
3
|
-
*/
|
|
1
|
+
/** Local SSH material (read-only): `~/.ssh/config` and `~/.ssh/known_hosts`; never writes, never prompts. */
|
|
4
2
|
import { existsSync } from 'node:fs';
|
|
5
3
|
import { readFile } from 'node:fs/promises';
|
|
6
4
|
import path from 'node:path';
|
|
@@ -10,21 +8,16 @@ export async function loadLocalSshMaterial(input) {
|
|
|
10
8
|
const knownHostsPath = path.join(sshDir, 'known_hosts');
|
|
11
9
|
const exists = input.exists ?? existsSync;
|
|
12
10
|
const read = input.readFileImpl ?? readFile;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
knownHostsText = await read(knownHostsPath, 'utf8');
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
knownHostsText = '';
|
|
28
|
-
}
|
|
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);
|
|
29
22
|
return { sshDir, configText, knownHostsText, configPath, knownHostsPath };
|
|
30
23
|
}
|
|
@@ -1,20 +1,31 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Detect the DSH version of the local installation; the remote runtime is pinned to this exact version, never to npm `latest`.
|
|
3
|
-
*/
|
|
1
|
+
/** Detect the DSH version of the local installation; the remote runtime is pinned to this exact version, never to npm `latest`. */
|
|
4
2
|
import { existsSync } from 'node:fs';
|
|
5
3
|
import { readFile } from 'node:fs/promises';
|
|
6
4
|
import path from 'node:path';
|
|
7
5
|
import { createKernelAdapter } from './kernel-adapter.js';
|
|
8
6
|
import { isPlainObject } from './util.js';
|
|
9
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
|
+
}
|
|
10
22
|
export async function detectLocalVersion(input = {}) {
|
|
11
23
|
const env = input.env ?? process.env;
|
|
12
24
|
const explicit = env.DSH_VERSION || env.DSH_RUNTIME_VERSION;
|
|
13
|
-
if (explicit && parseVersion(normalizeVersion(explicit)))
|
|
25
|
+
if (explicit && parseVersion(normalizeVersion(explicit)))
|
|
14
26
|
return { version: normalizeVersion(explicit), source: 'env' };
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
// releases share with the CLI package version.
|
|
27
|
+
// The web client-module registry publishes the Host release identity, which releases share with
|
|
28
|
+
// the CLI package version.
|
|
18
29
|
const release = createKernelAdapter(input.ctx).clientModuleRelease();
|
|
19
30
|
if (release.state === 'ok' && release.value !== null) {
|
|
20
31
|
const version = release.value.version;
|
|
@@ -28,48 +39,24 @@ export async function detectLocalVersion(input = {}) {
|
|
|
28
39
|
input.logger?.warn?.('[dsh-remote-go] the client-module release identity changed shape; falling back to package.json:', release.detail);
|
|
29
40
|
}
|
|
30
41
|
for (const candidate of candidatePackageJsons(input)) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (isPlainObject(parsed) &&
|
|
38
|
-
parsed.name === '@deepseek-ai/dsh' &&
|
|
39
|
-
typeof parsed.version === 'string' &&
|
|
40
|
-
parseVersion(normalizeVersion(parsed.version))) {
|
|
41
|
-
return { version: normalizeVersion(parsed.version), source: `package.json:${candidate}` };
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
catch {
|
|
45
|
-
// swallow-ok: a candidate that is absent or unparsable is skipped by design
|
|
46
|
-
}
|
|
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}` };
|
|
47
48
|
}
|
|
48
49
|
return { version: null, source: 'unknown' };
|
|
49
50
|
}
|
|
50
|
-
/**
|
|
51
|
-
*
|
|
52
|
-
* truth for the managed Node version the plugin installs on the remote: the
|
|
53
|
-
* remote runs this exact DSH version, so it must satisfy this declaration.
|
|
54
|
-
*
|
|
55
|
-
* Best-effort like the version probe: an unreadable or missing package yields
|
|
56
|
-
* `range: null` and the caller falls back to the plugin's own floor.
|
|
57
|
-
*/
|
|
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. */
|
|
58
53
|
export async function detectLocalEnginesNode(input = {}) {
|
|
59
54
|
for (const candidate of candidatePackageJsons(input)) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const range = engines?.node;
|
|
66
|
-
if (typeof range === 'string' && range.trim() !== '') {
|
|
67
|
-
return { range: range.trim(), source: `package.json:${candidate}` };
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
// swallow-ok: the same candidate scan as the version probe above
|
|
72
|
-
}
|
|
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}` };
|
|
73
60
|
}
|
|
74
61
|
return { range: null, source: 'unknown' };
|
|
75
62
|
}
|
|
@@ -78,15 +65,14 @@ export function candidatePackageJsons(input = {}) {
|
|
|
78
65
|
const argv1 = input.argv1 ?? process.argv[1] ?? '';
|
|
79
66
|
const marker = `${path.sep}@deepseek-ai${path.sep}dsh${path.sep}`;
|
|
80
67
|
const markerIndex = argv1.indexOf(marker);
|
|
81
|
-
if (markerIndex >= 0)
|
|
68
|
+
if (markerIndex >= 0)
|
|
82
69
|
out.push(path.join(argv1.slice(0, markerIndex + marker.length), 'package.json'));
|
|
83
|
-
}
|
|
84
70
|
// Electron's `resourcesPath` is not on the standard Process type; it is read duck-typed.
|
|
85
71
|
const resourcesPath = input.resourcesPath ?? process.resourcesPath;
|
|
86
72
|
if (typeof resourcesPath === 'string' && resourcesPath !== '') {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
+
}
|
|
90
76
|
}
|
|
91
77
|
// Walk up from the CLI entry looking for a nested package.
|
|
92
78
|
let dir = argv1 ? path.dirname(argv1) : (input.cwd ?? process.cwd());
|
|
@@ -3,10 +3,7 @@ export const NODE_DIST_BASES = Object.freeze([
|
|
|
3
3
|
'https://nodejs.org/dist',
|
|
4
4
|
'https://npmmirror.com/mirrors/node',
|
|
5
5
|
]);
|
|
6
|
-
/**
|
|
7
|
-
* Pick the downloader both install scripts share: curl when present, else wget.
|
|
8
|
-
* Exit 9 is the "neither tool" contract the caller maps to `no-fetch`.
|
|
9
|
-
*/
|
|
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`. */
|
|
10
7
|
const FETCH_PROLOGUE = `if command -v curl >/dev/null 2>&1; then
|
|
11
8
|
FETCH=curl
|
|
12
9
|
elif command -v wget >/dev/null 2>&1; then
|