@andy.ian/dsh-homing 2.1.4 → 2.1.5

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 (120) hide show
  1. package/README.md +42 -0
  2. package/SECURITY.md +90 -0
  3. package/bin/backup.mjs +3 -2
  4. package/bin/bootstrap-server.mjs +2 -1
  5. package/bin/renew-cert.mjs +2 -1
  6. package/index.js +686 -87
  7. package/lib/compress.mjs +29 -5
  8. package/lib/exec-path.mjs +85 -0
  9. package/lib/files-page.mjs +27 -2
  10. package/lib/files.mjs +3 -2
  11. package/lib/git.mjs +5 -2
  12. package/lib/guard.mjs +43 -0
  13. package/lib/local-socket.mjs +50 -0
  14. package/lib/log-file.mjs +50 -0
  15. package/lib/node-runtime.mjs +44 -0
  16. package/lib/office.mjs +133 -0
  17. package/lib/orientation.mjs +158 -0
  18. package/lib/pty.mjs +6 -3
  19. package/lib/secret.mjs +46 -0
  20. package/lib/shells.mjs +2 -1
  21. package/lib/term-page.mjs +2 -0
  22. package/lib/vendor/des-vnc.mjs +266 -0
  23. package/lib/vendor/novnc/LICENSE.txt +62 -0
  24. package/lib/vendor/novnc/core/base64.js +104 -0
  25. package/lib/vendor/novnc/core/crypto/aes.js +178 -0
  26. package/lib/vendor/novnc/core/crypto/bigint.js +34 -0
  27. package/lib/vendor/novnc/core/crypto/crypto.js +90 -0
  28. package/lib/vendor/novnc/core/crypto/des.js +330 -0
  29. package/lib/vendor/novnc/core/crypto/dh.js +55 -0
  30. package/lib/vendor/novnc/core/crypto/md5.js +82 -0
  31. package/lib/vendor/novnc/core/crypto/rsa.js +132 -0
  32. package/lib/vendor/novnc/core/decoders/copyrect.js +27 -0
  33. package/lib/vendor/novnc/core/decoders/h264.js +321 -0
  34. package/lib/vendor/novnc/core/decoders/hextile.js +181 -0
  35. package/lib/vendor/novnc/core/decoders/jpeg.js +161 -0
  36. package/lib/vendor/novnc/core/decoders/raw.js +59 -0
  37. package/lib/vendor/novnc/core/decoders/rre.js +44 -0
  38. package/lib/vendor/novnc/core/decoders/tight.js +393 -0
  39. package/lib/vendor/novnc/core/decoders/tightpng.js +27 -0
  40. package/lib/vendor/novnc/core/decoders/zlib.js +51 -0
  41. package/lib/vendor/novnc/core/decoders/zrle.js +185 -0
  42. package/lib/vendor/novnc/core/deflator.js +84 -0
  43. package/lib/vendor/novnc/core/display.js +578 -0
  44. package/lib/vendor/novnc/core/encodings.js +54 -0
  45. package/lib/vendor/novnc/core/inflator.js +65 -0
  46. package/lib/vendor/novnc/core/input/domkeytable.js +311 -0
  47. package/lib/vendor/novnc/core/input/fixedkeys.js +129 -0
  48. package/lib/vendor/novnc/core/input/gesturehandler.js +567 -0
  49. package/lib/vendor/novnc/core/input/keyboard.js +294 -0
  50. package/lib/vendor/novnc/core/input/keysym.js +616 -0
  51. package/lib/vendor/novnc/core/input/keysymdef.js +688 -0
  52. package/lib/vendor/novnc/core/input/util.js +191 -0
  53. package/lib/vendor/novnc/core/input/vkeys.js +116 -0
  54. package/lib/vendor/novnc/core/input/xtscancodes.js +173 -0
  55. package/lib/vendor/novnc/core/ra2.js +312 -0
  56. package/lib/vendor/novnc/core/rfb.js +3420 -0
  57. package/lib/vendor/novnc/core/util/browser.js +233 -0
  58. package/lib/vendor/novnc/core/util/cursor.js +249 -0
  59. package/lib/vendor/novnc/core/util/element.js +32 -0
  60. package/lib/vendor/novnc/core/util/events.js +138 -0
  61. package/lib/vendor/novnc/core/util/eventtarget.js +35 -0
  62. package/lib/vendor/novnc/core/util/int.js +15 -0
  63. package/lib/vendor/novnc/core/util/logging.js +56 -0
  64. package/lib/vendor/novnc/core/util/strings.js +28 -0
  65. package/lib/vendor/novnc/core/websock.js +369 -0
  66. package/lib/vendor/novnc/vendor/pako/LICENSE +21 -0
  67. package/lib/vendor/novnc/vendor/pako/README.md +6 -0
  68. package/lib/vendor/novnc/vendor/pako/lib/utils/common.js +45 -0
  69. package/lib/vendor/novnc/vendor/pako/lib/zlib/adler32.js +27 -0
  70. package/lib/vendor/novnc/vendor/pako/lib/zlib/constants.js +47 -0
  71. package/lib/vendor/novnc/vendor/pako/lib/zlib/crc32.js +36 -0
  72. package/lib/vendor/novnc/vendor/pako/lib/zlib/deflate.js +1846 -0
  73. package/lib/vendor/novnc/vendor/pako/lib/zlib/gzheader.js +35 -0
  74. package/lib/vendor/novnc/vendor/pako/lib/zlib/inffast.js +324 -0
  75. package/lib/vendor/novnc/vendor/pako/lib/zlib/inflate.js +1527 -0
  76. package/lib/vendor/novnc/vendor/pako/lib/zlib/inftrees.js +322 -0
  77. package/lib/vendor/novnc/vendor/pako/lib/zlib/messages.js +11 -0
  78. package/lib/vendor/novnc/vendor/pako/lib/zlib/trees.js +1195 -0
  79. package/lib/vendor/novnc/vendor/pako/lib/zlib/zstream.js +24 -0
  80. package/lib/vendor/vnc.bundle.js +14592 -0
  81. package/lib/vendor/ws/LICENSE +20 -0
  82. package/lib/vendor/ws/index.js +22 -0
  83. package/lib/vendor/ws/lib/buffer-util.js +142 -0
  84. package/lib/vendor/ws/lib/constants.js +19 -0
  85. package/lib/vendor/ws/lib/event-target.js +292 -0
  86. package/lib/vendor/ws/lib/extension.js +203 -0
  87. package/lib/vendor/ws/lib/limiter.js +55 -0
  88. package/lib/vendor/ws/lib/permessage-deflate.js +530 -0
  89. package/lib/vendor/ws/lib/receiver.js +743 -0
  90. package/lib/vendor/ws/lib/sender.js +610 -0
  91. package/lib/vendor/ws/lib/stream.js +161 -0
  92. package/lib/vendor/ws/lib/subprotocol.js +62 -0
  93. package/lib/vendor/ws/lib/validation.js +152 -0
  94. package/lib/vendor/ws/lib/websocket-server.js +562 -0
  95. package/lib/vendor/ws/lib/websocket.js +1426 -0
  96. package/lib/vendor/ws/package.json +74 -0
  97. package/lib/vendor/ws/wrapper.mjs +21 -0
  98. package/lib/vnc-app.js +567 -0
  99. package/lib/vnc-auth-proxy.mjs +179 -0
  100. package/lib/vnc-boot.js +45 -0
  101. package/lib/vnc-page.mjs +77 -0
  102. package/lib/vnc-pointer.mjs +17 -0
  103. package/lib/vnc.mjs +75 -0
  104. package/lib/window-app.js +155 -0
  105. package/lib/window-page.mjs +111 -0
  106. package/lib/window-remote.mjs +226 -0
  107. package/lib/ws-bridge.mjs +130 -0
  108. package/package.json +4 -2
  109. package/permissions.json +91 -0
  110. package/restart/client.mjs +85 -19
  111. package/restart/daemon/cli.mjs +55 -13
  112. package/restart/daemon/lib/detach.mjs +3 -2
  113. package/restart/daemon/lib/process-tree.mjs +17 -3
  114. package/restart/daemon/lib/restart-flow.mjs +36 -0
  115. package/restart/daemon/main.mjs +299 -80
  116. package/restart/engine.mjs +24 -8
  117. package/restart/port.mjs +88 -0
  118. package/restart/runner.mjs +4 -3
  119. package/restart/service.mjs +8 -5
  120. package/restart/watchdog.mjs +134 -0
