dsh-remote 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -28,7 +28,7 @@ After installation or an upgrade, restart DSH Web and refresh the page. Check au
28
28
 
29
29
  ## Start in 3 steps
30
30
 
31
- 1. **Configure a host**: open Settings → Remote workspaces to edit SSH configuration or save credentials. Connectivity checks run automatically; no manual connection step is required.
31
+ 1. **Configure a host**: open Settings → Remote workspaces to edit SSH configuration or save credentials. Hosts already in `~/.ssh/config` are listed there — open the row's `…` menu and choose **Edit** to save it as a machine and fill in its login password (the entry is the same `…` menu before and after saving). Connectivity checks run automatically; no manual connection step is required.
32
32
  2. **Create a workspace**: Add workspace → Remote, then choose a host and folder. Each workspace binds its own host; settings have no folder selection or global active host.
33
33
  3. **Develop remotely**: open workspace files or enter IDE mode to edit with syntax highlighting, search, run commands and collaborate with the agent. Files open ready to edit.
34
34
 
@@ -62,7 +62,7 @@ The terminal starts in the session's remote directory. A pixel fish welcomes you
62
62
 
63
63
  ### Port forwarding (SSH tunnels)
64
64
 
65
- Open the listener on the side that initiates the connection: a listener here is a local forward (`ssh -L`); one on the remote is a reverse forward (`ssh -R`). Run several at once, and tick auto reconnect per row to restore only the forwards that were running, scoped to their machine. Enabling it for a reverse forward first warns that the remote will listen again.
65
+ Open the listener on the side that initiates the connection: a listener here is a local forward (`ssh -L`); one on the remote is a reverse forward (`ssh -R`). Run several at once. Each forward names its **host**, so you can define a tunnel for a machine that is not connected yet and start it once that machine is. Every forward can be edited in place — direction, ports, target or owning host; a running one is stopped first and restarted with the new settings. Tick auto reconnect per row to restore only the forwards that were running, scoped to their machine — enabling it for a reverse forward first warns that the remote will listen again.
66
66
 
67
67
  Upgrading from an older version: auto-reconnect entries that predate `lastActive` are paused and marked "Re-confirm" in the row; tick them again to opt back in (reverse forwards also need the remote-listening warning acknowledged).
68
68
 
package/README.md CHANGED
@@ -28,7 +28,7 @@ dsh plugin --profile web add dsh-remote
28
28
 
29
29
  ## 3 步开始
30
30
 
31
- 1. **配置主机**:设置 → 远程工作区,编辑 SSH 配置或保存主机凭据;打开设置会自动检查连通性,无需手动“连接”。
31
+ 1. **配置主机**:设置 → 远程工作区,编辑 SSH 配置或保存主机凭据;直接写在 `~/.ssh/config` 里的 Host 会出现在主机列表中,点该行的「…」菜单 →「编辑」即可保存成机器并填写登录密码(保存前后入口都是同一个「…」菜单)。打开设置会自动检查连通性,无需手动“连接”。
32
32
  2. **创建工作区**:添加工作区 → 远程,选择主机与目录。每个工作区独立绑定主机,设置页不选目录或“当前远程机”。
33
33
  3. **开始开发**:打开工作区文件或切换 IDE 模式,文件打开即可编辑并显示语法高亮,在远端搜索、运行命令并与 Agent 协作。
34
34
 
@@ -62,7 +62,7 @@ dsh plugin --profile web add dsh-remote
62
62
 
63
63
  ### 端口转发(SSH 隧道)
64
64
 
65
- 谁要发起访问,就在谁那边开监听口:监听口开在本机是本地转发(`ssh -L`),开在远端是远程转发(`ssh -R`)。可同时运行多条;每行能直接勾选「自动重连」——只恢复上次运行中的转发,且按机器隔离,反向转发首次勾选会提示远端监听风险。
65
+ 谁要发起访问,就在谁那边开监听口:监听口开在本机是本地转发(`ssh -L`),开在远端是远程转发(`ssh -R`)。可同时运行多条;添加转发时可以选择「主机」,每条转发只属于一台机器——给还没连接的主机也能先存下转发,连上后再启动。每条转发都可以点「编辑」调整方向、端口、目标或所属主机,运行中的会先停止、保存后按新配置重新启动。每行能直接勾选「自动重连」——只恢复上次运行中的转发,且按机器隔离,反向转发首次勾选会提示远端监听风险。
66
66
 
