dsh-quay-bot 1.0.2 → 1.0.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 +28 -105
- package/botscreen/requirements.txt +1 -1
- package/client.js +123 -53
- package/cordis.patch.yml +1 -1
- package/host/api.js +4 -0
- package/host/remote.js +285 -8
- package/host/screen.js +22 -86
- package/package.json +34 -32
- package/remote/bin/dsh-remote.mjs +435 -0
- package/remote/package.json +27 -0
- package/remote/src/config.mjs +183 -0
- package/remote/src/crypto.mjs +220 -0
- package/remote/src/keys.mjs +226 -0
- package/remote/src/proxy.mjs +1028 -0
- package/remote/src/sessions.mjs +163 -0
- package/remote/src/shim.js +838 -0
- package/remote/src/supervisor.mjs +207 -0
- package/remote/src/tunnel.mjs +533 -0
- package/remote/src/upstream.mjs +403 -0
- package/remote/src/util.mjs +149 -0
- package/remote/src/ws.mjs +313 -0
- package/LICENSE +0 -21
- package/avatar/build.mjs +0 -81
- package/avatar/gallery/droid.json +0 -14
- package/avatar/gallery/ghost.json +0 -14
- package/avatar/gallery/grok-pod.json +0 -14
- package/avatar/gallery/guardian.json +0 -14
- package/avatar/gallery/mecha.json +0 -14
- package/avatar/gallery/mochi.json +0 -14
- package/avatar/gallery/neko.json +0 -14
- package/avatar/gallery/orbiter.json +0 -14
- package/avatar/gallery/relay.json +0 -14
- package/avatar/gallery/spark.json +0 -14
- package/avatar/src/bot/clay.ts +0 -135
- package/avatar/src/bot/effects.ts +0 -100
- package/avatar/src/bot/grok-bot.ts +0 -626
- package/avatar/src/bot/index.ts +0 -15
- package/avatar/src/bot/palettes.ts +0 -62
- package/avatar/src/bot/ring.ts +0 -219
- package/avatar/src/bot/scheduler.ts +0 -239
- package/avatar/src/bot/shapes.ts +0 -277
- package/avatar/src/bot/spring.ts +0 -196
- package/avatar/src/core/color.ts +0 -92
- package/avatar/src/core/eyes.ts +0 -297
- package/avatar/src/core/field.ts +0 -482
- package/avatar/src/core/frame.ts +0 -655
- package/avatar/src/core/fx.ts +0 -375
- package/avatar/src/core/index.ts +0 -49
- package/avatar/src/core/paint.ts +0 -331
- package/avatar/src/core/path.ts +0 -495
- package/avatar/src/core/pose.ts +0 -581
- package/avatar/src/core/svg.ts +0 -98
- package/avatar/src/core/types.ts +0 -153
- package/avatar/src/core/validate.ts +0 -296
- package/avatar/src/core/volume.ts +0 -681
- package/avatar/src/entry.ts +0 -260
- package/bin/doctor.mjs +0 -148
- package/botscreen/requirements-offline.txt +0 -11
- package/market/index.json +0 -41
- package/vendor/pip/pip-25.1.1-py3-none-any.whl +0 -0
- package/vendor/pip/setuptools-78.1.1-py3-none-any.whl +0 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supervisor:把 KeyRing / SessionStore / Upstream / Edge / Tunnel 串成一条链路,并负责:
|
|
3
|
+
* - 上游 launch token 与 cookie 的自动续期;
|
|
4
|
+
* - 对外 PSK 的定时轮换 + 向在线会话推送新密钥;
|
|
5
|
+
* - 隧道 URL 变化后重新生成链接;
|
|
6
|
+
* - 运行态落盘(runtime.json / link.txt)与优雅退出。
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, rmSync, writeFileSync } from 'node:fs'
|
|
9
|
+
import { join } from 'node:path'
|
|
10
|
+
import { assertConfig } from './config.mjs'
|
|
11
|
+
import { KeyRing, keyRingPath } from './keys.mjs'
|
|
12
|
+
import { SessionStore } from './sessions.mjs'
|
|
13
|
+
import { Upstream } from './upstream.mjs'
|
|
14
|
+
import { createEdge } from './proxy.mjs'
|
|
15
|
+
import { Tunnel } from './tunnel.mjs'
|
|
16
|
+
import { ensureDir, httpRequest, writeJsonAtomic } from './util.mjs'
|
|
17
|
+
|
|
18
|
+
export async function startSupervisor({ config, logger }) {
|
|
19
|
+
assertConfig(config)
|
|
20
|
+
ensureDir(config.stateDir)
|
|
21
|
+
ensureDir(config.logs.dir)
|
|
22
|
+
|
|
23
|
+
const runtimePath = join(config.stateDir, 'runtime.json')
|
|
24
|
+
const linkPath = join(config.home, 'link.txt')
|
|
25
|
+
|
|
26
|
+
const keys = new KeyRing({
|
|
27
|
+
statePath: keyRingPath(config.stateDir),
|
|
28
|
+
logger,
|
|
29
|
+
rotateHours: config.tokens.rotateHours,
|
|
30
|
+
graceMinutes: config.tokens.graceMinutes,
|
|
31
|
+
relayRotateHours: config.e2e.relayKeyRotateHours,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const sessions = new SessionStore({
|
|
35
|
+
ttlMs: config.e2e.sessionTtlHours * 3_600_000,
|
|
36
|
+
challengeTtlMs: config.e2e.handshakeTtlSeconds * 1_000,
|
|
37
|
+
logger,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const upstream = new Upstream({ config, logger })
|
|
41
|
+
await upstream.start()
|
|
42
|
+
const upstreamReady = await upstream.waitReady({ timeoutMs: 20_000 })
|
|
43
|
+
if (!upstreamReady) logger.warn('upstream not ready yet; relay will keep retrying', { log: config.upstream.log })
|
|
44
|
+
|
|
45
|
+
const edge = createEdge({ config, keys, sessions, upstream, logger })
|
|
46
|
+
const address = await edge.listen()
|
|
47
|
+
logger.info('relay listening', { address: `${address.address}:${String(address.port)}` })
|
|
48
|
+
|
|
49
|
+
const runtime = {
|
|
50
|
+
pid: process.pid,
|
|
51
|
+
startedAt: new Date().toISOString(),
|
|
52
|
+
version: 1,
|
|
53
|
+
listen: { host: config.listen.host, port: address.port },
|
|
54
|
+
publicUrl: null,
|
|
55
|
+
link: null,
|
|
56
|
+
appLink: null,
|
|
57
|
+
tunnel: null,
|
|
58
|
+
pskKid: keys.activePsk()?.kid ?? null,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const persist = () => {
|
|
62
|
+
const link = runtime.publicUrl ? keys.link(runtime.publicUrl) : null
|
|
63
|
+
runtime.link = link
|
|
64
|
+
runtime.pskKid = keys.activePsk()?.kid ?? null
|
|
65
|
+
runtime.tunnel = tunnel.status()
|
|
66
|
+
// App 直连通道只在使用者显式开启时暴露;token 长期复用,避免已连接设备失效
|
|
67
|
+
runtime.appLink = config.proxy.appBridge && runtime.publicUrl ? keys.appLink(runtime.publicUrl) : null
|
|
68
|
+
writeJsonAtomic(runtimePath, runtime)
|
|
69
|
+
if (link) {
|
|
70
|
+
writeFileSync(linkPath, `${link}\n`, { mode: 0o600 })
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const tunnel = new Tunnel({
|
|
75
|
+
config,
|
|
76
|
+
logger,
|
|
77
|
+
onUrl: (url) => {
|
|
78
|
+
runtime.publicUrl = url
|
|
79
|
+
persist()
|
|
80
|
+
logger.info('public endpoint ready', { url, link: runtime.link })
|
|
81
|
+
},
|
|
82
|
+
onState: (state) => {
|
|
83
|
+
runtime.tunnelState = state
|
|
84
|
+
persist()
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
let rotationTimer = null
|
|
89
|
+
let renewTimer = null
|
|
90
|
+
|
|
91
|
+
const startRotation = () => {
|
|
92
|
+
rotationTimer = setInterval(() => {
|
|
93
|
+
try {
|
|
94
|
+
keys.reload()
|
|
95
|
+
if (config.proxy.appBridge && runtime.publicUrl) {
|
|
96
|
+
const appLink = keys.appLink(runtime.publicUrl)
|
|
97
|
+
if (appLink !== runtime.appLink) {
|
|
98
|
+
runtime.appLink = appLink
|
|
99
|
+
persist()
|
|
100
|
+
logger.info('app link rotated', { appLink })
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const next = keys.rotatePsk()
|
|
104
|
+
if (next && next.kid !== runtime.pskKid) {
|
|
105
|
+
// 推送新 PSK:WS 在线会话立即收到,HTTP 会话在下一次响应元数据里收到
|
|
106
|
+
for (const session of sessions.all()) {
|
|
107
|
+
sessions.setRenewal(session, next)
|
|
108
|
+
edge.pushRenewal(session, { kid: next.kid, psk: next.psk })
|
|
109
|
+
}
|
|
110
|
+
persist()
|
|
111
|
+
logger.info('psk rotated and pushed', { kid: next.kid, sessions: sessions.size })
|
|
112
|
+
}
|
|
113
|
+
keys.rotateRelayIdentityIfDue()
|
|
114
|
+
} catch (error) {
|
|
115
|
+
logger.error('rotation failed', { error: String(error?.message ?? error) })
|
|
116
|
+
}
|
|
117
|
+
}, 60_000)
|
|
118
|
+
rotationTimer.unref?.()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const startUpstreamWatch = () => {
|
|
122
|
+
renewTimer = setInterval(async () => {
|
|
123
|
+
if (!upstream.ready) return
|
|
124
|
+
const exp = upstream.info.cookieExpiresAt
|
|
125
|
+
if (!exp) return
|
|
126
|
+
if (exp - Date.now() < config.upstream.cookieRenewSkewSeconds * 1000) {
|
|
127
|
+
try {
|
|
128
|
+
await upstream.ensure({ force: true })
|
|
129
|
+
} catch (error) {
|
|
130
|
+
logger.warn('scheduled upstream renewal failed', { error: String(error?.message ?? error) })
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}, 30_000)
|
|
134
|
+
renewTimer.unref?.()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const shutdown = async (signal = 'manual') => {
|
|
138
|
+
logger.info('shutting down', { signal })
|
|
139
|
+
if (existsSync(runtimePath)) rmSync(runtimePath, { force: true })
|
|
140
|
+
clearInterval(rotationTimer)
|
|
141
|
+
clearInterval(renewTimer)
|
|
142
|
+
try {
|
|
143
|
+
await tunnel.stop()
|
|
144
|
+
} catch {}
|
|
145
|
+
try {
|
|
146
|
+
upstream.stop()
|
|
147
|
+
} catch {}
|
|
148
|
+
try {
|
|
149
|
+
sessions.close()
|
|
150
|
+
} catch {}
|
|
151
|
+
try {
|
|
152
|
+
await edge.close()
|
|
153
|
+
} catch {}
|
|
154
|
+
logger.info('stopped')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const onSignal = (signal) => {
|
|
158
|
+
shutdown(signal)
|
|
159
|
+
.catch((error) => logger.error('shutdown error', { error: String(error?.message ?? error) }))
|
|
160
|
+
.finally(() => process.exit(0))
|
|
161
|
+
}
|
|
162
|
+
process.once('SIGINT', () => onSignal('SIGINT'))
|
|
163
|
+
process.once('SIGTERM', () => onSignal('SIGTERM'))
|
|
164
|
+
|
|
165
|
+
persist()
|
|
166
|
+
await tunnel.start()
|
|
167
|
+
if (config.public.mode === 'none') {
|
|
168
|
+
runtime.publicUrl = config.proxy.publicUrl ?? null
|
|
169
|
+
persist()
|
|
170
|
+
}
|
|
171
|
+
startRotation()
|
|
172
|
+
startUpstreamWatch()
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
keys,
|
|
176
|
+
sessions,
|
|
177
|
+
upstream,
|
|
178
|
+
edge,
|
|
179
|
+
tunnel,
|
|
180
|
+
runtime,
|
|
181
|
+
runtimePath,
|
|
182
|
+
linkPath,
|
|
183
|
+
async status() {
|
|
184
|
+
const local = await httpRequest(`http://127.0.0.1:${String(address.port)}/__dsh/health`, { timeoutMs: 3000 }).catch(
|
|
185
|
+
() => null,
|
|
186
|
+
)
|
|
187
|
+
const remote = runtime.publicUrl
|
|
188
|
+
? await httpRequest(`${runtime.publicUrl}/__dsh/health`, { timeoutMs: 10_000 }).catch(() => null)
|
|
189
|
+
: null
|
|
190
|
+
return {
|
|
191
|
+
pid: process.pid,
|
|
192
|
+
listen: runtime.listen,
|
|
193
|
+
publicUrl: runtime.publicUrl,
|
|
194
|
+
link: runtime.link,
|
|
195
|
+
appLink: runtime.appLink ?? null,
|
|
196
|
+
localHealth: local ? Boolean(JSON.parse(local.body.toString('utf8')).ok) : false,
|
|
197
|
+
remoteHealth: remote ? Boolean(JSON.parse(remote.body.toString('utf8')).ok) : false,
|
|
198
|
+
tunnel: tunnel.status(),
|
|
199
|
+
upstream: upstream.describe(),
|
|
200
|
+
keys: keys.describe(),
|
|
201
|
+
sessions: sessions.size,
|
|
202
|
+
edge: edge.describe(),
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
shutdown,
|
|
206
|
+
}
|
|
207
|
+
}
|