dsh-ccx 0.8.9 → 0.9.0
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 -21
- package/NOTICE +23 -23
- package/README.md +151 -143
- package/assets/icon.svg +27 -27
- package/cordis.patch.yml +16 -16
- package/lib/client.js +1481 -2244
- package/lib/index.js +716 -449
- package/lib/vendor/code-codex/ascii-tiles.js +145 -0
- package/lib/vendor/code-codex/aurora-ionosphere.js +7 -6
- package/lib/vendor/code-codex/black-hole.js +10 -7
- package/lib/vendor/code-codex/blinking-squares.js +6 -13
- package/lib/vendor/code-codex/cloud-train.js +9 -8
- package/lib/vendor/code-codex/galleries.json +100 -100
- package/lib/vendor/code-codex/glow-horizon.js +1 -1
- package/lib/vendor/code-codex/heavenly-cloud.js +5 -4
- package/lib/vendor/code-codex/milky-way.js +3 -2
- package/lib/vendor/code-codex/mountain.js +5 -4
- package/lib/vendor/code-codex/particle-image.js +435 -14
- package/lib/vendor/code-codex/pixel-sculpt.js +85 -5
- package/lib/vendor/code-codex/scene-defaults.json +94 -1
- package/lib/vendor/code-codex/scene-schemas.json +1008 -0
- package/lib/vendor/code-codex/shallow-ocean-thumbnails.json +12 -0
- package/lib/vendor/code-codex/shallow-ocean.js +5094 -0
- package/lib/vendor/code-codex/spectral-drape.js +172 -0
- package/package.json +70 -59
- package/src/client/api.ts +90 -0
- package/src/client/background/layer.ts +187 -0
- package/src/client/background/pixel-sculpt.ts +167 -0
- package/src/client/background/scene-runner.ts +317 -0
- package/src/client/background/shallow-ocean.ts +344 -0
- package/src/client/background/vendor-loader.ts +297 -0
- package/src/client/background/wallpaper.ts +87 -0
- package/src/client/index.ts +247 -0
- package/src/client/surfaces/corner-patch.ts +100 -0
- package/src/client/surfaces/surface-css.ts +155 -0
- package/src/client/ui/draft.ts +105 -0
- package/src/client/ui/icons.ts +47 -0
- package/src/client/ui/panel.ts +1524 -0
- package/src/client/ui/shallow-ocean-panel.ts +376 -0
- package/src/client/ui/styles.ts +1471 -0
- package/src/host/fence.ts +78 -0
- package/src/host/index.ts +26 -0
- package/src/host/routes.ts +367 -0
- package/src/host/storage.ts +109 -0
- package/src/host/window-chrome.ts +53 -0
- package/src/shared/constants.ts +239 -0
- package/src/shared/sanitize.ts +110 -0
- package/src/shared/types.ts +145 -0
- package/tsconfig.json +16 -0
package/lib/index.js
CHANGED
|
@@ -1,512 +1,779 @@
|
|
|
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
|
-
// ============================================================================
|
|
1
|
+
// Built from src/host/index.ts — do not edit lib/index.js directly.
|
|
14
2
|
|
|
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
3
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
4
|
+
// src/shared/constants.ts
|
|
5
|
+
var PLUGIN_ID = "dsh-ccx";
|
|
6
|
+
var API_BASE = "/dsh-appearances";
|
|
7
|
+
var SCENES = [
|
|
8
|
+
{
|
|
9
|
+
id: "milky-way",
|
|
10
|
+
label: "\u94F6\u6CB3",
|
|
11
|
+
en: "Milky Way",
|
|
12
|
+
cardTitle: "Milky Way Background",
|
|
13
|
+
icon: "milky-way",
|
|
14
|
+
ext: "",
|
|
15
|
+
layerBg: "#02090d",
|
|
16
|
+
storeKey: "code-codex:milky-way-background:v1"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
id: "glow-horizon",
|
|
20
|
+
label: "\u8F89\u5149\u5730\u5E73\u7EBF",
|
|
21
|
+
en: "Glow Horizon",
|
|
22
|
+
cardTitle: "Glow Horizon Background",
|
|
23
|
+
icon: "glow-horizon",
|
|
24
|
+
ext: "ext-glowhorizon",
|
|
25
|
+
layerBg: "#050507",
|
|
26
|
+
storeKey: "code-codex:glow-horizon-background:v1"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: "mountain",
|
|
30
|
+
label: "\u5C42\u5CE6",
|
|
31
|
+
en: "Layered Mountain",
|
|
32
|
+
cardTitle: "Layered Mountain Background",
|
|
33
|
+
icon: "mountain",
|
|
34
|
+
ext: "",
|
|
35
|
+
layerBg: "#02090d",
|
|
36
|
+
storeKey: "code-codex:mountain-settings:v1"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: "heavenly-cloud",
|
|
40
|
+
label: "\u4E91\u7A79",
|
|
41
|
+
en: "Heavenly Cloud",
|
|
42
|
+
cardTitle: "Heavenly Cloud Background",
|
|
43
|
+
icon: "heavenly-cloud",
|
|
44
|
+
ext: "ext-heavenlycloud",
|
|
45
|
+
layerBg: "#020408",
|
|
46
|
+
storeKey: "code-codex:heavenly-cloud-background:v1"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: "aurora-ionosphere",
|
|
50
|
+
label: "\u6781\u5149\u7535\u79BB\u5C42",
|
|
51
|
+
en: "Aurora Ionosphere",
|
|
52
|
+
cardTitle: "Aurora Ionosphere Background",
|
|
53
|
+
icon: "aurora-ionosphere",
|
|
54
|
+
ext: "",
|
|
55
|
+
layerBg: "#02090d",
|
|
56
|
+
storeKey: "code-codex:aurora-ionosphere-background:v1"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: "black-hole",
|
|
60
|
+
label: "\u9ED1\u6D1E",
|
|
61
|
+
en: "Black Hole",
|
|
62
|
+
cardTitle: "Black Hole Background",
|
|
63
|
+
icon: "black-hole",
|
|
64
|
+
ext: "ext-blackhole",
|
|
65
|
+
layerBg: "#000000",
|
|
66
|
+
storeKey: "code-codex:black-hole-background:v1"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
id: "cloud-train",
|
|
70
|
+
label: "\u4E91\u5217\u8F66",
|
|
71
|
+
en: "Cloud Train",
|
|
72
|
+
cardTitle: "Cloud Train Background",
|
|
73
|
+
icon: "cloud-train",
|
|
74
|
+
ext: "",
|
|
75
|
+
layerBg: "#02090d",
|
|
76
|
+
storeKey: "code-codex:cloud-train-settings:v1"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: "particle-image",
|
|
80
|
+
label: "\u7C92\u5B50\u6210\u50CF",
|
|
81
|
+
en: "Particle Image",
|
|
82
|
+
cardTitle: "Particle Image Background",
|
|
83
|
+
icon: "particle-image",
|
|
84
|
+
ext: "ext-particle",
|
|
85
|
+
layerBg: "#000000",
|
|
86
|
+
bgColorKey: "backgroundColor",
|
|
87
|
+
storeKey: "code-codex:particle-image-background:v1"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: "pixel-sculpt",
|
|
91
|
+
label: "\u50CF\u7D20\u96D5\u5851",
|
|
92
|
+
en: "Pixel Sculpt",
|
|
93
|
+
cardTitle: "Pixel Sculpt Background",
|
|
94
|
+
icon: "pixel-sculpt",
|
|
95
|
+
ext: "",
|
|
96
|
+
layerBg: "#02090d",
|
|
97
|
+
storeKey: "code-codex:pixel-sculpt-settings:v1"
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
id: "blinking-squares",
|
|
101
|
+
label: "\u95EA\u70C1\u65B9\u5757",
|
|
102
|
+
en: "Blinking Squares",
|
|
103
|
+
cardTitle: "Blinking Squares Background",
|
|
104
|
+
icon: "blinking-squares",
|
|
105
|
+
ext: "",
|
|
106
|
+
layerBg: "#000000",
|
|
107
|
+
bgColorKey: "backgroundColor",
|
|
108
|
+
storeKey: "code-codex:blinking-squares-background:v1"
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
id: "ascii-tiles",
|
|
112
|
+
label: "ASCII \u5B57\u7B26\u7816\u5757",
|
|
113
|
+
en: "ASCII Tiles",
|
|
114
|
+
cardTitle: "ASCII Tiles Background",
|
|
115
|
+
icon: "ascii-tiles",
|
|
116
|
+
ext: "ext-asciitiles",
|
|
117
|
+
layerBg: "#000000",
|
|
118
|
+
bgColorKey: "backgroundColor",
|
|
119
|
+
storeKey: "code-codex:ascii-tiles-background:v1"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
id: "shallow-ocean",
|
|
123
|
+
label: "\u6D45\u6D77\u6765\u4FE1",
|
|
124
|
+
en: "Shallow Ocean Letters",
|
|
125
|
+
cardTitle: "Shallow Ocean Letters Background",
|
|
126
|
+
icon: "shallow-ocean",
|
|
127
|
+
ext: "ext-shallowocean",
|
|
128
|
+
layerBg: "#181818",
|
|
129
|
+
storeKey: "code-codex:shallow-ocean-background:v1"
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
id: "spectral-drape",
|
|
133
|
+
label: "\u5149\u8C31\u70B9\u9635\u5E37\u5E55",
|
|
134
|
+
en: "Spectral Drape",
|
|
135
|
+
cardTitle: "Spectral Drape Background",
|
|
136
|
+
icon: "spectral-drape",
|
|
137
|
+
ext: "ext-spectraldrape",
|
|
138
|
+
layerBg: "#0a0a0a",
|
|
139
|
+
bgColorKey: "backgroundColor",
|
|
140
|
+
storeKey: "code-codex:spectral-drape-background:v1"
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
var SCENE_IDS = SCENES.map((s) => s.id);
|
|
144
|
+
var SCENE_META_MAP = Object.fromEntries(
|
|
145
|
+
SCENES.map((s) => [s.id, s])
|
|
146
|
+
);
|
|
147
|
+
var STORE_KEYS = Object.fromEntries(
|
|
148
|
+
SCENES.map((s) => [s.id, s.storeKey])
|
|
149
|
+
);
|
|
150
|
+
var SCENE_LAYER_BG = Object.fromEntries(
|
|
151
|
+
SCENES.map((s) => [s.id, s.layerBg])
|
|
152
|
+
);
|
|
153
|
+
var SURFACE_REGIONS = [
|
|
154
|
+
"sidebar",
|
|
155
|
+
"conversation",
|
|
156
|
+
"composer",
|
|
157
|
+
"right"
|
|
158
|
+
];
|
|
159
|
+
var DEFAULT_SETTINGS = Object.freeze({
|
|
46
160
|
version: 6,
|
|
47
161
|
enabled: true,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
scene: 'milky-way',
|
|
162
|
+
mode: "scene",
|
|
163
|
+
scene: "milky-way",
|
|
51
164
|
wallpaperId: null,
|
|
52
|
-
wallpaperFit:
|
|
53
|
-
wallpaperPosition:
|
|
54
|
-
|
|
55
|
-
// 与原版 inject.ts 的 --code-codex-particle-ui-surface[-strong] 同值)
|
|
56
|
-
surfaces: {
|
|
165
|
+
wallpaperFit: "cover",
|
|
166
|
+
wallpaperPosition: "center",
|
|
167
|
+
surfaces: Object.freeze({
|
|
57
168
|
enabled: true,
|
|
58
169
|
sidebar: 66,
|
|
59
170
|
conversation: 58,
|
|
60
171
|
composer: 86,
|
|
61
|
-
right: 66
|
|
62
|
-
},
|
|
63
|
-
// 每场景参数覆盖(键=场景 id,值=该场景 schema 字段的稀疏增量;面板"应用"写入)
|
|
172
|
+
right: 66
|
|
173
|
+
}),
|
|
64
174
|
sceneParams: {},
|
|
65
|
-
dim: 0,
|
|
66
|
-
blur: 0,
|
|
67
|
-
savedAt: 0
|
|
68
|
-
})
|
|
69
|
-
|
|
175
|
+
dim: 0,
|
|
176
|
+
blur: 0,
|
|
177
|
+
savedAt: 0
|
|
178
|
+
});
|
|
179
|
+
var CLAMPS = {
|
|
70
180
|
dim: [0, 0.8],
|
|
71
|
-
blur: [0, 20]
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
181
|
+
blur: [0, 20]
|
|
182
|
+
};
|
|
183
|
+
var MEDIA_FILE_RE = /^CodeCodex-background-(particle-image|pixel-sculpt)-(image|thumbnail)-[0-9a-f]{16}\.png$/;
|
|
184
|
+
var WALLPAPER_MIMES = {
|
|
185
|
+
".png": "image/png",
|
|
186
|
+
".jpg": "image/jpeg",
|
|
187
|
+
".jpeg": "image/jpeg",
|
|
188
|
+
".webp": "image/webp",
|
|
189
|
+
".gif": "image/gif",
|
|
190
|
+
".bmp": "image/bmp",
|
|
191
|
+
".avif": "image/avif",
|
|
192
|
+
".mp4": "video/mp4",
|
|
193
|
+
".webm": "video/webm",
|
|
194
|
+
".mov": "video/quicktime"
|
|
195
|
+
};
|
|
196
|
+
var WALLPAPER_MAX_BYTES = 200 * 1024 * 1024;
|
|
83
197
|
|
|
198
|
+
// src/shared/sanitize.ts
|
|
199
|
+
function clamp(value, min, max) {
|
|
200
|
+
return Math.min(max, Math.max(min, value));
|
|
201
|
+
}
|
|
84
202
|
function sanitizeSurfaces(input) {
|
|
85
|
-
const src = input && typeof input ===
|
|
86
|
-
const out = { ...DEFAULT_SETTINGS.surfaces }
|
|
87
|
-
out.enabled = src.enabled === true
|
|
203
|
+
const src = input && typeof input === "object" ? input : {};
|
|
204
|
+
const out = { ...DEFAULT_SETTINGS.surfaces };
|
|
205
|
+
out.enabled = src.enabled === true;
|
|
88
206
|
for (const key of SURFACE_REGIONS) {
|
|
89
|
-
const n = Number(src[key])
|
|
90
|
-
if (Number.isFinite(n)) out[key] = Math.
|
|
207
|
+
const n = Number(src[key]);
|
|
208
|
+
if (Number.isFinite(n)) out[key] = Math.round(clamp(n, 0, 100));
|
|
91
209
|
}
|
|
92
|
-
return out
|
|
210
|
+
return out;
|
|
93
211
|
}
|
|
94
|
-
|
|
95
212
|
function sanitizeSettings(input) {
|
|
96
|
-
const src = input && typeof input ===
|
|
97
|
-
const out = {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (src.
|
|
104
|
-
if (
|
|
105
|
-
|
|
213
|
+
const src = input && typeof input === "object" ? input : {};
|
|
214
|
+
const out = {
|
|
215
|
+
...DEFAULT_SETTINGS,
|
|
216
|
+
surfaces: { ...DEFAULT_SETTINGS.surfaces },
|
|
217
|
+
sceneParams: {}
|
|
218
|
+
};
|
|
219
|
+
if (typeof src.enabled === "boolean") out.enabled = src.enabled;
|
|
220
|
+
if (src.mode === "scene" || src.mode === "wallpaper" || src.mode === "off") out.mode = src.mode;
|
|
221
|
+
if (typeof src.scene === "string" && SCENE_IDS.includes(src.scene)) out.scene = src.scene;
|
|
222
|
+
if (typeof src.wallpaperId === "string" && /^[\w-]+$/.test(src.wallpaperId)) {
|
|
223
|
+
out.wallpaperId = src.wallpaperId;
|
|
224
|
+
} else if (src.wallpaperId === null) {
|
|
225
|
+
out.wallpaperId = null;
|
|
226
|
+
}
|
|
227
|
+
if (src.wallpaperFit === "cover" || src.wallpaperFit === "contain" || src.wallpaperFit === "tile") {
|
|
228
|
+
out.wallpaperFit = src.wallpaperFit;
|
|
229
|
+
}
|
|
230
|
+
if (src.wallpaperPosition === "center" || src.wallpaperPosition === "top" || src.wallpaperPosition === "bottom" || src.wallpaperPosition === "left" || src.wallpaperPosition === "right") {
|
|
231
|
+
out.wallpaperPosition = src.wallpaperPosition;
|
|
232
|
+
}
|
|
106
233
|
for (const key of Object.keys(CLAMPS)) {
|
|
107
|
-
const n = Number(src[key])
|
|
108
|
-
if (Number.isFinite(n)) out[key] =
|
|
234
|
+
const n = Number(src[key]);
|
|
235
|
+
if (Number.isFinite(n)) out[key] = clamp(n, CLAMPS[key][0], CLAMPS[key][1]);
|
|
109
236
|
}
|
|
110
|
-
|
|
111
|
-
let surfacesSrc = src.surfaces
|
|
237
|
+
let surfacesSrc = src.surfaces;
|
|
112
238
|
if (!surfacesSrc && Number.isFinite(Number(src.surfaceOpacity))) {
|
|
113
|
-
const legacy = Math.round(
|
|
114
|
-
surfacesSrc = {
|
|
239
|
+
const legacy = Math.round(clamp(Number(src.surfaceOpacity), 0.05, 1) * 100);
|
|
240
|
+
surfacesSrc = {
|
|
241
|
+
enabled: true,
|
|
242
|
+
sidebar: legacy,
|
|
243
|
+
conversation: legacy,
|
|
244
|
+
composer: Math.min(100, legacy + 20),
|
|
245
|
+
right: legacy
|
|
246
|
+
};
|
|
115
247
|
}
|
|
116
|
-
|
|
117
|
-
const paramsOut = {}
|
|
248
|
+
const paramsSrc = src.sceneParams && typeof src.sceneParams === "object" ? src.sceneParams : {};
|
|
249
|
+
const paramsOut = {};
|
|
118
250
|
for (const key of Object.keys(paramsSrc)) {
|
|
119
|
-
if (!/^[a-z-]+$/.test(key)) continue
|
|
120
|
-
const
|
|
121
|
-
if (
|
|
251
|
+
if (!/^[a-z-]+$/.test(key)) continue;
|
|
252
|
+
const val = paramsSrc[key];
|
|
253
|
+
if (val && typeof val === "object" && !Array.isArray(val)) {
|
|
254
|
+
paramsOut[key] = { ...val };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
out.sceneParams = paramsOut;
|
|
258
|
+
out.surfaces = sanitizeSurfaces(surfacesSrc);
|
|
259
|
+
if (Number(src.version) < 6 && out.surfaces.composer === 58) {
|
|
260
|
+
out.surfaces.composer = 86;
|
|
261
|
+
}
|
|
262
|
+
if (Number.isFinite(Number(src.savedAt))) {
|
|
263
|
+
out.savedAt = Number(src.savedAt);
|
|
122
264
|
}
|
|
123
|
-
out
|
|
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
|
|
265
|
+
return out;
|
|
130
266
|
}
|
|
131
267
|
|
|
132
|
-
|
|
268
|
+
// src/host/routes.ts
|
|
269
|
+
import fs2 from "node:fs";
|
|
270
|
+
import fsp2 from "node:fs/promises";
|
|
271
|
+
import path2 from "node:path";
|
|
272
|
+
import { fileURLToPath } from "node:url";
|
|
273
|
+
|
|
274
|
+
// src/host/fence.ts
|
|
275
|
+
function isLoopbackHostname(hn) {
|
|
276
|
+
const h = String(hn || "").toLowerCase().replace(/^\[/, "").replace(/\]$/, "");
|
|
277
|
+
if (!h) return false;
|
|
278
|
+
if (h === "localhost" || h.endsWith(".localhost")) return true;
|
|
279
|
+
if (h === "::1") return true;
|
|
280
|
+
const m = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(h);
|
|
281
|
+
if (!m) return false;
|
|
282
|
+
if (Number(m[1]) !== 127) return false;
|
|
283
|
+
return [m[2], m[3], m[4]].every((x) => Number(x) <= 255);
|
|
284
|
+
}
|
|
285
|
+
function getConnectionFence(ctx) {
|
|
133
286
|
try {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
287
|
+
const conn = (typeof ctx.get === "function" ? ctx.get("connection") : null) || ctx.connection;
|
|
288
|
+
if (conn && typeof conn === "object" && "requestRejection" in conn && typeof conn.requestRejection === "function") {
|
|
289
|
+
return conn;
|
|
290
|
+
}
|
|
291
|
+
return null;
|
|
292
|
+
} catch {
|
|
293
|
+
return null;
|
|
137
294
|
}
|
|
138
295
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
296
|
+
function selfRejection(req, ctx) {
|
|
297
|
+
try {
|
|
298
|
+
const headers = req?.headers || {};
|
|
299
|
+
let hostUrl;
|
|
300
|
+
try {
|
|
301
|
+
hostUrl = new URL(`http://${String(headers.host || "")}`);
|
|
302
|
+
} catch {
|
|
303
|
+
return 403;
|
|
304
|
+
}
|
|
305
|
+
const fence = getConnectionFence(ctx);
|
|
306
|
+
if (fence) {
|
|
307
|
+
const delegated = fence.requestRejection(req);
|
|
308
|
+
if (delegated) return delegated;
|
|
309
|
+
} else if (!isLoopbackHostname(hostUrl.hostname)) {
|
|
310
|
+
return 403;
|
|
311
|
+
}
|
|
312
|
+
const site = String(headers["sec-fetch-site"] || "").toLowerCase();
|
|
313
|
+
if (site === "cross-site") return 403;
|
|
314
|
+
const origin = headers.origin;
|
|
315
|
+
if (typeof origin === "string" && origin && origin !== "null") {
|
|
316
|
+
let originUrl;
|
|
317
|
+
try {
|
|
318
|
+
originUrl = new URL(origin);
|
|
319
|
+
} catch {
|
|
320
|
+
return 403;
|
|
321
|
+
}
|
|
322
|
+
if (originUrl.host.toLowerCase() !== hostUrl.host.toLowerCase()) return 403;
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
} catch {
|
|
326
|
+
return 403;
|
|
327
|
+
}
|
|
146
328
|
}
|
|
147
329
|
|
|
148
|
-
//
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
330
|
+
// src/host/storage.ts
|
|
331
|
+
import crypto from "node:crypto";
|
|
332
|
+
import fs from "node:fs";
|
|
333
|
+
import fsp from "node:fs/promises";
|
|
334
|
+
import os from "node:os";
|
|
335
|
+
import path from "node:path";
|
|
336
|
+
function dshHome() {
|
|
337
|
+
return process.env.DSH_HOME || path.join(os.homedir(), ".dsh");
|
|
338
|
+
}
|
|
339
|
+
function dataDir() {
|
|
340
|
+
return path.join(dshHome(), "appearances");
|
|
341
|
+
}
|
|
342
|
+
function wallpapersDir() {
|
|
343
|
+
return path.join(dataDir(), "wallpapers");
|
|
344
|
+
}
|
|
345
|
+
function settingsPath() {
|
|
346
|
+
return path.join(dataDir(), "settings.json");
|
|
347
|
+
}
|
|
348
|
+
function wallpaperUrl(id) {
|
|
349
|
+
return `${API_BASE}/wallpaper?id=${encodeURIComponent(id)}`;
|
|
350
|
+
}
|
|
351
|
+
function cloneDefaultSettings() {
|
|
352
|
+
return {
|
|
353
|
+
...DEFAULT_SETTINGS,
|
|
354
|
+
surfaces: { ...DEFAULT_SETTINGS.surfaces },
|
|
355
|
+
sceneParams: {}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
function readSettings() {
|
|
359
|
+
try {
|
|
360
|
+
return sanitizeSettings(JSON.parse(fs.readFileSync(settingsPath(), "utf8")));
|
|
361
|
+
} catch {
|
|
362
|
+
return cloneDefaultSettings();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
function writeSettings(value) {
|
|
366
|
+
fs.mkdirSync(dataDir(), { recursive: true });
|
|
367
|
+
const target = settingsPath();
|
|
368
|
+
const tmp = `${target}.tmp-${process.pid}-${Date.now()}`;
|
|
369
|
+
fs.writeFileSync(tmp, JSON.stringify(value, null, 2));
|
|
370
|
+
fs.renameSync(tmp, target);
|
|
160
371
|
}
|
|
161
|
-
const WALLPAPER_MAX_BYTES = 200 * 1024 * 1024 // 本机本地路由,放宽到 200MB(视频)
|
|
162
|
-
|
|
163
372
|
function listWallpapers() {
|
|
164
|
-
const dir = wallpapersDir()
|
|
165
|
-
let entries
|
|
373
|
+
const dir = wallpapersDir();
|
|
374
|
+
let entries;
|
|
166
375
|
try {
|
|
167
|
-
entries = fs.readdirSync(dir)
|
|
168
|
-
} catch
|
|
169
|
-
return []
|
|
376
|
+
entries = fs.readdirSync(dir);
|
|
377
|
+
} catch {
|
|
378
|
+
return [];
|
|
170
379
|
}
|
|
171
|
-
const items = []
|
|
380
|
+
const items = [];
|
|
172
381
|
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)
|
|
382
|
+
const ext = path.extname(name).toLowerCase();
|
|
383
|
+
const mime = WALLPAPER_MIMES[ext];
|
|
384
|
+
if (!mime) continue;
|
|
385
|
+
const id = name.slice(0, name.length - ext.length);
|
|
177
386
|
try {
|
|
178
|
-
const st = fs.statSync(path.join(dir, name))
|
|
179
|
-
|
|
180
|
-
|
|
387
|
+
const st = fs.statSync(path.join(dir, name));
|
|
388
|
+
if (!st.isFile()) continue;
|
|
389
|
+
items.push({ id, name, mime, size: st.size, mtimeMs: st.mtimeMs, url: wallpaperUrl(id) });
|
|
390
|
+
} catch {
|
|
391
|
+
}
|
|
181
392
|
}
|
|
182
|
-
items.sort((a, b) => b.mtimeMs - a.mtimeMs)
|
|
183
|
-
return items
|
|
393
|
+
items.sort((a, b) => (b.mtimeMs ?? 0) - (a.mtimeMs ?? 0));
|
|
394
|
+
return items;
|
|
184
395
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
396
|
+
async function saveWallpaperBytes(ext, bytes) {
|
|
397
|
+
const dir = wallpapersDir();
|
|
398
|
+
await fsp.mkdir(dir, { recursive: true });
|
|
399
|
+
const id = crypto.randomBytes(8).toString("hex");
|
|
400
|
+
const name = id + ext;
|
|
401
|
+
const file = path.join(dir, name);
|
|
402
|
+
const tmp = `${file}.tmp-${process.pid}-${Date.now()}`;
|
|
403
|
+
await fsp.writeFile(tmp, bytes);
|
|
404
|
+
await fsp.rename(tmp, file);
|
|
405
|
+
return {
|
|
406
|
+
id,
|
|
407
|
+
name,
|
|
408
|
+
mime: WALLPAPER_MIMES[ext],
|
|
409
|
+
size: bytes.length,
|
|
410
|
+
url: wallpaperUrl(id)
|
|
411
|
+
};
|
|
188
412
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
})
|
|
413
|
+
async function deleteWallpaperById(id) {
|
|
414
|
+
const item = listWallpapers().find((w) => w.id === id);
|
|
415
|
+
if (!item) return;
|
|
416
|
+
await fsp.rm(path.join(wallpapersDir(), item.name), { force: true });
|
|
213
417
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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)
|
|
418
|
+
async function clearAllWallpapers() {
|
|
419
|
+
const items = listWallpapers();
|
|
420
|
+
await Promise.all(
|
|
421
|
+
items.map((w) => fsp.rm(path.join(wallpapersDir(), w.name), { force: true }))
|
|
422
|
+
);
|
|
235
423
|
}
|
|
236
424
|
|
|
237
|
-
//
|
|
238
|
-
|
|
239
|
-
|
|
425
|
+
// src/host/window-chrome.ts
|
|
426
|
+
import { createRequire } from "node:module";
|
|
427
|
+
var require2 = createRequire(import.meta.url);
|
|
240
428
|
function withWindows(fn) {
|
|
241
429
|
try {
|
|
242
|
-
const electron =
|
|
243
|
-
const windows = electron
|
|
430
|
+
const electron = require2("electron");
|
|
431
|
+
const windows = electron?.BrowserWindow ? electron.BrowserWindow.getAllWindows() : [];
|
|
244
432
|
for (const win of windows) {
|
|
245
|
-
try {
|
|
433
|
+
try {
|
|
434
|
+
fn(win);
|
|
435
|
+
} catch {
|
|
436
|
+
}
|
|
246
437
|
}
|
|
247
|
-
} catch
|
|
438
|
+
} catch {
|
|
439
|
+
}
|
|
248
440
|
}
|
|
249
|
-
|
|
250
441
|
function hideNativeMenuBar() {
|
|
251
442
|
withWindows((win) => {
|
|
252
|
-
if (typeof win.setAutoHideMenuBar ===
|
|
253
|
-
if (typeof win.setMenuBarVisibility ===
|
|
254
|
-
})
|
|
443
|
+
if (typeof win.setAutoHideMenuBar === "function") win.setAutoHideMenuBar(true);
|
|
444
|
+
if (typeof win.setMenuBarVisibility === "function") win.setMenuBarVisibility(false);
|
|
445
|
+
});
|
|
255
446
|
}
|
|
256
|
-
|
|
257
447
|
function menuBarDebugInfo() {
|
|
258
|
-
let out = { error:
|
|
448
|
+
let out = { error: "no-window", platform: process.platform };
|
|
259
449
|
withWindows((win) => {
|
|
260
450
|
out = {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
451
|
+
platform: process.platform,
|
|
452
|
+
menuBarVisible: typeof win.isMenuBarVisible === "function" ? win.isMenuBarVisible() : null,
|
|
453
|
+
menuBarAutoHide: typeof win.isMenuBarAutoHide === "function" ? win.isMenuBarAutoHide() : null,
|
|
454
|
+
bounds: typeof win.getBounds === "function" ? win.getBounds() : null,
|
|
455
|
+
contentBounds: typeof win.getContentBounds === "function" ? win.getContentBounds() : null
|
|
456
|
+
};
|
|
457
|
+
});
|
|
458
|
+
return out;
|
|
268
459
|
}
|
|
269
460
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
461
|
+
// src/host/routes.ts
|
|
462
|
+
function sendJson(res, status, payload) {
|
|
463
|
+
const body = JSON.stringify(payload);
|
|
464
|
+
res.writeHead(status, {
|
|
465
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
466
|
+
"Cache-Control": "no-store",
|
|
467
|
+
"Content-Length": String(Buffer.byteLength(body))
|
|
468
|
+
});
|
|
469
|
+
res.end(body);
|
|
470
|
+
}
|
|
471
|
+
function readBody(req, maxBytes) {
|
|
472
|
+
return new Promise((resolve, reject) => {
|
|
473
|
+
const chunks = [];
|
|
474
|
+
let total = 0;
|
|
475
|
+
let done = false;
|
|
476
|
+
req.on("data", (chunk) => {
|
|
477
|
+
if (done) return;
|
|
478
|
+
total += chunk.length;
|
|
479
|
+
if (total > maxBytes) {
|
|
480
|
+
done = true;
|
|
481
|
+
reject(new Error("payload too large"));
|
|
482
|
+
req.destroy?.();
|
|
483
|
+
return;
|
|
286
484
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
485
|
+
chunks.push(chunk);
|
|
486
|
+
});
|
|
487
|
+
req.on("end", () => {
|
|
488
|
+
if (!done) resolve(Buffer.concat(chunks));
|
|
489
|
+
});
|
|
490
|
+
req.on("error", (err) => {
|
|
491
|
+
if (!done) reject(err);
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
function resolveVendorDir() {
|
|
496
|
+
const here = path2.dirname(fileURLToPath(import.meta.url));
|
|
497
|
+
const direct = path2.join(here, "vendor", "code-codex");
|
|
498
|
+
if (fs2.existsSync(direct)) return direct;
|
|
499
|
+
return path2.resolve(here, "../../lib/vendor/code-codex");
|
|
500
|
+
}
|
|
501
|
+
function parseByteRange(rangeHeader, size) {
|
|
502
|
+
if (!rangeHeader) return null;
|
|
503
|
+
const m = /^bytes=(\d*)-(\d*)$/i.exec(rangeHeader.trim());
|
|
504
|
+
if (!m) return "invalid";
|
|
505
|
+
const rawStart = m[1];
|
|
506
|
+
const rawEnd = m[2];
|
|
507
|
+
if (rawStart === "" && rawEnd === "") return "invalid";
|
|
508
|
+
let start;
|
|
509
|
+
let end;
|
|
510
|
+
if (rawStart === "") {
|
|
511
|
+
const suffixLen = Number(rawEnd);
|
|
512
|
+
if (!Number.isFinite(suffixLen) || suffixLen <= 0) return "invalid";
|
|
513
|
+
start = Math.max(0, size - suffixLen);
|
|
514
|
+
end = size - 1;
|
|
515
|
+
} else {
|
|
516
|
+
start = Number(rawStart);
|
|
517
|
+
end = rawEnd === "" ? size - 1 : Math.min(size - 1, Number(rawEnd));
|
|
518
|
+
if (!Number.isFinite(start) || !Number.isFinite(end) || start < 0 || start > end || start >= size) {
|
|
519
|
+
return "invalid";
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return { start, end };
|
|
523
|
+
}
|
|
524
|
+
async function sendFileRangeAware(req, res, filePath, mime) {
|
|
525
|
+
const st = await fsp2.stat(filePath);
|
|
526
|
+
const size = st.size;
|
|
527
|
+
const range = parseByteRange(req.headers?.range, size);
|
|
528
|
+
if (range === "invalid") {
|
|
529
|
+
res.writeHead(416, { "Content-Range": `bytes */${size}`, "Content-Length": "0" });
|
|
530
|
+
res.end();
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
const canPipe = typeof res.write === "function" && typeof res.on === "function";
|
|
534
|
+
if (range) {
|
|
535
|
+
const len = range.end - range.start + 1;
|
|
536
|
+
res.writeHead(206, {
|
|
537
|
+
"Content-Type": mime,
|
|
538
|
+
"Accept-Ranges": "bytes",
|
|
539
|
+
"Content-Range": `bytes ${range.start}-${range.end}/${size}`,
|
|
540
|
+
"Content-Length": String(len),
|
|
541
|
+
"Cache-Control": "no-store"
|
|
542
|
+
});
|
|
543
|
+
if (canPipe) {
|
|
544
|
+
fs2.createReadStream(filePath, { start: range.start, end: range.end }).pipe(res);
|
|
545
|
+
} else {
|
|
546
|
+
const fh = await fsp2.open(filePath, "r");
|
|
547
|
+
try {
|
|
548
|
+
const buf = Buffer.allocUnsafe(len);
|
|
549
|
+
await fh.read(buf, 0, len, range.start);
|
|
550
|
+
res.end(buf);
|
|
551
|
+
} finally {
|
|
552
|
+
await fh.close();
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
res.writeHead(200, {
|
|
558
|
+
"Content-Type": mime,
|
|
559
|
+
"Accept-Ranges": "bytes",
|
|
560
|
+
"Content-Length": String(size),
|
|
561
|
+
"Cache-Control": "no-store"
|
|
562
|
+
});
|
|
563
|
+
if (canPipe) {
|
|
564
|
+
fs2.createReadStream(filePath).pipe(res);
|
|
565
|
+
} else {
|
|
566
|
+
res.end(await fsp2.readFile(filePath));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
function registerAppearanceRoutes(ctx) {
|
|
570
|
+
const disposers = [];
|
|
571
|
+
const vendorDir = resolveVendorDir();
|
|
572
|
+
function registerRoute(route) {
|
|
573
|
+
const inner = route.handler;
|
|
574
|
+
disposers.push(
|
|
575
|
+
ctx.webServer.register({
|
|
576
|
+
kind: route.kind,
|
|
577
|
+
path: route.path,
|
|
578
|
+
handler: async (req, res) => {
|
|
579
|
+
const code = selfRejection(req, ctx);
|
|
580
|
+
if (code) {
|
|
581
|
+
sendJson(res, code, { ok: false, error: "rejected by trust fence" });
|
|
582
|
+
return;
|
|
310
583
|
}
|
|
311
|
-
return
|
|
312
|
-
} catch (err) {
|
|
313
|
-
return 403
|
|
584
|
+
return inner(req, res);
|
|
314
585
|
}
|
|
586
|
+
})
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
const serveVendorFile = (file, contentType) => async (_req, res) => {
|
|
590
|
+
try {
|
|
591
|
+
const bytes = await fsp2.readFile(path2.join(vendorDir, file));
|
|
592
|
+
res.writeHead(200, {
|
|
593
|
+
"Content-Type": contentType,
|
|
594
|
+
"Cache-Control": "no-store",
|
|
595
|
+
"Content-Length": String(bytes.length)
|
|
596
|
+
});
|
|
597
|
+
res.end(bytes);
|
|
598
|
+
} catch (err) {
|
|
599
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
600
|
+
sendJson(res, 404, { ok: false, error: msg.slice(0, 120) });
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
registerRoute({
|
|
604
|
+
kind: "exact",
|
|
605
|
+
path: `${API_BASE}/wininfo.json`,
|
|
606
|
+
handler: (_req, res) => sendJson(res, 200, { ok: true, win: menuBarDebugInfo() })
|
|
607
|
+
});
|
|
608
|
+
for (const sceneId of SCENE_IDS) {
|
|
609
|
+
registerRoute({
|
|
610
|
+
kind: "exact",
|
|
611
|
+
path: `${API_BASE}/vendor/${sceneId}.js`,
|
|
612
|
+
handler: serveVendorFile(`${sceneId}.js`, "text/javascript; charset=utf-8")
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
registerRoute({
|
|
616
|
+
kind: "exact",
|
|
617
|
+
path: `${API_BASE}/vendor-galleries.json`,
|
|
618
|
+
handler: serveVendorFile("galleries.json", "application/json; charset=utf-8")
|
|
619
|
+
});
|
|
620
|
+
registerRoute({
|
|
621
|
+
kind: "exact",
|
|
622
|
+
path: `${API_BASE}/vendor-shallow-ocean-thumbs.json`,
|
|
623
|
+
handler: serveVendorFile("shallow-ocean-thumbnails.json", "application/json; charset=utf-8")
|
|
624
|
+
});
|
|
625
|
+
registerRoute({
|
|
626
|
+
kind: "exact",
|
|
627
|
+
path: `${API_BASE}/vendor-media`,
|
|
628
|
+
handler: async (req, res) => {
|
|
629
|
+
try {
|
|
630
|
+
const u = new URL(req.url || "/", "http://localhost");
|
|
631
|
+
const name = u.searchParams.get("file") || "";
|
|
632
|
+
const match = MEDIA_FILE_RE.exec(name);
|
|
633
|
+
if (!match) throw new Error("bad media file");
|
|
634
|
+
const bytes = await fsp2.readFile(path2.join(vendorDir, match[1], "media", name));
|
|
635
|
+
res.writeHead(200, {
|
|
636
|
+
"Content-Type": "image/png",
|
|
637
|
+
"Cache-Control": "no-store",
|
|
638
|
+
"Content-Length": String(bytes.length)
|
|
639
|
+
});
|
|
640
|
+
res.end(bytes);
|
|
641
|
+
} catch (err) {
|
|
642
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
643
|
+
sendJson(res, 404, { ok: false, error: msg.slice(0, 120) });
|
|
315
644
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
registerRoute({
|
|
648
|
+
kind: "exact",
|
|
649
|
+
path: `${API_BASE}/scene-schemas.json`,
|
|
650
|
+
handler: serveVendorFile("scene-schemas.json", "application/json; charset=utf-8")
|
|
651
|
+
});
|
|
652
|
+
registerRoute({
|
|
653
|
+
kind: "exact",
|
|
654
|
+
path: `${API_BASE}/state.json`,
|
|
655
|
+
handler: (_req, res) => {
|
|
656
|
+
const settings = readSettings();
|
|
657
|
+
const wallpapers = listWallpapers();
|
|
658
|
+
sendJson(res, 200, {
|
|
659
|
+
ok: true,
|
|
660
|
+
settings,
|
|
661
|
+
defaults: DEFAULT_SETTINGS,
|
|
662
|
+
scenes: SCENE_IDS,
|
|
663
|
+
wallpapers
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
registerRoute({
|
|
668
|
+
kind: "exact",
|
|
669
|
+
path: `${API_BASE}/state-save.json`,
|
|
670
|
+
handler: async (req, res) => {
|
|
671
|
+
try {
|
|
672
|
+
const raw = await readBody(req, 64 * 1024);
|
|
673
|
+
const parsed = JSON.parse(raw.toString("utf8"));
|
|
674
|
+
const settings = sanitizeSettings(parsed?.settings ?? parsed);
|
|
675
|
+
settings.savedAt = Date.now();
|
|
676
|
+
writeSettings(settings);
|
|
677
|
+
sendJson(res, 200, { ok: true, settings });
|
|
678
|
+
} catch (err) {
|
|
679
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
680
|
+
sendJson(res, 400, { ok: false, error: msg.slice(0, 200) });
|
|
329
681
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
}
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
registerRoute({
|
|
685
|
+
kind: "exact",
|
|
686
|
+
path: `${API_BASE}/wallpaper-upload`,
|
|
687
|
+
handler: async (req, res) => {
|
|
688
|
+
try {
|
|
689
|
+
if (req.method !== "POST") throw new Error("method not allowed");
|
|
690
|
+
const u = new URL(req.url || "/", "http://localhost");
|
|
691
|
+
const rawName = String(u.searchParams.get("name") || "");
|
|
692
|
+
const ext = path2.extname(rawName).toLowerCase();
|
|
693
|
+
if (!WALLPAPER_MIMES[ext]) throw new Error(`unsupported file type: ${ext || "(none)"}`);
|
|
694
|
+
const bytes = await readBody(req, WALLPAPER_MAX_BYTES);
|
|
695
|
+
if (!bytes.length) throw new Error("empty body");
|
|
696
|
+
const item = await saveWallpaperBytes(ext, bytes);
|
|
697
|
+
sendJson(res, 200, {
|
|
698
|
+
ok: true,
|
|
699
|
+
wallpaper: item,
|
|
700
|
+
wallpapers: listWallpapers()
|
|
701
|
+
});
|
|
702
|
+
} catch (err) {
|
|
703
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
704
|
+
sendJson(res, 400, { ok: false, error: msg.slice(0, 200) });
|
|
354
705
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
registerRoute({
|
|
709
|
+
kind: "exact",
|
|
710
|
+
path: `${API_BASE}/wallpaper-delete.json`,
|
|
711
|
+
handler: async (req, res) => {
|
|
712
|
+
try {
|
|
713
|
+
const raw = await readBody(req, 4 * 1024);
|
|
714
|
+
const parsed = JSON.parse(raw.toString("utf8"));
|
|
715
|
+
const id = String(parsed?.id ?? "");
|
|
716
|
+
if (!/^[\w-]+$/.test(id)) throw new Error("bad wallpaper id");
|
|
717
|
+
await deleteWallpaperById(id);
|
|
718
|
+
sendJson(res, 200, { ok: true, wallpapers: listWallpapers() });
|
|
719
|
+
} catch (err) {
|
|
720
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
721
|
+
sendJson(res, 400, { ok: false, error: msg.slice(0, 200) });
|
|
361
722
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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
|
-
},
|
|
723
|
+
}
|
|
724
|
+
});
|
|
725
|
+
registerRoute({
|
|
726
|
+
kind: "exact",
|
|
727
|
+
path: `${API_BASE}/wallpaper-clear.json`,
|
|
728
|
+
handler: async (_req, res) => {
|
|
729
|
+
try {
|
|
730
|
+
await clearAllWallpapers();
|
|
731
|
+
sendJson(res, 200, { ok: true, wallpapers: [] });
|
|
732
|
+
} catch (err) {
|
|
733
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
734
|
+
sendJson(res, 400, { ok: false, error: msg.slice(0, 200) });
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
const serveWallpaper = async (req, res) => {
|
|
739
|
+
try {
|
|
740
|
+
const u = new URL(req.url || "/", "http://localhost");
|
|
741
|
+
const id = String(u.searchParams.get("id") || "");
|
|
742
|
+
if (!/^[\w-]+$/.test(id)) throw new Error("bad wallpaper id");
|
|
743
|
+
const item = listWallpapers().find((w) => w.id === id);
|
|
744
|
+
if (!item) throw new Error("wallpaper not found");
|
|
745
|
+
await sendFileRangeAware(req, res, path2.join(wallpapersDir(), item.name), item.mime);
|
|
746
|
+
} catch (err) {
|
|
747
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
748
|
+
sendJson(res, 404, { ok: false, error: msg.slice(0, 200) });
|
|
749
|
+
}
|
|
750
|
+
};
|
|
751
|
+
registerRoute({ kind: "exact", path: `${API_BASE}/wallpaper`, handler: serveWallpaper });
|
|
752
|
+
registerRoute({ kind: "exact", path: `${API_BASE}/wallpaper.png`, handler: serveWallpaper });
|
|
753
|
+
return () => {
|
|
754
|
+
for (const d of disposers) {
|
|
755
|
+
try {
|
|
756
|
+
d();
|
|
757
|
+
} catch {
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
};
|
|
512
761
|
}
|
|
762
|
+
|
|
763
|
+
// src/host/index.ts
|
|
764
|
+
var index_default = {
|
|
765
|
+
name: PLUGIN_ID,
|
|
766
|
+
apply(root) {
|
|
767
|
+
hideNativeMenuBar();
|
|
768
|
+
root.inject(["webServer"], (ctx) => {
|
|
769
|
+
const dispose = registerAppearanceRoutes(ctx);
|
|
770
|
+
root.effect(() => dispose, `${PLUGIN_ID}: routes`);
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
export {
|
|
775
|
+
DEFAULT_SETTINGS,
|
|
776
|
+
index_default as default,
|
|
777
|
+
sanitizeSettings,
|
|
778
|
+
sanitizeSurfaces
|
|
779
|
+
};
|