dsh-remote 0.8.91 → 0.8.93

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.
Files changed (181) hide show
  1. package/LICENSE +21 -21
  2. package/README.en.md +369 -368
  3. package/README.md +354 -353
  4. package/cordis.patch.yml +9 -9
  5. package/lib/audit.js +45 -45
  6. package/lib/binding.js +172 -172
  7. package/lib/client.js +4647 -4550
  8. package/lib/credential.js +270 -263
  9. package/lib/errors.js +141 -141
  10. package/lib/exec-job.js +242 -242
  11. package/lib/file-reference.js +457 -457
  12. package/lib/forwards.js +258 -258
  13. package/lib/host-api.js +822 -822
  14. package/lib/hostkey.js +108 -108
  15. package/lib/http-transport.js +201 -201
  16. package/lib/ignore.js +202 -202
  17. package/lib/index.js +4488 -4394
  18. package/lib/instructions-dynamic.js +415 -415
  19. package/lib/instructions.js +288 -288
  20. package/lib/open-folder.js +56 -56
  21. package/lib/openssh.js +940 -881
  22. package/lib/paths.js +292 -292
  23. package/lib/pool.js +1029 -970
  24. package/lib/posix-shell.cjs +34 -0
  25. package/lib/registry.js +296 -296
  26. package/lib/remote-fs.js +173 -173
  27. package/lib/remote-sandbox.js +183 -183
  28. package/lib/remote-shell-child.cjs +397 -400
  29. package/lib/remote-shell.js +697 -697
  30. package/lib/remote-skills.js +312 -0
  31. package/lib/routes-audit.js +58 -58
  32. package/lib/routes-forwards.js +72 -72
  33. package/lib/routes-fs.js +251 -245
  34. package/lib/routes-machines.js +186 -186
  35. package/lib/routes-misc.js +187 -150
  36. package/lib/routes-term.js +169 -169
  37. package/lib/routes-update.js +143 -143
  38. package/lib/routes-web-attach.js +112 -112
  39. package/lib/routes-workspace.js +312 -312
  40. package/lib/search.js +235 -235
  41. package/lib/sftp-surface.js +119 -119
  42. package/lib/sshconfig.js +779 -779
  43. package/lib/sync.js +350 -350
  44. package/lib/tasks.js +95 -95
  45. package/lib/telemetry.js +247 -247
  46. package/lib/terminal.js +205 -205
  47. package/lib/tui/flow.js +130 -130
  48. package/lib/tui/format.js +380 -380
  49. package/lib/tui/i18n.js +350 -350
  50. package/lib/tui/index.js +531 -531
  51. package/lib/tui/provider.js +344 -344
  52. package/lib/tui/scene.js +986 -986
  53. package/lib/update.js +490 -472
  54. package/lib/web-attach.js +860 -850
  55. package/lib/workspace-cleanup.js +337 -337
  56. package/package.json +103 -100
  57. package/node_modules/buildcheck/.eslintrc.js +0 -5
  58. package/node_modules/buildcheck/.github/workflows/ci.yml +0 -88
  59. package/node_modules/buildcheck/.github/workflows/lint.yml +0 -27
  60. package/node_modules/buildcheck/.npmignore +0 -3
  61. package/node_modules/buildcheck/LICENSE +0 -19
  62. package/node_modules/buildcheck/README.md +0 -106
  63. package/node_modules/buildcheck/deps/Find-VisualStudio.cs +0 -250
  64. package/node_modules/buildcheck/lib/findvs.js +0 -361
  65. package/node_modules/buildcheck/lib/index.js +0 -738
  66. package/node_modules/buildcheck/package.json +0 -36
  67. package/node_modules/buildcheck/test/test.js +0 -8
  68. package/node_modules/cpu-features/.eslintrc.js +0 -5
  69. package/node_modules/cpu-features/.github/workflows/ci.yml +0 -74
  70. package/node_modules/cpu-features/.github/workflows/lint.yml +0 -27
  71. package/node_modules/cpu-features/LICENSE +0 -19
  72. package/node_modules/cpu-features/README.md +0 -59
  73. package/node_modules/cpu-features/binding.gyp +0 -16
  74. package/node_modules/cpu-features/buildcheck.js +0 -32
  75. package/node_modules/cpu-features/deps/cpu_features/.clang-format +0 -4
  76. package/node_modules/cpu-features/deps/cpu_features/.dockerignore +0 -31
  77. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/Dockerfile +0 -5
  78. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/aarch64_linux_cmake.yml +0 -30
  79. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_freebsd_cmake.yml +0 -22
  80. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_linux_bazel.yml +0 -26
  81. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_linux_cmake.yml +0 -31
  82. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_macos_cmake.yml +0 -43
  83. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/amd64_windows_cmake.yml +0 -25
  84. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/arm_linux_cmake.yml +0 -31
  85. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/clang_format.yml +0 -24
  86. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/mips_linux_cmake.yml +0 -30
  87. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/power_linux_cmake.yml +0 -29
  88. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/riscv_linux_cmake.yml +0 -28
  89. package/node_modules/cpu-features/deps/cpu_features/.github/workflows/s390x_linux_cmake.yml +0 -27
  90. package/node_modules/cpu-features/deps/cpu_features/.grenrc.yml +0 -21
  91. package/node_modules/cpu-features/deps/cpu_features/BUILD.bazel +0 -329
  92. package/node_modules/cpu-features/deps/cpu_features/CMakeLists.txt +0 -261
  93. package/node_modules/cpu-features/deps/cpu_features/CONTRIBUTING.md +0 -23
  94. package/node_modules/cpu-features/deps/cpu_features/LICENSE +0 -230
  95. package/node_modules/cpu-features/deps/cpu_features/README.md +0 -272
  96. package/node_modules/cpu-features/deps/cpu_features/WORKSPACE +0 -19
  97. package/node_modules/cpu-features/deps/cpu_features/bazel/ci/README.md +0 -5
  98. package/node_modules/cpu-features/deps/cpu_features/bazel/platforms.bzl +0 -11
  99. package/node_modules/cpu-features/deps/cpu_features/cmake/CpuFeaturesConfig.cmake.in +0 -3
  100. package/node_modules/cpu-features/deps/cpu_features/cmake/CpuFeaturesNdkCompatConfig.cmake.in +0 -3
  101. package/node_modules/cpu-features/deps/cpu_features/cmake/README.md +0 -30
  102. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/Makefile +0 -252
  103. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/README.md +0 -40
  104. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/docker.dot +0 -64
  105. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/docker.svg +0 -312
  106. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/doc/generate_image.sh +0 -7
  107. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/docker/amd64/Dockerfile +0 -48
  108. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/docker/toolchain/Dockerfile +0 -34
  109. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/sample/CMakeLists.txt +0 -22
  110. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/sample/main.cpp +0 -11
  111. package/node_modules/cpu-features/deps/cpu_features/cmake/ci/vagrant/freebsd/Vagrantfile +0 -107
  112. package/node_modules/cpu-features/deps/cpu_features/cmake/googletest.CMakeLists.txt.in +0 -15
  113. package/node_modules/cpu-features/deps/cpu_features/cpu_features.gyp +0 -105
  114. package/node_modules/cpu-features/deps/cpu_features/include/cpu_features_cache_info.h +0 -54
  115. package/node_modules/cpu-features/deps/cpu_features/include/cpu_features_macros.h +0 -384
  116. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_aarch64.h +0 -259
  117. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_arm.h +0 -121
  118. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_mips.h +0 -74
  119. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_ppc.h +0 -149
  120. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_riscv.h +0 -72
  121. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_s390x.h +0 -108
  122. package/node_modules/cpu-features/deps/cpu_features/include/cpuinfo_x86.h +0 -288
  123. package/node_modules/cpu-features/deps/cpu_features/include/internal/bit_utils.h +0 -40
  124. package/node_modules/cpu-features/deps/cpu_features/include/internal/cpuid_x86.h +0 -37
  125. package/node_modules/cpu-features/deps/cpu_features/include/internal/filesystem.h +0 -39
  126. package/node_modules/cpu-features/deps/cpu_features/include/internal/hwcaps.h +0 -240
  127. package/node_modules/cpu-features/deps/cpu_features/include/internal/stack_line_reader.h +0 -49
  128. package/node_modules/cpu-features/deps/cpu_features/include/internal/string_view.h +0 -110
  129. package/node_modules/cpu-features/deps/cpu_features/include/internal/windows_utils.h +0 -70
  130. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/CMakeLists.txt +0 -60
  131. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/README.md +0 -4
  132. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/cpu-features.c +0 -205
  133. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/cpu-features.h +0 -320
  134. package/node_modules/cpu-features/deps/cpu_features/ndk_compat/ndk-compat-test.c +0 -12
  135. package/node_modules/cpu-features/deps/cpu_features/patches/0001-Add-Apple-Silicon-Support.patch +0 -456
  136. package/node_modules/cpu-features/deps/cpu_features/scripts/generate_badges.d +0 -165
  137. package/node_modules/cpu-features/deps/cpu_features/scripts/make_release.sh +0 -75
  138. package/node_modules/cpu-features/deps/cpu_features/scripts/run_integration.sh +0 -490
  139. package/node_modules/cpu-features/deps/cpu_features/scripts/test_integration.sh +0 -79
  140. package/node_modules/cpu-features/deps/cpu_features/src/copy.inl +0 -19
  141. package/node_modules/cpu-features/deps/cpu_features/src/define_introspection.inl +0 -86
  142. package/node_modules/cpu-features/deps/cpu_features/src/define_introspection_and_hwcaps.inl +0 -26
  143. package/node_modules/cpu-features/deps/cpu_features/src/equals.inl +0 -22
  144. package/node_modules/cpu-features/deps/cpu_features/src/filesystem.c +0 -62
  145. package/node_modules/cpu-features/deps/cpu_features/src/hwcaps.c +0 -169
  146. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64__base_implementation.inl +0 -88
  147. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_linux_or_android.c +0 -79
  148. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_macos_or_iphone.c +0 -82
  149. package/node_modules/cpu-features/deps/cpu_features/src/impl_aarch64_windows.c +0 -138
  150. package/node_modules/cpu-features/deps/cpu_features/src/impl_arm_linux_or_android.c +0 -212
  151. package/node_modules/cpu-features/deps/cpu_features/src/impl_mips_linux_or_android.c +0 -93
  152. package/node_modules/cpu-features/deps/cpu_features/src/impl_ppc_linux.c +0 -163
  153. package/node_modules/cpu-features/deps/cpu_features/src/impl_riscv_linux.c +0 -111
  154. package/node_modules/cpu-features/deps/cpu_features/src/impl_s390x_linux.c +0 -120
  155. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86__base_implementation.inl +0 -2055
  156. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_freebsd.c +0 -68
  157. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_linux_or_android.c +0 -58
  158. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_macos.c +0 -57
  159. package/node_modules/cpu-features/deps/cpu_features/src/impl_x86_windows.c +0 -58
  160. package/node_modules/cpu-features/deps/cpu_features/src/stack_line_reader.c +0 -132
  161. package/node_modules/cpu-features/deps/cpu_features/src/string_view.c +0 -192
  162. package/node_modules/cpu-features/deps/cpu_features/src/utils/list_cpu_features.c +0 -461
  163. package/node_modules/cpu-features/deps/cpu_features/test/CMakeLists.txt +0 -114
  164. package/node_modules/cpu-features/deps/cpu_features/test/bit_utils_test.cc +0 -53
  165. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_aarch64_test.cc +0 -396
  166. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_arm_test.cc +0 -384
  167. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_mips_test.cc +0 -176
  168. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_ppc_test.cc +0 -131
  169. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_riscv_test.cc +0 -180
  170. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_s390x_test.cc +0 -82
  171. package/node_modules/cpu-features/deps/cpu_features/test/cpuinfo_x86_test.cc +0 -1729
  172. package/node_modules/cpu-features/deps/cpu_features/test/filesystem_for_testing.cc +0 -103
  173. package/node_modules/cpu-features/deps/cpu_features/test/filesystem_for_testing.h +0 -61
  174. package/node_modules/cpu-features/deps/cpu_features/test/hwcaps_for_testing.cc +0 -52
  175. package/node_modules/cpu-features/deps/cpu_features/test/hwcaps_for_testing.h +0 -31
  176. package/node_modules/cpu-features/deps/cpu_features/test/stack_line_reader_test.cc +0 -132
  177. package/node_modules/cpu-features/deps/cpu_features/test/string_view_test.cc +0 -202
  178. package/node_modules/cpu-features/lib/index.js +0 -5
  179. package/node_modules/cpu-features/package.json +0 -40
  180. package/node_modules/cpu-features/src/binding.cc +0 -151
  181. package/node_modules/cpu-features/test/test.js +0 -12
