dsh-ccx 0.0.0-stage → 0.8.9
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/LICENSE +21 -0
- package/NOTICE +23 -0
- package/README.md +142 -2
- package/assets/icon.svg +27 -0
- package/assets/preview-home.png +0 -0
- package/assets/preview-market.png +0 -0
- package/assets/preview-milkyway.png +0 -0
- package/assets/preview-mountain.png +0 -0
- package/assets/preview-settings.png +0 -0
- package/assets/preview-surface.png +0 -0
- package/assets/scene-aurora-ionosphere.png +0 -0
- package/assets/scene-black-hole.png +0 -0
- package/assets/scene-blinking-squares.png +0 -0
- package/assets/scene-cloud-train.png +0 -0
- package/assets/scene-glow-horizon.png +0 -0
- package/assets/scene-heavenly-cloud.png +0 -0
- package/assets/scene-milky-way.png +0 -0
- package/assets/scene-mountain.png +0 -0
- package/assets/scene-particle-image.png +0 -0
- package/assets/scene-pixel-sculpt.png +0 -0
- package/cordis.patch.yml +16 -0
- package/lib/client.js +2244 -0
- package/lib/index.js +512 -0
- package/lib/vendor/code-codex/aurora-ionosphere.js +441 -0
- package/lib/vendor/code-codex/black-hole.js +640 -0
- package/lib/vendor/code-codex/blinking-squares.js +408 -0
- package/lib/vendor/code-codex/cloud-train.js +626 -0
- package/lib/vendor/code-codex/galleries.json +101 -0
- package/lib/vendor/code-codex/glow-horizon.js +260 -0
- package/lib/vendor/code-codex/heavenly-cloud.js +358 -0
- package/lib/vendor/code-codex/milky-way.js +303 -0
- package/lib/vendor/code-codex/mountain.js +335 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-image-098c5fbaa5c415ec.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-image-39894de0da1fa952.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-image-6447780fc6368ce2.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-image-746ff537a0eca39e.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-image-89d064cef83721eb.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-image-e1c658aa459cfcef.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-thumbnail-098c5fbaa5c415ec.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-thumbnail-39894de0da1fa952.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-thumbnail-6447780fc6368ce2.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-thumbnail-746ff537a0eca39e.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-thumbnail-89d064cef83721eb.png +0 -0
- package/lib/vendor/code-codex/particle-image/media/CodeCodex-background-particle-image-thumbnail-e1c658aa459cfcef.png +0 -0
- package/lib/vendor/code-codex/particle-image.js +678 -0
- package/lib/vendor/code-codex/pixel-sculpt/media/CodeCodex-background-pixel-sculpt-image-079ae375628c240b.png +0 -0
- package/lib/vendor/code-codex/pixel-sculpt/media/CodeCodex-background-pixel-sculpt-image-0e0ec1222ff7a1bc.png +0 -0
- package/lib/vendor/code-codex/pixel-sculpt/media/CodeCodex-background-pixel-sculpt-image-3fde0595509bc4d5.png +0 -0
- package/lib/vendor/code-codex/pixel-sculpt.js +358 -0
- package/lib/vendor/code-codex/scene-defaults.json +215 -0
- package/lib/vendor/code-codex/scene-schemas.json +1957 -0
- package/package.json +57 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// dsh-ccx —— 宿主半侧(cordis 插件,Node 侧)
|
|
3
|
+
//
|
|
4
|
+
// 职责只有两件事:
|
|
5
|
+
// ① 外观设置(settings.json)的读写与校验 —— 浏览器半侧经 /dsh-appearances/state.json
|
|
6
|
+
// 拉取/保存;设置落在 $DSH_HOME/appearances/,桌面端与 Web 端共用同一份。
|
|
7
|
+
// ② 壁纸库(wallpapers/)的上传、列表、按 id 取图与删除。
|
|
8
|
+
//
|
|
9
|
+
// 不注入 index(浏览器半侧走 DSH 客户端模块系统 /plugins/<id>/client.js 正规通道,
|
|
10
|
+
// 参见 package.json 的 dsh.client 声明),不依赖任何 @deepseek-ai/* 包:
|
|
11
|
+
// webServer 服务通过 inject 等待,connection 服务用 ctx.get() 容错降级。
|
|
12
|
+
// 信任栅栏沿用 dsh-whale-widget-bowl 的三层方案(自校验 → 委托宿主 → fail-closed)。
|
|
13
|
+
// ============================================================================
|
|
14
|
+
|
|
15
|
+
import fs from 'node:fs'
|
|
16
|
+
import os from 'node:os'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
import crypto from 'node:crypto'
|
|
19
|
+
import { fileURLToPath } from 'node:url'
|
|
20
|
+
|
|
21
|
+
const PLUGIN_NAME = 'dsh-ccx'
|
|
22
|
+
|
|
23
|
+
// —— 存储位置:$DSH_HOME 优先(桌面客户端的发布副本只认它),~/.dsh 兜底 ——
|
|
24
|
+
function dshHome() {
|
|
25
|
+
return process.env.DSH_HOME || path.join(os.homedir(), '.dsh')
|
|
26
|
+
}
|
|
27
|
+
function dataDir() {
|
|
28
|
+
return path.join(dshHome(), 'appearances')
|
|
29
|
+
}
|
|
30
|
+
function wallpapersDir() {
|
|
31
|
+
return path.join(dataDir(), 'wallpapers')
|
|
32
|
+
}
|
|
33
|
+
function settingsPath() {
|
|
34
|
+
return path.join(dataDir(), 'settings.json')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// —— 设置 schema:客户端半侧 (lib/client.js) 与此处的校验保持同一份默认值 ——
|
|
38
|
+
// savedAt 是宿主写入的版本戳:客户端轮询它来发现"另一端改了设置"(双开同步)。
|
|
39
|
+
// v3:surfaceOpacity/dim 等旧的整面透明度改为 surfaces 分区蒙版(对齐原仓库
|
|
40
|
+
// surface-opacity:每个区域一个 0-100 的不透明度,0=完全透明)。
|
|
41
|
+
// v5:蒙版默认对齐原版两档(主区/输入 .58、侧栏 .66、标题栏透明)、dim 默认关
|
|
42
|
+
// (原版无压暗层)、去掉死字段 quality(质量在每场景参数里)。
|
|
43
|
+
// v6:蒙版目标改为真实绘制容器(槽位标记全是 0×0,上色无视觉,见 client REGION_ROWS);
|
|
44
|
+
// 输入区默认 58→86(原版 surface-opacity 面板 composer 档),标题栏分区删除(DSH 无独立绘制面)。
|
|
45
|
+
export const DEFAULT_SETTINGS = Object.freeze({
|
|
46
|
+
version: 6,
|
|
47
|
+
enabled: true,
|
|
48
|
+
// 'scene' = 原版动态场景;'wallpaper' = 用户壁纸/视频;'off' = 仅透明化不画背景
|
|
49
|
+
mode: 'scene',
|
|
50
|
+
scene: 'milky-way',
|
|
51
|
+
wallpaperId: null,
|
|
52
|
+
wallpaperFit: 'cover', // cover | contain | tile
|
|
53
|
+
wallpaperPosition: 'center', // center | top | bottom | left | right
|
|
54
|
+
// UI 表面分区蒙版(0–100;base 色:主区/输入 rgb(11,12,15),侧栏 rgb(16,17,20),
|
|
55
|
+
// 与原版 inject.ts 的 --code-codex-particle-ui-surface[-strong] 同值)
|
|
56
|
+
surfaces: {
|
|
57
|
+
enabled: true,
|
|
58
|
+
sidebar: 66,
|
|
59
|
+
conversation: 58,
|
|
60
|
+
composer: 86,
|
|
61
|
+
right: 66,
|
|
62
|
+
},
|
|
63
|
+
// 每场景参数覆盖(键=场景 id,值=该场景 schema 字段的稀疏增量;面板"应用"写入)
|
|
64
|
+
sceneParams: {},
|
|
65
|
+
dim: 0, // 背景压暗 0–0.8(原版无此层,默认关)
|
|
66
|
+
blur: 0, // 背景模糊 0–20px
|
|
67
|
+
savedAt: 0,
|
|
68
|
+
})
|
|
69
|
+
const CLAMPS = {
|
|
70
|
+
dim: [0, 0.8],
|
|
71
|
+
blur: [0, 20],
|
|
72
|
+
}
|
|
73
|
+
// 速度/饱和度/质量已随 v0.5 移入每场景参数(sceneParams,原版抽屉字段)
|
|
74
|
+
// 原仓库 code-codex 首方场景包(lib/vendor/code-codex/,MIT,含第三方声明)
|
|
75
|
+
const SCENE_IDS = [
|
|
76
|
+
'milky-way', 'glow-horizon', 'mountain', 'heavenly-cloud',
|
|
77
|
+
'aurora-ionosphere', 'black-hole', 'cloud-train',
|
|
78
|
+
'particle-image', 'pixel-sculpt', 'blinking-squares',
|
|
79
|
+
]
|
|
80
|
+
// 画廊媒体白名单:particle-image / pixel-sculpt 的默认图片(文件名含内容哈希)
|
|
81
|
+
const MEDIA_FILE_RE = /^CodeCodex-background-(particle-image|pixel-sculpt)-(image|thumbnail)-[0-9a-f]{16}\.png$/
|
|
82
|
+
const SURFACE_REGIONS = ['sidebar', 'conversation', 'composer', 'right']
|
|
83
|
+
|
|
84
|
+
function sanitizeSurfaces(input) {
|
|
85
|
+
const src = input && typeof input === 'object' ? input : {}
|
|
86
|
+
const out = { ...DEFAULT_SETTINGS.surfaces }
|
|
87
|
+
out.enabled = src.enabled === true
|
|
88
|
+
for (const key of SURFACE_REGIONS) {
|
|
89
|
+
const n = Number(src[key])
|
|
90
|
+
if (Number.isFinite(n)) out[key] = Math.min(100, Math.max(0, Math.round(n)))
|
|
91
|
+
}
|
|
92
|
+
return out
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function sanitizeSettings(input) {
|
|
96
|
+
const src = input && typeof input === 'object' ? input : {}
|
|
97
|
+
const out = { ...DEFAULT_SETTINGS }
|
|
98
|
+
if (typeof src.enabled === 'boolean') out.enabled = src.enabled
|
|
99
|
+
if (src.mode === 'scene' || src.mode === 'wallpaper' || src.mode === 'off') out.mode = src.mode
|
|
100
|
+
if (typeof src.scene === 'string' && SCENE_IDS.includes(src.scene)) out.scene = src.scene
|
|
101
|
+
if (typeof src.wallpaperId === 'string' && /^[\w-]+$/.test(src.wallpaperId)) out.wallpaperId = src.wallpaperId
|
|
102
|
+
else if (src.wallpaperId === null) out.wallpaperId = null
|
|
103
|
+
if (src.wallpaperFit === 'cover' || src.wallpaperFit === 'contain' || src.wallpaperFit === 'tile') out.wallpaperFit = src.wallpaperFit
|
|
104
|
+
if (['center', 'top', 'bottom', 'left', 'right'].includes(src.wallpaperPosition)) out.wallpaperPosition = src.wallpaperPosition
|
|
105
|
+
// v4 遗留死字段 quality 不再接受写入(逐场景质量在 sceneParams/store)
|
|
106
|
+
for (const key of Object.keys(CLAMPS)) {
|
|
107
|
+
const n = Number(src[key])
|
|
108
|
+
if (Number.isFinite(n)) out[key] = Math.min(CLAMPS[key][1], Math.max(CLAMPS[key][0], n))
|
|
109
|
+
}
|
|
110
|
+
// v1/v2 老设置的 surfaceOpacity 折算为各区域统一蒙版(composer +20 保输入区可读)
|
|
111
|
+
let surfacesSrc = src.surfaces
|
|
112
|
+
if (!surfacesSrc && Number.isFinite(Number(src.surfaceOpacity))) {
|
|
113
|
+
const legacy = Math.round(Math.min(1, Math.max(0.05, Number(src.surfaceOpacity))) * 100)
|
|
114
|
+
surfacesSrc = { enabled: true, sidebar: legacy, conversation: legacy, composer: Math.min(100, legacy + 20), header: legacy, right: legacy }
|
|
115
|
+
}
|
|
116
|
+
let paramsSrc = src.sceneParams && typeof src.sceneParams === 'object' ? src.sceneParams : {}
|
|
117
|
+
const paramsOut = {}
|
|
118
|
+
for (const key of Object.keys(paramsSrc)) {
|
|
119
|
+
if (!/^[a-z-]+$/.test(key)) continue
|
|
120
|
+
const v = paramsSrc[key]
|
|
121
|
+
if (v && typeof v === 'object' && !Array.isArray(v)) paramsOut[key] = v
|
|
122
|
+
}
|
|
123
|
+
out.sceneParams = paramsOut
|
|
124
|
+
out.surfaces = sanitizeSurfaces(surfacesSrc)
|
|
125
|
+
// v5→v6:输入区默认 58(v5 默认值)升 86(原版面板 composer 档);header 分区随白名单丢弃。
|
|
126
|
+
// 只迁移「等于 v5 默认」的值,用户显式调过的其它值不动。
|
|
127
|
+
if (Number(src.version) < 6 && out.surfaces.composer === 58) out.surfaces.composer = 86
|
|
128
|
+
if (Number.isFinite(Number(src.savedAt))) out.savedAt = Number(src.savedAt)
|
|
129
|
+
return out
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function readSettings() {
|
|
133
|
+
try {
|
|
134
|
+
return sanitizeSettings(JSON.parse(fs.readFileSync(settingsPath(), 'utf8')))
|
|
135
|
+
} catch (err) {
|
|
136
|
+
return { ...DEFAULT_SETTINGS }
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 原子写(tmp + rename),避免半截 JSON 让客户端读到坏状态
|
|
141
|
+
function writeSettings(value) {
|
|
142
|
+
fs.mkdirSync(dataDir(), { recursive: true })
|
|
143
|
+
const tmp = settingsPath() + '.tmp-' + process.pid
|
|
144
|
+
fs.writeFileSync(tmp, JSON.stringify(value, null, 2))
|
|
145
|
+
fs.renameSync(tmp, settingsPath())
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// —— 壁纸文件管理 ——
|
|
149
|
+
const WALLPAPER_MIMES = {
|
|
150
|
+
'.png': 'image/png',
|
|
151
|
+
'.jpg': 'image/jpeg',
|
|
152
|
+
'.jpeg': 'image/jpeg',
|
|
153
|
+
'.webp': 'image/webp',
|
|
154
|
+
'.gif': 'image/gif',
|
|
155
|
+
'.bmp': 'image/bmp',
|
|
156
|
+
'.avif': 'image/avif',
|
|
157
|
+
'.mp4': 'video/mp4',
|
|
158
|
+
'.webm': 'video/webm',
|
|
159
|
+
'.mov': 'video/quicktime',
|
|
160
|
+
}
|
|
161
|
+
const WALLPAPER_MAX_BYTES = 200 * 1024 * 1024 // 本机本地路由,放宽到 200MB(视频)
|
|
162
|
+
|
|
163
|
+
function listWallpapers() {
|
|
164
|
+
const dir = wallpapersDir()
|
|
165
|
+
let entries = []
|
|
166
|
+
try {
|
|
167
|
+
entries = fs.readdirSync(dir)
|
|
168
|
+
} catch (err) {
|
|
169
|
+
return []
|
|
170
|
+
}
|
|
171
|
+
const items = []
|
|
172
|
+
for (const name of entries) {
|
|
173
|
+
const ext = path.extname(name).toLowerCase()
|
|
174
|
+
const mime = WALLPAPER_MIMES[ext]
|
|
175
|
+
if (!mime) continue
|
|
176
|
+
const id = name.slice(0, name.length - ext.length)
|
|
177
|
+
try {
|
|
178
|
+
const st = fs.statSync(path.join(dir, name))
|
|
179
|
+
items.push({ id, name: name, mime, size: st.size, mtimeMs: st.mtimeMs })
|
|
180
|
+
} catch (err) {}
|
|
181
|
+
}
|
|
182
|
+
items.sort((a, b) => b.mtimeMs - a.mtimeMs)
|
|
183
|
+
return items
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function wallpaperUrl(id) {
|
|
187
|
+
return '/dsh-appearances/wallpaper?id=' + encodeURIComponent(id)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function readBody(req, maxBytes) {
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
const chunks = []
|
|
193
|
+
let total = 0
|
|
194
|
+
let done = false
|
|
195
|
+
req.on('data', (chunk) => {
|
|
196
|
+
if (done) return
|
|
197
|
+
total += chunk.length
|
|
198
|
+
if (total > maxBytes) {
|
|
199
|
+
done = true
|
|
200
|
+
reject(new Error('payload too large'))
|
|
201
|
+
req.destroy()
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
chunks.push(chunk)
|
|
205
|
+
})
|
|
206
|
+
req.on('end', () => {
|
|
207
|
+
if (!done) resolve(Buffer.concat(chunks))
|
|
208
|
+
})
|
|
209
|
+
req.on('error', (err) => {
|
|
210
|
+
if (!done) reject(err)
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function sendJson(res, status, payload) {
|
|
216
|
+
const body = JSON.stringify(payload)
|
|
217
|
+
res.writeHead(status, {
|
|
218
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
219
|
+
'Cache-Control': 'no-store',
|
|
220
|
+
'Content-Length': String(Buffer.byteLength(body)),
|
|
221
|
+
})
|
|
222
|
+
res.end(body)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// —— 环回主机判定(照抄 whale 的逐段校验,防 127.0.0.1.evil.com 相似域名)——
|
|
226
|
+
function isLoopbackHostname(hn) {
|
|
227
|
+
const h = String(hn || '').toLowerCase().replace(/^\[/, '').replace(/\]$/, '')
|
|
228
|
+
if (!h) return false
|
|
229
|
+
if (h === 'localhost' || h.endsWith('.localhost')) return true
|
|
230
|
+
if (h === '::1') return true
|
|
231
|
+
const m = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(h)
|
|
232
|
+
if (!m) return false
|
|
233
|
+
if (Number(m[1]) !== 127) return false
|
|
234
|
+
return [m[2], m[3], m[4]].every((x) => Number(x) <= 255)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// 顶栏(应用/编辑)是 Electron 原生应用菜单,页面 CSS 够不到;把原生菜单栏
|
|
238
|
+
// 设为自动隐藏(Alt 呼出),背景即可延伸到窗口顶部。bundle 运行在宿主主进程,
|
|
239
|
+
// electron 模块可用;任何一步失败都静默跳过,绝不影响插件装配。
|
|
240
|
+
function withWindows(fn) {
|
|
241
|
+
try {
|
|
242
|
+
const electron = require('electron')
|
|
243
|
+
const windows = electron && electron.BrowserWindow ? electron.BrowserWindow.getAllWindows() : []
|
|
244
|
+
for (const win of windows) {
|
|
245
|
+
try { fn(win) } catch (err) { /* 单窗口失败不影响其余窗口 */ }
|
|
246
|
+
}
|
|
247
|
+
} catch (err) { /* electron 不可用(非主进程/测试环境):跳过 */ }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function hideNativeMenuBar() {
|
|
251
|
+
withWindows((win) => {
|
|
252
|
+
if (typeof win.setAutoHideMenuBar === 'function') win.setAutoHideMenuBar(true)
|
|
253
|
+
if (typeof win.setMenuBarVisibility === 'function') win.setMenuBarVisibility(false)
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function menuBarDebugInfo() {
|
|
258
|
+
let out = { error: 'no-window' }
|
|
259
|
+
withWindows((win) => {
|
|
260
|
+
out = {
|
|
261
|
+
menuBarVisible: typeof win.isMenuBarVisible === 'function' ? win.isMenuBarVisible() : null,
|
|
262
|
+
menuBarAutoHide: typeof win.isMenuBarAutoHide === 'function' ? win.isMenuBarAutoHide() : null,
|
|
263
|
+
bounds: typeof win.getBounds === 'function' ? win.getBounds() : null,
|
|
264
|
+
contentBounds: typeof win.getContentBounds === 'function' ? win.getContentBounds() : null,
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
return out
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export default {
|
|
271
|
+
name: PLUGIN_NAME,
|
|
272
|
+
apply(root) {
|
|
273
|
+
hideNativeMenuBar()
|
|
274
|
+
|
|
275
|
+
root.inject(['webServer'], (ctx) => {
|
|
276
|
+
const disposers = []
|
|
277
|
+
|
|
278
|
+
// —— 信任栅栏:宿主栅栏可用则委托;否则自校验(fail-closed)——
|
|
279
|
+
function connectionFence() {
|
|
280
|
+
try {
|
|
281
|
+
const conn = ctx.get('connection') || ctx.connection
|
|
282
|
+
return conn && typeof conn.requestRejection === 'function' ? conn : null
|
|
283
|
+
} catch (err) {
|
|
284
|
+
return null
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function selfRejection(req) {
|
|
288
|
+
try {
|
|
289
|
+
const headers = (req && req.headers) || {}
|
|
290
|
+
let hostUrl = null
|
|
291
|
+
try {
|
|
292
|
+
hostUrl = new URL('http://' + String(headers.host || ''))
|
|
293
|
+
} catch (err) {
|
|
294
|
+
return 403
|
|
295
|
+
}
|
|
296
|
+
// 宿主栅栏在场时把非环回 Host 交给它判(它知道 --trusted-host 声明),
|
|
297
|
+
// 缺席才回落到"只认环回"。
|
|
298
|
+
if (!isLoopbackHostname(hostUrl.hostname) && !connectionFence()) return 403
|
|
299
|
+
const site = String(headers['sec-fetch-site'] || '').toLowerCase()
|
|
300
|
+
if (site === 'cross-site') return 403
|
|
301
|
+
const origin = headers.origin
|
|
302
|
+
if (typeof origin === 'string' && origin && origin !== 'null') {
|
|
303
|
+
let originUrl = null
|
|
304
|
+
try {
|
|
305
|
+
originUrl = new URL(origin)
|
|
306
|
+
} catch (err) {
|
|
307
|
+
return 403
|
|
308
|
+
}
|
|
309
|
+
if (originUrl.host.toLowerCase() !== hostUrl.host.toLowerCase()) return 403
|
|
310
|
+
}
|
|
311
|
+
return null
|
|
312
|
+
} catch (err) {
|
|
313
|
+
return 403
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function registerRoute(route) {
|
|
317
|
+
const inner = route && route.handler
|
|
318
|
+
const wrapped = Object.assign({}, route, {
|
|
319
|
+
handler: async (req, res) => {
|
|
320
|
+
const code = selfRejection(req)
|
|
321
|
+
if (code) {
|
|
322
|
+
sendJson(res, code, { ok: false, error: 'rejected by trust fence' })
|
|
323
|
+
return
|
|
324
|
+
}
|
|
325
|
+
return inner(req, res)
|
|
326
|
+
},
|
|
327
|
+
})
|
|
328
|
+
return ctx.webServer.register(wrapped)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// webserver 的路由表不区分 HTTP 方法,且"同一张表内的重复路径会抛错"
|
|
332
|
+
// (host-webserver README)—— 所以读写用不同路径,POST 一律落 *.save.json。
|
|
333
|
+
// ---- GET /dsh-appearances/wininfo.json:原生菜单栏/窗口状态(诊断用) ----
|
|
334
|
+
disposers.push(registerRoute({
|
|
335
|
+
kind: 'exact',
|
|
336
|
+
path: '/dsh-appearances/wininfo.json',
|
|
337
|
+
handler: (req, res) => sendJson(res, 200, { ok: true, win: menuBarDebugInfo() }),
|
|
338
|
+
}))
|
|
339
|
+
// ---- GET /dsh-appearances/vendor/<id>.js:原仓库 code-codex 场景渲染器 ----
|
|
340
|
+
// (webserver 的 prefix 匹配行为不可靠,直接给每个场景注册 exact 路由)
|
|
341
|
+
const vendorDir = path.join(path.dirname(fileURLToPath(import.meta.url)), 'vendor', 'code-codex')
|
|
342
|
+
const serveVendor = (file) => (req, res) => {
|
|
343
|
+
try {
|
|
344
|
+
const bytes = fs.readFileSync(path.join(vendorDir, file))
|
|
345
|
+
res.writeHead(200, {
|
|
346
|
+
'Content-Type': 'text/javascript; charset=utf-8',
|
|
347
|
+
'Cache-Control': 'no-store',
|
|
348
|
+
'Content-Length': String(bytes.length),
|
|
349
|
+
})
|
|
350
|
+
res.end(bytes)
|
|
351
|
+
} catch (err) {
|
|
352
|
+
sendJson(res, 404, { ok: false, error: String((err && err.message) || err).slice(0, 120) })
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
for (const sceneId of SCENE_IDS) {
|
|
356
|
+
disposers.push(registerRoute({
|
|
357
|
+
kind: 'exact',
|
|
358
|
+
path: `/dsh-appearances/vendor/${sceneId}.js`,
|
|
359
|
+
handler: serveVendor(`${sceneId}.js`),
|
|
360
|
+
}))
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// ---- GET /dsh-appearances/vendor-galleries.json:画廊索引(sha256/文件名/计数)----
|
|
364
|
+
disposers.push(registerRoute({
|
|
365
|
+
kind: 'exact',
|
|
366
|
+
path: '/dsh-appearances/vendor-galleries.json',
|
|
367
|
+
handler: serveVendor('galleries.json'),
|
|
368
|
+
}))
|
|
369
|
+
|
|
370
|
+
// ---- GET /dsh-appearances/vendor-media?file=<白名单文件名>:画廊图片字节 ----
|
|
371
|
+
disposers.push(registerRoute({
|
|
372
|
+
kind: 'exact',
|
|
373
|
+
path: '/dsh-appearances/vendor-media',
|
|
374
|
+
handler: (req, res) => {
|
|
375
|
+
try {
|
|
376
|
+
const u = new URL(req.url || '/', 'http://localhost')
|
|
377
|
+
const name = u.searchParams.get('file') || ''
|
|
378
|
+
const match = MEDIA_FILE_RE.exec(name)
|
|
379
|
+
if (!match) throw new Error('bad media file')
|
|
380
|
+
const bytes = fs.readFileSync(path.join(vendorDir, match[1], 'media', name))
|
|
381
|
+
res.writeHead(200, {
|
|
382
|
+
'Content-Type': 'image/png',
|
|
383
|
+
'Cache-Control': 'no-store',
|
|
384
|
+
'Content-Length': String(bytes.length),
|
|
385
|
+
})
|
|
386
|
+
res.end(bytes)
|
|
387
|
+
} catch (err) {
|
|
388
|
+
sendJson(res, 404, { ok: false, error: String((err && err.message) || err).slice(0, 120) })
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
}))
|
|
392
|
+
|
|
393
|
+
// ---- GET /dsh-appearances/scene-schemas.json:每场景设置 schema(原版抽屉字段)----
|
|
394
|
+
disposers.push(registerRoute({
|
|
395
|
+
kind: 'exact',
|
|
396
|
+
path: '/dsh-appearances/scene-schemas.json',
|
|
397
|
+
handler: serveVendor('scene-schemas.json'),
|
|
398
|
+
}))
|
|
399
|
+
|
|
400
|
+
// ---- GET /dsh-appearances/state.json:当前外观设置 + 壁纸清单 ----
|
|
401
|
+
disposers.push(registerRoute({
|
|
402
|
+
kind: 'exact',
|
|
403
|
+
path: '/dsh-appearances/state.json',
|
|
404
|
+
handler: (req, res) => {
|
|
405
|
+
const settings = readSettings()
|
|
406
|
+
const wallpapers = listWallpapers().map((w) => ({ ...w, url: wallpaperUrl(w.id) }))
|
|
407
|
+
sendJson(res, 200, {
|
|
408
|
+
ok: true,
|
|
409
|
+
settings,
|
|
410
|
+
defaults: DEFAULT_SETTINGS,
|
|
411
|
+
scenes: SCENE_IDS,
|
|
412
|
+
wallpapers,
|
|
413
|
+
})
|
|
414
|
+
},
|
|
415
|
+
}))
|
|
416
|
+
|
|
417
|
+
// ---- POST /dsh-appearances/state-save.json:保存设置 ----
|
|
418
|
+
disposers.push(registerRoute({
|
|
419
|
+
kind: 'exact',
|
|
420
|
+
path: '/dsh-appearances/state-save.json',
|
|
421
|
+
handler: async (req, res) => {
|
|
422
|
+
try {
|
|
423
|
+
const raw = await readBody(req, 64 * 1024)
|
|
424
|
+
const parsed = JSON.parse(raw.toString('utf8'))
|
|
425
|
+
const settings = sanitizeSettings(parsed && parsed.settings ? parsed.settings : parsed)
|
|
426
|
+
settings.savedAt = Date.now()
|
|
427
|
+
writeSettings(settings)
|
|
428
|
+
sendJson(res, 200, { ok: true, settings })
|
|
429
|
+
} catch (err) {
|
|
430
|
+
sendJson(res, 400, { ok: false, error: String((err && err.message) || err).slice(0, 200) })
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
}))
|
|
434
|
+
|
|
435
|
+
// ---- POST /dsh-appearances/wallpaper-upload?name=<文件名>:上传壁纸(原始字节)----
|
|
436
|
+
disposers.push(registerRoute({
|
|
437
|
+
kind: 'exact',
|
|
438
|
+
path: '/dsh-appearances/wallpaper-upload',
|
|
439
|
+
handler: async (req, res) => {
|
|
440
|
+
try {
|
|
441
|
+
if (req.method !== 'POST') throw new Error('method not allowed')
|
|
442
|
+
const u = new URL(req.url || '/', 'http://localhost')
|
|
443
|
+
const rawName = String(u.searchParams.get('name') || '')
|
|
444
|
+
const ext = path.extname(rawName).toLowerCase()
|
|
445
|
+
if (!WALLPAPER_MIMES[ext]) throw new Error('unsupported file type: ' + (ext || '(none)'))
|
|
446
|
+
const bytes = await readBody(req, WALLPAPER_MAX_BYTES)
|
|
447
|
+
if (!bytes.length) throw new Error('empty body')
|
|
448
|
+
fs.mkdirSync(wallpapersDir(), { recursive: true })
|
|
449
|
+
const id = crypto.randomBytes(8).toString('hex')
|
|
450
|
+
const file = path.join(wallpapersDir(), id + ext)
|
|
451
|
+
const tmp = file + '.tmp-' + process.pid
|
|
452
|
+
fs.writeFileSync(tmp, bytes)
|
|
453
|
+
fs.renameSync(tmp, file)
|
|
454
|
+
const item = { id, name: id + ext, mime: WALLPAPER_MIMES[ext], size: bytes.length, url: wallpaperUrl(id) }
|
|
455
|
+
sendJson(res, 200, { ok: true, wallpaper: item, wallpapers: listWallpapers().map((w) => ({ ...w, url: wallpaperUrl(w.id) })) })
|
|
456
|
+
} catch (err) {
|
|
457
|
+
sendJson(res, 400, { ok: false, error: String((err && err.message) || err).slice(0, 200) })
|
|
458
|
+
}
|
|
459
|
+
},
|
|
460
|
+
}))
|
|
461
|
+
|
|
462
|
+
// ---- POST /dsh-appearances/wallpaper-delete.json {id}:删除壁纸 ----
|
|
463
|
+
disposers.push(registerRoute({
|
|
464
|
+
kind: 'exact',
|
|
465
|
+
path: '/dsh-appearances/wallpaper-delete.json',
|
|
466
|
+
handler: async (req, res) => {
|
|
467
|
+
try {
|
|
468
|
+
const raw = await readBody(req, 4 * 1024)
|
|
469
|
+
const parsed = JSON.parse(raw.toString('utf8'))
|
|
470
|
+
const id = String((parsed && parsed.id) || '')
|
|
471
|
+
if (!/^[\w-]+$/.test(id)) throw new Error('bad wallpaper id')
|
|
472
|
+
const item = listWallpapers().find((w) => w.id === id)
|
|
473
|
+
if (item) fs.rmSync(path.join(wallpapersDir(), item.name), { force: true })
|
|
474
|
+
sendJson(res, 200, { ok: true, wallpapers: listWallpapers().map((w) => ({ ...w, url: wallpaperUrl(w.id) })) })
|
|
475
|
+
} catch (err) {
|
|
476
|
+
sendJson(res, 400, { ok: false, error: String((err && err.message) || err).slice(0, 200) })
|
|
477
|
+
}
|
|
478
|
+
},
|
|
479
|
+
}))
|
|
480
|
+
|
|
481
|
+
// ---- GET /dsh-appearances/wallpaper?id=<id>:取壁纸字节 ----
|
|
482
|
+
disposers.push(registerRoute({
|
|
483
|
+
kind: 'exact',
|
|
484
|
+
path: '/dsh-appearances/wallpaper.png',
|
|
485
|
+
handler: (req, res) => {
|
|
486
|
+
try {
|
|
487
|
+
const u = new URL(req.url || '/', 'http://localhost')
|
|
488
|
+
const id = String(u.searchParams.get('id') || '')
|
|
489
|
+
if (!/^[\w-]+$/.test(id)) throw new Error('bad wallpaper id')
|
|
490
|
+
const item = listWallpapers().find((w) => w.id === id)
|
|
491
|
+
if (!item) throw new Error('wallpaper not found')
|
|
492
|
+
const bytes = fs.readFileSync(path.join(wallpapersDir(), item.name))
|
|
493
|
+
res.writeHead(200, {
|
|
494
|
+
'Content-Type': item.mime,
|
|
495
|
+
'Cache-Control': 'no-store',
|
|
496
|
+
'Content-Length': String(bytes.length),
|
|
497
|
+
})
|
|
498
|
+
res.end(bytes)
|
|
499
|
+
} catch (err) {
|
|
500
|
+
sendJson(res, 404, { ok: false, error: String((err && err.message) || err).slice(0, 200) })
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
}))
|
|
504
|
+
|
|
505
|
+
root.effect(() => () => {
|
|
506
|
+
for (const d of disposers) {
|
|
507
|
+
try { d() } catch (err) {}
|
|
508
|
+
}
|
|
509
|
+
}, PLUGIN_NAME + ': routes')
|
|
510
|
+
})
|
|
511
|
+
},
|
|
512
|
+
}
|