@andy.ian/dsh-homing 2.1.4 → 2.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/SECURITY.md +90 -0
- package/bin/backup.mjs +3 -2
- package/bin/bootstrap-server.mjs +2 -1
- package/bin/inbox.mjs +46 -0
- package/bin/renew-cert.mjs +2 -1
- package/bin/window-agent-daemon.mjs +268 -0
- package/index.js +686 -87
- package/lib/compress.mjs +29 -5
- package/lib/exec-path.mjs +85 -0
- package/lib/files-page.mjs +27 -2
- package/lib/files.mjs +3 -2
- package/lib/git.mjs +5 -2
- package/lib/guard.mjs +43 -0
- package/lib/local-socket.mjs +50 -0
- package/lib/log-file.mjs +50 -0
- package/lib/node-runtime.mjs +44 -0
- package/lib/office.mjs +133 -0
- package/lib/orientation.mjs +158 -0
- package/lib/pty.mjs +6 -3
- package/lib/secret.mjs +46 -0
- package/lib/shells.mjs +2 -1
- package/lib/term-page.mjs +2 -0
- package/lib/vendor/des-vnc.mjs +266 -0
- package/lib/vendor/novnc/LICENSE.txt +62 -0
- package/lib/vendor/novnc/core/base64.js +104 -0
- package/lib/vendor/novnc/core/crypto/aes.js +178 -0
- package/lib/vendor/novnc/core/crypto/bigint.js +34 -0
- package/lib/vendor/novnc/core/crypto/crypto.js +90 -0
- package/lib/vendor/novnc/core/crypto/des.js +330 -0
- package/lib/vendor/novnc/core/crypto/dh.js +55 -0
- package/lib/vendor/novnc/core/crypto/md5.js +82 -0
- package/lib/vendor/novnc/core/crypto/rsa.js +132 -0
- package/lib/vendor/novnc/core/decoders/copyrect.js +27 -0
- package/lib/vendor/novnc/core/decoders/h264.js +321 -0
- package/lib/vendor/novnc/core/decoders/hextile.js +181 -0
- package/lib/vendor/novnc/core/decoders/jpeg.js +161 -0
- package/lib/vendor/novnc/core/decoders/raw.js +59 -0
- package/lib/vendor/novnc/core/decoders/rre.js +44 -0
- package/lib/vendor/novnc/core/decoders/tight.js +393 -0
- package/lib/vendor/novnc/core/decoders/tightpng.js +27 -0
- package/lib/vendor/novnc/core/decoders/zlib.js +51 -0
- package/lib/vendor/novnc/core/decoders/zrle.js +185 -0
- package/lib/vendor/novnc/core/deflator.js +84 -0
- package/lib/vendor/novnc/core/display.js +578 -0
- package/lib/vendor/novnc/core/encodings.js +54 -0
- package/lib/vendor/novnc/core/inflator.js +65 -0
- package/lib/vendor/novnc/core/input/domkeytable.js +311 -0
- package/lib/vendor/novnc/core/input/fixedkeys.js +129 -0
- package/lib/vendor/novnc/core/input/gesturehandler.js +567 -0
- package/lib/vendor/novnc/core/input/keyboard.js +294 -0
- package/lib/vendor/novnc/core/input/keysym.js +616 -0
- package/lib/vendor/novnc/core/input/keysymdef.js +688 -0
- package/lib/vendor/novnc/core/input/util.js +191 -0
- package/lib/vendor/novnc/core/input/vkeys.js +116 -0
- package/lib/vendor/novnc/core/input/xtscancodes.js +173 -0
- package/lib/vendor/novnc/core/ra2.js +312 -0
- package/lib/vendor/novnc/core/rfb.js +3420 -0
- package/lib/vendor/novnc/core/util/browser.js +233 -0
- package/lib/vendor/novnc/core/util/cursor.js +249 -0
- package/lib/vendor/novnc/core/util/element.js +32 -0
- package/lib/vendor/novnc/core/util/events.js +138 -0
- package/lib/vendor/novnc/core/util/eventtarget.js +35 -0
- package/lib/vendor/novnc/core/util/int.js +15 -0
- package/lib/vendor/novnc/core/util/logging.js +56 -0
- package/lib/vendor/novnc/core/util/strings.js +28 -0
- package/lib/vendor/novnc/core/websock.js +369 -0
- package/lib/vendor/novnc/vendor/pako/LICENSE +21 -0
- package/lib/vendor/novnc/vendor/pako/README.md +6 -0
- package/lib/vendor/novnc/vendor/pako/lib/utils/common.js +45 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/adler32.js +27 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/constants.js +47 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/crc32.js +36 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/deflate.js +1846 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/gzheader.js +35 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/inffast.js +324 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/inflate.js +1527 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/inftrees.js +322 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/messages.js +11 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/trees.js +1195 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/zstream.js +24 -0
- package/lib/vendor/vnc.bundle.js +14592 -0
- package/lib/vendor/ws/LICENSE +20 -0
- package/lib/vendor/ws/index.js +22 -0
- package/lib/vendor/ws/lib/buffer-util.js +142 -0
- package/lib/vendor/ws/lib/constants.js +19 -0
- package/lib/vendor/ws/lib/event-target.js +292 -0
- package/lib/vendor/ws/lib/extension.js +203 -0
- package/lib/vendor/ws/lib/limiter.js +55 -0
- package/lib/vendor/ws/lib/permessage-deflate.js +530 -0
- package/lib/vendor/ws/lib/receiver.js +743 -0
- package/lib/vendor/ws/lib/sender.js +610 -0
- package/lib/vendor/ws/lib/stream.js +161 -0
- package/lib/vendor/ws/lib/subprotocol.js +62 -0
- package/lib/vendor/ws/lib/validation.js +152 -0
- package/lib/vendor/ws/lib/websocket-server.js +562 -0
- package/lib/vendor/ws/lib/websocket.js +1426 -0
- package/lib/vendor/ws/package.json +74 -0
- package/lib/vendor/ws/wrapper.mjs +21 -0
- package/lib/vnc-app.js +567 -0
- package/lib/vnc-auth-proxy.mjs +179 -0
- package/lib/vnc-boot.js +45 -0
- package/lib/vnc-page.mjs +77 -0
- package/lib/vnc-pointer.mjs +17 -0
- package/lib/vnc.mjs +75 -0
- package/lib/window-app.js +155 -0
- package/lib/window-page.mjs +111 -0
- package/lib/window-remote.mjs +227 -0
- package/lib/ws-bridge.mjs +130 -0
- package/package.json +6 -2
- package/permissions.json +91 -0
- package/restart/client.mjs +85 -19
- package/restart/daemon/cli.mjs +55 -13
- package/restart/daemon/lib/detach.mjs +3 -2
- package/restart/daemon/lib/process-tree.mjs +17 -3
- package/restart/daemon/lib/restart-flow.mjs +36 -0
- package/restart/daemon/main.mjs +299 -80
- package/restart/engine.mjs +24 -8
- package/restart/port.mjs +88 -0
- package/restart/runner.mjs +4 -3
- package/restart/service.mjs +8 -5
- package/restart/watchdog.mjs +134 -0
|
@@ -0,0 +1,227 @@
|
|
|
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
|
+
import { findNodeRuntime } from './node-runtime.mjs'
|
|
22
|
+
|
|
23
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
|
24
|
+
const DAEMON = path.join(HERE, '..', 'bin', 'window-agent-daemon.mjs')
|
|
25
|
+
|
|
26
|
+
const isWindows = process.platform === 'win32'
|
|
27
|
+
|
|
28
|
+
const PIPE_NAME = socketName()
|
|
29
|
+
const PIPE_PATH = socketPath(PIPE_NAME)
|
|
30
|
+
const TOKEN = loadOrCreateToken()
|
|
31
|
+
|
|
32
|
+
let socket = null
|
|
33
|
+
let rl = null
|
|
34
|
+
let seq = 0
|
|
35
|
+
let pending = new Map()
|
|
36
|
+
let chain = Promise.resolve()
|
|
37
|
+
let restarts = 0
|
|
38
|
+
let connecting = null
|
|
39
|
+
|
|
40
|
+
const sleep = (ms) => new Promise((resolve) => { setTimeout(resolve, ms) })
|
|
41
|
+
|
|
42
|
+
function fail(error) {
|
|
43
|
+
for (const [, resolve] of pending) resolve({ ok: false, error: String(error?.message ?? error) })
|
|
44
|
+
pending.clear()
|
|
45
|
+
socket = null
|
|
46
|
+
rl = null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function attach(stream) {
|
|
50
|
+
socket = stream
|
|
51
|
+
rl = readline.createInterface({ input: stream })
|
|
52
|
+
rl.on('line', (line) => {
|
|
53
|
+
const text = line.trim()
|
|
54
|
+
if (!text.startsWith('{')) return
|
|
55
|
+
let message = null
|
|
56
|
+
try { message = JSON.parse(text) } catch { return }
|
|
57
|
+
const resolve = pending.get(message.id)
|
|
58
|
+
if (resolve === undefined) return
|
|
59
|
+
pending.delete(message.id)
|
|
60
|
+
resolve(message)
|
|
61
|
+
})
|
|
62
|
+
stream.on('close', () => fail(new Error('窗口代理连接已断开')))
|
|
63
|
+
stream.on('error', () => fail(new Error('窗口代理连接错误')))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function connectOnce(timeoutMs = 400) {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
const stream = net.connect(PIPE_PATH)
|
|
69
|
+
let settled = false
|
|
70
|
+
const timer = setTimeout(() => {
|
|
71
|
+
if (settled) return
|
|
72
|
+
settled = true
|
|
73
|
+
stream.destroy()
|
|
74
|
+
reject(new Error('connect timeout'))
|
|
75
|
+
}, timeoutMs)
|
|
76
|
+
stream.once('connect', () => {
|
|
77
|
+
if (settled) return
|
|
78
|
+
settled = true
|
|
79
|
+
clearTimeout(timer)
|
|
80
|
+
resolve(stream)
|
|
81
|
+
})
|
|
82
|
+
stream.once('error', (error) => {
|
|
83
|
+
if (settled) return
|
|
84
|
+
settled = true
|
|
85
|
+
clearTimeout(timer)
|
|
86
|
+
reject(error)
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function spawnDaemon() {
|
|
92
|
+
const idle = Number(process.env.DSH_AGENT_IDLE_SECONDS) || 300
|
|
93
|
+
// 守护进程是 Node:可以安全使用 detached(PowerShell 5.1 在
|
|
94
|
+
// DETACHED_PROCESS 下会直接退出,所以之前不能用 detached)。
|
|
95
|
+
const child = spawn(
|
|
96
|
+
findNodeRuntime(),
|
|
97
|
+
[DAEMON],
|
|
98
|
+
{
|
|
99
|
+
windowsHide: true,
|
|
100
|
+
detached: true,
|
|
101
|
+
stdio: 'ignore',
|
|
102
|
+
env: {
|
|
103
|
+
...process.env,
|
|
104
|
+
DSH_AGENT_PIPE: PIPE_NAME,
|
|
105
|
+
DSH_AGENT_TOKEN: TOKEN,
|
|
106
|
+
DSH_AGENT_IDLE_SECONDS: String(idle),
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
)
|
|
110
|
+
child.unref()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function ensureDaemon() {
|
|
114
|
+
if (socket !== null && !socket.destroyed) return
|
|
115
|
+
if (connecting !== null) {
|
|
116
|
+
await connecting
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
connecting = (async () => {
|
|
120
|
+
try {
|
|
121
|
+
attach(await connectOnce())
|
|
122
|
+
return
|
|
123
|
+
} catch { /* 没在跑,拉起守护进程 */ }
|
|
124
|
+
restarts += 1
|
|
125
|
+
spawnDaemon()
|
|
126
|
+
const deadline = Date.now() + 8000
|
|
127
|
+
let lastError = null
|
|
128
|
+
while (Date.now() < deadline) {
|
|
129
|
+
try {
|
|
130
|
+
attach(await connectOnce())
|
|
131
|
+
return
|
|
132
|
+
} catch (error) {
|
|
133
|
+
lastError = error
|
|
134
|
+
await sleep(150)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
throw new Error('窗口代理守护进程未启动:' + String(lastError?.message ?? lastError))
|
|
138
|
+
})()
|
|
139
|
+
try {
|
|
140
|
+
await connecting
|
|
141
|
+
} finally {
|
|
142
|
+
connecting = null
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function rawCall(request, timeoutMs) {
|
|
147
|
+
await ensureDaemon()
|
|
148
|
+
const id = String(++seq)
|
|
149
|
+
return new Promise((resolve) => {
|
|
150
|
+
const timer = setTimeout(() => {
|
|
151
|
+
pending.delete(id)
|
|
152
|
+
resolve({ id, ok: false, error: 'timeout' })
|
|
153
|
+
}, timeoutMs)
|
|
154
|
+
pending.set(id, (message) => {
|
|
155
|
+
clearTimeout(timer)
|
|
156
|
+
resolve(message)
|
|
157
|
+
})
|
|
158
|
+
try {
|
|
159
|
+
socket.write(JSON.stringify({ id, token: TOKEN, ...request }) + '\n')
|
|
160
|
+
} catch (error) {
|
|
161
|
+
clearTimeout(timer)
|
|
162
|
+
pending.delete(id)
|
|
163
|
+
resolve({ id, ok: false, error: String(error?.message ?? error) })
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 串行执行;任何异常都收敛成 {ok:false},绝不向上抛 */
|
|
169
|
+
function call(request, timeoutMs = 15_000) {
|
|
170
|
+
const next = chain
|
|
171
|
+
.then(() => rawCall(request, timeoutMs))
|
|
172
|
+
.catch((error) => ({ ok: false, error: String(error?.message ?? error) }))
|
|
173
|
+
chain = next.then(() => undefined, () => undefined)
|
|
174
|
+
return next
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export const listWindows = async () => {
|
|
178
|
+
const result = await call({ cmd: 'list' })
|
|
179
|
+
return {
|
|
180
|
+
ok: result.ok === true,
|
|
181
|
+
windows: Array.isArray(result.windows) ? result.windows : [],
|
|
182
|
+
screenWidth: Number(result.screenWidth) || 0,
|
|
183
|
+
screenHeight: Number(result.screenHeight) || 0,
|
|
184
|
+
error: result.error ?? null,
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export const captureWindow = async (hwnd, maxWidth = 1280) => {
|
|
189
|
+
const result = await call({ cmd: 'capture', hwnd: String(hwnd), maxWidth: Number(maxWidth) || 1280 }, 20_000)
|
|
190
|
+
if (result.ok !== true || typeof result.data !== 'string') {
|
|
191
|
+
return { ok: false, error: result.error ?? '抓图失败' }
|
|
192
|
+
}
|
|
193
|
+
const buffer = Buffer.from(result.data, 'base64')
|
|
194
|
+
return { ok: true, width: Number(result.width) || 0, height: Number(result.height) || 0, buffer, bytes: buffer.length }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export const clickWindow = (hwnd, x, y, button = 'left') => call({ cmd: 'click', hwnd: String(hwnd), x: Number(x) || 0, y: Number(y) || 0, button })
|
|
198
|
+
export const wheelWindow = (hwnd, delta) => call({ cmd: 'wheel', hwnd: String(hwnd), delta: Number(delta) || 0 })
|
|
199
|
+
export const keyWindow = (hwnd, { text = '', keys = [] } = {}) => call({ cmd: 'key', hwnd: String(hwnd), text: String(text), keys: Array.isArray(keys) ? keys : [] })
|
|
200
|
+
export const focusWindow = (hwnd) => call({ cmd: 'focus', hwnd: String(hwnd) })
|
|
201
|
+
export const closeWindow = (hwnd = '') => call({ cmd: 'close', hwnd: String(hwnd) })
|
|
202
|
+
export const killWindow = (hwnd) => call({ cmd: 'kill', hwnd: String(hwnd) })
|
|
203
|
+
export const rotateScreen = (mode) => call({ cmd: 'rotate', mode: String(mode) }, 20_000)
|
|
204
|
+
export const cursorPos = () => call({ cmd: 'cursor' })
|
|
205
|
+
export const openPath = (filePath) => call({ cmd: 'open', path: String(filePath) }, 20_000)
|
|
206
|
+
|
|
207
|
+
export const isSupported = () => isWindows
|
|
208
|
+
export const stats = () => ({ restarts, running: socket !== null, pipe: PIPE_PATH })
|
|
209
|
+
|
|
210
|
+
/** 关闭连接(守护进程空闲后自行退出;不杀进程,避免影响其他客户端) */
|
|
211
|
+
export function stopAgent() {
|
|
212
|
+
try {
|
|
213
|
+
if (socket !== null && !socket.destroyed) {
|
|
214
|
+
socket.write(JSON.stringify({ id: String(++seq), token: TOKEN, cmd: 'shutdown' }) + '\n')
|
|
215
|
+
socket.end()
|
|
216
|
+
}
|
|
217
|
+
} catch { /* 忽略 */ }
|
|
218
|
+
socket = null
|
|
219
|
+
rl = null
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** 主动请求守护进程退出(升级/测试用) */
|
|
223
|
+
export async function shutdownDaemon() {
|
|
224
|
+
const result = await call({ cmd: 'shutdown' }, 5_000)
|
|
225
|
+
stopAgent()
|
|
226
|
+
return result
|
|
227
|
+
}
|
|
@@ -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.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
|
|
6
6
|
"keywords": [
|
|
@@ -52,7 +52,11 @@
|
|
|
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",
|
|
58
|
+
"bin/window-agent-daemon.mjs",
|
|
59
|
+
"bin/inbox.mjs"
|
|
56
60
|
],
|
|
57
61
|
"publishConfig": {
|
|
58
62
|
"access": "public",
|
package/permissions.json
ADDED
|
@@ -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
|
+
}
|
package/restart/client.mjs
CHANGED
|
@@ -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 (
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
61
|
-
|
|
71
|
+
if (opt) opt.checked = false;
|
|
72
|
+
if (action) action.value = 'normal';
|
|
62
73
|
}
|
|
63
74
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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);
|
|
71
|
-
}
|
|
82
|
+
timer = setTimeout(reset, 5000);
|
|
83
|
+
});
|
|
72
84
|
|
|
73
|
-
|
|
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 + ' 次'
|
|
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 || '等待空闲')
|
|
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
|
-
|
|
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
|
-
|
|
220
|
+
wasOffline = true;
|
|
221
|
+
setLive('连接中断,等待服务恢复…');
|
|
156
222
|
};
|
|
157
223
|
es.onmessage = onFrame;
|
|
158
224
|
}
|