dsh-clash-subscription 0.4.2-probe.5 → 0.5.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 -0
- package/README.md +188 -2
- package/client.js +1028 -0
- package/cordis.patch.yml +5 -0
- package/icon.svg +14 -0
- package/index.js +1551 -1
- package/lib/cn-cidr.js +5526 -0
- package/lib/core.js +867 -0
- package/lib/corefetch.js +618 -0
- package/lib/discovery.js +52 -0
- package/lib/geo.js +130 -0
- package/lib/install.js +171 -0
- package/lib/routes.js +309 -0
- package/lib/routing.js +173 -0
- package/lib/select.js +178 -0
- package/lib/subscription.js +334 -0
- package/lib/vendored.js +167 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +67 -6
- package/vendor/SHA256SUMS.json +7 -0
- package/vendor/mihomo.exe.gz +0 -0
package/client.js
ADDED
|
@@ -0,0 +1,1028 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({id:"dsh-clash-subscription",factory:(require)=>{
|
|
2
|
+
const React = require('react')
|
|
3
|
+
const h = React.createElement
|
|
4
|
+
const { Fragment, useEffect, useMemo, useState } = React
|
|
5
|
+
|
|
6
|
+
/** Locale dictionaries, registered as one namespace so every label is translated. */
|
|
7
|
+
const en = {
|
|
8
|
+
summary: 'Import a Clash subscription, pick the exit node, and set how often nodes are re-tested.',
|
|
9
|
+
openPanel: 'Clash proxy',
|
|
10
|
+
title: 'Subscription',
|
|
11
|
+
header: 'Clash subscription proxy',
|
|
12
|
+
close: 'Close',
|
|
13
|
+
subscriptionLabel: 'Import links',
|
|
14
|
+
subscriptionHint:
|
|
15
|
+
'One subscription per line. A bare https:// URL or a clash://install-config?url=… import link both work; put a name in front of a URL to label it. There is no default: the plugin never reads another client’s configuration.',
|
|
16
|
+
subscriptionPlaceholder: 'https://…/subscribe?token=…\nbackup https://…/subscribe?token=…',
|
|
17
|
+
probe: 'Check all links',
|
|
18
|
+
probing: 'Checking…',
|
|
19
|
+
probeOk: 'All {total} subscriptions reachable: {count} nodes, {size} KB.',
|
|
20
|
+
probePartial: '{reachable}/{total} subscriptions reachable — see the rows below.',
|
|
21
|
+
probeFailed: 'Check failed: {reason}',
|
|
22
|
+
probeNeedsSave: 'Save first — this page writes the links, the running core reads the saved ones.',
|
|
23
|
+
subscriptions: 'Subscriptions',
|
|
24
|
+
subscriptionCount: '{count} configured',
|
|
25
|
+
subscriptionRow: '{name}',
|
|
26
|
+
subscriptionNodes: '{count} nodes',
|
|
27
|
+
subscriptionPending: 'not fetched',
|
|
28
|
+
subscriptionUpdated: 'updated {time}',
|
|
29
|
+
refreshOne: 'Refresh this one',
|
|
30
|
+
noSubscriptions: 'No subscription yet — add one above.',
|
|
31
|
+
routing: 'Exit node',
|
|
32
|
+
routingHint: 'The node foreign traffic leaves through. Domestic sites stay direct either way.',
|
|
33
|
+
autoSelect: 'Automatic (lowest latency, working nodes only)',
|
|
34
|
+
currentNode: 'Current: {node}',
|
|
35
|
+
noNodes: 'No node table yet — start routing first.',
|
|
36
|
+
select: 'Use',
|
|
37
|
+
testing: 'Measuring…',
|
|
38
|
+
testNow: 'Re-test now',
|
|
39
|
+
delay: '{ms} ms',
|
|
40
|
+
unreachable: 'no answer',
|
|
41
|
+
untested: 'not measured',
|
|
42
|
+
testAll: 'Measure every node',
|
|
43
|
+
cycle: 'Automatic speed test',
|
|
44
|
+
cycleHint:
|
|
45
|
+
'How often the plugin re-measures its nodes and picks a working exit. 0 turns the cycle off; the button above still works.',
|
|
46
|
+
cycleOff: 'Off',
|
|
47
|
+
cycleEvery: 'Every {minutes} min',
|
|
48
|
+
count: 'Nodes per cycle',
|
|
49
|
+
countHint: 'How many nodes one cycle measures. A long subscription costs time, so the rest stay unmeasured until you pick them.',
|
|
50
|
+
behaviour: 'Behaviour',
|
|
51
|
+
mode: 'Routing mode',
|
|
52
|
+
modeRule: 'Rule (domestic direct)',
|
|
53
|
+
modeGlobal: 'Global (everything through the node)',
|
|
54
|
+
modeHint: 'Rule mode is what keeps Chinese sites unchanged. Global sends every request through the node.',
|
|
55
|
+
autoStart: 'Start with the Harness',
|
|
56
|
+
autoStartHint: 'Start the private core and install the outbound policy as soon as this plugin loads.',
|
|
57
|
+
lists: 'Domain lists',
|
|
58
|
+
directDomains: 'Always direct',
|
|
59
|
+
directDomainsHint: 'Comma-separated domain suffixes that must never use the node, e.g. an intranet host.',
|
|
60
|
+
proxyDomains: 'Always proxied',
|
|
61
|
+
proxyDomainsHint: 'Comma-separated domain suffixes that must always use the node, even if they resolve to a Chinese address.',
|
|
62
|
+
runtime: 'Runtime',
|
|
63
|
+
corePath: 'Core binary',
|
|
64
|
+
corePathHint: 'Path to a mihomo/mihomo-compatible executable. Leave empty to use the copy bundled with this plugin.',
|
|
65
|
+
capability: 'Node capability',
|
|
66
|
+
capabilityHint: 'A node is chosen only when it can reach every required service. Latency alone is not enough: a fast node that OpenAI blocks silently breaks one thing after another.',
|
|
67
|
+
requiredProbes: 'Required services',
|
|
68
|
+
requiredProbesHint: 'Comma-separated probe ids a node must reach, e.g. openai, google. Empty means every probe. Built-ins: gstatic, google, openai, anthropic.',
|
|
69
|
+
probes: 'Extra probes',
|
|
70
|
+
probesHint: 'One id=url per line, for services the built-in list does not cover. Example: hf=https://huggingface.co',
|
|
71
|
+
reselect: 'Re-pick node by capability',
|
|
72
|
+
reselecting: 'Testing nodes…',
|
|
73
|
+
chosenNode: 'Chosen: {node}',
|
|
74
|
+
noUsableNode: 'No node reached every required service.',
|
|
75
|
+
usableCount: '{usable} / {total} nodes usable',
|
|
76
|
+
autoFetchCore: 'Fetch a core automatically',
|
|
77
|
+
autoFetchCoreHint: 'When no mihomo binary is present, download a verified private copy on startup instead of waiting for you to press Download.',
|
|
78
|
+
dns: 'DNS servers',
|
|
79
|
+
dnsHint: 'Comma-separated resolvers the core uses for its own lookups.',
|
|
80
|
+
start: 'Start',
|
|
81
|
+
downloadCore: 'Download core',
|
|
82
|
+
downloadingCore: 'Downloading…',
|
|
83
|
+
coreMissing: 'The bundled core could not be unpacked. This platform has no bundled build — download one here, verified against the publisher’s SHA-256.',
|
|
84
|
+
coreProgress: 'Downloading {mb} MB…',
|
|
85
|
+
coreInstalled: 'Core installed: {tag}',
|
|
86
|
+
coreFailed: 'Core download failed: {reason}',
|
|
87
|
+
coreUnsupported: 'No released core build for this platform; set the core binary path instead.',
|
|
88
|
+
stop: 'Stop',
|
|
89
|
+
refresh: 'Refresh subscription',
|
|
90
|
+
save: 'Save',
|
|
91
|
+
saving: 'Saving…',
|
|
92
|
+
reset: 'Reset',
|
|
93
|
+
saved: 'Saved.',
|
|
94
|
+
rejected: 'The host rejected the change.',
|
|
95
|
+
loading: 'Loading…',
|
|
96
|
+
unavailable: 'Configuration is not editable here.',
|
|
97
|
+
statusRunning: 'Core running at {endpoint}',
|
|
98
|
+
statusStopped: 'Core stopped',
|
|
99
|
+
statusRouting: 'Outbound policy installed',
|
|
100
|
+
statusDirect: 'Outbound policy not installed (direct)',
|
|
101
|
+
measuredAt: 'Last measurement: {time}',
|
|
102
|
+
never: 'never',
|
|
103
|
+
readOnly: 'This panel is read-only here; open it from Settings to edit.',
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const zh = {
|
|
107
|
+
summary: '导入 Clash 订阅、指定出口节点、设定自动测速周期。',
|
|
108
|
+
openPanel: 'Clash 代理',
|
|
109
|
+
title: '订阅',
|
|
110
|
+
header: 'Clash 订阅代理',
|
|
111
|
+
close: '关闭',
|
|
112
|
+
subscriptionLabel: '订阅链接',
|
|
113
|
+
subscriptionHint:
|
|
114
|
+
'一行一条订阅。裸 https:// 地址和 clash://install-config?url=… 一键导入链接都可以;想在面板里有名字,就在地址前加一个名字。没有默认值:插件不会读取其他客户端的配置。',
|
|
115
|
+
subscriptionPlaceholder: 'https://…/subscribe?token=…\n备用 https://…/subscribe?token=…',
|
|
116
|
+
probe: '检测全部',
|
|
117
|
+
probing: '检测中…',
|
|
118
|
+
probeOk: '全部 {total} 条可达:共 {count} 个节点,{size} KB。',
|
|
119
|
+
probePartial: '{reachable}/{total} 条可达——看下面的逐条结果。',
|
|
120
|
+
probeFailed: '检测失败:{reason}',
|
|
121
|
+
probeNeedsSave: '先保存——这个面板写的是配置,跑着的内核读的是已保存的那份。',
|
|
122
|
+
subscriptions: '订阅列表',
|
|
123
|
+
subscriptionCount: '已配置 {count} 条',
|
|
124
|
+
subscriptionRow: '{name}',
|
|
125
|
+
subscriptionNodes: '{count} 个节点',
|
|
126
|
+
subscriptionPending: '未拉到节点',
|
|
127
|
+
subscriptionUpdated: '更新于 {time}',
|
|
128
|
+
refreshOne: '刷新这一条',
|
|
129
|
+
noSubscriptions: '还没有订阅——在上面的输入框里添一条。',
|
|
130
|
+
routing: '出口节点',
|
|
131
|
+
routingHint: '外网流量从这个节点出去;国内站点无论选哪个都保持直连。',
|
|
132
|
+
autoSelect: '自动(选延迟最低的可用节点)',
|
|
133
|
+
currentNode: '当前:{node}',
|
|
134
|
+
noNodes: '还没有节点表——先启动一次。',
|
|
135
|
+
select: '用这个',
|
|
136
|
+
testing: '测速中…',
|
|
137
|
+
testNow: '立即重测',
|
|
138
|
+
delay: '{ms} ms',
|
|
139
|
+
unreachable: '无响应',
|
|
140
|
+
untested: '未测速',
|
|
141
|
+
testAll: '测全部节点',
|
|
142
|
+
cycle: '自动测速',
|
|
143
|
+
cycleHint: '插件多久重新测一次节点并挑一个可用的出口。填 0 关闭周期,上面的按钮仍然可用。',
|
|
144
|
+
cycleOff: '关闭',
|
|
145
|
+
cycleEvery: '每 {minutes} 分钟',
|
|
146
|
+
count: '每轮测几个节点',
|
|
147
|
+
countHint: '一轮最多测多少个节点。节点多时全测很慢,其余的要等你手动选或重测。',
|
|
148
|
+
behaviour: '行为',
|
|
149
|
+
mode: '分流模式',
|
|
150
|
+
modeRule: '规则(国内直连)',
|
|
151
|
+
modeGlobal: '全局(全部走节点)',
|
|
152
|
+
modeHint: '规则模式才是让国内站点完全不受影响的那个。全局会把每个请求都送进节点。',
|
|
153
|
+
autoStart: '随 Harness 启动',
|
|
154
|
+
autoStartHint: '插件一加载就启动私有内核并装上出网策略。',
|
|
155
|
+
lists: '域名名单',
|
|
156
|
+
directDomains: '始终直连',
|
|
157
|
+
directDomainsHint: '逗号分隔的域名后缀,永远不走节点,例如内网主机。',
|
|
158
|
+
proxyDomains: '始终走代理',
|
|
159
|
+
proxyDomainsHint: '逗号分隔的域名后缀,即使解析到中国 IP 也走节点。',
|
|
160
|
+
runtime: '运行时',
|
|
161
|
+
corePath: '内核程序',
|
|
162
|
+
corePathHint: 'mihomo 兼容内核的可执行文件路径。留空则使用插件自带的那个。',
|
|
163
|
+
capability: '节点能力',
|
|
164
|
+
capabilityHint: '只有能访问所有必需服务的节点才会被选中。只看延迟不够:延迟很低但被 OpenAI 屏蔽的节点,会让人以为是插件坏了。',
|
|
165
|
+
requiredProbes: '必需服务',
|
|
166
|
+
requiredProbesHint: '逗号分隔的探测项,节点必须全部可达,例如 openai, google。留空表示全部。内置:gstatic、google、openai、anthropic。',
|
|
167
|
+
probes: '额外探测',
|
|
168
|
+
probesHint: '一行一个 id=url,用于补充内置清单之外的服务。例:hf=https://huggingface.co',
|
|
169
|
+
reselect: '按能力重挑节点',
|
|
170
|
+
reselecting: '正在测节点…',
|
|
171
|
+
chosenNode: '已选:{node}',
|
|
172
|
+
noUsableNode: '没有任何节点能访问全部必需服务。',
|
|
173
|
+
usableCount: '{usable} / {total} 个节点可用',
|
|
174
|
+
autoFetchCore: '自动获取内核',
|
|
175
|
+
autoFetchCoreHint: '机器上没有 mihomo 时,启动时自动下载一份经过校验的私有内核,不用手动点下载。',
|
|
176
|
+
dns: 'DNS 服务器',
|
|
177
|
+
dnsHint: '逗号分隔,内核自己解析时使用。',
|
|
178
|
+
start: '启动',
|
|
179
|
+
downloadCore: '下载内核',
|
|
180
|
+
downloadingCore: '下载中…',
|
|
181
|
+
coreMissing: '自带内核解压失败,或本平台没有自带构建。可以在这里下载一个——会用发布页的 SHA-256 校验,通过才使用。',
|
|
182
|
+
coreProgress: '正在下载 {mb} MB…',
|
|
183
|
+
coreInstalled: '内核已安装:{tag}',
|
|
184
|
+
coreFailed: '内核下载失败:{reason}',
|
|
185
|
+
coreUnsupported: '本平台没有可用的内核构建,请改为手动指定内核路径。',
|
|
186
|
+
stop: '停止',
|
|
187
|
+
refresh: '刷新订阅',
|
|
188
|
+
save: '保存',
|
|
189
|
+
saving: '保存中…',
|
|
190
|
+
reset: '重置',
|
|
191
|
+
saved: '已保存。',
|
|
192
|
+
rejected: '宿主拒绝了这次修改。',
|
|
193
|
+
loading: '加载中…',
|
|
194
|
+
unavailable: '这里无法编辑配置。',
|
|
195
|
+
statusRunning: '内核运行中 {endpoint}',
|
|
196
|
+
statusStopped: '内核未运行',
|
|
197
|
+
statusRouting: '出网策略已安装',
|
|
198
|
+
statusDirect: '出网策略未安装(直连)',
|
|
199
|
+
measuredAt: '上次测速:{time}',
|
|
200
|
+
never: '尚未测过',
|
|
201
|
+
readOnly: '这里是只读预览;要修改请从设置里打开。',
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Format one translated string, replacing `{name}` placeholders.
|
|
206
|
+
*
|
|
207
|
+
* @param template - the dictionary value.
|
|
208
|
+
* @param values - placeholder values by name.
|
|
209
|
+
* @returns the composed string.
|
|
210
|
+
*/
|
|
211
|
+
function fill(template, values) {
|
|
212
|
+
if (template === undefined) return ''
|
|
213
|
+
return String(template).replace(/\{(\w+)\}/g, (match, key) =>
|
|
214
|
+
values !== undefined && values[key] !== undefined ? String(values[key]) : match,
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Read the row form's saved values as a flat object. */
|
|
219
|
+
const readValues = (value) => (value !== null && typeof value === 'object' ? { ...value } : {})
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Split the multi-line subscription field into display rows.
|
|
223
|
+
*
|
|
224
|
+
* A panel function, deliberately not a second parser: the host owns URL validation and
|
|
225
|
+
* duplicate removal, and duplicating that here would let the two disagree. This only
|
|
226
|
+
* needs enough to draw "name — URL" rows before the first status poll lands.
|
|
227
|
+
*
|
|
228
|
+
* @param text - the field value.
|
|
229
|
+
* @returns `{ name, provider, url }` rows.
|
|
230
|
+
*/
|
|
231
|
+
function parseLines(text) {
|
|
232
|
+
const rows = []
|
|
233
|
+
for (const line of String(text ?? '').split(/\r?\n/)) {
|
|
234
|
+
const trimmed = line.trim()
|
|
235
|
+
if (trimmed === '' || trimmed.startsWith('#') || trimmed.startsWith('//')) continue
|
|
236
|
+
const split = trimmed.match(/^(\S+)\s+(\S.*)$/)
|
|
237
|
+
const named = split !== null && !split[1].includes('://') && split[2].includes('://')
|
|
238
|
+
const name = named ? split[1] : ''
|
|
239
|
+
const url = named ? split[2].trim() : trimmed
|
|
240
|
+
const index = rows.length
|
|
241
|
+
rows.push({
|
|
242
|
+
name: name !== '' ? name : `订阅 ${index + 1}`,
|
|
243
|
+
provider: `sub${index}`,
|
|
244
|
+
url,
|
|
245
|
+
index,
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
return rows
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Page styles, registered once per mount and removed with it. */
|
|
252
|
+
const CSS = `
|
|
253
|
+
.clxp { display: flex; flex-direction: column; gap: 14px; font-size: 13px; line-height: 1.5; color: var(--dsh-text-primary, #1f2328); }
|
|
254
|
+
.clxp-status { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; padding: 10px 12px; border: 1px solid var(--dsh-border-default, #d8dee4); border-radius: 8px; background: var(--dsh-surface-subtle, #f6f8fa); }
|
|
255
|
+
.clxp-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dsh-text-tertiary, #8b949e); flex: 0 0 auto; }
|
|
256
|
+
.clxp-dot.on { background: #2da44e; }
|
|
257
|
+
.clxp-dot.warn { background: #bf8700; }
|
|
258
|
+
.clxp-status-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
|
|
259
|
+
.clxp-status-actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
|
|
260
|
+
.clxp-card { border: 1px solid var(--dsh-border-default, #d8dee4); border-radius: 8px; padding: 13px; display: flex; flex-direction: column; gap: 11px; }
|
|
261
|
+
.clxp-card > h4 { margin: 0; font-size: 13px; font-weight: 600; }
|
|
262
|
+
.clxp-field { display: flex; flex-direction: column; gap: 4px; }
|
|
263
|
+
.clxp-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 11px; }
|
|
264
|
+
.clxp-label { font-weight: 500; }
|
|
265
|
+
.clxp-hint { color: var(--dsh-text-secondary, #57606a); font-size: 12px; margin: 0; }
|
|
266
|
+
.clxp-error { color: #cf222e; font-size: 12px; margin: 0; }
|
|
267
|
+
.clxp-ok { color: #1a7f37; font-size: 12px; margin: 0; }
|
|
268
|
+
.clxp input[type="text"], .clxp input[type="number"], .clxp select { font: inherit; padding: 5px 8px; border: 1px solid var(--dsh-border-default, #d8dee4); border-radius: 6px; background: var(--dsh-surface-default, #fff); color: inherit; min-width: 0; }
|
|
269
|
+
.clxp input:focus, .clxp select:focus { outline: 2px solid var(--dsh-accent, #0969da); outline-offset: -1px; }
|
|
270
|
+
.clxp-inline { display: flex; gap: 6px; align-items: flex-start; }
|
|
271
|
+
.clxp-inline > input, .clxp-inline > textarea { flex: 1 1 auto; }
|
|
272
|
+
.clxp-field.wide { grid-column: 1 / -1; }
|
|
273
|
+
.clxp-pre { margin: 0; padding: 6px 8px; border-radius: 6px; background: var(--dsh-surface-subtle, #f6f8fa); font-size: 12px; white-space: pre-wrap; word-break: break-all; }
|
|
274
|
+
.clxp-subs { display: flex; flex-direction: column; gap: 6px; }
|
|
275
|
+
.clxp-sub { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; padding: 6px 8px; border: 1px solid var(--dsh-border-muted, #eaeef2); border-radius: 6px; }
|
|
276
|
+
.clxp-sub .clxp-node-name { flex: 1 1 140px; }
|
|
277
|
+
.clxp-probe { display: flex; flex-direction: column; gap: 2px; }
|
|
278
|
+
.clxp-probe > p { margin: 0; }
|
|
279
|
+
.clxp button { font: inherit; padding: 4px 10px; border-radius: 6px; border: 1px solid var(--dsh-border-default, #d8dee4); background: var(--dsh-surface-default, #fff); color: inherit; cursor: pointer; }
|
|
280
|
+
.clxp button:hover:not(:disabled) { background: var(--dsh-surface-subtle, #f6f8fa); }
|
|
281
|
+
.clxp button:disabled { opacity: 0.55; cursor: default; }
|
|
282
|
+
.clxp button.clxp-primary { background: var(--dsh-accent, #0969da); border-color: var(--dsh-accent, #0969da); color: #fff; font-weight: 500; }
|
|
283
|
+
.clxp-nodes { display: flex; flex-direction: column; max-height: 300px; overflow: auto; border: 1px solid var(--dsh-border-default, #d8dee4); border-radius: 6px; }
|
|
284
|
+
.clxp-node { display: grid; grid-template-columns: 1fr auto auto; gap: 10px; align-items: center; padding: 5px 8px; border: 0; border-radius: 0; text-align: left; background: none; width: 100%; }
|
|
285
|
+
.clxp-node + .clxp-node { border-top: 1px solid var(--dsh-border-muted, #eaeef2); }
|
|
286
|
+
.clxp-node-act { padding: 1px 6px; font-size: 12px; line-height: 1.4; border-radius: 4px; }
|
|
287
|
+
.clxp-node.current { background: var(--dsh-surface-subtle, #f6f8fa); font-weight: 600; }
|
|
288
|
+
.clxp-node-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
289
|
+
.clxp-delay { font-variant-numeric: tabular-nums; color: var(--dsh-text-secondary, #57606a); font-size: 12px; }
|
|
290
|
+
.clxp-delay.good { color: #1a7f37; }
|
|
291
|
+
.clxp-delay.bad { color: #cf222e; }
|
|
292
|
+
.clxp-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
|
|
293
|
+
.clxp-savebar { position: sticky; bottom: 0; z-index: 2; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 10px 0 4px; margin-top: 4px; background: linear-gradient(to top, var(--dsh-surface-default, #fff) 72%, transparent); }
|
|
294
|
+
.clxp-dirty { font-size: 12px; color: #bf8700; }
|
|
295
|
+
.clxp-note { color: var(--dsh-text-secondary, #57606a); font-size: 12px; margin: 0; }
|
|
296
|
+
.clxp-disclosure { display: flex; align-items: center; gap: 6px; background: none; border: 0; padding: 0; font-weight: 600; cursor: pointer; color: inherit; }
|
|
297
|
+
.clxp-checkbox { display: flex; align-items: center; gap: 8px; }
|
|
298
|
+
`
|
|
299
|
+
|
|
300
|
+
/** Section definitions for the form. */
|
|
301
|
+
function sections(t) {
|
|
302
|
+
return [
|
|
303
|
+
{
|
|
304
|
+
id: 'subscription',
|
|
305
|
+
title: t('title'),
|
|
306
|
+
fields: [
|
|
307
|
+
{ key: 'subscriptionUrls', kind: 'textarea', label: t('subscriptionLabel'), hint: t('subscriptionHint'), placeholder: t('subscriptionPlaceholder'), probe: true },
|
|
308
|
+
],
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
id: 'behaviour',
|
|
312
|
+
title: t('behaviour'),
|
|
313
|
+
fields: [
|
|
314
|
+
{ key: 'mode', kind: 'select', label: t('mode'), hint: t('modeHint'), options: [{ value: 'rule', label: t('modeRule') }, { value: 'global', label: t('modeGlobal') }] },
|
|
315
|
+
{ key: 'autoStart', kind: 'switch', label: t('autoStart'), hint: t('autoStartHint') },
|
|
316
|
+
{ key: 'speedTestMinutes', kind: 'number', label: t('cycle'), hint: t('cycleHint'), min: 0, max: 1440, step: 5 },
|
|
317
|
+
{ key: 'speedTestCount', kind: 'number', label: t('count'), hint: t('countHint'), min: 1, max: 40, step: 1 },
|
|
318
|
+
],
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
id: 'capability',
|
|
322
|
+
title: t('capability'),
|
|
323
|
+
fields: [
|
|
324
|
+
{ key: 'requiredProbes', kind: 'text', label: t('requiredProbes'), hint: t('requiredProbesHint'), placeholder: 'openai, google' },
|
|
325
|
+
{ key: 'probes', kind: 'text', label: t('probes'), hint: t('probesHint'), placeholder: 'hf=https://huggingface.co' },
|
|
326
|
+
],
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
id: 'lists',
|
|
330
|
+
title: t('lists'),
|
|
331
|
+
collapsed: true,
|
|
332
|
+
fields: [
|
|
333
|
+
{ key: 'directDomains', kind: 'text', label: t('directDomains'), hint: t('directDomainsHint'), placeholder: 'intranet.example, corp.example' },
|
|
334
|
+
{ key: 'proxyDomains', kind: 'text', label: t('proxyDomains'), hint: t('proxyDomainsHint'), placeholder: 'openai.com, githubusercontent.com' },
|
|
335
|
+
{ key: 'dnsServers', kind: 'text', label: t('dns'), hint: t('dnsHint'), placeholder: '223.5.5.5, 119.29.29.29' },
|
|
336
|
+
{ key: 'corePath', kind: 'text', label: t('corePath'), hint: t('corePathHint'), placeholder: 'C:\\path\\to\\mihomo.exe' },
|
|
337
|
+
{ key: 'autoFetchCore', kind: 'switch', label: t('autoFetchCore'), hint: t('autoFetchCoreHint') },
|
|
338
|
+
],
|
|
339
|
+
},
|
|
340
|
+
]
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The whole panel: live status, node table, and every configurable field.
|
|
345
|
+
*
|
|
346
|
+
* One component serves three mounts — the row configuration page, the sidebar
|
|
347
|
+
* entry's sheet, and (when editable) the same sheet from the footer — so a value
|
|
348
|
+
* typed in one place renders identically in the others.
|
|
349
|
+
*
|
|
350
|
+
* @param props - see {@link CONFIG_FIELDS}; `editable` decides whether fields render as controls.
|
|
351
|
+
* @returns the panel.
|
|
352
|
+
*/
|
|
353
|
+
function Panel(props) {
|
|
354
|
+
const { t, draft, live, delays, busy, message, probe, saving, dirty, open, editable } = props
|
|
355
|
+
const nodes = Array.isArray(live?.nodes) ? live.nodes : []
|
|
356
|
+
const subscriptions = Array.isArray(live?.subscriptions) && live.subscriptions.length > 0
|
|
357
|
+
? live.subscriptions
|
|
358
|
+
: // Before the first status poll resolves, fall back to the draft's own lines so the
|
|
359
|
+
// list does not flash "no subscription" on a configured profile.
|
|
360
|
+
parseLines(editable ? draft.subscriptionUrls : live?.config?.subscriptionUrls)
|
|
361
|
+
const providers = Array.isArray(live?.providers) ? live.providers : []
|
|
362
|
+
const running = live?.running === true
|
|
363
|
+
const routingOn = live?.routing === true
|
|
364
|
+
const [expanded, setExpanded] = useState({})
|
|
365
|
+
|
|
366
|
+
// Three distinct states, not two. A node the cycle has not reached yet is "未测速",
|
|
367
|
+
// not "无响应": a 40-node subscription is measured `speedTestCount` at a time, so
|
|
368
|
+
// painting the unmeasured remainder as dead is a lie the panel used to tell.
|
|
369
|
+
const delayClass = (value) => (value === undefined ? 'clxp-delay' : value === null ? 'clxp-delay bad' : value < 400 ? 'clxp-delay good' : 'clxp-delay')
|
|
370
|
+
const delayText = (value) =>
|
|
371
|
+
value === undefined ? t('untested') : value === null ? t('unreachable') : fill(t('delay'), { ms: value })
|
|
372
|
+
|
|
373
|
+
const field = (spec) => {
|
|
374
|
+
const value = editable ? draft[spec.key] : live?.config?.[spec.key]
|
|
375
|
+
// The multi-line subscription editor needs the full width: a grid cell clips both a
|
|
376
|
+
// long URL and the "one per line" shape the field is asking for.
|
|
377
|
+
const wide = spec.kind === 'textarea'
|
|
378
|
+
if (!editable) {
|
|
379
|
+
return h('div', { className: `clxp-field${wide ? ' wide' : ''}`, key: spec.key }, [
|
|
380
|
+
h('span', { key: 'l', className: 'clxp-label' }, spec.label),
|
|
381
|
+
wide
|
|
382
|
+
? h('pre', { key: 'v', className: 'clxp-pre' }, value === '' || value === undefined ? '—' : String(value))
|
|
383
|
+
: h('code', { key: 'v' }, value === '' || value === undefined ? '—' : String(value)),
|
|
384
|
+
])
|
|
385
|
+
}
|
|
386
|
+
const control =
|
|
387
|
+
spec.kind === 'switch'
|
|
388
|
+
? h('label', { className: 'clxp-checkbox' }, [
|
|
389
|
+
h('input', { key: 'i', type: 'checkbox', checked: value !== false, disabled: saving, onChange: (event) => props.onEdit(spec.key, event.target.checked) }),
|
|
390
|
+
h('span', { key: 't', className: 'clxp-hint' }, spec.hint ?? ''),
|
|
391
|
+
])
|
|
392
|
+
: spec.kind === 'select'
|
|
393
|
+
? h('select', { value: value ?? spec.options[0].value, disabled: saving, onChange: (event) => props.onEdit(spec.key, event.target.value) },
|
|
394
|
+
spec.options.map((option) => h('option', { key: option.value, value: option.value }, option.label)))
|
|
395
|
+
: spec.kind === 'number'
|
|
396
|
+
? h('input', { type: 'number', value: value ?? 0, min: spec.min, max: spec.max, step: spec.step, disabled: saving, onChange: (event) => props.onEdit(spec.key, event.target.value === '' ? '' : Number(event.target.value)) })
|
|
397
|
+
: h('textarea', {
|
|
398
|
+
rows: 4,
|
|
399
|
+
value: value ?? '',
|
|
400
|
+
placeholder: spec.placeholder ?? '',
|
|
401
|
+
disabled: saving,
|
|
402
|
+
spellCheck: false,
|
|
403
|
+
onChange: (event) => props.onEdit(spec.key, event.target.value),
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
return h('div', { className: `clxp-field${wide ? ' wide' : ''}`, key: spec.key }, [
|
|
407
|
+
h('span', { key: 'l', className: 'clxp-label' }, spec.label),
|
|
408
|
+
spec.probe
|
|
409
|
+
? h('div', { key: 'r', className: 'clxp-inline' }, [control, h('button', { key: 'p', type: 'button', disabled: saving || busy === 'probe', onClick: () => void props.onProbe() }, busy === 'probe' ? t('probing') : t('probe'))])
|
|
410
|
+
: control,
|
|
411
|
+
spec.hint !== undefined && spec.kind !== 'switch' ? h('p', { key: 'h', className: 'clxp-hint' }, spec.hint) : null,
|
|
412
|
+
spec.key === 'subscriptionUrls' && probe !== undefined ? probeReport(probe) : null,
|
|
413
|
+
].filter(Boolean))
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Render the multi-subscription probe answer.
|
|
418
|
+
*
|
|
419
|
+
* One line per subscription, because the whole reason to support several is that
|
|
420
|
+
* one of them breaks while the others keep working — a single OK/FAIL hides exactly
|
|
421
|
+
* the case a user needs to see.
|
|
422
|
+
*
|
|
423
|
+
* @param answer - the `/clash-proxy/probe` body.
|
|
424
|
+
* @returns the report element.
|
|
425
|
+
*/
|
|
426
|
+
const probeReport = (answer) => {
|
|
427
|
+
const results = Array.isArray(answer.results) ? answer.results : []
|
|
428
|
+
const headline =
|
|
429
|
+
results.length <= 1
|
|
430
|
+
? answer.ok
|
|
431
|
+
? fill(t('probeOk'), { total: 1, count: answer.nodeCount ?? 0, size: Math.round((answer.bytes ?? 0) / 1024) })
|
|
432
|
+
: fill(t('probeFailed'), { reason: answer.error ?? 'unknown' })
|
|
433
|
+
: answer.ok
|
|
434
|
+
? fill(t('probeOk'), { total: answer.total ?? results.length, count: answer.nodeCount ?? 0, size: Math.round((answer.bytes ?? 0) / 1024) })
|
|
435
|
+
: fill(t('probePartial'), { reachable: answer.reachable ?? 0, total: answer.total ?? results.length })
|
|
436
|
+
return h('div', { key: 'pr', className: 'clxp-probe', role: 'status' }, [
|
|
437
|
+
h('p', { key: 'h', className: answer.ok ? 'clxp-ok' : 'clxp-error' }, headline),
|
|
438
|
+
...results.map((row) =>
|
|
439
|
+
h('p', { key: `r-${row.provider ?? row.url}`, className: row.ok ? 'clxp-note' : 'clxp-error' }, [
|
|
440
|
+
h('span', { key: 'n' }, `${row.name ?? row.url}:`),
|
|
441
|
+
row.ok
|
|
442
|
+
? `${fill(t('subscriptionNodes'), { count: row.nodeCount ?? 0 })},${row.format ?? ''},${Math.round((row.bytes ?? 0) / 1024)} KB`
|
|
443
|
+
: (row.error ?? `the server answered ${row.status}`),
|
|
444
|
+
]),
|
|
445
|
+
),
|
|
446
|
+
...(Array.isArray(answer.errors) && answer.errors.length > 0
|
|
447
|
+
? answer.errors.map((row) => h('p', { key: `e-${row.line}`, className: 'clxp-error' }, `第 ${row.line} 行:${row.reason}`))
|
|
448
|
+
: []),
|
|
449
|
+
])
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const sectionsList = sections(t)
|
|
453
|
+
|
|
454
|
+
return h('div', { className: 'clxp' }, [
|
|
455
|
+
// Live state.
|
|
456
|
+
h('div', { key: 'status', className: 'clxp-status' }, [
|
|
457
|
+
h('span', { key: 'd', className: `clxp-dot${running ? (routingOn ? ' on' : ' warn') : ''}` }),
|
|
458
|
+
h('div', { key: 't', className: 'clxp-status-text' }, [
|
|
459
|
+
h('span', { key: 'c' }, running ? fill(t('statusRunning'), { endpoint: live?.endpoint ?? '' }) : t('statusStopped')),
|
|
460
|
+
h('span', { key: 'p', className: 'clxp-note' }, routingOn ? t('statusRouting') : t('statusDirect')),
|
|
461
|
+
live?.measuredAt !== undefined && live.measuredAt !== null
|
|
462
|
+
? h('span', { key: 'm', className: 'clxp-note' }, fill(t('measuredAt'), { time: new Date(live.measuredAt).toLocaleTimeString() }))
|
|
463
|
+
: null,
|
|
464
|
+
].filter(Boolean)),
|
|
465
|
+
h('div', { key: 'a', className: 'clxp-status-actions' }, [
|
|
466
|
+
h('button', { key: 'tg', type: 'button', className: 'clxp-primary', disabled: busy !== '', onClick: () => void props.onAction(running ? 'stop' : 'start') }, running ? t('stop') : t('start')),
|
|
467
|
+
h('button', { key: 'rf', type: 'button', disabled: busy !== '' || !running, onClick: () => void props.onAction('refresh') }, t('refresh')),
|
|
468
|
+
]),
|
|
469
|
+
]),
|
|
470
|
+
live?.blocker !== undefined ? h('p', { key: 'bl', className: 'clxp-error' }, live.blocker) : null,
|
|
471
|
+
|
|
472
|
+
// No core yet: this is the one blocker the panel can fix without the user finding a
|
|
473
|
+
// binary themselves, so it gets a control instead of only being named.
|
|
474
|
+
live?.ready === false
|
|
475
|
+
? h('section', { key: 'core', className: 'clxp-card' }, [
|
|
476
|
+
h('h4', { key: 'ti' }, t('corePath')),
|
|
477
|
+
h('p', { key: 'h', className: 'clxp-hint' }, t('coreMissing')),
|
|
478
|
+
h('div', { key: 'a', className: 'clxp-actions' }, [
|
|
479
|
+
h('button', {
|
|
480
|
+
key: 'd',
|
|
481
|
+
type: 'button',
|
|
482
|
+
className: 'clxp-primary',
|
|
483
|
+
disabled: busy !== '' || live?.platformSupported === false,
|
|
484
|
+
onClick: () => void props.onAction('bootstrap'),
|
|
485
|
+
}, busy === 'bootstrap' ? t('downloadingCore') : t('downloadCore')),
|
|
486
|
+
]),
|
|
487
|
+
live?.platformSupported === false
|
|
488
|
+
? h('p', { key: 'u', className: 'clxp-error' }, t('coreUnsupported'))
|
|
489
|
+
: null,
|
|
490
|
+
live?.bootstrap !== undefined
|
|
491
|
+
? h('p', { key: 'p', className: live.bootstrap.phase === 'failed' ? 'clxp-error' : 'clxp-note', role: 'status' },
|
|
492
|
+
live.bootstrap.phase === 'failed'
|
|
493
|
+
? fill(t('coreFailed'), { reason: live.bootstrap.error ?? 'unknown' })
|
|
494
|
+
: live.bootstrap.phase === 'done'
|
|
495
|
+
? fill(t('coreInstalled'), { tag: live.bootstrap.tag ?? '' })
|
|
496
|
+
: live.bootstrap.phase === 'downloading'
|
|
497
|
+
? fill(t('coreProgress'), { mb: (Math.round(((live.bootstrap.received ?? 0) / 1024 / 1024) * 10) / 10).toFixed(1) })
|
|
498
|
+
: live.bootstrap.phase)
|
|
499
|
+
: null,
|
|
500
|
+
])
|
|
501
|
+
: null,
|
|
502
|
+
|
|
503
|
+
// Subscription rows: one per configured link, with its own fetch state. This is the
|
|
504
|
+
// surface that makes several subscriptions manageable — a single "nodes: 54" cannot
|
|
505
|
+
// say which provider is stale or dead.
|
|
506
|
+
h('section', { key: 'subs', className: 'clxp-card' }, [
|
|
507
|
+
h('h4', { key: 'ti' }, t('subscriptions')),
|
|
508
|
+
h('p', { key: 'ct', className: 'clxp-note' },
|
|
509
|
+
subscriptions.length === 0
|
|
510
|
+
? t('noSubscriptions')
|
|
511
|
+
: fill(t('subscriptionCount'), { count: subscriptions.length })),
|
|
512
|
+
subscriptions.length === 0
|
|
513
|
+
? null
|
|
514
|
+
: h('div', { key: 'ls', className: 'clxp-subs' },
|
|
515
|
+
subscriptions.map((row) => {
|
|
516
|
+
const state = providers.find((candidate) => candidate.provider === row.provider)
|
|
517
|
+
return h('div', { key: row.provider, className: 'clxp-sub' }, [
|
|
518
|
+
h('span', { key: 'n', className: 'clxp-node-name', title: row.url }, row.name),
|
|
519
|
+
h('span', {
|
|
520
|
+
key: 's',
|
|
521
|
+
className: `clxp-delay${state === undefined ? '' : state.ok ? ' good' : ' bad'}`,
|
|
522
|
+
}, state === undefined
|
|
523
|
+
? t('subscriptionPending')
|
|
524
|
+
: fill(t('subscriptionNodes'), { count: state.nodeCount ?? 0 })),
|
|
525
|
+
state?.updatedAt !== undefined && state.updatedAt !== null
|
|
526
|
+
? h('span', { key: 'u', className: 'clxp-note' }, fill(t('subscriptionUpdated'), { time: new Date(state.updatedAt).toLocaleTimeString() }))
|
|
527
|
+
: null,
|
|
528
|
+
h('button', {
|
|
529
|
+
key: 'r',
|
|
530
|
+
type: 'button',
|
|
531
|
+
className: 'clxp-node-act',
|
|
532
|
+
title: `${t('refreshOne')}: ${row.name}`,
|
|
533
|
+
'aria-label': `${t('refreshOne')}: ${row.name}`,
|
|
534
|
+
disabled: busy !== '' || !running,
|
|
535
|
+
onClick: () => void props.onAction('refreshOne', { provider: row.provider }),
|
|
536
|
+
}, '⟳'),
|
|
537
|
+
].filter(Boolean))
|
|
538
|
+
})),
|
|
539
|
+
]),
|
|
540
|
+
|
|
541
|
+
// Exit node table.
|
|
542
|
+
h('section', { key: 'nodes', className: 'clxp-card' }, [
|
|
543
|
+
h('h4', { key: 'ti' }, t('routing')),
|
|
544
|
+
h('p', { key: 'hi', className: 'clxp-hint' }, t('routingHint')),
|
|
545
|
+
h('p', { key: 'cu', className: 'clxp-note' }, fill(t('currentNode'), { node: live?.node ?? t('autoSelect') })),
|
|
546
|
+
live?.capability !== undefined && Array.isArray(live.capability.nodes) && live.capability.nodes.length > 0
|
|
547
|
+
? h('p', { key: 'cp', className: 'clxp-note' }, fill(t('usableCount'), {
|
|
548
|
+
usable: live.capability.nodes.filter((row) => row.usable).length,
|
|
549
|
+
total: live.capability.nodes.length,
|
|
550
|
+
}) + ' · ' + t('requiredProbes') + ': ' + (live.capability.required ?? []).join(', '))
|
|
551
|
+
: null,
|
|
552
|
+
live?.capability !== undefined && Array.isArray(live.capability.nodes)
|
|
553
|
+
? h('div', { key: 'cpl', className: 'clxp-subs' },
|
|
554
|
+
live.capability.nodes
|
|
555
|
+
.filter((row) => !row.usable)
|
|
556
|
+
.slice(0, 6)
|
|
557
|
+
.map((row) => h('div', { key: row.name, className: 'clxp-sub' }, [
|
|
558
|
+
h('span', { key: 'n', className: 'clxp-node-name' }, row.name),
|
|
559
|
+
h('span', { key: 'f', className: 'clxp-delay bad' }, (row.failed ?? []).join(', ') || '-'),
|
|
560
|
+
])))
|
|
561
|
+
: null,
|
|
562
|
+
|
|
563
|
+
nodes.length === 0
|
|
564
|
+
? h('p', { key: 'em', className: 'clxp-note' }, t('noNodes'))
|
|
565
|
+
: h('div', { key: 'ls', className: 'clxp-nodes', role: 'listbox', 'aria-label': t('routing') },
|
|
566
|
+
nodes.map((name) => h('div', {
|
|
567
|
+
key: name,
|
|
568
|
+
className: `clxp-node${name === live?.node ? ' current' : ''}`,
|
|
569
|
+
role: 'option',
|
|
570
|
+
'aria-selected': name === live?.node,
|
|
571
|
+
}, [
|
|
572
|
+
h('span', { key: 'n', className: 'clxp-node-name', title: name }, name),
|
|
573
|
+
h('span', { key: 'd', className: delayClass(delays[name]) }, delayText(delays[name])),
|
|
574
|
+
h('button', {
|
|
575
|
+
key: 't',
|
|
576
|
+
type: 'button',
|
|
577
|
+
className: 'clxp-node-act',
|
|
578
|
+
title: `${t('testNow')}: ${name}`,
|
|
579
|
+
'aria-label': `${t('testNow')}: ${name}`,
|
|
580
|
+
disabled: busy !== '' || !running,
|
|
581
|
+
onClick: () => void props.onAction('test', { node: name }),
|
|
582
|
+
}, '⟳'),
|
|
583
|
+
h('button', {
|
|
584
|
+
key: 'p',
|
|
585
|
+
type: 'button',
|
|
586
|
+
className: 'clxp-node-act',
|
|
587
|
+
disabled: busy !== '' || !running || name === live?.node,
|
|
588
|
+
onClick: () => void props.onAction('select', { node: name }),
|
|
589
|
+
}, name === live?.node ? '✓' : t('select')),
|
|
590
|
+
]))),
|
|
591
|
+
h('div', { key: 'ac', className: 'clxp-actions' }, [
|
|
592
|
+
h('button', { key: 'te', type: 'button', disabled: busy !== '' || !running, onClick: () => void props.onAction('test') }, busy === 'test' ? t('testing') : t('testNow')),
|
|
593
|
+
h('button', { key: 'ta', type: 'button', disabled: busy !== '' || !running, onClick: () => void props.onAction('test', { all: true }) }, busy === 'test' ? t('testing') : t('testAll')),
|
|
594
|
+
h('button', { key: 'rs', type: 'button', className: 'clxp-primary', disabled: busy !== '' || !running, onClick: () => void props.onAction('reselect') }, busy === 'reselect' ? t('reselecting') : t('reselect')),
|
|
595
|
+
]),
|
|
596
|
+
]),
|
|
597
|
+
|
|
598
|
+
// Configurable fields.
|
|
599
|
+
...sectionsList.map((group) => {
|
|
600
|
+
const isOpen = expanded[group.id] ?? group.collapsed !== true
|
|
601
|
+
const head = h('h4', null, group.title)
|
|
602
|
+
return h('section', { key: group.id, className: 'clxp-card' }, [
|
|
603
|
+
group.collapsed === true
|
|
604
|
+
? h('button', { key: 'hd', type: 'button', className: 'clxp-disclosure', 'aria-expanded': isOpen, onClick: () => setExpanded({ ...expanded, [group.id]: !isOpen }) },
|
|
605
|
+
[h('span', { key: 'c', 'aria-hidden': true }, isOpen ? '▾' : '▸'), head])
|
|
606
|
+
: head,
|
|
607
|
+
isOpen ? h('div', { key: 'bd', className: 'clxp-grid' }, group.fields.map(field)) : null,
|
|
608
|
+
])
|
|
609
|
+
}),
|
|
610
|
+
|
|
611
|
+
// A missing subscription is the one blocker a user can fix here, so it is named
|
|
612
|
+
// explicitly next to the save control instead of only in the status blob.
|
|
613
|
+
editable && String(props.draft?.subscriptionUrls ?? '').trim() === '' && String(props.draft?.subscriptionUrl ?? '').trim() === '' && props.live?.source !== 'configured'
|
|
614
|
+
? h('p', { key: 'need', className: 'clxp-error', role: 'status' }, '还没有订阅链接:粘贴到上方「订阅链接」(一行一条)并保存,内核才会启动。')
|
|
615
|
+
: null,
|
|
616
|
+
|
|
617
|
+
editable
|
|
618
|
+
? h('div', { key: 'sub', className: 'clxp-savebar' }, [
|
|
619
|
+
h('button', { key: 's', type: 'submit', className: 'clxp-primary', disabled: saving }, saving ? t('saving') : t('save')),
|
|
620
|
+
h('button', { key: 'r', type: 'button', disabled: saving || !dirty, onClick: () => props.onReset() }, t('reset')),
|
|
621
|
+
dirty
|
|
622
|
+
? h('span', { key: 'd', className: 'clxp-dirty' }, '有未保存的修改')
|
|
623
|
+
: h('span', { key: 'c', className: 'clxp-note' }, '已保存'),
|
|
624
|
+
message !== '' ? h('span', { key: 'm', className: 'clxp-note', role: 'status' }, message) : null,
|
|
625
|
+
].filter(Boolean))
|
|
626
|
+
: null,
|
|
627
|
+
live?.blocker === undefined && !editable ? null : null,
|
|
628
|
+
].filter(Boolean))
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* The row configuration page: owns the draft, saving, and the live status poll.
|
|
633
|
+
*
|
|
634
|
+
* @param props - the host's slot props.
|
|
635
|
+
* @returns the page.
|
|
636
|
+
*/
|
|
637
|
+
function RowConfigPage(props) {
|
|
638
|
+
const { form, t } = props
|
|
639
|
+
const [draft, setDraft] = useState(() => readValues(form?.state.value))
|
|
640
|
+
const [baseline, setBaseline] = useState(() => readValues(form?.state.value))
|
|
641
|
+
const [revision, setRevision] = useState(form?.state.revision)
|
|
642
|
+
const [dirty, setDirty] = useState(false)
|
|
643
|
+
const [saving, setSaving] = useState(false)
|
|
644
|
+
const [message, setMessage] = useState('')
|
|
645
|
+
const [live, setLive] = useState()
|
|
646
|
+
const [liveError, setLiveError] = useState()
|
|
647
|
+
const [busy, setBusy] = useState('')
|
|
648
|
+
const [probe, setProbe] = useState()
|
|
649
|
+
|
|
650
|
+
const ready = form?.state.status === 'ready' && form.state.writable !== false
|
|
651
|
+
|
|
652
|
+
useEffect(() => {
|
|
653
|
+
if (dirty) return
|
|
654
|
+
setDraft(readValues(form?.state.value))
|
|
655
|
+
setBaseline(readValues(form?.state.value))
|
|
656
|
+
setRevision(form?.state.revision)
|
|
657
|
+
}, [form?.state.revision, form?.state.status, dirty])
|
|
658
|
+
|
|
659
|
+
useEffect(() => {
|
|
660
|
+
let alive = true
|
|
661
|
+
const load = async () => {
|
|
662
|
+
try {
|
|
663
|
+
const response = await fetch('/clash-proxy/status', { credentials: 'same-origin' })
|
|
664
|
+
if (!response.ok) throw new Error(`status ${response.status}`)
|
|
665
|
+
const data = await response.json()
|
|
666
|
+
if (alive) {
|
|
667
|
+
setLive(data)
|
|
668
|
+
setLiveError(undefined)
|
|
669
|
+
}
|
|
670
|
+
} catch (error) {
|
|
671
|
+
if (alive) setLiveError(error instanceof Error ? error.message : String(error))
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
void load()
|
|
675
|
+
const timer = setInterval(() => void load(), 15000)
|
|
676
|
+
return () => {
|
|
677
|
+
alive = false
|
|
678
|
+
clearInterval(timer)
|
|
679
|
+
}
|
|
680
|
+
}, [])
|
|
681
|
+
|
|
682
|
+
const edit = (key, value) => {
|
|
683
|
+
setDraft((current) => ({ ...current, [key]: value }))
|
|
684
|
+
setDirty(true)
|
|
685
|
+
setMessage('')
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const save = async (event) => {
|
|
689
|
+
event.preventDefault()
|
|
690
|
+
if (!ready || saving || revision === undefined) return
|
|
691
|
+
const operations = []
|
|
692
|
+
for (const [key, value] of Object.entries(draft)) {
|
|
693
|
+
if (value === baseline[key]) continue
|
|
694
|
+
operations.push({ op: 'set', path: [key], value })
|
|
695
|
+
}
|
|
696
|
+
if (operations.length === 0) {
|
|
697
|
+
setDirty(false)
|
|
698
|
+
return
|
|
699
|
+
}
|
|
700
|
+
setSaving(true)
|
|
701
|
+
setMessage('')
|
|
702
|
+
try {
|
|
703
|
+
const accepted = await form.mutate(operations, revision)
|
|
704
|
+
setMessage(t(accepted ? 'saved' : 'rejected'))
|
|
705
|
+
if (accepted) setDirty(false)
|
|
706
|
+
} catch (error) {
|
|
707
|
+
setMessage(error instanceof Error ? error.message : String(error))
|
|
708
|
+
} finally {
|
|
709
|
+
setSaving(false)
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
const reset = () => {
|
|
714
|
+
setDraft(readValues(form?.state.value))
|
|
715
|
+
setBaseline(readValues(form?.state.value))
|
|
716
|
+
setRevision(form?.state.revision)
|
|
717
|
+
setDirty(false)
|
|
718
|
+
setMessage('')
|
|
719
|
+
setProbe(undefined)
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
const runAction = async (action, body) => {
|
|
723
|
+
setBusy(action)
|
|
724
|
+
setLiveError(undefined)
|
|
725
|
+
try {
|
|
726
|
+
const response = await fetch('/clash-proxy/action', {
|
|
727
|
+
method: 'POST',
|
|
728
|
+
credentials: 'same-origin',
|
|
729
|
+
headers: { 'content-type': 'application/json' },
|
|
730
|
+
body: JSON.stringify({ action, ...(body ?? {}) }),
|
|
731
|
+
})
|
|
732
|
+
const answer = await response.json()
|
|
733
|
+
if (!response.ok) throw new Error(answer?.error ?? `status ${response.status}`)
|
|
734
|
+
const refreshed = await fetch('/clash-proxy/status', { credentials: 'same-origin' })
|
|
735
|
+
if (refreshed.ok) setLive(await refreshed.json())
|
|
736
|
+
} catch (error) {
|
|
737
|
+
setLiveError(error instanceof Error ? error.message : String(error))
|
|
738
|
+
} finally {
|
|
739
|
+
setBusy('')
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
const checkLink = async () => {
|
|
744
|
+
setBusy('probe')
|
|
745
|
+
setProbe(undefined)
|
|
746
|
+
try {
|
|
747
|
+
const response = await fetch('/clash-proxy/probe', {
|
|
748
|
+
method: 'POST',
|
|
749
|
+
credentials: 'same-origin',
|
|
750
|
+
headers: { 'content-type': 'application/json' },
|
|
751
|
+
body: JSON.stringify({ url: draft.subscriptionUrls ?? draft.subscriptionUrl ?? '' }),
|
|
752
|
+
})
|
|
753
|
+
setProbe(await response.json())
|
|
754
|
+
} catch (error) {
|
|
755
|
+
setProbe({ ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
756
|
+
} finally {
|
|
757
|
+
setBusy('')
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
if (!ready) {
|
|
762
|
+
return h('p', { role: 'status', className: 'clxp-note' }, t(form?.state.status === 'loading' ? 'loading' : 'unavailable'))
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
return h('form', { className: 'clxp-form', onSubmit: (event) => void save(event) },
|
|
766
|
+
[
|
|
767
|
+
h(Panel, {
|
|
768
|
+
key: 'panel',
|
|
769
|
+
t,
|
|
770
|
+
draft,
|
|
771
|
+
live,
|
|
772
|
+
delays: live?.delays ?? {},
|
|
773
|
+
busy,
|
|
774
|
+
message,
|
|
775
|
+
probe,
|
|
776
|
+
saving,
|
|
777
|
+
dirty,
|
|
778
|
+
editable: true,
|
|
779
|
+
onEdit: edit,
|
|
780
|
+
onAction: runAction,
|
|
781
|
+
onProbe: checkLink,
|
|
782
|
+
onReset: reset,
|
|
783
|
+
}),
|
|
784
|
+
liveError !== undefined ? h('p', { key: 'le', className: 'clxp-error', role: 'status' }, liveError) : null,
|
|
785
|
+
].filter(Boolean))
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* The settings page, mounted at `settings.section`.
|
|
790
|
+
*
|
|
791
|
+
* This is the page the user actually opens from the sidebar's Settings row, so it
|
|
792
|
+
* owns its own data instead of inheriting a row form: it reads and writes through
|
|
793
|
+
* `/clash-proxy/config`, which is the one surface that both persists to the profile
|
|
794
|
+
* and can restart routing when a routing-relevant field changed.
|
|
795
|
+
*
|
|
796
|
+
* @param props - section owner props (`close`) plus the slot's `t`.
|
|
797
|
+
* @returns the settings page.
|
|
798
|
+
*/
|
|
799
|
+
function SettingsSection(props) {
|
|
800
|
+
const { t } = props
|
|
801
|
+
const [config, setConfig] = useState()
|
|
802
|
+
const [draft, setDraft] = useState()
|
|
803
|
+
const [baseline, setBaseline] = useState()
|
|
804
|
+
const [live, setLive] = useState()
|
|
805
|
+
const [loadError, setLoadError] = useState()
|
|
806
|
+
const [statusError, setStatusError] = useState()
|
|
807
|
+
const [busy, setBusy] = useState('')
|
|
808
|
+
const [saving, setSaving] = useState(false)
|
|
809
|
+
const [message, setMessage] = useState('')
|
|
810
|
+
const [probe, setProbe] = useState()
|
|
811
|
+
|
|
812
|
+
const reload = useMemo(
|
|
813
|
+
() => async () => {
|
|
814
|
+
try {
|
|
815
|
+
const response = await fetch('/clash-proxy/status', { credentials: 'same-origin' })
|
|
816
|
+
if (!response.ok) throw new Error(`status ${response.status}`)
|
|
817
|
+
const data = await response.json()
|
|
818
|
+
setLive(data)
|
|
819
|
+
setStatusError(undefined)
|
|
820
|
+
return data
|
|
821
|
+
} catch (error) {
|
|
822
|
+
// A status failure must not clear the configuration error: they come from
|
|
823
|
+
// different routes, and clearing it here left a page stuck on "loading"
|
|
824
|
+
// whenever config failed but status succeeded.
|
|
825
|
+
setStatusError(error instanceof Error ? error.message : String(error))
|
|
826
|
+
return undefined
|
|
827
|
+
}
|
|
828
|
+
},
|
|
829
|
+
[],
|
|
830
|
+
)
|
|
831
|
+
|
|
832
|
+
const loadConfig = useMemo(
|
|
833
|
+
() => async () => {
|
|
834
|
+
try {
|
|
835
|
+
const response = await fetch('/clash-proxy/config', { credentials: 'same-origin' })
|
|
836
|
+
if (!response.ok) throw new Error(`config ${response.status}`)
|
|
837
|
+
const data = await response.json()
|
|
838
|
+
if (data?.config === undefined || typeof data.config !== 'object') {
|
|
839
|
+
throw new Error('the config route returned no config object')
|
|
840
|
+
}
|
|
841
|
+
setConfig(data.config)
|
|
842
|
+
setDraft(data.config)
|
|
843
|
+
setBaseline(data.config)
|
|
844
|
+
setLoadError(undefined)
|
|
845
|
+
return true
|
|
846
|
+
} catch (error) {
|
|
847
|
+
setLoadError(error instanceof Error ? error.message : String(error))
|
|
848
|
+
return false
|
|
849
|
+
}
|
|
850
|
+
},
|
|
851
|
+
[],
|
|
852
|
+
)
|
|
853
|
+
|
|
854
|
+
useEffect(() => {
|
|
855
|
+
let alive = true
|
|
856
|
+
void (async () => {
|
|
857
|
+
if (!alive) return
|
|
858
|
+
await loadConfig()
|
|
859
|
+
})()
|
|
860
|
+
void reload()
|
|
861
|
+
const timer = setInterval(() => void reload(), 15000)
|
|
862
|
+
return () => {
|
|
863
|
+
alive = false
|
|
864
|
+
clearInterval(timer)
|
|
865
|
+
}
|
|
866
|
+
}, [loadConfig, reload])
|
|
867
|
+
|
|
868
|
+
const edit = (key, value) => {
|
|
869
|
+
setDraft((current) => ({ ...current, [key]: value }))
|
|
870
|
+
setMessage('')
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const dirty = draft !== undefined && baseline !== undefined && Object.keys(draft).some((key) => draft[key] !== baseline[key])
|
|
874
|
+
|
|
875
|
+
const save = async (event) => {
|
|
876
|
+
event.preventDefault()
|
|
877
|
+
if (!dirty || saving) return
|
|
878
|
+
const patch = {}
|
|
879
|
+
for (const key of Object.keys(draft)) if (draft[key] !== baseline[key]) patch[key] = draft[key]
|
|
880
|
+
setSaving(true)
|
|
881
|
+
setMessage('')
|
|
882
|
+
try {
|
|
883
|
+
const response = await fetch('/clash-proxy/config', {
|
|
884
|
+
method: 'POST',
|
|
885
|
+
credentials: 'same-origin',
|
|
886
|
+
headers: { 'content-type': 'application/json' },
|
|
887
|
+
body: JSON.stringify({ patch }),
|
|
888
|
+
})
|
|
889
|
+
const answer = await response.json()
|
|
890
|
+
if (!response.ok) throw new Error(answer?.error ?? `status ${response.status}`)
|
|
891
|
+
setConfig(answer.config)
|
|
892
|
+
setDraft(answer.config)
|
|
893
|
+
setBaseline(answer.config)
|
|
894
|
+
setMessage((answer.warnings ?? []).length > 0 ? `${t('saved')} ${answer.warnings.join('; ')}` : t('saved'))
|
|
895
|
+
await reload()
|
|
896
|
+
} catch (error) {
|
|
897
|
+
setMessage(error instanceof Error ? error.message : String(error))
|
|
898
|
+
} finally {
|
|
899
|
+
setSaving(false)
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const reset = () => {
|
|
904
|
+
setDraft(config)
|
|
905
|
+
setMessage('')
|
|
906
|
+
setProbe(undefined)
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
const runAction = async (action, body) => {
|
|
910
|
+
setBusy(action)
|
|
911
|
+
setLoadError(undefined)
|
|
912
|
+
try {
|
|
913
|
+
const response = await fetch('/clash-proxy/action', {
|
|
914
|
+
method: 'POST',
|
|
915
|
+
credentials: 'same-origin',
|
|
916
|
+
headers: { 'content-type': 'application/json' },
|
|
917
|
+
body: JSON.stringify({ action, ...(body ?? {}) }),
|
|
918
|
+
})
|
|
919
|
+
const answer = await response.json()
|
|
920
|
+
if (!response.ok) throw new Error(answer?.error ?? `status ${response.status}`)
|
|
921
|
+
await reload()
|
|
922
|
+
} catch (error) {
|
|
923
|
+
setLoadError(error instanceof Error ? error.message : String(error))
|
|
924
|
+
} finally {
|
|
925
|
+
setBusy('')
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
const checkLink = async () => {
|
|
930
|
+
setBusy('probe')
|
|
931
|
+
setProbe(undefined)
|
|
932
|
+
try {
|
|
933
|
+
const response = await fetch('/clash-proxy/probe', {
|
|
934
|
+
method: 'POST',
|
|
935
|
+
credentials: 'same-origin',
|
|
936
|
+
headers: { 'content-type': 'application/json' },
|
|
937
|
+
body: JSON.stringify({ url: draft?.subscriptionUrls ?? draft?.subscriptionUrl ?? '' }),
|
|
938
|
+
})
|
|
939
|
+
setProbe(await response.json())
|
|
940
|
+
} catch (error) {
|
|
941
|
+
setProbe({ ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
942
|
+
} finally {
|
|
943
|
+
setBusy('')
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// Distinguish three states the old branch collapsed into one: still loading, the load
|
|
948
|
+
// failed, and loaded-but-empty. The first-run render is `undefined` for both `config`
|
|
949
|
+
// and `draft`, so testing `draft` alone made a healthy page announce "not editable
|
|
950
|
+
// here" before its first fetch resolved.
|
|
951
|
+
if (draft === undefined) {
|
|
952
|
+
if (loadError !== undefined) {
|
|
953
|
+
return h('div', { className: 'clxp' }, [
|
|
954
|
+
h('p', { key: 'e', className: 'clxp-error', role: 'status' }, `载入配置失败:${loadError}`),
|
|
955
|
+
h(
|
|
956
|
+
'p',
|
|
957
|
+
{ key: 'h', className: 'clxp-note' },
|
|
958
|
+
`读取 /clash-proxy/config 没有成功。请在浏览器里直接打开这个地址确认它返回 JSON;若返回 401,说明页面会话已经过期,重新从桌面端打开即可。`,
|
|
959
|
+
),
|
|
960
|
+
h(
|
|
961
|
+
'div',
|
|
962
|
+
{ key: 'a', className: 'clxp-actions' },
|
|
963
|
+
[
|
|
964
|
+
h('button', { key: 'r', type: 'button', className: 'clxp-primary', onClick: () => void loadConfig() }, '重试'),
|
|
965
|
+
h('button', { key: 'f', type: 'button', onClick: () => window.location.reload() }, '刷新页面'),
|
|
966
|
+
],
|
|
967
|
+
),
|
|
968
|
+
])
|
|
969
|
+
}
|
|
970
|
+
return h('p', { className: 'clxp-note', role: 'status' }, `${t('loading')}(正在读取 /clash-proxy/config …)`)
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
return h('form', { className: 'clxp-form', onSubmit: (event) => void save(event) }, [
|
|
974
|
+
h(Panel, {
|
|
975
|
+
key: 'p',
|
|
976
|
+
t,
|
|
977
|
+
draft,
|
|
978
|
+
live,
|
|
979
|
+
delays: live?.delays ?? {},
|
|
980
|
+
busy,
|
|
981
|
+
message,
|
|
982
|
+
probe,
|
|
983
|
+
saving,
|
|
984
|
+
dirty,
|
|
985
|
+
editable: true,
|
|
986
|
+
onEdit: edit,
|
|
987
|
+
onAction: runAction,
|
|
988
|
+
onProbe: checkLink,
|
|
989
|
+
onReset: reset,
|
|
990
|
+
}),
|
|
991
|
+
loadError !== undefined ? h('p', { key: 'e', className: 'clxp-error', role: 'status' }, loadError) : null,
|
|
992
|
+
statusError !== undefined ? h('p', { key: 'se', className: 'clxp-error', role: 'status' }, `状态接口:${statusError}`) : null,
|
|
993
|
+
].filter(Boolean))
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
return {
|
|
997
|
+
inject: ['slots', 'locale'],
|
|
998
|
+
apply(ctx) {
|
|
999
|
+
ctx.effect(() => ctx.locale.register('dshClashProxy', { en, zh }), 'dsh-clash-proxy: dictionaries')
|
|
1000
|
+
ctx.effect(() => {
|
|
1001
|
+
const style = document.createElement('style')
|
|
1002
|
+
style.dataset.dshClashProxy = 'true'
|
|
1003
|
+
style.textContent = CSS
|
|
1004
|
+
document.head.append(style)
|
|
1005
|
+
return () => style.remove()
|
|
1006
|
+
}, 'dsh-clash-proxy: styles')
|
|
1007
|
+
|
|
1008
|
+
// The row configuration page still carries the editable form (the Plugins page
|
|
1009
|
+
// reaches it through the package's row list).
|
|
1010
|
+
ctx.slots.inject('plugins.row.config', function* () {
|
|
1011
|
+
yield ctx.slots.register(
|
|
1012
|
+
{ name: 'plugins.row.config', key: 'dsh-clash-subscription#clash-proxy', locale: 'dshClashProxy' },
|
|
1013
|
+
(props) => (props.view === 'summary' ? h(Fragment, null, props.t('summary')) : h(RowConfigPage, { ...props })),
|
|
1014
|
+
)
|
|
1015
|
+
})
|
|
1016
|
+
|
|
1017
|
+
// The primary door: a page of its own inside Settings, next to General / Models /
|
|
1018
|
+
// Plugins. A settings section owns its data and its save path, so it does not
|
|
1019
|
+
// depend on the row form being reachable at all.
|
|
1020
|
+
ctx.slots.inject('settings.section', function* () {
|
|
1021
|
+
yield ctx.slots.register(
|
|
1022
|
+
{ name: 'settings.section', id: 'clash-proxy', order: 32, label: () => 'Clash 代理', locale: 'dshClashProxy' },
|
|
1023
|
+
(props) => h(SettingsSection, { ...props }),
|
|
1024
|
+
)
|
|
1025
|
+
})
|
|
1026
|
+
},
|
|
1027
|
+
}
|
|
1028
|
+
}});
|