@@ -0,0 +1,226 @@
1
+ /**
2
+ * 窗口共享:本机守护进程的 Node 客户端。
3
+ *
4
+ * 架构(Node 守护进程 + 本地套接字):
5
+ * - 守护进程是 bin/window-agent-daemon.mjs(Node)
6
+ * - 套接字:Windows 命名管道 / POSIX Unix domain socket
7
+ * - 单实例由套接字名独占保证(第二个实例退出码 3)
8
+ * - 每次请求带 token(%LOCALAPPDATA%\dsh-homing\agent.token,仅本人可读)
9
+ * - 客户端连不上时以 detached 方式拉起守护进程,连上即用
10
+ * - 守护进程空闲 IdleSeconds 后自行退出,不留孤儿
11
+ *
12
+ * 一个守护进程同一时刻服务一个客户端(插件),请求在客户端侧串行。
13
+ */
14
+ import { spawn } from 'node:child_process'
15
+ import net from 'node:net'
16
+ import path from 'node:path'
17
+ import readline from 'node:readline'
18
+ import { fileURLToPath } from 'node:url'
19
+
20
+ import { loadOrCreateToken, socketName, socketPath } from './local-socket.mjs'
21
+
22
+ const HERE = path.dirname(fileURLToPath(import.meta.url))
23
+ const DAEMON = path.join(HERE, '..', 'bin', 'window-agent-daemon.mjs')
24
+
25
+ const isWindows = process.platform === 'win32'
26
+
27
+ const PIPE_NAME = socketName()
28
+ const PIPE_PATH = socketPath(PIPE_NAME)
29
+ const TOKEN = loadOrCreateToken()
30
+
31
+ let socket = null
32
+ let rl = null
33
+ let seq = 0
34
+ let pending = new Map()
35
+ let chain = Promise.resolve()
36
+ let restarts = 0
37
+ let connecting = null
38
+
39
+ const sleep = (ms) => new Promise((resolve) => { setTimeout(resolve, ms) })
40
+
41
+ function fail(error) {
42
+ for (const [, resolve] of pending) resolve({ ok: false, error: String(error?.message ?? error) })
43
+ pending.clear()
44
+ socket = null
45
+ rl = null
46
+ }
47
+
48
+ function attach(stream) {
49
+ socket = stream
50
+ rl = readline.createInterface({ input: stream })
51
+ rl.on('line', (line) => {
52
+ const text = line.trim()
53
+ if (!text.startsWith('{')) return
54
+ let message = null
55
+ try { message = JSON.parse(text) } catch { return }
56
+ const resolve = pending.get(message.id)
57
+ if (resolve === undefined) return
58
+ pending.delete(message.id)
59
+ resolve(message)
60
+ })
61
+ stream.on('close', () => fail(new Error('窗口代理连接已断开')))
62
+ stream.on('error', () => fail(new Error('窗口代理连接错误')))
63
+ }
64
+
65
+ function connectOnce(timeoutMs = 400) {
66
+ return new Promise((resolve, reject) => {
67
+ const stream = net.connect(PIPE_PATH)
68
+ let settled = false
69
+ const timer = setTimeout(() => {
70
+ if (settled) return
71
+ settled = true
72
+ stream.destroy()
73
+ reject(new Error('connect timeout'))
74
+ }, timeoutMs)
75
+ stream.once('connect', () => {
76
+ if (settled) return
77
+ settled = true
78
+ clearTimeout(timer)
79
+ resolve(stream)
80
+ })
81
+ stream.once('error', (error) => {
82
+ if (settled) return
83
+ settled = true
84
+ clearTimeout(timer)
85
+ reject(error)
86
+ })
87
+ })
88
+ }
89
+
90
+ function spawnDaemon() {
91
+ const idle = Number(process.env.DSH_AGENT_IDLE_SECONDS) || 300
92
+ // 守护进程是 Node:可以安全使用 detached(PowerShell 5.1 在
93
+ // DETACHED_PROCESS 下会直接退出,所以之前不能用 detached)。
94
+ const child = spawn(
95
+ findNodeRuntime(),
96
+ [DAEMON],
97
+ {
98
+ windowsHide: true,
99
+ detached: true,
100
+ stdio: 'ignore',
101
+ env: {
102
+ ...process.env,
103
+ DSH_AGENT_PIPE: PIPE_NAME,
104
+ DSH_AGENT_TOKEN: TOKEN,
105
+ DSH_AGENT_IDLE_SECONDS: String(idle),
106
+ },
107
+ },
108
+ )
109
+ child.unref()
110
+ }
111
+
112
+ async function ensureDaemon() {
113
+ if (socket !== null && !socket.destroyed) return
114
+ if (connecting !== null) {
115
+ await connecting
116
+ return
117
+ }
118
+ connecting = (async () => {
119
+ try {
120
+ attach(await connectOnce())
121
+ return
122
+ } catch { /* 没在跑,拉起守护进程 */ }
123
+ restarts += 1
124
+ spawnDaemon()
125
+ const deadline = Date.now() + 8000
126
+ let lastError = null
127
+ while (Date.now() < deadline) {
128
+ try {
129
+ attach(await connectOnce())
130
+ return
131
+ } catch (error) {
132
+ lastError = error
133
+ await sleep(150)
134
+ }
135
+ }
136
+ throw new Error('窗口代理守护进程未启动:' + String(lastError?.message ?? lastError))
137
+ })()
138
+ try {
139
+ await connecting
140
+ } finally {
141
+ connecting = null
142
+ }
143
+ }
144
+
145
+ async function rawCall(request, timeoutMs) {
146
+ await ensureDaemon()
147
+ const id = String(++seq)
148
+ return new Promise((resolve) => {
149
+ const timer = setTimeout(() => {
150
+ pending.delete(id)
151
+ resolve({ id, ok: false, error: 'timeout' })
152
+ }, timeoutMs)
153
+ pending.set(id, (message) => {
154
+ clearTimeout(timer)
155
+ resolve(message)
156
+ })
157
+ try {
158
+ socket.write(JSON.stringify({ id, token: TOKEN, ...request }) + '\n')
159
+ } catch (error) {
160
+ clearTimeout(timer)
161
+ pending.delete(id)
162
+ resolve({ id, ok: false, error: String(error?.message ?? error) })
163
+ }
164
+ })
165
+ }
166
+
167
+ /** 串行执行;任何异常都收敛成 {ok:false},绝不向上抛 */
168
+ function call(request, timeoutMs = 15_000) {
169
+ const next = chain
170
+ .then(() => rawCall(request, timeoutMs))
171
+ .catch((error) => ({ ok: false, error: String(error?.message ?? error) }))
172
+ chain = next.then(() => undefined, () => undefined)
173
+ return next
174
+ }
175
+
176
+ export const listWindows = async () => {
177
+ const result = await call({ cmd: 'list' })
178
+ return {
179
+ ok: result.ok === true,
180
+ windows: Array.isArray(result.windows) ? result.windows : [],
181
+ screenWidth: Number(result.screenWidth) || 0,
182
+ screenHeight: Number(result.screenHeight) || 0,
183
+ error: result.error ?? null,
184
+ }
185
+ }
186
+
187
+ export const captureWindow = async (hwnd, maxWidth = 1280) => {
188
+ const result = await call({ cmd: 'capture', hwnd: String(hwnd), maxWidth: Number(maxWidth) || 1280 }, 20_000)
189
+ if (result.ok !== true || typeof result.data !== 'string') {
190
+ return { ok: false, error: result.error ?? '抓图失败' }
191
+ }
192
+ const buffer = Buffer.from(result.data, 'base64')
193
+ return { ok: true, width: Number(result.width) || 0, height: Number(result.height) || 0, buffer, bytes: buffer.length }
194
+ }
195
+
196
+ export const clickWindow = (hwnd, x, y, button = 'left') => call({ cmd: 'click', hwnd: String(hwnd), x: Number(x) || 0, y: Number(y) || 0, button })
197
+ export const wheelWindow = (hwnd, delta) => call({ cmd: 'wheel', hwnd: String(hwnd), delta: Number(delta) || 0 })
198
+ export const keyWindow = (hwnd, { text = '', keys = [] } = {}) => call({ cmd: 'key', hwnd: String(hwnd), text: String(text), keys: Array.isArray(keys) ? keys : [] })
199
+ export const focusWindow = (hwnd) => call({ cmd: 'focus', hwnd: String(hwnd) })
200
+ export const closeWindow = (hwnd = '') => call({ cmd: 'close', hwnd: String(hwnd) })
201
+ export const killWindow = (hwnd) => call({ cmd: 'kill', hwnd: String(hwnd) })
202
+ export const rotateScreen = (mode) => call({ cmd: 'rotate', mode: String(mode) }, 20_000)
203
+ export const cursorPos = () => call({ cmd: 'cursor' })
204
+ export const openPath = (filePath) => call({ cmd: 'open', path: String(filePath) }, 20_000)
205
+
206
+ export const isSupported = () => isWindows
207
+ export const stats = () => ({ restarts, running: socket !== null, pipe: PIPE_PATH })
208
+
209
+ /** 关闭连接(守护进程空闲后自行退出;不杀进程,避免影响其他客户端) */
210
+ export function stopAgent() {
211
+ try {
212
+ if (socket !== null && !socket.destroyed) {
213
+ socket.write(JSON.stringify({ id: String(++seq), token: TOKEN, cmd: 'shutdown' }) + '\n')
214
+ socket.end()
215
+ }
216
+ } catch { /* 忽略 */ }
217
+ socket = null
218
+ rl = null
219
+ }
220
+
221
+ /** 主动请求守护进程退出(升级/测试用) */
222
+ export async function shutdownDaemon() {
223
+ const result = await call({ cmd: 'shutdown' }, 5_000)
224
+ stopAgent()
225
+ return result
226
+ }
@@ -0,0 +1,130 @@
1
+ /**
2
+ * WebSocket ⇄ TCP 桥(用于把 noVNC 的 WS 连接转到本机 VNC 的 TCP 端口)
3
+ *
4
+ * 浏览器与我们的代理之间是 WebSocket 帧;代理与 VNC 之间是裸 TCP。
5
+ * 所以必须:
6
+ * 客户端 → 服务端:解析 WS 帧,去掩码,把 payload 写进 TCP
7
+ * 服务端 → 客户端:把 TCP 字节包成「二进制、不掩码」的 WS 帧
8
+ * 直接 pipe 裸字节会让浏览器认为帧非法并断开(这正是之前的 bug)。
9
+ */
10
+
11
+ const OP_CONT = 0x0
12
+ const OP_TEXT = 0x1
13
+ const OP_BINARY = 0x2
14
+ const OP_CLOSE = 0x8
15
+ const OP_PING = 0x9
16
+ const OP_PONG = 0xa
17
+
18
+ /** 把一段 payload 包成服务端帧(不掩码) */
19
+ export function encodeFrame(payload, opcode = OP_BINARY) {
20
+ const length = payload.length
21
+ let header
22
+ if (length < 126) {
23
+ header = Buffer.from([0x80 | opcode, length])
24
+ } else if (length < 65536) {
25
+ header = Buffer.alloc(4)
26
+ header[0] = 0x80 | opcode
27
+ header[1] = 126
28
+ header.writeUInt16BE(length, 2)
29
+ } else {
30
+ header = Buffer.alloc(10)
31
+ header[0] = 0x80 | opcode
32
+ header[1] = 127
33
+ header.writeBigUInt64BE(BigInt(length), 2)
34
+ }
35
+ return Buffer.concat([header, payload])
36
+ }
37
+
38
+ function unmask(payload, mask) {
39
+ const out = Buffer.from(payload)
40
+ for (let i = 0; i < out.length; i += 1) out[i] ^= mask[i % 4]
41
+ return out
42
+ }
43
+
44
+ /**
45
+ * 桥接一个已完成 101 握手的客户端 socket 与已连接的 TCP socket。
46
+ * 返回 { close };调用方负责在握手完成后调用。
47
+ */
48
+ export function bridge(client, tcp, { onClose } = {}) {
49
+ let pending = Buffer.alloc(0)
50
+ let fragments = []
51
+ let closed = false
52
+
53
+ const finish = () => {
54
+ if (closed) return
55
+ closed = true
56
+ try { client.destroy() } catch { /* 已关闭 */ }
57
+ try { tcp.destroy() } catch { /* 已关闭 */ }
58
+ if (typeof onClose === 'function') onClose()
59
+ }
60
+
61
+ const sendControl = (opcode, payload = Buffer.alloc(0)) => {
62
+ if (closed) return
63
+ try { client.write(encodeFrame(payload, opcode)) } catch { finish() }
64
+ }
65
+
66
+ client.on('data', (chunk) => {
67
+ pending = Buffer.concat([pending, chunk])
68
+ while (pending.length >= 2) {
69
+ const b0 = pending[0]
70
+ const b1 = pending[1]
71
+ const fin = (b0 & 0x80) !== 0
72
+ const opcode = b0 & 0x0f
73
+ const masked = (b1 & 0x80) !== 0
74
+ let length = b1 & 0x7f
75
+ let offset = 2
76
+ if (length === 126) {
77
+ if (pending.length < 4) return
78
+ length = pending.readUInt16BE(2)
79
+ offset = 4
80
+ } else if (length === 127) {
81
+ if (pending.length < 10) return
82
+ const big = pending.readBigUInt64BE(2)
83
+ if (big > BigInt(16 * 1024 * 1024)) { finish(); return }
84
+ length = Number(big)
85
+ offset = 10
86
+ }
87
+ const maskLength = masked ? 4 : 0
88
+ if (pending.length < offset + maskLength + length) return
89
+ const mask = masked ? pending.subarray(offset, offset + 4) : null
90
+ const payload = pending.subarray(offset + maskLength, offset + maskLength + length)
91
+ pending = pending.subarray(offset + maskLength + length)
92
+ const data = masked ? unmask(payload, mask) : Buffer.from(payload)
93
+
94
+ if (opcode === OP_CLOSE) { finish(); return }
95
+ if (opcode === OP_PING) { sendControl(OP_PONG, data); continue }
96
+ if (opcode === OP_PONG) continue
97
+
98
+ if (opcode === OP_TEXT || opcode === OP_BINARY) {
99
+ fragments = [data]
100
+ if (fin) {
101
+ const full = Buffer.concat(fragments)
102
+ fragments = []
103
+ if (!closed) tcp.write(full)
104
+ }
105
+ continue
106
+ }
107
+ if (opcode === OP_CONT) {
108
+ fragments.push(data)
109
+ if (fin) {
110
+ const full = Buffer.concat(fragments)
111
+ fragments = []
112
+ if (!closed) tcp.write(full)
113
+ }
114
+ continue
115
+ }
116
+ }
117
+ })
118
+
119
+ tcp.on('data', (chunk) => {
120
+ if (closed) return
121
+ try { client.write(encodeFrame(chunk)) } catch { finish() }
122
+ })
123
+
124
+ client.on('error', finish)
125
+ tcp.on('error', finish)
126
+ client.on('close', finish)
127
+ tcp.on('close', finish)
128
+
129
+ return { close: finish }
130
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andy.ian/dsh-homing",
3
- "version": "2.1.4",
3
+ "version": "2.1.5",
4
4
  "type": "module",
5
5
  "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
6
6
  "keywords": [
@@ -52,7 +52,9 @@
52
52
  "lib/vendor/**",
53
53
  "restart/**",
54
54
  "!lib/vendor/node_modules/ssh2/test/**",
55
- "!lib/vendor/node_modules/ssh2/examples/**"
55
+ "!lib/vendor/node_modules/ssh2/examples/**",
56
+ "SECURITY.md",
57
+ "permissions.json"
56
58
  ],
57
59
  "publishConfig": {
58
60
  "access": "public",
@@ -0,0 +1,91 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "plugin": "@andy.ian/dsh-homing",
4
+ "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机/微信浏览器;含文件、Git、终端、桌面共享(noVNC)与窗口操作。",
5
+ "capabilities": [
6
+ {
7
+ "id": "network.inbound_bridge",
8
+ "level": "medium",
9
+ "default": "on",
10
+ "title": "本机桥接端口",
11
+ "detail": "在本机监听桥接端口(默认 8787),所有访问需登录 + TOTP + 设备白名单;入口令牌可撤销。"
12
+ },
13
+ {
14
+ "id": "network.tunnel",
15
+ "level": "high",
16
+ "default": "user-configured",
17
+ "title": "SSH 反向隧道",
18
+ "detail": "用系统 ssh 把桥接端口反向映射到用户自己的服务器;不连接第三方服务器。"
19
+ },
20
+ {
21
+ "id": "network.outbound",
22
+ "level": "medium",
23
+ "default": "on",
24
+ "title": "出站网络",
25
+ "detail": "仅连接用户配置的 SSH/服务器地址与端口;无遥测、无第三方上报。"
26
+ },
27
+ {
28
+ "id": "filesystem.read_write",
29
+ "level": "medium",
30
+ "default": "on",
31
+ "title": "文件读写",
32
+ "detail": "文件页、备份、解压、Office 预览需要;仅访问 DSH 用户可访问的路径。"
33
+ },
34
+ {
35
+ "id": "process.launch",
36
+ "level": "medium",
37
+ "default": "on",
38
+ "title": "启动进程",
39
+ "detail": "打开文件、启动终端 shell、重启守护进程;外部命令统一使用系统绝对路径。"
40
+ },
41
+ {
42
+ "id": "desktop.control",
43
+ "level": "high",
44
+ "default": "off",
45
+ "title": "桌面控制",
46
+ "detail": "窗口截图/点击/键盘输入与 VNC 桌面共享;默认关闭,需显式开启,等同于远程控制本机。"
47
+ },
48
+ {
49
+ "id": "credentials.store",
50
+ "level": "medium",
51
+ "default": "on",
52
+ "title": "凭据存储",
53
+ "detail": "登录密码哈希、TOTP 密钥、VNC 密码(DPAPI/系统钥匙串加密)、SSH 私钥路径;不保存明文密码。"
54
+ },
55
+ {
56
+ "id": "local.socket",
57
+ "level": "medium",
58
+ "default": "on",
59
+ "title": "本地套接字",
60
+ "detail": "窗口代理与重启守护进程使用本地套接字/命名管道 + token,仅当前用户可访问。"
61
+ }
62
+ ],
63
+ "dataFlows": [
64
+ {
65
+ "data": "页面/文件内容",
66
+ "direction": "phone->local",
67
+ "path": "手机浏览器 → 用户服务器 TLS → SSH 隧道 → 本机桥接端口"
68
+ },
69
+ {
70
+ "data": "登录密码",
71
+ "direction": "local-only",
72
+ "note": "本机 scrypt 校验,不落明文、不上传"
73
+ },
74
+ {
75
+ "data": "VNC 密码",
76
+ "direction": "local-only",
77
+ "note": "DPAPI/钥匙串加密存储;服务端代认证,不下发浏览器"
78
+ },
79
+ {
80
+ "data": "审计日志",
81
+ "direction": "local-only",
82
+ "path": "~/.dsh/integrations/dsh-homing/audit.log"
83
+ }
84
+ ],
85
+ "knownLimits": [
86
+ "管理员权限窗口(UIPI)不可操作",
87
+ "锁屏/UAC 安全桌面不可操作",
88
+ "RDP/多会话下只能操作当前会话",
89
+ "同用户恶意程序可读 token/注入进程,超出插件防护范围"
90
+ ]
91
+ }
@@ -13,11 +13,12 @@
13
13
  var live = document.getElementById('live');
14
14
 
15
15
  function mark(text) {
16
+ try { console.log('[restart] ' + text); } catch (error) {}
17
+ }
18
+
19
+ function setLive(text) {
16
20
  if (!live) live = document.getElementById('live');
17
- if (!live) return;
18
- steps.push(text);
19
- if (steps.length > 5) steps.shift();
20
- live.textContent = steps.join(' → ');
21
+ if (live) live.textContent = text;
21
22
  }
22
23
 
23
24
  window.onerror = function (message, source, line) {
@@ -35,6 +36,11 @@
35
36
  try { history.replaceState(null, '', location.pathname); } catch (error) {}
36
37
  }
37
38
 
39
+ var msgEl = document.getElementById('restart-msg');
40
+ var pendingMeta = null;
41
+ try { pendingMeta = document.querySelector('meta[name="restart-pending"]'); } catch (error) {}
42
+ var pendingRestart = (pendingMeta && pendingMeta.content === '1')
43
+ || !!(msgEl && msgEl.textContent && msgEl.textContent.indexOf('已排队') >= 0);
38
44
  mark('脚本 v2 已加载');
39
45
 
40
46
  var sup = document.getElementById('sup-state');
@@ -48,35 +54,82 @@
48
54
  }