67
67
  从旧版本升级:旧版没有 `lastActive` 的「自动重连」配置会暂停,行上显示「需重新确认」;重新勾选后才会参与自动恢复(反向转发需重新确认远端监听)。
68
68
 
package/lib/binding.js CHANGED
@@ -11,7 +11,7 @@
11
11
  // `username`/`remotePath`) when the workspace is picked, so a session's cwd
12
12
  // alone identifies both the machine to connect to and the workspace root.
13
13
 
14
- import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
14
+ import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from 'node:fs'
15
15
  import path from 'node:path'
16
16
  import zlib from 'node:zlib'
17
17
 
@@ -19,8 +19,37 @@ import zlib from 'node:zlib'
19
19
  const norm = (p) => path.resolve(String(p || '')).replace(/[\\/]+$/, '') || ''
20
20
 
21
21
  /** Whether `base` is `dir` itself or nested under it (separator-aware, so
22
- * `/a/mirror-2` is NOT treated as living inside `/a/mirror`). */
23
- const isUnder = (base, dir) => base === dir || base.startsWith(dir + path.sep) || base.startsWith(dir + '/')
22
+ * `/a/mirror-2` is NOT treated as living inside `/a/mirror`). Windows paths
23
+ * compare case-insensitively: the same directory can be spelled `F:\\Work`
24
+ * and `f:\\work`, and a drive letter's case is not stable across APIs. */
25
+ const samePath = (a, b) => (process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b)
26
+ const isUnder = (base, dir) => {
27
+ const b = process.platform === 'win32' ? base.toLowerCase() : base
28
+ const d = process.platform === 'win32' ? dir.toLowerCase() : dir
29
+ return b === d || b.startsWith(d + path.sep) || b.startsWith(d + '/')
30
+ }
31
+
32
+ /** Real path when the OS can resolve it, else the normalized spelling.
33
+ * `path.resolve` is purely lexical: it never resolves a junction/symlink, so
34
+ * the same physical directory can arrive spelled two different ways. */
35
+ const tryRealpath = (p) => { try { return norm(realpathSync(p)) } catch { return norm(p) } }
36
+
37
+ /** Path depth, used to pick the deepest matching mirror regardless of spelling. */
38
+ const depth = (p) => p.split(/[\\/]+/).filter(Boolean).length
39
+
40
+ /** Re-spell `mirrorReal` the way `base` spells the SAME directory: step up
41
+ * from `base` once per segment `baseReal` sits below `mirrorReal`. Walking
42
+ * with path.dirname keeps the platform's root intact ('/' on POSIX, 'C:\\'
43
+ * or a UNC share on Windows) — rebuilding the string from segments loses it.
44
+ * This keeps a resolved mirror path comparable with the session cwd it came
45
+ * from, even when only one of the two was written through a link. */
46
+ function spellLike(base, mirrorReal, baseReal) {
47
+ const rel = path.relative(baseReal, mirrorReal)
48
+ if (!rel) return base
49
+ let out = base
50
+ for (let i = 0; i < rel.split(/[\\/]+/).filter(Boolean).length; i++) out = path.dirname(out)
51
+ return out
52
+ }
24
53
 