@@ -1,400 +1,397 @@
1
- #!/usr/bin/env node
2
- /**
3
- * The program the host terminal runs when the user picks the "Remote SSH" shell.
4
- *
5
- * It is started by a generated launcher (see `remote-shell.js`) inside the
6
- * session's working directory, resolves which remote checkout that directory
7
- * belongs to, and then replaces itself with `ssh`. Inheriting stdio is the whole
8
- * point: the local pty stays in charge of the screen, so resize and key handling
9
- * keep working without this script knowing anything about terminals.
10
- *
11
- * Plain CommonJS on purpose: it is spawned as a standalone script, and it must
12
- * not depend on how the plugin package itself is loaded.
13
- */
14
-
15
- 'use strict'
16
-
17
- const { spawn } = require('node:child_process')
18
- const { existsSync, fstatSync, readFileSync } = require('node:fs')
19
- const path = require('node:path')
20
-
21
- /** Name of the marker the host half writes into every local mirror root. */
22
- const META_FILE = '.dsh-remote-meta.json'
23
- /** How far up from the working directory a marker may be found. */
24
- const MAX_WALK_UP = 12
25
-
26
- /**
27
- * Read and parse a JSON file, returning undefined for anything unusable.
28
- * @param {string} file
29
- * @returns {any}
30
- */
31
- function readJson(file) {
32
- try {
33
- return JSON.parse(readFileSync(file, 'utf8'))
34
- } catch {
35
- return undefined
36
- }
37
- }
38
-
39
- /**
40
- * Find the mirror root that contains `cwd` by walking upwards for the marker.
41
- * @param {string} cwd - session working directory
42
- * @param {{ exists?: (p: string) => boolean, readJsonFile?: (p: string) => any, maxUp?: number }} deps
43
- * @returns {{ dir: string, meta: object } | null}
44
- */
45
- function findMirror(cwd, deps = {}) {
46
- const exists = deps.exists || ((p) => existsSync(p))
47
- const read = deps.readJsonFile || readJson
48
- const maxUp = deps.maxUp || MAX_WALK_UP
49
- let dir = path.resolve(cwd || '.')
50
- for (let depth = 0; depth <= maxUp; depth += 1) {
51
- const marker = path.join(dir, META_FILE)
52
- if (exists(marker)) {
53
- const meta = read(marker)
54
- if (meta && typeof meta === 'object' && meta.host) return { dir, meta }
55
- }
56
- const parent = path.dirname(dir)
57
- if (parent === dir) break
58
- dir = parent
59
- }
60
- return null
61
- }
62
-
63
- /**
64
- * Pick the saved connection that owns a mirror.
65
- *
66
- * The marker records the SSH alias when the machine was saved from
67
- * `~/.ssh/config` (issue #38), so the alias is the most precise key; otherwise
68
- * host + user + port identify it. A missing entry is not fatal — the marker
69
- * alone is enough to connect, and ssh will use its own defaults for the key.
70
- *
71
- * @param {object} meta - mirror marker
72
- * @param {{ list?: object[] }} registry - parsed `machines.json`
73
- * @returns {object | undefined}
74
- */
75
- function matchMachine(meta, registry) {
76
- const list = registry && Array.isArray(registry.list) ? registry.list : []
77
- const port = Number(meta.port || 22)
78
- if (meta.alias) {
79
- const byAlias = list.find((m) => m && m.host === meta.alias)
80
- if (byAlias) return byAlias
81
- }
82
- return list.find((m) => m && m.host === meta.host
83
- && String(m.username || '') === String(meta.username || '')
84
- && Number(m.port || 22) === port)
85
- }
86
-
87
- /**
88
- * Quote one path for a POSIX shell, single quotes only.
89
- * @param {string} value
90
- * @returns {string}
91
- */
92
- function quotePosix(value) {
93
- return "'" + String(value).replace(/'/g, "'\\''") + "'"
94
- }
95
-
96
- /**
97
- * Build the `cd` half of the remote command, keeping `~` for the *remote* shell
98
- * to expand: the local home is not the remote home, so expanding it here would
99
- * silently land in the wrong directory.
100
- * @param {string} remotePath
101
- * @returns {string}
102
- */
103
- function remoteCd(remotePath) {
104
- const raw = String(remotePath || '').trim()
105
- if (raw === '' || raw === '~') return 'cd "$HOME"'
106
- if (raw === '~/') return 'cd "$HOME"'
107
- if (raw.startsWith('~/')) return 'cd "$HOME"/' + quotePosix(raw.slice(2))
108
- return 'cd ' + quotePosix(raw)
109
- }
110
-
111
- /**
112
- * The remote command: land in the workspace, then hand over to a login shell.
113
- *
114
- * `exec` keeps the pty attached to a single interactive shell, so the exit code
115
- * the terminal sees is the shell's own, and a deleted workspace falls back to
116
- * `$HOME` instead of leaving a broken terminal.
117
- *
118
- * @param {string} remotePath
119
- * @returns {string}
120
- */
121
- function remoteCommand(remotePath) {
122
- return `${remoteCd(remotePath)} 2>/dev/null || cd "$HOME"; exec "\${SHELL:-/bin/bash}" -l`
123
- }
124
-
125
- /** Absolute Windows path: `D:\x`, `D:/x`, or a `\\server\share` UNC path.
126
- * These cannot be the operand of a POSIX `cd`. */
127
- const WINDOWS_REMOTE_PATH = /^(?:[a-zA-Z]:[\\/]|\\\\)/
128
-
129
- /**
130
- * Whether a remote workspace path is a Windows one.
131
- *
132
- * A Windows remote speaks cmd or PowerShell in its default shell, so the POSIX
133
- * `cd … || …; exec "$SHELL"` line would fail outright there — `2>/dev/null`,
134
- * `||` and `exec` are not cmd syntax at all. The marker records no remote
135
- * platform, but the path form is unambiguous: `/srv/x` and `~/x` are POSIX,
136
- * while `D:\x` and `\\server\share` are not valid POSIX paths.
137
- *
138
- * @param {string} p - `remotePath` from the mirror marker
139
- * @returns {boolean}
140
- */
141
- function isWindowsRemotePath(p) {
142
- return WINDOWS_REMOTE_PATH.test(String(p || '').trim())
143
- }
144
-
145
- /**
146
- * Argument list (without the ssh binary) for one connection.
147
- *
148
- * `-tt` is mandatory: the terminal needs a tty even though a command is given,
149
- * otherwise the shell comes up non-interactive and every key press is ignored.
150
- * Options are pushed in order and never spliced between an `-o` and its value —
151
- * an inserted `-J` there once reached ssh as the value of `-o`, which aborted
152
- * every connection that used a jump host (issue #21).
153
- *
154
- * @param {object} target - `{ host, port, username, alias, remotePath }`
155
- * @param {object} [machine] - saved connection, for the key and transport hints
156
- * @returns {string[]}
157
- */
158
- function buildSshArgs(target, machine) {
159
- const args = ['-tt']
160
- const alias = target.alias || (machine && machine.useSshConfig ? machine.host : '')
161
- if (alias) {
162
- args.push(String(alias))
163
- } else {
164
- const port = Number(target.port || (machine && machine.port) || 22)
165
- if (port !== 22) args.push('-p', String(port))
166
- const key = machine && machine.privateKeyPath
167
- if (key && !machine.useAgent) args.push('-i', String(key))
168
- const user = target.username || (machine && machine.username) || ''
169
- args.push(user ? `${user}@${target.host}` : String(target.host))
170
- }
171
- // A Windows workspace cannot be reached with the POSIX line: the remote's
172
- // default shell may be cmd or PowerShell, and picking one would be a guess.
173
- // Opening ssh with no command leaves the remote's own default shell in charge,
174
- // so the terminal still works; only the automatic `cd` is given up.
175
- if (!isWindowsRemotePath(target.remotePath)) args.push(remoteCommand(target.remotePath))
176
- return args
177
- }
178
-
179
- /**
180
- * Prefer the OS OpenSSH binary on Windows. A PATH lookup there can reach an
181
- * MSYS2/git-bash `ssh`, which resolves `~/.ssh` against a different home and
182
- * makes keys saved by the host half look missing.
183
- * @param {string} [platform]
184
- * @param {object} [env]
185
- * @param {(p: string) => boolean} [exists]
186
- * @returns {string}
187
- */
188
- function sshBinary(platform = process.platform, env = process.env, exists = existsSync) {
189
- if (platform === 'win32') {
190
- const system = env.SystemRoot || env.windir || 'C:\\Windows'
191
- const native = path.join(system, 'System32', 'OpenSSH', 'ssh.exe')
192
- if (exists(native)) return native
193
- }
194
- return 'ssh'
195
- }
196
-
197
- /**
198
- * The shell to fall back to when this directory is not a remote checkout.
199
- * @param {string} [platform]
200
- * @param {object} [env]
201
- * @returns {string}
202
- */
203
- function localShell(platform = process.platform, env = process.env) {
204
- if (platform === 'win32') return env.ComSpec || 'cmd.exe'
205
- return env.SHELL || '/bin/sh'
206
- }
207
-
208
- /**
209
- * Parse the arguments the launcher passes.
210
- * @param {string[]} argv
211
- * @returns {{ machines?: string }}
212
- */
213
- function parseArgs(argv) {
214
- const out = {}
215
- for (let i = 0; i < argv.length; i += 1) {
216
- if (argv[i] === '--machines' && argv[i + 1]) {
217
- out.machines = argv[i + 1]
218
- i += 1
219
- }
220
- }
221
- return out
222
- }
223
-
224
- /**
225
- * Resolve everything needed to connect from a working directory.
226
- * @param {{ cwd?: string, machines?: string, exists?: Function, readJsonFile?: Function }} input
227
- * @returns {{ target?: object, machine?: object, found: boolean }}
228
- */
229
- function resolveTarget(input = {}) {
230
- const cwd = input.cwd || process.cwd()
231
- const mirror = findMirror(cwd, input)
232
- if (!mirror) return { found: false }
233
- const registry = input.machines ? (input.readJsonFile || readJson)(input.machines) : undefined
234
- const machine = matchMachine(mirror.meta, registry)
235
- const target = {
236
- host: mirror.meta.host,
237
- port: mirror.meta.port || (machine && machine.port) || 22,
238
- username: mirror.meta.username || (machine && machine.username) || '',
239
- alias: mirror.meta.alias || '',
240
- remotePath: mirror.meta.remotePath || (machine && machine.workspace) || '',
241
- }
242
- return { found: true, target, machine, dir: mirror.dir }
243
- }
244
-
245
- /**
246
- * Whether stdin is a console this process can actually receive keys from.
247
- *
248
- * A terminal that inherits stdio from `ELECTRON_RUN_AS_NODE` gets pipes, not
249
- * the ConPTY console: output flows while every keystroke is dropped, leaving a
250
- * tab that looks alive and accepts nothing (issue #91). Detecting that here
251
- * turns a silent dead end into an explicit, actionable refusal. `isTTY` is the
252
- * cheap check; `fstat(0)` catches runtimes that never set the flag.
253
- *
254
- * @param {{ stdin?: object, fstat?: (fd: number) => any }} [deps]
255
- * @returns {boolean}
256
- */
257
- function stdinIsInteractive(deps = {}) {
258
- const stdin = deps.stdin === undefined ? process.stdin : deps.stdin
259
- const fstat = deps.fstat || fstatSync
260
- if (stdin && stdin.isTTY === true) return true
261
- try {
262
- const stat = fstat(0)
263
- return Boolean(stat && typeof stat.isCharacterDevice === 'function' && stat.isCharacterDevice())
264
- } catch {
265
- return false
266
- }
267
- }
268
-
269
- /**
270
- * Whether the machine's password auth needs (and can use) the credential
271
- * bridge. Keys, agents and ssh-config aliases authenticate without us;
272
- * keyboard-interactive (OTP) prompts are per-login and must stay on the tty;
273
- * everything else with a stored credential (plaintext registry or OS keychain)
274
- * goes through the bridge.
275
- *
276
- * @param {object|undefined} machine - the matched registry record.
277
- * @returns {boolean}
278
- */
279
- function needsAskpass(machine) {
280
- if (!machine) return false
281
- if (machine.useAgent || machine.useSshConfig || machine.keyboardInteractive) return false
282
- if (String(machine.privateKeyPath || '')) return false
283
- if (String(machine.password || '')) return true
284
- return Boolean(machine.credentialBackend && machine.credentialBackend !== 'plain')
285
- }
286
-
287
- /**
288
- * Environment additions that route ssh's password prompt to the bridge helper.
289
- * Only applied when needsAskpass() held AND the payload file exists.
290
- *
291
- * `SSH_ASKPASS_REQUIRE=force` (OpenSSH ≥ 8.4) is what makes askpass run at all
292
- * on a tty; DISPLAY feeds prehistoric ssh builds. DSH_TS_* carries the machine
293
- * identity to the helper through the process environment.
294
- *
295
- * @param {object} machine
296
- * @param {string} termDir - the launcher's directory (DSH_TERM_DIR).
297
- * @param {string} [platform]
298
- * @returns {object} env entries to merge over process.env
299
- */
300
- function askpassEnv(machine, termDir, platform = process.platform) {
301
- const wrapper = path.join(termDir, platform === 'win32' ? 'term-askpass.cmd' : 'term-askpass')
302
- return {
303
- SSH_ASKPASS: wrapper,
304
- SSH_ASKPASS_REQUIRE: 'force',
305
- DISPLAY: process.env.DISPLAY || 'dsh-remote:0',
306
- DSH_TS_HOST: String(machine.host || ''),
307
- DSH_TS_PORT: String(Number(machine.port) || 22),
308
- DSH_TS_USERNAME: String(machine.username || ''),
309
- DSH_TS_ALIAS: String(machine.alias || ''),
310
- }
311
- }
312
-
313
- /**
314
- * Hand the current process over to `file args…`, propagating the exit code.
315
- * @param {string} file
316
- * @param {string[]} args
317
- * @param {object} [extraEnv] - env entries merged over process.env (askpass).
318
- * @returns {import('node:child_process').ChildProcess}
319
- */
320
- function handover(file, args, extraEnv) {
321
- const child = spawn(file, args, { stdio: 'inherit', env: extraEnv ? { ...process.env, ...extraEnv } : process.env })
322
- child.on('error', (error) => {
323
- process.stderr.write(`[dsh-remote] cannot run ${file}: ${error && error.message}\n`)
324
- process.exit(127)
325
- })
326
- child.on('exit', (code, signal) => {
327
- if (signal) process.kill(process.pid, signal)
328
- else process.exit(code == null ? 0 : code)
329
- })
330
- return child
331
- }
332
-
333
- /**
334
- * Entry point: connect, or explain why a local shell was used instead.
335
- * @param {string[]} [argv]
336
- * @param {{ cwd?: string, platform?: string, env?: object,
337
- * stdin?: object, fstat?: (fd: number) => any,
338
- * write?: (line: string) => void, exit?: (code: number) => any }} [deps]
339
- */
340
- function main(argv = process.argv.slice(2), deps = {}) {
341
- const platform = deps.platform || process.platform
342
- const env = deps.env || process.env
343
- const write = deps.write || ((line) => process.stderr.write(line))
344
- const exit = deps.exit || ((code) => process.exit(code))
345
- if (!stdinIsInteractive(deps)) {
346
- // Anything we could hand over to — ssh, cmd, sh — reads stdin the same
347
- // way, so there is no useful fallback here: the tab would render the
348
- // remote prompt and ignore every key. Refuse loudly instead (issue #91).
349
- write('[dsh-remote] this terminal cannot receive input: stdin is not a real console.\n')
350
- write('[dsh-remote] point DSH_REMOTE_TERMINAL_NODE at an absolute node.exe path and restart, so the launcher does not have to reuse the app runtime.\n')
351
- write('[dsh-remote] 终端无法接收输入(stdin 不是真实控制台)。请把 DSH_REMOTE_TERMINAL_NODE 指向 node.exe 的绝对路径后重启。\n')
352
- exit(126)
353
- return undefined
354
- }
355
- const options = parseArgs(argv)
356
- const resolved = resolveTarget({ cwd: deps.cwd || process.cwd(), machines: options.machines })
357
- if (!resolved.found) {
358
- process.stderr.write('[dsh-remote] this directory is not a remote workspace; opening a local shell.\n')
359
- process.stderr.write('[dsh-remote] 当前目录不是远程工作区,已打开本地终端。\n')
360
- return handover(localShell(platform, env), platform === 'win32' ? [] : ['-l'])
361
- }
362
- const ssh = sshBinary(platform, env)
363
- const args = buildSshArgs(resolved.target, resolved.machine)
364
- const user = resolved.target.username || (resolved.machine && resolved.machine.username) || ''
365
- process.stderr.write(`[dsh-remote] connecting to ${user ? user + '@' : ''}${resolved.target.host}…\n`)
366
- if (isWindowsRemotePath(resolved.target.remotePath)) {
367
- process.stderr.write('[dsh-remote] the remote workspace is a Windows path; opening the remote default shell without changing directory.\n')
368
- process.stderr.write('[dsh-remote] 远端工作区是 Windows 路径,已打开默认 shell(未自动切换目录)。\n')
369
- }
370
- // Saved-password bridge: when the machine needs password auth and the host
371
- // wrote the bridge payload (Web composition only), answer ssh's password
372
- // prompt through the plugin's credential chain instead of the tty.
373
- const termDir = env.DSH_TERM_DIR || ''
374
- if (termDir && needsAskpass(resolved.machine) && existsSync(path.join(termDir, 'term-secret.json'))) {
375
- process.stderr.write('[dsh-remote] using the saved password via the local credential bridge.\n')
376
- process.stderr.write('[dsh-remote] 已保存的密码将通过本机凭据桥自动应答。\n')
377
- return handover(ssh, args, askpassEnv(resolved.machine, termDir, platform))
378
- }
379
- return handover(ssh, args)
380
- }
381
-
382
- module.exports = {
383
- META_FILE,
384
- askpassEnv,
385
- buildSshArgs,
386
- findMirror,
387
- needsAskpass,
388
- isWindowsRemotePath,
389
- localShell,
390
- main,
391
- matchMachine,
392
- parseArgs,
393
- remoteCd,
394
- remoteCommand,
395
- resolveTarget,
396
- sshBinary,
397
- stdinIsInteractive,
398
- }
399
-
400
- if (require.main === module) main()
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The program the host terminal runs when the user picks the "Remote SSH" shell.
4
+ *
5
+ * It is started by a generated launcher (see `remote-shell.js`) inside the
6
+ * session's working directory, resolves which remote checkout that directory
7
+ * belongs to, and then replaces itself with `ssh`. Inheriting stdio is the whole
8
+ * point: the local pty stays in charge of the screen, so resize and key handling
9
+ * keep working without this script knowing anything about terminals.
10
+ *
11
+ * Plain CommonJS on purpose: it is spawned as a standalone script, and it must
12
+ * not depend on how the plugin package itself is loaded.
13
+ */
14
+
15
+ 'use strict'
16
+
17
+ const { spawn } = require('node:child_process')
18
+ const { existsSync, fstatSync, readFileSync } = require('node:fs')
19
+ const path = require('node:path')
20
+ const { posixViaSh, quotePosix } = require('./posix-shell.cjs')
21
+
22
+ /** Name of the marker the host half writes into every local mirror root. */
23
+ const META_FILE = '.dsh-remote-meta.json'
24
+ /** How far up from the working directory a marker may be found. */
25
+ const MAX_WALK_UP = 12
26
+
27
+ /**
28
+ * Read and parse a JSON file, returning undefined for anything unusable.
29
+ * @param {string} file
30
+ * @returns {any}
31
+ */
32
+ function readJson(file) {
33
+ try {
34
+ return JSON.parse(readFileSync(file, 'utf8'))
35
+ } catch {
36
+ return undefined
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Find the mirror root that contains `cwd` by walking upwards for the marker.
42
+ * @param {string} cwd - session working directory
43
+ * @param {{ exists?: (p: string) => boolean, readJsonFile?: (p: string) => any, maxUp?: number }} deps
44
+ * @returns {{ dir: string, meta: object } | null}
45
+ */
46
+ function findMirror(cwd, deps = {}) {
47
+ const exists = deps.exists || ((p) => existsSync(p))
48
+ const read = deps.readJsonFile || readJson
49
+ const maxUp = deps.maxUp || MAX_WALK_UP
50
+ let dir = path.resolve(cwd || '.')
51
+ for (let depth = 0; depth <= maxUp; depth += 1) {
52
+ const marker = path.join(dir, META_FILE)
53
+ if (exists(marker)) {
54
+ const meta = read(marker)
55
+ if (meta && typeof meta === 'object' && meta.host) return { dir, meta }
56
+ }
57
+ const parent = path.dirname(dir)
58
+ if (parent === dir) break
59
+ dir = parent
60
+ }
61
+ return null
62
+ }
63
+
64
+ /**
65
+ * Pick the saved connection that owns a mirror.
66
+ *
67
+ * The marker records the SSH alias when the machine was saved from
68
+ * `~/.ssh/config` (issue #38), so the alias is the most precise key; otherwise
69
+ * host + user + port identify it. A missing entry is not fatal — the marker
70
+ * alone is enough to connect, and ssh will use its own defaults for the key.
71
+ *
72
+ * @param {object} meta - mirror marker
73
+ * @param {{ list?: object[] }} registry - parsed `machines.json`
74
+ * @returns {object | undefined}
75
+ */
76
+ function matchMachine(meta, registry) {
77
+ const list = registry && Array.isArray(registry.list) ? registry.list : []
78
+ const port = Number(meta.port || 22)
79
+ if (meta.alias) {
80
+ const byAlias = list.find((m) => m && m.host === meta.alias)
81
+ if (byAlias) return byAlias
82
+ }
83
+ return list.find((m) => m && m.host === meta.host
84
+ && String(m.username || '') === String(meta.username || '')
85
+ && Number(m.port || 22) === port)
86
+ }
87
+
88
+ // Paths and the outer sh -c boundary share the same POSIX quoting rules.
89
+ // WebAttach uses this same helper so both generated command paths quote identically.
90
+ /**
91
+ * Build the `cd` half of the remote command, keeping `~` for the *remote* shell
92
+ * to expand: the local home is not the remote home, so expanding it here would
93
+ * silently land in the wrong directory.
94
+ * @param {string} remotePath
95
+ * @returns {string}
96
+ */
97
+ function remoteCd(remotePath) {
98
+ const raw = String(remotePath || '').trim()
99
+ if (raw === '' || raw === '~') return 'cd "$HOME"'
100
+ if (raw === '~/') return 'cd "$HOME"'
101
+ if (raw.startsWith('~/')) return 'cd "$HOME"/' + quotePosix(raw.slice(2))
102
+ return 'cd ' + quotePosix(raw)
103
+ }
104
+
105
+ /**
106
+ * The remote command: land in the workspace, then hand over to a login shell.
107
+ *
108
+ * `exec` keeps the pty attached to a single interactive shell, so the exit code
109
+ * the terminal sees is the shell's own, and a deleted workspace falls back to
110
+ * `$HOME` instead of leaving a broken terminal. The POSIX script goes through
111
+ * `sh -c` because sshd otherwise hands it to a possibly non-POSIX login shell
112
+ * (fish/csh); the final interactive shell is still the user's own `$SHELL`.
113
+ *
114
+ * @param {string} remotePath
115
+ * @returns {string}
116
+ */
117
+ function remoteCommand(remotePath) {
118
+ const script = `${remoteCd(remotePath)} 2>/dev/null || cd "$HOME"; exec "\${SHELL:-/bin/bash}" -l`
119
+ return posixViaSh(script)
120
+ }
121
+
122
+ /** Absolute Windows path: `D:\x`, `D:/x`, or a `\\server\share` UNC path.
123
+ * These cannot be the operand of a POSIX `cd`. */
124
+ const WINDOWS_REMOTE_PATH = /^(?:[a-zA-Z]:[\\/]|\\\\)/
125
+
126
+ /**
127
+ * Whether a remote workspace path is a Windows one.
128
+ *
129
+ * A Windows remote speaks cmd or PowerShell in its default shell, so the POSIX
130
+ * `cd … || …; exec "$SHELL"` line would fail outright there — `2>/dev/null`,
131
+ * `||` and `exec` are not cmd syntax at all. The marker records no remote
132
+ * platform, but the path form is unambiguous: `/srv/x` and `~/x` are POSIX,
133
+ * while `D:\x` and `\\server\share` are not valid POSIX paths.
134
+ *
135
+ * @param {string} p - `remotePath` from the mirror marker
136
+ * @returns {boolean}
137
+ */
138
+ function isWindowsRemotePath(p) {
139
+ return WINDOWS_REMOTE_PATH.test(String(p || '').trim())
140
+ }
141
+
142
+ /**
143
+ * Argument list (without the ssh binary) for one connection.
144
+ *
145
+ * `-tt` is mandatory: the terminal needs a tty even though a command is given,
146
+ * otherwise the shell comes up non-interactive and every key press is ignored.
147
+ * Options are pushed in order and never spliced between an `-o` and its value —
148
+ * an inserted `-J` there once reached ssh as the value of `-o`, which aborted
149
+ * every connection that used a jump host (issue #21).
150
+ *
151
+ * @param {object} target - `{ host, port, username, alias, remotePath }`
152
+ * @param {object} [machine] - saved connection, for the key and transport hints
153
+ * @returns {string[]}
154
+ */
155
+ function buildSshArgs(target, machine) {
156
+ const args = ['-tt']
157
+ const alias = target.alias || (machine && machine.useSshConfig ? machine.host : '')
158
+ if (alias) {
159
+ args.push(String(alias))
160
+ } else {
161
+ const port = Number(target.port || (machine && machine.port) || 22)
162
+ if (port !== 22) args.push('-p', String(port))
163
+ const key = machine && machine.privateKeyPath
164
+ if (key && !machine.useAgent) args.push('-i', String(key))
165
+ const user = target.username || (machine && machine.username) || ''
166
+ args.push(user ? `${user}@${target.host}` : String(target.host))
167
+ }
168
+ // A Windows workspace cannot be reached with the POSIX line: the remote's
169
+ // default shell may be cmd or PowerShell, and picking one would be a guess.
170
+ // Opening ssh with no command leaves the remote's own default shell in charge,
171
+ // so the terminal still works; only the automatic `cd` is given up.
172
+ if (!isWindowsRemotePath(target.remotePath)) args.push(remoteCommand(target.remotePath))
173
+ return args
174
+ }
175
+
176
+ /**
177
+ * Prefer the OS OpenSSH binary on Windows. A PATH lookup there can reach an
178
+ * MSYS2/git-bash `ssh`, which resolves `~/.ssh` against a different home and
179
+ * makes keys saved by the host half look missing.
180
+ * @param {string} [platform]
181
+ * @param {object} [env]
182
+ * @param {(p: string) => boolean} [exists]
183
+ * @returns {string}
184
+ */
185
+ function sshBinary(platform = process.platform, env = process.env, exists = existsSync) {
186
+ if (platform === 'win32') {
187
+ const system = env.SystemRoot || env.windir || 'C:\\Windows'
188
+ const native = path.join(system, 'System32', 'OpenSSH', 'ssh.exe')
189
+ if (exists(native)) return native
190
+ }
191
+ return 'ssh'
192
+ }
193
+
194
+ /**
195
+ * The shell to fall back to when this directory is not a remote checkout.
196
+ * @param {string} [platform]
197
+ * @param {object} [env]
198
+ * @returns {string}
199
+ */
200
+ function localShell(platform = process.platform, env = process.env) {
201
+ if (platform === 'win32') return env.ComSpec || 'cmd.exe'
202
+ return env.SHELL || '/bin/sh'
203
+ }
204
+
205
+ /**
206
+ * Parse the arguments the launcher passes.
207
+ * @param {string[]} argv
208
+ * @returns {{ machines?: string }}
209
+ */
210
+ function parseArgs(argv) {
211
+ const out = {}
212
+ for (let i = 0; i < argv.length; i += 1) {
213
+ if (argv[i] === '--machines' && argv[i + 1]) {
214
+ out.machines = argv[i + 1]
215
+ i += 1
216
+ }
217
+ }
218
+ return out
219
+ }
220
+
221
+ /**
222
+ * Resolve everything needed to connect from a working directory.
223
+ * @param {{ cwd?: string, machines?: string, exists?: Function, readJsonFile?: Function }} input
224
+ * @returns {{ target?: object, machine?: object, found: boolean }}
225
+ */
226
+ function resolveTarget(input = {}) {
227
+ const cwd = input.cwd || process.cwd()
228
+ const mirror = findMirror(cwd, input)
229
+ if (!mirror) return { found: false }
230
+ const registry = input.machines ? (input.readJsonFile || readJson)(input.machines) : undefined
231
+ const machine = matchMachine(mirror.meta, registry)
232
+ const target = {
233
+ host: mirror.meta.host,
234
+ port: mirror.meta.port || (machine && machine.port) || 22,
235
+ username: mirror.meta.username || (machine && machine.username) || '',
236
+ alias: mirror.meta.alias || '',
237
+ remotePath: mirror.meta.remotePath || (machine && machine.workspace) || '',
238
+ }
239
+ return { found: true, target, machine, dir: mirror.dir }
240
+ }
241
+
242
+ /**
243
+ * Whether stdin is a console this process can actually receive keys from.
244
+ *
245
+ * A terminal that inherits stdio from `ELECTRON_RUN_AS_NODE` gets pipes, not
246
+ * the ConPTY console: output flows while every keystroke is dropped, leaving a
247
+ * tab that looks alive and accepts nothing (issue #91). Detecting that here
248
+ * turns a silent dead end into an explicit, actionable refusal. `isTTY` is the
249
+ * cheap check; `fstat(0)` catches runtimes that never set the flag.
250
+ *
251
+ * @param {{ stdin?: object, fstat?: (fd: number) => any }} [deps]
252
+ * @returns {boolean}
253
+ */
254
+ function stdinIsInteractive(deps = {}) {
255
+ const stdin = deps.stdin === undefined ? process.stdin : deps.stdin
256
+ const fstat = deps.fstat || fstatSync
257
+ if (stdin && stdin.isTTY === true) return true
258
+ try {
259
+ const stat = fstat(0)
260
+ return Boolean(stat && typeof stat.isCharacterDevice === 'function' && stat.isCharacterDevice())
261
+ } catch {
262
+ return false
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Whether the machine's password auth needs (and can use) the credential
268
+ * bridge. Keys, agents and ssh-config aliases authenticate without us;
269
+ * keyboard-interactive (OTP) prompts are per-login and must stay on the tty;
270
+ * everything else with a stored credential (plaintext registry or OS keychain)
271
+ * goes through the bridge.
272
+ *
273
+ * @param {object|undefined} machine - the matched registry record.
274
+ * @returns {boolean}
275
+ */
276
+ function needsAskpass(machine) {
277
+ if (!machine) return false
278
+ if (machine.useAgent || machine.useSshConfig || machine.keyboardInteractive) return false
279
+ if (String(machine.privateKeyPath || '')) return false
280
+ if (String(machine.password || '')) return true
281
+ return Boolean(machine.credentialBackend && machine.credentialBackend !== 'plain')
282
+ }
283
+
284
+ /**
285
+ * Environment additions that route ssh's password prompt to the bridge helper.
286
+ * Only applied when needsAskpass() held AND the payload file exists.
287
+ *
288
+ * `SSH_ASKPASS_REQUIRE=force` (OpenSSH ≥ 8.4) is what makes askpass run at all
289
+ * on a tty; DISPLAY feeds prehistoric ssh builds. DSH_TS_* carries the machine
290
+ * identity to the helper through the process environment.
291
+ *
292
+ * @param {object} machine
293
+ * @param {string} termDir - the launcher's directory (DSH_TERM_DIR).
294
+ * @param {string} [platform]
295
+ * @returns {object} env entries to merge over process.env
296
+ */
297
+ function askpassEnv(machine, termDir, platform = process.platform) {
298
+ const wrapper = path.join(termDir, platform === 'win32' ? 'term-askpass.cmd' : 'term-askpass')
299
+ return {
300
+ SSH_ASKPASS: wrapper,
301
+ SSH_ASKPASS_REQUIRE: 'force',
302
+ DISPLAY: process.env.DISPLAY || 'dsh-remote:0',
303
+ DSH_TS_HOST: String(machine.host || ''),
304
+ DSH_TS_PORT: String(Number(machine.port) || 22),
305
+ DSH_TS_USERNAME: String(machine.username || ''),
306
+ DSH_TS_ALIAS: String(machine.alias || ''),
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Hand the current process over to `file args…`, propagating the exit code.
312
+ * @param {string} file
313
+ * @param {string[]} args
314
+ * @param {object} [extraEnv] - env entries merged over process.env (askpass).
315
+ * @returns {import('node:child_process').ChildProcess}
316
+ */
317
+ function handover(file, args, extraEnv) {
318
+ const child = spawn(file, args, { stdio: 'inherit', env: extraEnv ? { ...process.env, ...extraEnv } : process.env })
319
+ child.on('error', (error) => {
320
+ process.stderr.write(`[dsh-remote] cannot run ${file}: ${error && error.message}\n`)
321
+ process.exit(127)
322
+ })
323
+ child.on('exit', (code, signal) => {
324
+ if (signal) process.kill(process.pid, signal)
325
+ else process.exit(code == null ? 0 : code)
326
+ })
327
+ return child
328
+ }
329
+
330
+ /**
331
+ * Entry point: connect, or explain why a local shell was used instead.
332
+ * @param {string[]} [argv]
333
+ * @param {{ cwd?: string, platform?: string, env?: object,
334
+ * stdin?: object, fstat?: (fd: number) => any,
335
+ * write?: (line: string) => void, exit?: (code: number) => any }} [deps]
336
+ */
337
+ function main(argv = process.argv.slice(2), deps = {}) {
338
+ const platform = deps.platform || process.platform
339
+ const env = deps.env || process.env
340
+ const write = deps.write || ((line) => process.stderr.write(line))
341
+ const exit = deps.exit || ((code) => process.exit(code))
342
+ if (!stdinIsInteractive(deps)) {
343
+ // Anything we could hand over to — ssh, cmd, sh — reads stdin the same
344
+ // way, so there is no useful fallback here: the tab would render the
345
+ // remote prompt and ignore every key. Refuse loudly instead (issue #91).
346
+ write('[dsh-remote] this terminal cannot receive input: stdin is not a real console.\n')
347
+ write('[dsh-remote] point DSH_REMOTE_TERMINAL_NODE at an absolute node.exe path and restart, so the launcher does not have to reuse the app runtime.\n')
348
+ write('[dsh-remote] 终端无法接收输入(stdin 不是真实控制台)。请把 DSH_REMOTE_TERMINAL_NODE 指向 node.exe 的绝对路径后重启。\n')
349
+ exit(126)
350
+ return undefined
351
+ }
352
+ const options = parseArgs(argv)
353
+ const resolved = resolveTarget({ cwd: deps.cwd || process.cwd(), machines: options.machines })
354
+ if (!resolved.found) {
355
+ process.stderr.write('[dsh-remote] this directory is not a remote workspace; opening a local shell.\n')
356
+ process.stderr.write('[dsh-remote] 当前目录不是远程工作区,已打开本地终端。\n')
357
+ return handover(localShell(platform, env), platform === 'win32' ? [] : ['-l'])
358
+ }
359
+ const ssh = sshBinary(platform, env)
360
+ const args = buildSshArgs(resolved.target, resolved.machine)
361
+ const user = resolved.target.username || (resolved.machine && resolved.machine.username) || ''
362
+ process.stderr.write(`[dsh-remote] connecting to ${user ? user + '@' : ''}${resolved.target.host}…\n`)
363
+ if (isWindowsRemotePath(resolved.target.remotePath)) {
364
+ process.stderr.write('[dsh-remote] the remote workspace is a Windows path; opening the remote default shell without changing directory.\n')
365
+ process.stderr.write('[dsh-remote] 远端工作区是 Windows 路径,已打开默认 shell(未自动切换目录)。\n')
366
+ }
367
+ // Saved-password bridge: when the machine needs password auth and the host
368
+ // wrote the bridge payload (Web composition only), answer ssh's password
369
+ // prompt through the plugin's credential chain instead of the tty.
370
+ const termDir = env.DSH_TERM_DIR || ''
371
+ if (termDir && needsAskpass(resolved.machine) && existsSync(path.join(termDir, 'term-secret.json'))) {
372
+ process.stderr.write('[dsh-remote] using the saved password via the local credential bridge.\n')
373
+ process.stderr.write('[dsh-remote] 已保存的密码将通过本机凭据桥自动应答。\n')
374
+ return handover(ssh, args, askpassEnv(resolved.machine, termDir, platform))
375
+ }
376
+ return handover(ssh, args)
377
+ }
378
+
379
+ module.exports = {
380
+ META_FILE,
381
+ askpassEnv,
382
+ buildSshArgs,
383
+ findMirror,
384
+ needsAskpass,
385
+ isWindowsRemotePath,
386
+ localShell,
387
+ main,
388
+ matchMachine,
389
+ parseArgs,
390
+ remoteCd,
391
+ remoteCommand,
392
+ resolveTarget,
393
+ sshBinary,
394
+ stdinIsInteractive,
395
+ }
396
+
397
+ if (require.main === module) main()