49
55
  // 强制重启两步确认:首次点击只展开确认区,5 秒内不确认自动还原
50
56
  (function setupForceConfirm() {
51
- var btn = document.getElementById('btn-force');
57
+ var form = document.getElementById('restart-form');
52
58
  var box = document.getElementById('force-confirm');
53
59
  var cancel = document.getElementById('btn-force-cancel');
54
- if (!btn || !box) return;
60
+ var confirmBtn = document.getElementById('btn-force-confirm');
61
+ var opt = document.getElementById('opt-force');
62
+ var action = document.getElementById('restart-action');
63
+ if (!form || !box) return;
55
64
  var timer = null;
65
+ var armed = false;
56
66
 
57
67
  function reset() {
58
68
  if (timer) { clearTimeout(timer); timer = null; }
69
+ armed = false;
59
70
  box.style.display = 'none';
60
- btn.style.display = '';
61
- btn.disabled = false;
71
+ if (opt) opt.checked = false;
72
+ if (action) action.value = 'normal';
62
73
  }
63
74
 
64
- function expand(event) {
65
- if (event && event.preventDefault) event.preventDefault(); // 拦截提交,先确认
66
- btn.style.display = 'none';
67
- btn.disabled = true;
75
+ // 勾选了"立即强制重启"才拦截,先展开确认区;5 秒不确认自动还原
76
+ form.addEventListener('submit', function (event) {
77
+ if (!opt || !opt.checked || armed) return; // 普通重启 / 已确认 → 放行
78
+ if (event && event.preventDefault) event.preventDefault();
68
79
  box.style.display = '';
80
+ if (action) action.value = 'force';
69
81
  if (timer) clearTimeout(timer);
70
- timer = setTimeout(reset, 5000); // 5 秒不确认 → 还原
71
- }
82
+ timer = setTimeout(reset, 5000);
83
+ });
72
84
 
73
- btn.addEventListener('click', expand);
85
+ if (confirmBtn) confirmBtn.addEventListener('click', function (event) {
86
+ if (event && event.preventDefault) event.preventDefault();
87
+ armed = true;
88
+ if (timer) { clearTimeout(timer); timer = null; }
89
+ if (opt) opt.checked = true;
90
+ if (action) action.value = 'force';
91
+
92
+ // 与普通重启完全一致的体验:
93
+ // 提交后不做整页跳转、不显示全屏遮罩,直接交给页面上的 SSE 状态:
94
+ // SSE 断开 → "连接中断,等待服务恢复…"
95
+ // 服务恢复 → 自动刷新页面
96
+ var tokenInput = document.getElementById('restart-token')
97
+ var body = 'action=force&confirm=1' + (tokenInput ? '&token=' + encodeURIComponent(tokenInput.value) : '')
98
+ fetch('/dsh_homing/restart/do', {
99
+ method: 'POST',
100
+ headers: { 'content-type': 'application/x-www-form-urlencoded', 'x-dsh-fetch': '1' },
101
+ body: body,
102
+ cache: 'no-store',
103
+ keepalive: true,
104
+ }).then(function (res) {
105
+ return res.json()
106
+ }).then(function (payload) {
107
+ if (!payload || payload.ok !== true) {
108
+ setLive('强制重启失败:' + ((payload && payload.message) || '未知原因'))
109
+ reset()
110
+ return
111
+ }
112
+ // 标记:SSE 重连后自动刷新(与普通重启排队后同一逻辑)
113
+ pendingRestart = true
114
+ box.style.display = 'none'
115
+ if (timer) { clearTimeout(timer); timer = null }
116
+ setLive('重启已开始,等待服务恢复…(连接会短暂中断)')
117
+ }).catch(function () {
118
+ setLive('强制重启失败:网络异常,请重试')
119
+ reset()
120
+ })
121
+ });
74
122
  if (cancel) cancel.addEventListener('click', reset);
123
+ if (opt) opt.addEventListener('change', function () {
124
+ if (opt.checked) return;
125
+ if (box.style.display !== 'none') reset();
126
+ });
75
127
  })();