25
54
  /**
26
55
  * Resolve which remote workspace a LOCAL path belongs to by scanning the mirror
@@ -50,11 +79,60 @@ export function resolveMirror(local, root) {
50
79
  let machine = null
51
80
  if (!local || !root || !existsSync(root)) return { mirrorDir, remotePath, machine }
52
81
  const base = norm(local)
82
+ const realBase = tryRealpath(local)
83
+ // Issue #173: the registry root is spelled the way DSH_HOME is written
84
+ // (`~/.dsh/...`), while a session's cwd can be spelled through a junction or
85
+ // symlink to that same directory (or the reverse). A purely lexical prefix
86
+ // comparison then says "not inside any mirror" and every rw_* tool refuses
87
+ // the session as LOCAL. Scanning both spellings keeps a plain prefix test
88
+ // usable and lets the reported mirrorDir keep the cwd's own spelling.
89
+ const scanRoots = [norm(root)]
90
+ const realRoot = tryRealpath(root)
91
+ if (!samePath(realRoot, scanRoots[0])) scanRoots.push(realRoot)
92
+ const seen = new Set()
93
+ for (const scanRoot of scanRoots) {
94
+ for (const mirror of mirrorsUnder(scanRoot)) {
95
+ // Prefer the spelling the cwd already uses; fall back to resolving the
96
+ // mirror itself when only the mirror's tail is a link.
97
+ const resolved = isUnder(base, mirror.dir)
98
+ ? mirror.dir
99
+ : (isUnder(realBase, tryRealpath(mirror.dir)) ? spellLike(base, tryRealpath(mirror.dir), realBase) : null)
100
+ if (!resolved) continue
101
+ const key = process.platform === 'win32' ? resolved.toLowerCase() : resolved
102
+ if (seen.has(key)) continue
103
+ // Deepest match wins; the same physical mirror seen under two spellings
104
+ // must not be reported twice.
105
+ if (mirrorDir && depth(resolved) <= depth(mirrorDir)) continue
106
+ let meta
107
+ try {
108
+ meta = JSON.parse(readFileSync(mirror.metaPath, 'utf8'))
109
+ } catch { continue /* unparsable meta → not a usable binding */ }
110
+ if (!meta || !meta.host) continue
111
+ seen.add(key)
112
+ mirrorDir = resolved
113
+ remotePath = String(meta.remotePath || '')
114
+ machine = {
115
+ host: String(meta.host),
116
+ port: Number(meta.port) || 22,
117
+ username: String(meta.username || ''),
118
+ // The ~/.ssh/config alias this mirror was created through, when the
119
+ // machine was saved as one (issue #38): it keeps the mirror bound to
120
+ // its machine even if that alias's HostName/port later change.
121
+ alias: String(meta.alias || ''),
122
+ }
123
+ }
124
+ }
125
+ return { mirrorDir, remotePath, machine }
126
+ }
127
+
128
+ /** Every mirror directory (one that carries a meta file) directly under `root`. */
129
+ function mirrorsUnder(root) {
130
+ const found = []
53
131
  let hostDirs
54
132
  try {
55
133
  hostDirs = readdirSync(root)
56
134
  } catch {
57
- return { mirrorDir, remotePath, machine }
135
+ return found
58
136
  }
59
137
  for (const hostDir of hostDirs) {
60
138
  const hostPath = path.join(root, hostDir)
@@ -66,30 +144,12 @@ export function resolveMirror(local, root) {
66
144
  continue
67
145
  }
68
146
  for (const entry of entries) {
69
- const metaPath = path.join(hostPath, entry, '.dsh-remote-meta.json')
70
- if (!existsSync(metaPath)) continue
71
- const mirrorAbs = norm(path.join(hostPath, entry))
72
- if (!isUnder(base, mirrorAbs)) continue
73
- // Deepest match wins; an equal-length path cannot occur twice.
74
- if (mirrorDir && mirrorAbs.length <= mirrorDir.length) continue
75
- try {
76
- const meta = JSON.parse(readFileSync(metaPath, 'utf8'))
77
- if (!meta || !meta.host) continue
78
- mirrorDir = mirrorAbs
79
- remotePath = String(meta.remotePath || '')
80
- machine = {
81
- host: String(meta.host),
82
- port: Number(meta.port) || 22,
83
- username: String(meta.username || ''),
84
- // The ~/.ssh/config alias this mirror was created through, when the
85
- // machine was saved as one (issue #38): it keeps the mirror bound to
86
- // its machine even if that alias's HostName/port later change.
87
- alias: String(meta.alias || ''),
88
- }
89
- } catch { /* unparsable meta → not a usable binding */ }
147
+ const dir = norm(path.join(hostPath, entry))
148
+ const metaPath = path.join(dir, '.dsh-remote-meta.json')
149
+ if (existsSync(metaPath)) found.push({ dir, metaPath })
90
150
  }
91
151
  }
92
- return { mirrorDir, remotePath, machine }
152
+ return found
93
153
  }
94
154
 
95
155
  /**