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
@@ -0,0 +1,34 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * Quote one string for a POSIX shell using single quotes only.
5
+ *
6
+ * A single quote inside the value is emitted as the three-step sequence
7
+ * close-quote, escaped-quote, reopen-quote. The result preserves the value
8
+ * literally, including newlines, dollar signs, backticks and double quotes.
9
+ *
10
+ * @param {unknown} value
11
+ * @returns {string}
12
+ */
13
+ function quotePosix(value) {
14
+ return "'" + String(value).replace(/'/g, "'\\''") + "'"
15
+ }
16
+
17
+ /**
18
+ * Run a generated POSIX script through `sh` instead of the remote login shell.
19
+ *
20
+ * sshd gives an exec request to the user's login shell. That shell may be fish,
21
+ * csh or another non-POSIX implementation, while this package's internal launch
22
+ * and probe scripts are deliberately POSIX. The outer shell only has to parse
23
+ * the simple `sh -c '<quoted script>'` invocation; `sh` then parses the actual
24
+ * script. Generic rw_exec commands are NOT routed through this helper, so users
25
+ * retain their login-shell semantics.
26
+ *
27
+ * @param {string} command
28
+ * @returns {string}
29
+ */
30
+ function posixViaSh(command) {
31
+ return `sh -c ${quotePosix(command)}`
32
+ }
33
+
34
+ module.exports = { posixViaSh, quotePosix }
package/lib/registry.js CHANGED
@@ -1,296 +1,296 @@
1
- // Machine-registry pure logic for dsh-remote.
2
- //
3
- // Issue #13: a SAVED machine is only a standby connection — it must never
4
- // silently become the "active remote context" of every session. The registry
5
- // therefore keeps `currentId` as an EXPLICIT user choice:
6
- // • loadMachines never falls back to the first saved machine,
7
- // • a stale currentId (machine deleted meanwhile) resolves to null,
8
- // • add/update never auto-activate; only an explicit "set current" does,
9
- // • deleting the current machine leaves NO current (no auto-promotion).
10
- // Keeping this logic in one module makes the guarantee unit-testable and
11
- // shared between the HTTP routes and the tools (rw_connect).
12
-
13
- import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
14
- import path from 'node:path'
15
-
16
- /** Load { list, currentId, explicitNone } from the registry file. */
17
- export function loadMachines(file) {
18
- try {
19
- const j = JSON.parse(readFileSync(file, 'utf8'))
20
- if (Array.isArray(j.list)) {
21
- const list = j.list
22
- // Explicit choice only — no fallback to the first machine, no activation
23
- // of a stale/deleted id (issue #13).
24
- const currentId = j.currentId && list.some((m) => m.id === j.currentId) ? j.currentId : null
25
- // A persisted `currentId: null` is a deliberate "active remote = none"
26
- // (the user cleared the current machine). It must survive restarts so a
27
- // config-level default host cannot silently reactivate a context the user
28
- // explicitly turned off. `explicitNone` is true when the file carries an
29
- // explicit null (vs. a fresh file where currentId is simply absent).
30
- const explicitNone = Object.prototype.hasOwnProperty.call(j, 'currentId') && j.currentId == null
31
- return { list, currentId, explicitNone }
32
- }
33
- } catch {}
34
- return { list: [], currentId: null, explicitNone: false }
35
- }
36
-
37
- /** Persist { list, currentId }. `currentId` may be null → active remote = none.
38
- * Pass `keepCurrentKey: false` (add/update a machine without touching the
39
- * current selection) to PRESERVE whatever `currentId` the file already holds
40
- * — so merely saving a machine never flips a fresh registry into "explicit
41
- * none", nor clears an explicit choice. When no previous file exists the
42
- * `currentId` key is omitted entirely (fresh registry = "no choice yet"). */
43
- export function saveMachines(file, list, currentId, keepCurrentKey = true) {
44
- try { mkdirSync(path.dirname(file), { recursive: true }) } catch {}
45
- let stored = currentId
46
- let hasStored = keepCurrentKey
47
- if (!keepCurrentKey) {
48
- try {
49
- const prev = JSON.parse(readFileSync(file, 'utf8'))
50
- if (prev && Object.prototype.hasOwnProperty.call(prev, 'currentId')) {
51
- stored = prev.currentId
52
- hasStored = true
53
- }
54
- } catch { /* no previous file → omit the key */ }
55
- }
56
- const payload = { list }
57
- if (hasStored) payload.currentId = stored
58
- writeFileSync(file, JSON.stringify(payload, null, 2))
59
- }
60
-
61
- /**
62
- * Does this record actually HAVE a password the plugin can use?
63
- *
64
- * `passwordSet` used to be `!!m.password`, i.e. "a PLAINTEXT password exists in
65
- * machines.json". A machine whose password lives in the OS keychain stores
66
- * `password: ''` (by design — issue #30), so it reported `passwordSet: false`
67
- * and the settings page showed an empty password box with no indication that a
68
- * credential was saved. That is the difference between "the plugin forgot my
69
- * password" and "the plugin has it and will not echo it".
70
- *
71
- * `secretStored` is written by the save route from the keychain result, so new
72
- * records carry the truth. Records saved before that flag existed only carry the
73
- * backend name, which is the best evidence available for them (a non-plain
74
- * backend means a secret was meant to live in the OS store).
75
- *
76
- * @param {object} m - a registry record.
77
- * @returns {boolean}
78
- */
79
- export function hasStoredPassword(m) {
80
- if (!m) return false
81
- if (m.password) return true
82
- if (m.secretStored === true) return true
83
- if (m.secretStored === undefined && m.credentialBackend && m.credentialBackend !== 'plain') return true
84
- return false
85
- }
86
-
87
- /** Strip secrets before sending a machine to the client.
88
- *
89
- * Secrets never round-trip to the browser: a password is replaced by
90
- * `passwordSet`, and the private-key `passphrase` — which IS stored in the
91
- * record — is replaced by `passphraseSet`. Both flags exist so the settings form
92
- * can say "已保存(••••••••),留空 = 不修改" instead of showing an empty box that
93
- * looks like the plugin lost the value. */
94
- export function sanitizeMachine(m) {
95
- if (!m) return m
96
- const { password, passphrase, proxy, ...rest } = m
97
- const out = {
98
- ...rest,
99
- passwordSet: hasStoredPassword(m),
100
- passphraseSet: !!(passphrase && String(passphrase).length),
101
- }
102
- if (proxy) {
103
- out.proxy = {
104
- ...proxy,
105
- password: proxy.password ? '' : undefined,
106
- passwordSet: !!(proxy.password && proxy.password.length),
107
- }
108
- }
109
- return out
110
- }
111
-
112
- /**
113
- * Resolve the credentials a TEST probe must dial with (issue: "编辑已连接机器 → 测试
114
- * 连接" always failed with `no credentials`).
115
- *
116
- * The settings form never receives stored secrets, so an edit-then-test posted an
117
- * EMPTY password while a real connect (which resolves the keychain/plain secret
118
- * through the pool's `passwordResolver`) worked fine. The probe therefore has to
119
- * be a MERGE: the form is an override layer, and anything it leaves blank falls
120
- * back to what is stored for that machine.
121
- *
122
- * Rules, deliberately one-way (a form value never gets discarded):
123
- * · a non-empty form string wins;
124
- * · an empty/absent form string keeps the stored value;
125
- * · booleans (`useAgent`, `keyboardInteractive`) are taken from the form when
126
- * the form actually supplied a boolean, otherwise from the record — the
127
- * probe must exercise the SAME auth method the machine will use;
128
- * · a jump-host (`proxy`) password the form did not re-supply is carried over
129
- * from the stored proxy, otherwise a bastion-protected machine "fails to
130
- * test" for a reason that has nothing to do with the target.
131
- *
132
- * @param {object} body - the probe request body (form values).
133
- * @param {object|null} rec - the saved machine record, if any.
134
- * @param {string} [storedPassword] - the record's resolvable password (may come
135
- * from the OS keychain, hence the separate argument).
136
- * @returns {{password:string,passphrase:string,privateKeyPath:string,useAgent:boolean,
137
- * keyboardInteractive:boolean,hostKeyMode:string,sshBinary:string,backend:string,
138
- * proxy:object|undefined,usedSavedCredential:boolean}}
139
- */
140
- export function resolveProbeCredentials(body = {}, rec = null, storedPassword = '') {
141
- const form = body || {}
142
- const saved = rec || {}
143
- const str = (v) => (v === undefined || v === null ? '' : String(v))
144
- const pick = (formVal, storedVal) => {
145
- const f = str(formVal).trim()
146
- return f !== '' ? str(formVal) : str(storedVal)
147
- }
148
- const pickBool = (formVal, storedVal) => (typeof formVal === 'boolean' ? formVal : !!storedVal)
149
-
150
- const formPassword = str(form.password)
151
- const password = formPassword !== '' ? formPassword : str(storedPassword)
152
- const formProxy = form.proxy && form.proxy.host ? form.proxy : null
153
- const savedProxy = saved.proxy && saved.proxy.host ? saved.proxy : null
154
- const baseProxy = formProxy || savedProxy
155
- const proxy = baseProxy
156
- ? {
157
- host: String(baseProxy.host),
158
- port: Number(baseProxy.port) || 22,
159
- username: str(baseProxy.username),
160
- password: formProxy && str(formProxy.password) !== ''
161
- ? str(formProxy.password)
162
- : str(savedProxy && savedProxy.password),
163
- privateKeyPath: str(baseProxy.privateKeyPath),
164
- }
165
- : undefined
166
-
167
- return {
168
- password,
169
- passphrase: pick(form.passphrase, saved.passphrase),
170
- privateKeyPath: pick(form.privateKeyPath, saved.privateKeyPath),
171
- useAgent: pickBool(form.useAgent, saved.useAgent),
172
- keyboardInteractive: pickBool(form.keyboardInteractive, saved.keyboardInteractive),
173
- hostKeyMode: pick(form.hostKeyMode, saved.hostKeyMode),
174
- sshBinary: pick(form.sshBinary, saved.sshBinary),
175
- backend: pick(form.backend, saved.backend) || 'native',
176
- proxy,
177
- // True when the form could not supply the credential and the stored one had
178
- // to be used — the UI says so, because "测试成功" with an empty password box
179
- // is otherwise indistinguishable from a form-only probe.
180
- usedSavedCredential: formPassword === '' && password !== '',
181
- }
182
- }
183
-
184
- /** Machine grouping metadata (host organisation).
185
- *
186
- * A flat list does not survive contact with reality: once a user has a dozen
187
- * machines (prod/staging/GPU boxes/laptops) "pick the right one" becomes the
188
- * dominant failure mode, and picking the WRONG one on a destructive tool is
189
- * how accidents happen. These four fields are pure LABELS — they change no
190
- * connection behaviour — so they live beside it and can be edited freely.
191
- *
192
- * Normalization is deliberately conservative: a bad value is dropped (not
193
- * coerced into something surprising), and every field is optional so existing
194
- * registries keep loading byte-for-byte.
195
- *
196
- * @param {object} raw - a (possibly partial) group patch.
197
- * @returns {{environment?: string, location?: string, description?: string, tags?: string[]}}
198
- */
199
- export function normalizeMachineGroup(raw) {
200
- const out = {}
201
- if (!raw || typeof raw !== 'object') return out
202
- const str = (v, max) => {
203
- const s = typeof v === 'string' ? v.trim() : ''
204
- return s ? s.slice(0, max) : ''
205
- }
206
- const env = str(raw.environment, 40)
207
- if (env) out.environment = env
208
- const loc = str(raw.location, 40)
209
- if (loc) out.location = loc
210
- const desc = str(raw.description, 200)
211
- if (desc) out.description = desc
212
- // Tags: accept an array or a comma/space separated string (what a UI text box
213
- // naturally produces), then de-duplicate case-insensitively while KEEPING the
214
- // first spelling so the display stays stable.
215
- let tags = raw.tags
216
- if (typeof tags === 'string') tags = tags.split(/[,\s]+/)
217
- if (Array.isArray(tags)) {
218
- const seen = new Set()
219
- const kept = []
220
- for (const t of tags) {
221
- const s = str(t, 32)
222
- if (!s) continue
223
- const k = s.toLowerCase()
224
- if (seen.has(k)) continue
225
- seen.add(k)
226
- kept.push(s)
227
- if (kept.length >= 12) break // a dozen labels is already too many to scan
228
- }
229
- if (kept.length) out.tags = kept
230
- }
231
- return out
232
- }
233
-
234
- /**
235
- * Select machines by group metadata — the predicate behind "run this on every
236
- * staging box". All supplied filters must match (AND); tags are OR within
237
- * themselves (any requested tag qualifies) which is how people read a tag row.
238
- *
239
- * @param {Array<object>} machines
240
- * @param {{environment?: string, location?: string, tags?: string[]|string, query?: string}} filter
241
- * @returns {Array<object>}
242
- */
243
- export function filterMachinesByGroup(machines, filter = {}) {
244
- const list = Array.isArray(machines) ? machines : []
245
- const env = typeof filter.environment === 'string' ? filter.environment.trim().toLowerCase() : ''
246
- const loc = typeof filter.location === 'string' ? filter.location.trim().toLowerCase() : ''
247
- let wantTags = filter.tags
248
- if (typeof wantTags === 'string') wantTags = wantTags.split(/[,\s]+/)
249
- wantTags = (Array.isArray(wantTags) ? wantTags : []).map((t) => String(t).trim().toLowerCase()).filter(Boolean)
250
- const query = typeof filter.query === 'string' ? filter.query.trim().toLowerCase() : ''
251
- return list.filter((m) => {
252
- if (!m) return false
253
- if (env && String(m.environment || '').toLowerCase() !== env) return false
254
- if (loc && String(m.location || '').toLowerCase() !== loc) return false
255
- if (wantTags.length) {
256
- const have = (Array.isArray(m.tags) ? m.tags : []).map((t) => String(t).toLowerCase())
257
- if (!wantTags.some((t) => have.includes(t))) return false
258
- }
259
- if (query) {
260
- const hay = [m.name, m.host, m.username, m.environment, m.location, m.description, ...(Array.isArray(m.tags) ? m.tags : [])]
261
- .filter(Boolean).join(' ').toLowerCase()
262
- if (!hay.includes(query)) return false
263
- }
264
- return true
265
- })
266
- }
267
-
268
- /** Apply a machine's fields onto the live config object (pool + tools read it). */
269
- export function applyMachine(config, m) {
270
- if (!m) return
271
- config.host = m.host
272
- config.port = Number(m.port) || 22
273
- config.username = m.username || ''
274
- config.password = m.password || ''
275
- config.privateKeyPath = m.privateKeyPath || ''
276
- config.passphrase = m.passphrase || ''
277
- // IdentityAgent / IdentitiesOnly from ~/.ssh/config (set by withSshConfig).
278
- config.agentSocket = m.agentSocket || ''
279
- config.identitiesOnly = !!m.identitiesOnly
280
- config.workspace = m.workspace || (config.workspace || '')
281
- if (m.hostKeyMode) config.hostKeyMode = m.hostKeyMode
282
- if (typeof m.useAgent === 'boolean') config.useAgent = m.useAgent
283
- if (typeof m.keyboardInteractive === 'boolean') config.keyboardInteractive = m.keyboardInteractive
284
- // Issue #51: the transport backend is PER MACHINE. An enterprise box that only
285
- // accepts Kerberos must go through the system OpenSSH backend while every
286
- // other machine can stay on the native one, so this cannot be a purely global
287
- // default.
288
- if (m.backend) config.backend = m.backend
289
- if (typeof m.sshBinary === 'string') config.sshBinary = m.sshBinary
290
- config.proxy = m.proxy && m.proxy.host ? m.proxy : undefined
291
- }
292
-
293
- /** Mint a fresh machine id. */
294
- export function machineId() {
295
- return 'm-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 6)
296
- }
1
+ // Machine-registry pure logic for dsh-remote.
2
+ //
3
+ // Issue #13: a SAVED machine is only a standby connection — it must never
4
+ // silently become the "active remote context" of every session. The registry
5
+ // therefore keeps `currentId` as an EXPLICIT user choice:
6
+ // • loadMachines never falls back to the first saved machine,
7
+ // • a stale currentId (machine deleted meanwhile) resolves to null,
8
+ // • add/update never auto-activate; only an explicit "set current" does,
9
+ // • deleting the current machine leaves NO current (no auto-promotion).
10
+ // Keeping this logic in one module makes the guarantee unit-testable and
11
+ // shared between the HTTP routes and the tools (rw_connect).
12
+
13
+ import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
14
+ import path from 'node:path'
15
+
16
+ /** Load { list, currentId, explicitNone } from the registry file. */
17
+ export function loadMachines(file) {
18
+ try {
19
+ const j = JSON.parse(readFileSync(file, 'utf8'))
20
+ if (Array.isArray(j.list)) {
21
+ const list = j.list
22
+ // Explicit choice only — no fallback to the first machine, no activation
23
+ // of a stale/deleted id (issue #13).
24
+ const currentId = j.currentId && list.some((m) => m.id === j.currentId) ? j.currentId : null
25
+ // A persisted `currentId: null` is a deliberate "active remote = none"
26
+ // (the user cleared the current machine). It must survive restarts so a
27
+ // config-level default host cannot silently reactivate a context the user
28
+ // explicitly turned off. `explicitNone` is true when the file carries an
29
+ // explicit null (vs. a fresh file where currentId is simply absent).
30
+ const explicitNone = Object.prototype.hasOwnProperty.call(j, 'currentId') && j.currentId == null
31
+ return { list, currentId, explicitNone }
32
+ }
33
+ } catch {}
34
+ return { list: [], currentId: null, explicitNone: false }
35
+ }
36
+
37
+ /** Persist { list, currentId }. `currentId` may be null → active remote = none.
38
+ * Pass `keepCurrentKey: false` (add/update a machine without touching the
39
+ * current selection) to PRESERVE whatever `currentId` the file already holds
40
+ * — so merely saving a machine never flips a fresh registry into "explicit
41
+ * none", nor clears an explicit choice. When no previous file exists the
42
+ * `currentId` key is omitted entirely (fresh registry = "no choice yet"). */
43
+ export function saveMachines(file, list, currentId, keepCurrentKey = true) {
44
+ try { mkdirSync(path.dirname(file), { recursive: true }) } catch {}
45
+ let stored = currentId
46
+ let hasStored = keepCurrentKey
47
+ if (!keepCurrentKey) {
48
+ try {
49
+ const prev = JSON.parse(readFileSync(file, 'utf8'))
50
+ if (prev && Object.prototype.hasOwnProperty.call(prev, 'currentId')) {
51
+ stored = prev.currentId
52
+ hasStored = true
53
+ }
54
+ } catch { /* no previous file → omit the key */ }
55
+ }
56
+ const payload = { list }
57
+ if (hasStored) payload.currentId = stored
58
+ writeFileSync(file, JSON.stringify(payload, null, 2))
59
+ }
60
+
61
+ /**
62
+ * Does this record actually HAVE a password the plugin can use?
63
+ *
64
+ * `passwordSet` used to be `!!m.password`, i.e. "a PLAINTEXT password exists in
65
+ * machines.json". A machine whose password lives in the OS keychain stores
66
+ * `password: ''` (by design — issue #30), so it reported `passwordSet: false`
67
+ * and the settings page showed an empty password box with no indication that a
68
+ * credential was saved. That is the difference between "the plugin forgot my
69
+ * password" and "the plugin has it and will not echo it".
70
+ *
71
+ * `secretStored` is written by the save route from the keychain result, so new
72
+ * records carry the truth. Records saved before that flag existed only carry the
73
+ * backend name, which is the best evidence available for them (a non-plain
74
+ * backend means a secret was meant to live in the OS store).
75
+ *
76
+ * @param {object} m - a registry record.
77
+ * @returns {boolean}
78
+ */
79
+ export function hasStoredPassword(m) {
80
+ if (!m) return false
81
+ if (m.password) return true
82
+ if (m.secretStored === true) return true
83
+ if (m.secretStored === undefined && m.credentialBackend && m.credentialBackend !== 'plain') return true
84
+ return false
85
+ }
86
+
87
+ /** Strip secrets before sending a machine to the client.
88
+ *
89
+ * Secrets never round-trip to the browser: a password is replaced by
90
+ * `passwordSet`, and the private-key `passphrase` — which IS stored in the
91
+ * record — is replaced by `passphraseSet`. Both flags exist so the settings form
92
+ * can say "已保存(••••••••),留空 = 不修改" instead of showing an empty box that
93
+ * looks like the plugin lost the value. */
94
+ export function sanitizeMachine(m) {
95
+ if (!m) return m
96
+ const { password, passphrase, proxy, ...rest } = m
97
+ const out = {
98
+ ...rest,
99
+ passwordSet: hasStoredPassword(m),
100
+ passphraseSet: !!(passphrase && String(passphrase).length),
101
+ }
102
+ if (proxy) {
103
+ out.proxy = {
104
+ ...proxy,
105
+ password: proxy.password ? '' : undefined,
106
+ passwordSet: !!(proxy.password && proxy.password.length),
107
+ }
108
+ }
109
+ return out
110
+ }
111
+
112
+ /**
113
+ * Resolve the credentials a TEST probe must dial with (issue: "编辑已连接机器 → 测试
114
+ * 连接" always failed with `no credentials`).
115
+ *
116
+ * The settings form never receives stored secrets, so an edit-then-test posted an
117
+ * EMPTY password while a real connect (which resolves the keychain/plain secret
118
+ * through the pool's `passwordResolver`) worked fine. The probe therefore has to
119
+ * be a MERGE: the form is an override layer, and anything it leaves blank falls
120
+ * back to what is stored for that machine.
121
+ *
122
+ * Rules, deliberately one-way (a form value never gets discarded):
123
+ * · a non-empty form string wins;
124
+ * · an empty/absent form string keeps the stored value;
125
+ * · booleans (`useAgent`, `keyboardInteractive`) are taken from the form when
126
+ * the form actually supplied a boolean, otherwise from the record — the
127
+ * probe must exercise the SAME auth method the machine will use;
128
+ * · a jump-host (`proxy`) password the form did not re-supply is carried over
129
+ * from the stored proxy, otherwise a bastion-protected machine "fails to
130
+ * test" for a reason that has nothing to do with the target.
131
+ *
132
+ * @param {object} body - the probe request body (form values).
133
+ * @param {object|null} rec - the saved machine record, if any.
134
+ * @param {string} [storedPassword] - the record's resolvable password (may come
135
+ * from the OS keychain, hence the separate argument).
136
+ * @returns {{password:string,passphrase:string,privateKeyPath:string,useAgent:boolean,
137
+ * keyboardInteractive:boolean,hostKeyMode:string,sshBinary:string,backend:string,
138
+ * proxy:object|undefined,usedSavedCredential:boolean}}
139
+ */
140
+ export function resolveProbeCredentials(body = {}, rec = null, storedPassword = '') {
141
+ const form = body || {}
142
+ const saved = rec || {}
143
+ const str = (v) => (v === undefined || v === null ? '' : String(v))
144
+ const pick = (formVal, storedVal) => {
145
+ const f = str(formVal).trim()
146
+ return f !== '' ? str(formVal) : str(storedVal)
147
+ }
148
+ const pickBool = (formVal, storedVal) => (typeof formVal === 'boolean' ? formVal : !!storedVal)
149
+
150
+ const formPassword = str(form.password)
151
+ const password = formPassword !== '' ? formPassword : str(storedPassword)
152
+ const formProxy = form.proxy && form.proxy.host ? form.proxy : null
153
+ const savedProxy = saved.proxy && saved.proxy.host ? saved.proxy : null
154
+ const baseProxy = formProxy || savedProxy
155
+ const proxy = baseProxy
156
+ ? {
157
+ host: String(baseProxy.host),
158
+ port: Number(baseProxy.port) || 22,
159
+ username: str(baseProxy.username),
160
+ password: formProxy && str(formProxy.password) !== ''
161
+ ? str(formProxy.password)
162
+ : str(savedProxy && savedProxy.password),
163
+ privateKeyPath: str(baseProxy.privateKeyPath),
164
+ }
165
+ : undefined
166
+
167
+ return {
168
+ password,
169
+ passphrase: pick(form.passphrase, saved.passphrase),
170
+ privateKeyPath: pick(form.privateKeyPath, saved.privateKeyPath),
171
+ useAgent: pickBool(form.useAgent, saved.useAgent),
172
+ keyboardInteractive: pickBool(form.keyboardInteractive, saved.keyboardInteractive),
173
+ hostKeyMode: pick(form.hostKeyMode, saved.hostKeyMode),
174
+ sshBinary: pick(form.sshBinary, saved.sshBinary),
175
+ backend: pick(form.backend, saved.backend) || 'native',
176
+ proxy,
177
+ // True when the form could not supply the credential and the stored one had
178
+ // to be used — the UI says so, because "测试成功" with an empty password box
179
+ // is otherwise indistinguishable from a form-only probe.
180
+ usedSavedCredential: formPassword === '' && password !== '',
181
+ }
182
+ }
183
+
184
+ /** Machine grouping metadata (host organisation).
185
+ *
186
+ * A flat list does not survive contact with reality: once a user has a dozen
187
+ * machines (prod/staging/GPU boxes/laptops) "pick the right one" becomes the
188
+ * dominant failure mode, and picking the WRONG one on a destructive tool is
189
+ * how accidents happen. These four fields are pure LABELS — they change no
190
+ * connection behaviour — so they live beside it and can be edited freely.
191
+ *
192
+ * Normalization is deliberately conservative: a bad value is dropped (not
193
+ * coerced into something surprising), and every field is optional so existing
194
+ * registries keep loading byte-for-byte.
195
+ *
196
+ * @param {object} raw - a (possibly partial) group patch.
197
+ * @returns {{environment?: string, location?: string, description?: string, tags?: string[]}}
198
+ */
199
+ export function normalizeMachineGroup(raw) {
200
+ const out = {}
201
+ if (!raw || typeof raw !== 'object') return out
202
+ const str = (v, max) => {
203
+ const s = typeof v === 'string' ? v.trim() : ''
204
+ return s ? s.slice(0, max) : ''
205
+ }
206
+ const env = str(raw.environment, 40)
207
+ if (env) out.environment = env
208
+ const loc = str(raw.location, 40)
209
+ if (loc) out.location = loc
210
+ const desc = str(raw.description, 200)
211
+ if (desc) out.description = desc
212
+ // Tags: accept an array or a comma/space separated string (what a UI text box
213
+ // naturally produces), then de-duplicate case-insensitively while KEEPING the
214
+ // first spelling so the display stays stable.
215
+ let tags = raw.tags
216
+ if (typeof tags === 'string') tags = tags.split(/[,\s]+/)
217
+ if (Array.isArray(tags)) {
218
+ const seen = new Set()
219
+ const kept = []
220
+ for (const t of tags) {
221
+ const s = str(t, 32)
222
+ if (!s) continue
223
+ const k = s.toLowerCase()
224
+ if (seen.has(k)) continue
225
+ seen.add(k)
226
+ kept.push(s)
227
+ if (kept.length >= 12) break // a dozen labels is already too many to scan
228
+ }
229
+ if (kept.length) out.tags = kept
230
+ }
231
+ return out
232
+ }
233
+
234
+ /**
235
+ * Select machines by group metadata — the predicate behind "run this on every
236
+ * staging box". All supplied filters must match (AND); tags are OR within
237
+ * themselves (any requested tag qualifies) which is how people read a tag row.
238
+ *
239
+ * @param {Array<object>} machines
240
+ * @param {{environment?: string, location?: string, tags?: string[]|string, query?: string}} filter
241
+ * @returns {Array<object>}
242
+ */
243
+ export function filterMachinesByGroup(machines, filter = {}) {
244
+ const list = Array.isArray(machines) ? machines : []
245
+ const env = typeof filter.environment === 'string' ? filter.environment.trim().toLowerCase() : ''
246
+ const loc = typeof filter.location === 'string' ? filter.location.trim().toLowerCase() : ''
247
+ let wantTags = filter.tags
248
+ if (typeof wantTags === 'string') wantTags = wantTags.split(/[,\s]+/)
249
+ wantTags = (Array.isArray(wantTags) ? wantTags : []).map((t) => String(t).trim().toLowerCase()).filter(Boolean)
250
+ const query = typeof filter.query === 'string' ? filter.query.trim().toLowerCase() : ''
251
+ return list.filter((m) => {
252
+ if (!m) return false
253
+ if (env && String(m.environment || '').toLowerCase() !== env) return false
254
+ if (loc && String(m.location || '').toLowerCase() !== loc) return false
255
+ if (wantTags.length) {
256
+ const have = (Array.isArray(m.tags) ? m.tags : []).map((t) => String(t).toLowerCase())
257
+ if (!wantTags.some((t) => have.includes(t))) return false
258
+ }
259
+ if (query) {
260
+ const hay = [m.name, m.host, m.username, m.environment, m.location, m.description, ...(Array.isArray(m.tags) ? m.tags : [])]
261
+ .filter(Boolean).join(' ').toLowerCase()
262
+ if (!hay.includes(query)) return false
263
+ }
264
+ return true
265
+ })
266
+ }
267
+
268
+ /** Apply a machine's fields onto the live config object (pool + tools read it). */
269
+ export function applyMachine(config, m) {
270
+ if (!m) return
271
+ config.host = m.host
272
+ config.port = Number(m.port) || 22
273
+ config.username = m.username || ''
274
+ config.password = m.password || ''
275
+ config.privateKeyPath = m.privateKeyPath || ''
276
+ config.passphrase = m.passphrase || ''
277
+ // IdentityAgent / IdentitiesOnly from ~/.ssh/config (set by withSshConfig).
278
+ config.agentSocket = m.agentSocket || ''
279
+ config.identitiesOnly = !!m.identitiesOnly
280
+ config.workspace = m.workspace || (config.workspace || '')
281
+ if (m.hostKeyMode) config.hostKeyMode = m.hostKeyMode
282
+ if (typeof m.useAgent === 'boolean') config.useAgent = m.useAgent
283
+ if (typeof m.keyboardInteractive === 'boolean') config.keyboardInteractive = m.keyboardInteractive
284
+ // Issue #51: the transport backend is PER MACHINE. An enterprise box that only
285
+ // accepts Kerberos must go through the system OpenSSH backend while every
286
+ // other machine can stay on the native one, so this cannot be a purely global
287
+ // default.
288
+ if (m.backend) config.backend = m.backend
289
+ if (typeof m.sshBinary === 'string') config.sshBinary = m.sshBinary
290
+ config.proxy = m.proxy && m.proxy.host ? m.proxy : undefined
291
+ }
292
+
293
+ /** Mint a fresh machine id. */
294
+ export function machineId() {
295
+ return 'm-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 6)
296
+ }