@andy.ian/dsh-homing 2.0.0 → 2.0.2
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 +3 -3
- package/bin/auth.mjs +2 -2
- package/bin/backup.mjs +4 -4
- package/bin/bootstrap-server.mjs +4 -4
- package/bin/entry-link.mjs +2 -2
- package/bin/renew-cert.mjs +1 -1
- package/bin/supervisor.mjs +488 -0
- package/bin/watchdog.mjs +99 -113
- package/cordis.patch.yml +4 -4
- package/index.js +197 -229
- package/lib/files-page.mjs +228 -138
- package/lib/files.mjs +89 -2
- package/lib/limits.mjs +1 -1
- package/lib/pty.mjs +10 -3
- package/package.json +4 -4
- package/lib/session.mjs +0 -216
- package/lib/shell-page.mjs +0 -430
- package/lib/shell.mjs +0 -274
- package/ops/watchdog/dsh-watchdog.ps1 +0 -105
- package/ops/watchdog/restart-now.ps1 +0 -247
package/bin/watchdog.mjs
CHANGED
|
@@ -1,158 +1,144 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* 守护进程的安装 / 状态 / 卸载命令(纯 Node,无 PowerShell、无计划任务)。
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* dsh-homing-watchdog remove # 卸载计划任务(保留脚本)
|
|
13
|
-
* dsh-homing-watchdog restart # 立刻重启 DSH(默认等空闲)
|
|
14
|
-
* dsh-homing-watchdog restart --immediate # 一空闲就重启
|
|
15
|
-
*
|
|
16
|
-
* 只支持 Windows(脚本是 PowerShell + 计划任务)。
|
|
5
|
+
* 守护逻辑在 bin/supervisor.mjs:常驻、监视 DSH、崩溃自动拉起、升级期间等待。
|
|
6
|
+
* 本命令只负责入口管理:
|
|
7
|
+
* install 写入「登录时启动守护进程」(Win 启动文件夹 / macOS LaunchAgent / Linux systemd user unit)
|
|
8
|
+
* status 向守护进程控制接口取状态
|
|
9
|
+
* remove 删除启动项
|
|
10
|
+
* restart 请求重启(默认等空闲,--immediate 立即)
|
|
11
|
+
* stop 让守护进程退出(不再拉起 DSH)
|
|
17
12
|
*/
|
|
18
13
|
import fs from 'node:fs'
|
|
19
14
|
import path from 'node:path'
|
|
20
|
-
import
|
|
15
|
+
import os from 'node:os'
|
|
16
|
+
import http from 'node:http'
|
|
21
17
|
import { fileURLToPath } from 'node:url'
|
|
22
18
|
|
|
23
19
|
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
24
20
|
const pkgRoot = path.resolve(here, '..')
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const targetDir = path.join(dshHome, 'watchdog')
|
|
21
|
+
const supervisor = path.join(pkgRoot, 'bin', 'supervisor.mjs')
|
|
22
|
+
const controlPort = Number(process.env.DSH_HOMING_CONTROL ?? 8790)
|
|
28
23
|
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
const EXE = 'powershell.exe'
|
|
24
|
+
const has = (name) => process.argv.includes('--' + name)
|
|
25
|
+
const [command] = process.argv.slice(2)
|
|
32
26
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
27
|
+
function call(pathname, method = 'POST') {
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
const req = http.request({ host: '127.0.0.1', port: controlPort, path: pathname, method, timeout: 8000 }, (res) => {
|
|
30
|
+
const chunks = []
|
|
31
|
+
res.on('data', (chunk) => chunks.push(chunk))
|
|
32
|
+
res.on('end', () => resolve({ status: res.statusCode, body: Buffer.concat(chunks).toString('utf8') }))
|
|
33
|
+
})
|
|
34
|
+
req.on('timeout', () => { req.destroy(); reject(new Error('timeout')) })
|
|
35
|
+
req.on('error', reject)
|
|
36
|
+
req.end()
|
|
37
|
+
})
|
|
43
38
|
}
|
|
44
39
|
|
|
45
|
-
function
|
|
46
|
-
if (process.platform
|
|
47
|
-
|
|
48
|
-
process.exit(1)
|
|
40
|
+
function startupPath() {
|
|
41
|
+
if (process.platform === 'win32') {
|
|
42
|
+
return path.join(process.env.APPDATA ?? '', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup', 'dsh-homing-supervisor.vbs')
|
|
49
43
|
}
|
|
44
|
+
if (process.platform === 'darwin') {
|
|
45
|
+
return path.join(os.homedir(), 'Library', 'LaunchAgents', 'online.gitsrc.dsh-homing.plist')
|
|
46
|
+
}
|
|
47
|
+
return path.join(os.homedir(), '.config', 'systemd', 'user', 'dsh-homing-supervisor.service')
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
function install() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
51
|
+
const file = startupPath()
|
|
52
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
53
|
+
if (process.platform === 'win32') {
|
|
54
|
+
// 用 wscript 启动,登录时才不会闪控制台窗口
|
|
55
|
+
const vbs = `' 由 dsh-homing 生成:登录时以隐藏窗口启动守护进程\r\nSet sh = CreateObject("WScript.Shell")\r\nsh.Run """${process.execPath}"" ""${supervisor}"" --detach", 0, False\r\n`
|
|
56
|
+
fs.writeFileSync(file, vbs, 'ascii')
|
|
57
|
+
} else if (process.platform === 'darwin') {
|
|
58
|
+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
59
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
60
|
+
<plist version="1.0"><dict>
|
|
61
|
+
<key>Label</key><string>online.gitsrc.dsh-homing</string>
|
|
62
|
+
<key>ProgramArguments</key><array><string>${process.execPath}</string><string>${supervisor}</string><string>--detach</string></array>
|
|
63
|
+
<key>RunAtLoad</key><true/>
|
|
64
|
+
</dict></plist>
|
|
65
|
+
`
|
|
66
|
+
fs.writeFileSync(file, xml, 'utf8')
|
|
67
|
+
} else {
|
|
68
|
+
const text = `[Unit]
|
|
69
|
+
Description=DSH Homing supervisor
|
|
64
70
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
Register-ScheduledTask -TaskName '${TASK_WATCHDOG}' -Action $action -Trigger $trigger1,$trigger2 -Settings $settings -Principal $principal -Force | Out-Null
|
|
73
|
-
Write-Output 'registered'
|
|
74
|
-
`
|
|
75
|
-
const registered = ps(register)
|
|
76
|
-
if (!String(registered.stdout).includes('registered')) {
|
|
77
|
-
console.error('注册看门狗计划任务失败:' + (registered.stderr || registered.stdout || '').slice(0, 400))
|
|
78
|
-
process.exit(1)
|
|
71
|
+
[Service]
|
|
72
|
+
ExecStart=${process.execPath} ${supervisor} --detach
|
|
73
|
+
|
|
74
|
+
[Install]
|
|
75
|
+
WantedBy=default.target
|
|
76
|
+
`
|
|
77
|
+
fs.writeFileSync(file, text, 'utf8')
|
|
79
78
|
}
|
|
80
|
-
console.log(` ✓
|
|
81
|
-
console.log('
|
|
82
|
-
console.log('
|
|
83
|
-
console.log(' dsh-homing-watchdog
|
|
84
|
-
console.log(' dsh-homing-watchdog restart --immediate
|
|
79
|
+
console.log(` ✓ 已写入启动项:${file}`)
|
|
80
|
+
if (process.platform === 'linux') console.log(' 启用:systemctl --user enable --now dsh-homing-supervisor')
|
|
81
|
+
console.log('\n常用命令:')
|
|
82
|
+
console.log(' dsh-homing-watchdog status')
|
|
83
|
+
console.log(' dsh-homing-watchdog restart [--immediate]')
|
|
84
|
+
console.log(' dsh-homing-watchdog stop')
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
function status() {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
async function status() {
|
|
88
|
+
try {
|
|
89
|
+
const res = await call('/status', 'GET')
|
|
90
|
+
console.log(res.body.trim())
|
|
91
|
+
} catch {
|
|
92
|
+
console.error('守护进程没在运行(控制端口连不上)。先 dsh-homing-watchdog install,或手动运行 supervisor.mjs --detach。')
|
|
92
93
|
process.exit(1)
|
|
93
94
|
}
|
|
94
|
-
const result = psFile(restart, ['-Check'])
|
|
95
|
-
const text = String(result.stdout || '').replace(/^\uFEFF/u, '')
|
|
96
|
-
if (text.trim() === '') {
|
|
97
|
-
console.error('读取状态失败:' + (result.stderr || '').slice(0, 300))
|
|
98
|
-
process.exit(1)
|
|
99
|
-
}
|
|
100
|
-
console.log(text.trim())
|
|
101
95
|
}
|
|
102
96
|
|
|
103
97
|
function remove() {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
console.log(` ✓
|
|
98
|
+
const file = startupPath()
|
|
99
|
+
if (fs.existsSync(file)) {
|
|
100
|
+
fs.rmSync(file, { force: true })
|
|
101
|
+
console.log(` ✓ 已删除启动项:${file}`)
|
|
108
102
|
} else {
|
|
109
|
-
console.
|
|
110
|
-
process.exit(1)
|
|
103
|
+
console.log(' 启动项不存在')
|
|
111
104
|
}
|
|
112
105
|
}
|
|
113
106
|
|
|
114
|
-
function restart(
|
|
115
|
-
requireWindows()
|
|
116
|
-
const script = path.join(targetDir, 'restart-now.ps1')
|
|
117
|
-
if (!fs.existsSync(script)) {
|
|
118
|
-
console.error('还没安装:先跑 dsh-homing-watchdog install')
|
|
119
|
-
process.exit(1)
|
|
120
|
-
}
|
|
107
|
+
async function restart(immediate) {
|
|
121
108
|
const mode = immediate ? 'immediate' : 'default'
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 30)
|
|
128
|
-
$principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\\$env:USERNAME" -LogonType Interactive -RunLevel Limited
|
|
129
|
-
Register-ScheduledTask -TaskName '${TASK_RESTART}' -Action $action -Trigger $trigger -Settings $settings -Principal $principal -Force | Out-Null
|
|
130
|
-
Write-Output 'queued'
|
|
131
|
-
`
|
|
132
|
-
const result = ps(cmd)
|
|
133
|
-
if (String(result.stdout).includes('queued')) {
|
|
134
|
-
console.log(`已排队(模式 ${mode}),5 秒后开始等空闲;日志:${path.join(targetDir, 'restart.log')}`)
|
|
135
|
-
} else {
|
|
136
|
-
console.error('排队失败:' + (result.stderr || '').slice(0, 300))
|
|
109
|
+
try {
|
|
110
|
+
const res = await call(`/restart?mode=${mode}`)
|
|
111
|
+
console.log(res.status === 202 ? ` 已请求重启(模式 ${mode})` : ` 请求被拒绝(HTTP ${res.status})`)
|
|
112
|
+
} catch {
|
|
113
|
+
console.error('守护进程没在运行。先 install 或手动启动 supervisor.mjs --detach。')
|
|
137
114
|
process.exit(1)
|
|
138
115
|
}
|
|
139
116
|
}
|
|
140
117
|
|
|
141
|
-
|
|
142
|
-
|
|
118
|
+
async function stop() {
|
|
119
|
+
try {
|
|
120
|
+
await call('/stop')
|
|
121
|
+
console.log(' 守护进程已退出(不会自动拉起 DSH)')
|
|
122
|
+
} catch {
|
|
123
|
+
console.error('守护进程没在运行')
|
|
124
|
+
process.exit(1)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
143
127
|
|
|
144
128
|
switch (command) {
|
|
145
129
|
case 'install': install(); break
|
|
146
|
-
case 'status': status(); break
|
|
130
|
+
case 'status': await status(); break
|
|
147
131
|
case 'remove': remove(); break
|
|
148
|
-
case 'restart': restart(
|
|
132
|
+
case 'restart': await restart(has('--immediate') || has('-i')); break
|
|
133
|
+
case 'stop': await stop(); break
|
|
149
134
|
default:
|
|
150
|
-
console.log(`用法: dsh-homing-watchdog <install|status|remove|restart> [--immediate]
|
|
135
|
+
console.log(`用法: dsh-homing-watchdog <install|status|remove|restart|stop> [--immediate]
|
|
151
136
|
|
|
152
|
-
install
|
|
153
|
-
status
|
|
154
|
-
remove
|
|
155
|
-
restart
|
|
137
|
+
install 写入登录自启(Windows 启动项 / macOS LaunchAgent / Linux systemd user unit)
|
|
138
|
+
status 向守护进程查询状态
|
|
139
|
+
remove 删除登录自启
|
|
140
|
+
restart 请求重启 DSH(默认等空闲,--immediate 立即)
|
|
141
|
+
stop 让守护进程退出(不再拉起 DSH)
|
|
156
142
|
`)
|
|
157
143
|
process.exit(command === undefined ? 0 : 1)
|
|
158
144
|
}
|
package/cordis.patch.yml
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# dsh-
|
|
1
|
+
# dsh-homing:把本机 DSH 开放给手机/微信(随 DSH 启动)
|
|
2
2
|
#
|
|
3
3
|
# 配置说明:
|
|
4
4
|
# serverHost 云服务器 IP(留空则只在本机/局域网提供服务,不起隧道)
|
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
# bindAddress 本机监听地址;'::' 表示局域网 + 回环都监听(默认)
|
|
10
10
|
# publicUrl 手机通过公网访问的入口,只用于启动日志
|
|
11
11
|
# funcValue 入口暗号:必须带 ?func=<这个值>,留空则关闭该闸门
|
|
12
|
-
# login 是否要求用户名密码登录(账号密码存在 $DSH_HOME/integrations/dsh-
|
|
12
|
+
# login 是否要求用户名密码登录(账号密码存在 $DSH_HOME/integrations/dsh-homing/auth.json)
|
|
13
13
|
# tls 'auto' = 自动生成自签证书(局域网也是 HTTPS);'files' = 用 tlsCert/tlsKey;'off' = 明文(仅本机调试)
|
|
14
14
|
# tlsCert/tlsKey tls=files 时的证书与私钥路径
|
|
15
15
|
- insert:
|
|
16
|
-
- id: dsh-
|
|
16
|
+
- id: dsh-homing
|
|
17
17
|
name: '@andy.ian/dsh-homing'
|
|
18
18
|
config:
|
|
19
|
-
serverHost: '' # 服务器地址建议写在 $DSH_HOME/integrations/dsh-
|
|
19
|
+
serverHost: '' # 服务器地址建议写在 $DSH_HOME/integrations/dsh-homing/settings.json,改完自动重连
|
|
20
20
|
sshUser: ubuntu
|
|
21
21
|
sshPort: 22
|
|
22
22
|
sshKey: ''
|