76
128
 
77
129
  mark('元素就绪');
78
130
 
79
131
  var lastFrame = Date.now();
132
+ var wasOffline = false;
80
133
  var acks = 0;
81
134
  var es = null;
82
135
  var gotFirst = false;
@@ -102,8 +155,12 @@
102
155
  return;
103
156
  }
104
157
  if (sup) {
158
+ var queueActive = state.policy && state.policy.active;
159
+ var waited = queueActive ? (state.policy.waitedSeconds || 0) : 0;
105
160
  sup.textContent = state.running
106
- ? ('运行中 · 重启 ' + state.restarts + ' 次' + (state.paused ? (' · 暂停剩 ' + state.pausedSecondsLeft + ' 秒') : ''))
161
+ ? ('运行中 · 重启 ' + state.restarts + ' 次'
162
+ + (state.paused ? (' · 暂停剩 ' + state.pausedSecondsLeft + ' 秒') : '')
163
+ + (queueActive ? (' · 排队已等 ' + waited + ' 秒') : ''))
107
164
  : '未运行';
108
165
  }
109
166
  if (policyEl && state.config) {
@@ -116,10 +173,12 @@
116
173
  }
117
174
  if (queueEl) {
118
175
  var p = state.policy;
176
+ // 排队行保持单行:只显示原因;已等秒数不再拼接,避免手机端换行
119
177
  queueEl.textContent = (p && p.active)
120
- ? ('排队中 · ' + (p.reason || '等待空闲') + '(已等 ' + (p.waitedSeconds || 0) + ' 秒)')
178
+ ? ('排队中 · ' + (p.reason || '等待空闲'))
121
179
  : '未排队';
122
180
  }
181
+ if (state.policy && state.policy.active) pendingRestart = true;
123
182
  setBusy(state);
124
183
  lastFrame = Date.now();
125
184
  if (!gotFirst) {
@@ -134,7 +193,7 @@
134
193
  body: JSON.stringify({ client: state.client, seq: state.seq, ts: state.ts }),
135
194
  });
136
195
  } catch (error) {}
137
- if (live) live.textContent = '实时更新(SSE)· 已确认 ' + acks + ' 次';
196
+ setLive('实时更新(SSE)· 已确认 ' + acks + ' 次');
138
197
  }
139
198
 
140
199
  function connect() {
@@ -150,9 +209,16 @@
150
209
  }
151
210
  es.onopen = function () {
152
211
  mark('SSE 已连接');
212
+ if (wasOffline && pendingRestart) {
213
+ setLive('服务已恢复,正在刷新…');
214
+ setTimeout(function () { location.reload(); }, 800);
215
+ return;
216
+ }
217
+ setLive('实时更新(SSE)');
153
218
  };
154
219
  es.onerror = function () {
155
- mark('SSE 出错(readyState=' + (es ? es.readyState : '?') + ')');
220
+ wasOffline = true;
221
+ setLive('连接中断,等待服务恢复…');
156
222
  };
157
223
  es.onmessage = onFrame;
158
224
  }