@lucky_zone/net-proxy 0.0.0-stage → 1.0.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 +32 -2
- package/client.js +747 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +11 -0
- package/index.js +522 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +45 -4
package/client.js
ADDED
|
@@ -0,0 +1,747 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser half of the network-proxy plugin: the left sidebar entry and the panel
|
|
3
|
+
* it opens, plus the row configuration page the Plugins manager renders.
|
|
4
|
+
*
|
|
5
|
+
* Both surfaces edit the Host entry `net-proxy` through the shared configuration
|
|
6
|
+
* forms service, so the sidebar, the Plugins page, and the Host policy always read
|
|
7
|
+
* one document.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
window.__ModuleLoader__.load({
|
|
11
|
+
id: '@lucky_zone/net-proxy',
|
|
12
|
+
factory(require) {
|
|
13
|
+
const React = require('react')
|
|
14
|
+
const h = React.createElement
|
|
15
|
+
|
|
16
|
+
/** Locale namespace owned by this plugin. */
|
|
17
|
+
const NS = 'net-proxy'
|
|
18
|
+
/** Loader entry id of the Host row; the configuration form is keyed by it. */
|
|
19
|
+
const ENTRY_ID = 'net-proxy'
|
|
20
|
+
/** Panel id shared by the sidebar entry and the main-column panel it opens. */
|
|
21
|
+
const PANEL_ID = 'net-proxy'
|
|
22
|
+
/** `plugins.row.config` key of the row this bundle inserts. */
|
|
23
|
+
const ROW_KEY = '@lucky_zone/net-proxy#net-proxy'
|
|
24
|
+
|
|
25
|
+
const EN = {
|
|
26
|
+
panel: 'Network proxy',
|
|
27
|
+
title: 'Network proxy',
|
|
28
|
+
lead: 'Send the listed domains through your own proxy server; everything else connects directly.',
|
|
29
|
+
statusOn: 'Proxied',
|
|
30
|
+
statusOff: 'Direct',
|
|
31
|
+
statusIdleHint: 'Requests connect directly. Turn on \u201cUse proxy\u201d to route the listed domains.',
|
|
32
|
+
statusOnHint: 'Only the listed domains go through {proxy}; everything else connects directly.',
|
|
33
|
+
invalidEnabled: 'The proxy address is not usable, so requests still connect directly.',
|
|
34
|
+
emptyDomains: 'No usable domain is listed, so every request connects directly.',
|
|
35
|
+
covers: 'What it covers',
|
|
36
|
+
coversHost: 'Host requests: model calls, web fetch, HTTP MCP',
|
|
37
|
+
coversChild: 'Commands and terminals started after the change',
|
|
38
|
+
directAlways: 'localhost and 127.0.0.1 always connect directly',
|
|
39
|
+
enabledLabel: 'Use proxy',
|
|
40
|
+
enabledHint: 'Applies to the Host\u2019s own requests and to commands and terminals started afterwards.',
|
|
41
|
+
proxyLabel: 'Proxy server',
|
|
42
|
+
proxyHint: 'http:// or https:// only, for example http://127.0.0.1:7890.',
|
|
43
|
+
proxyInvalid: 'Enter an http:// or https:// proxy address.',
|
|
44
|
+
domainsLabel: 'Proxied domains',
|
|
45
|
+
domainsHint: 'One per line or comma-separated. Each entry also matches its subdomains; every other host connects directly.',
|
|
46
|
+
domainsInvalid: 'Write host names only, without a scheme or path, for example github.com.',
|
|
47
|
+
domainsPlaceholder: 'github.com',
|
|
48
|
+
save: 'Save',
|
|
49
|
+
saving: 'Saving\u2026',
|
|
50
|
+
saved: 'Saved',
|
|
51
|
+
unsaved: 'Unsaved changes',
|
|
52
|
+
failed: 'The Host refused the change. Check the fields and try again.',
|
|
53
|
+
reset: 'Reset to defaults',
|
|
54
|
+
loading: 'Reading configuration\u2026',
|
|
55
|
+
unavailable: 'Configuration is unavailable. Check that this plugin is enabled.',
|
|
56
|
+
summaryOn: 'Enabled',
|
|
57
|
+
summaryOff: 'Disabled',
|
|
58
|
+
summaryNone: 'no domains',
|
|
59
|
+
summaryInvalidProxy: 'proxy unusable',
|
|
60
|
+
summaryDomainOne: '1 domain',
|
|
61
|
+
summaryDomains: '{count} domains',
|
|
62
|
+
managerHint: 'The same settings are on this plugin\u2019s page in Plugins; click its row title to open them.',
|
|
63
|
+
openManager: 'Open in Plugins',
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const ZH = {
|
|
67
|
+
panel: '网络代理',
|
|
68
|
+
title: '网络代理',
|
|
69
|
+
lead: '只把列表内的域名交给你的代理服务器,其余站点直连。',
|
|
70
|
+
statusOn: '走代理',
|
|
71
|
+
statusOff: '直连',
|
|
72
|
+
statusIdleHint: '请求当前直连。打开「使用代理」后,列表内的域名改为走代理。',
|
|
73
|
+
statusOnHint: '只有列表内的域名经由 {proxy},其余站点一律直连。',
|
|
74
|
+
invalidEnabled: '代理地址不可用,请求仍然直连。',
|
|
75
|
+
emptyDomains: '列表中没有可用的域名,所有请求直连。',
|
|
76
|
+
covers: '生效范围',
|
|
77
|
+
coversHost: '宿主请求:模型调用、网页抓取、HTTP MCP',
|
|
78
|
+
coversChild: '改动之后新启动的命令与终端',
|
|
79
|
+
directAlways: 'localhost 与 127.0.0.1 始终直连',
|
|
80
|
+
enabledLabel: '使用代理',
|
|
81
|
+
enabledHint: '作用于宿主自身的请求,以及之后新启动的命令与终端。',
|
|
82
|
+
proxyLabel: '代理服务器',
|
|
83
|
+
proxyHint: '仅支持 http:// 与 https://,例如 http://127.0.0.1:7890。',
|
|
84
|
+
proxyInvalid: '请填写 http:// 或 https:// 代理地址。',
|
|
85
|
+
domainsLabel: '走代理的域名',
|
|
86
|
+
domainsHint: '每行一个或用逗号分隔;条目同时匹配其子域名,其余主机一律直连。',
|
|
87
|
+
domainsInvalid: '只填主机名,不要带协议或路径,例如 github.com。',
|
|
88
|
+
domainsPlaceholder: 'github.com',
|
|
89
|
+
save: '保存',
|
|
90
|
+
saving: '保存中…',
|
|
91
|
+
saved: '已保存',
|
|
92
|
+
unsaved: '有未保存的改动',
|
|
93
|
+
failed: '宿主拒绝了这次改动,请检查字段后重试。',
|
|
94
|
+
reset: '恢复默认',
|
|
95
|
+
loading: '正在读取配置…',
|
|
96
|
+
unavailable: '配置不可用,请确认插件已启用。',
|
|
97
|
+
summaryOn: '已启用',
|
|
98
|
+
summaryOff: '未启用',
|
|
99
|
+
summaryNone: '未指定域名',
|
|
100
|
+
summaryInvalidProxy: '代理不可用',
|
|
101
|
+
summaryDomainOne: '1 个域名',
|
|
102
|
+
summaryDomains: '{count} 个域名',
|
|
103
|
+
managerHint: '同样的设置也在「插件管理」中本插件的配置页:点击这一行的标题打开。',
|
|
104
|
+
openManager: '在插件管理中打开',
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const CSS = `
|
|
108
|
+
.dsh-np-page {
|
|
109
|
+
display: flex;
|
|
110
|
+
flex-direction: column;
|
|
111
|
+
height: 100%;
|
|
112
|
+
overflow-y: auto;
|
|
113
|
+
box-sizing: border-box;
|
|
114
|
+
padding: 32px clamp(20px, 4vw, 48px) 48px;
|
|
115
|
+
color: var(--dsw-alias-label-primary);
|
|
116
|
+
font-size: 14px;
|
|
117
|
+
}
|
|
118
|
+
.dsh-np-inner {
|
|
119
|
+
display: flex;
|
|
120
|
+
flex-direction: column;
|
|
121
|
+
gap: 16px;
|
|
122
|
+
width: 100%;
|
|
123
|
+
max-width: 720px;
|
|
124
|
+
margin: 0 auto;
|
|
125
|
+
}
|
|
126
|
+
.dsh-np-head { display: flex; flex-direction: column; gap: 6px; }
|
|
127
|
+
.dsh-np-title { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }
|
|
128
|
+
.dsh-np-lead { margin: 0; font-size: 13px; line-height: 1.6; color: var(--dsw-alias-label-secondary); }
|
|
129
|
+
.dsh-np-card {
|
|
130
|
+
display: flex;
|
|
131
|
+
flex-direction: column;
|
|
132
|
+
gap: 18px;
|
|
133
|
+
padding: 18px 20px;
|
|
134
|
+
border: 1px solid var(--dsw-alias-border-l1);
|
|
135
|
+
border-radius: 12px;
|
|
136
|
+
background: var(--dsw-alias-bg-layer-1);
|
|
137
|
+
}
|
|
138
|
+
.dsh-np-status { display: flex; flex-direction: column; gap: 8px; }
|
|
139
|
+
.dsh-np-statusRow { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
|
|
140
|
+
.dsh-np-pill {
|
|
141
|
+
display: inline-flex;
|
|
142
|
+
align-items: center;
|
|
143
|
+
gap: 6px;
|
|
144
|
+
flex: none;
|
|
145
|
+
padding: 3px 10px;
|
|
146
|
+
border: 1px solid var(--dsw-alias-border-l1);
|
|
147
|
+
border-radius: 999px;
|
|
148
|
+
font-size: 12px;
|
|
149
|
+
font-weight: 600;
|
|
150
|
+
color: var(--dsw-alias-label-secondary);
|
|
151
|
+
}
|
|
152
|
+
.dsh-np-pill::before {
|
|
153
|
+
content: '';
|
|
154
|
+
width: 6px;
|
|
155
|
+
height: 6px;
|
|
156
|
+
border-radius: 50%;
|
|
157
|
+
background: currentColor;
|
|
158
|
+
}
|
|
159
|
+
.dsh-np-pill[data-tone="on"] { color: var(--dsw-alias-state-success-primary); }
|
|
160
|
+
.dsh-np-pill[data-tone="warn"] { color: var(--dsw-alias-state-warn-primary); }
|
|
161
|
+
.dsh-np-statusText { font-size: 13px; line-height: 1.55; color: var(--dsw-alias-label-secondary); }
|
|
162
|
+
.dsh-np-covers { display: flex; flex-direction: column; gap: 6px; }
|
|
163
|
+
.dsh-np-coversTitle {
|
|
164
|
+
font-size: 11px;
|
|
165
|
+
font-weight: 600;
|
|
166
|
+
letter-spacing: 0.04em;
|
|
167
|
+
text-transform: uppercase;
|
|
168
|
+
color: var(--dsw-alias-label-secondary);
|
|
169
|
+
}
|
|
170
|
+
.dsh-np-list { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 4px; }
|
|
171
|
+
.dsh-np-list li {
|
|
172
|
+
position: relative;
|
|
173
|
+
padding-left: 16px;
|
|
174
|
+
font-size: 13px;
|
|
175
|
+
line-height: 1.55;
|
|
176
|
+
color: var(--dsw-alias-label-secondary);
|
|
177
|
+
}
|
|
178
|
+
.dsh-np-list li::before {
|
|
179
|
+
content: '';
|
|
180
|
+
position: absolute;
|
|
181
|
+
left: 3px;
|
|
182
|
+
top: 8px;
|
|
183
|
+
width: 5px;
|
|
184
|
+
height: 5px;
|
|
185
|
+
border-radius: 50%;
|
|
186
|
+
background: var(--dsw-alias-border-l2);
|
|
187
|
+
}
|
|
188
|
+
.dsh-np-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
|
|
189
|
+
.dsh-np-rowText { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
|
|
190
|
+
.dsh-np-label { font-size: 13px; font-weight: 500; line-height: 1.5; }
|
|
191
|
+
.dsh-np-hint { font-size: 12px; line-height: 1.55; color: var(--dsw-alias-label-secondary); }
|
|
192
|
+
.dsh-np-hint[data-invalid="true"] { color: var(--dsw-alias-state-error-primary); }
|
|
193
|
+
.dsh-np-hint[data-warn="true"] { color: var(--dsw-alias-state-warn-primary); }
|
|
194
|
+
.dsh-np-switch {
|
|
195
|
+
position: relative;
|
|
196
|
+
flex: none;
|
|
197
|
+
width: 38px;
|
|
198
|
+
height: 22px;
|
|
199
|
+
margin-top: 2px;
|
|
200
|
+
padding: 0;
|
|
201
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
202
|
+
border-radius: 999px;
|
|
203
|
+
background: var(--dsw-alias-bg-layer-2);
|
|
204
|
+
cursor: pointer;
|
|
205
|
+
}
|
|
206
|
+
.dsh-np-switch::after {
|
|
207
|
+
content: '';
|
|
208
|
+
position: absolute;
|
|
209
|
+
top: 2px;
|
|
210
|
+
left: 2px;
|
|
211
|
+
width: 16px;
|
|
212
|
+
height: 16px;
|
|
213
|
+
border-radius: 50%;
|
|
214
|
+
background: #ffffff;
|
|
215
|
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
|
|
216
|
+
transition: transform 120ms ease;
|
|
217
|
+
}
|
|
218
|
+
.dsh-np-switch[aria-checked="true"] {
|
|
219
|
+
border-color: var(--dsw-alias-brand-primary);
|
|
220
|
+
background: var(--dsw-alias-brand-primary);
|
|
221
|
+
}
|
|
222
|
+
.dsh-np-switch[aria-checked="true"]::after { transform: translateX(16px); }
|
|
223
|
+
.dsh-np-switch:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 2px; }
|
|
224
|
+
.dsh-np-switch:disabled { opacity: 0.5; cursor: default; }
|
|
225
|
+
.dsh-np-field { display: flex; flex-direction: column; gap: 6px; }
|
|
226
|
+
.dsh-np-input {
|
|
227
|
+
box-sizing: border-box;
|
|
228
|
+
width: 100%;
|
|
229
|
+
padding: 8px 10px;
|
|
230
|
+
border: 1px solid var(--dsw-alias-border-l1);
|
|
231
|
+
border-radius: 8px;
|
|
232
|
+
background: var(--dsw-alias-bg-base);
|
|
233
|
+
color: var(--dsw-alias-label-primary);
|
|
234
|
+
font: inherit;
|
|
235
|
+
font-size: 13px;
|
|
236
|
+
line-height: 1.5;
|
|
237
|
+
}
|
|
238
|
+
.dsh-np-input::placeholder { color: var(--dsw-alias-label-secondary); opacity: 0.7; }
|
|
239
|
+
.dsh-np-input:focus-visible {
|
|
240
|
+
outline: none;
|
|
241
|
+
border-color: var(--dsw-alias-brand-primary);
|
|
242
|
+
box-shadow: 0 0 0 1px var(--dsw-alias-brand-primary);
|
|
243
|
+
}
|
|
244
|
+
.dsh-np-input[aria-invalid="true"] { border-color: var(--dsw-alias-state-error-primary); }
|
|
245
|
+
.dsh-np-textarea {
|
|
246
|
+
min-height: 88px;
|
|
247
|
+
resize: vertical;
|
|
248
|
+
}
|
|
249
|
+
.dsh-np-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
|
250
|
+
.dsh-np-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
|
|
251
|
+
.dsh-np-button {
|
|
252
|
+
padding: 7px 14px;
|
|
253
|
+
border: 1px solid var(--dsw-alias-border-l1);
|
|
254
|
+
border-radius: 8px;
|
|
255
|
+
background: transparent;
|
|
256
|
+
color: var(--dsw-alias-label-primary);
|
|
257
|
+
font: inherit;
|
|
258
|
+
font-size: 13px;
|
|
259
|
+
line-height: 1.4;
|
|
260
|
+
cursor: pointer;
|
|
261
|
+
}
|
|
262
|
+
.dsh-np-button:hover:not(:disabled) { background: var(--dsw-alias-bg-layer-2); }
|
|
263
|
+
.dsh-np-button:disabled { opacity: 0.5; cursor: default; }
|
|
264
|
+
.dsh-np-button:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 2px; }
|
|
265
|
+
.dsh-np-button[data-variant="primary"] {
|
|
266
|
+
border-color: var(--dsw-alias-brand-primary);
|
|
267
|
+
background: var(--dsw-alias-brand-primary);
|
|
268
|
+
color: #ffffff;
|
|
269
|
+
}
|
|
270
|
+
.dsh-np-button[data-variant="primary"]:hover:not(:disabled) {
|
|
271
|
+
background: var(--dsw-alias-brand-primary);
|
|
272
|
+
filter: brightness(1.06);
|
|
273
|
+
}
|
|
274
|
+
.dsh-np-button[data-variant="ghost"] { border-color: transparent; color: var(--dsw-alias-label-secondary); }
|
|
275
|
+
.dsh-np-feedback {
|
|
276
|
+
min-height: 18px;
|
|
277
|
+
font-size: 12px;
|
|
278
|
+
line-height: 1.5;
|
|
279
|
+
color: var(--dsw-alias-label-secondary);
|
|
280
|
+
}
|
|
281
|
+
.dsh-np-feedback[data-tone="ok"] { color: var(--dsw-alias-state-success-primary); }
|
|
282
|
+
.dsh-np-feedback[data-tone="error"] { color: var(--dsw-alias-state-error-primary); }
|
|
283
|
+
.dsh-np-note { margin: 0; font-size: 12px; line-height: 1.6; color: var(--dsw-alias-label-secondary); }
|
|
284
|
+
.dsh-np-placeholder {
|
|
285
|
+
padding: 18px 20px;
|
|
286
|
+
border: 1px dashed var(--dsw-alias-border-l1);
|
|
287
|
+
border-radius: 12px;
|
|
288
|
+
font-size: 13px;
|
|
289
|
+
line-height: 1.6;
|
|
290
|
+
color: var(--dsw-alias-label-secondary);
|
|
291
|
+
}
|
|
292
|
+
@media (max-width: 640px) {
|
|
293
|
+
.dsh-np-page { padding: 24px 16px 40px; }
|
|
294
|
+
.dsh-np-card { padding: 16px; }
|
|
295
|
+
}
|
|
296
|
+
`
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Read the three fields from a form value.
|
|
300
|
+
* @param value - accepted configuration values, possibly incomplete.
|
|
301
|
+
* @returns the fields this plugin owns.
|
|
302
|
+
*/
|
|
303
|
+
function pick(value) {
|
|
304
|
+
return {
|
|
305
|
+
enabled: value !== undefined && value !== null && value.enabled === true,
|
|
306
|
+
proxyUrl: value !== undefined && value !== null && typeof value.proxyUrl === 'string' ? value.proxyUrl : '',
|
|
307
|
+
domains: value !== undefined && value !== null && typeof value.domains === 'string' ? value.domains : '',
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Split configured text into domains, dropping blanks and repeats.
|
|
313
|
+
* @param text - the field's text, separated by newlines, commas, or spaces.
|
|
314
|
+
* @returns domain entries in the order written.
|
|
315
|
+
*/
|
|
316
|
+
function splitDomains(text) {
|
|
317
|
+
const seen = new Set()
|
|
318
|
+
const entries = []
|
|
319
|
+
for (const raw of String(text).split(/[\s,]+/)) {
|
|
320
|
+
const entry = raw.trim()
|
|
321
|
+
if (entry === '') continue
|
|
322
|
+
const key = entry.toLowerCase()
|
|
323
|
+
if (seen.has(key)) continue
|
|
324
|
+
seen.add(key)
|
|
325
|
+
entries.push(entry)
|
|
326
|
+
}
|
|
327
|
+
return entries
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Canonical field text: one domain per line, without blanks or repeats.
|
|
332
|
+
* @param text - the field's text.
|
|
333
|
+
* @returns the stored form of the allowlist.
|
|
334
|
+
*/
|
|
335
|
+
function normalizeDomains(text) {
|
|
336
|
+
return splitDomains(text).join('\n')
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Whether one entry can match a host: a host name, optionally with a port,
|
|
341
|
+
* a leading `*.`, or `*` alone.
|
|
342
|
+
* @param entry - one already-trimmed entry.
|
|
343
|
+
* @returns true for an entry the router can use.
|
|
344
|
+
*/
|
|
345
|
+
function isValidDomainEntry(entry) {
|
|
346
|
+
if (entry === '*') return true
|
|
347
|
+
if (entry.length > 255 || /[\s/?#@]/.test(entry)) return false
|
|
348
|
+
if (entry.startsWith('[')) return /^\[[0-9A-Fa-f:.]+\](:\d{1,5})?$/.test(entry)
|
|
349
|
+
const colon = entry.indexOf(':')
|
|
350
|
+
const host = colon === -1 ? entry : entry.slice(0, colon)
|
|
351
|
+
const port = colon === -1 ? undefined : entry.slice(colon + 1)
|
|
352
|
+
if (port !== undefined && !/^\d{1,5}$/.test(port)) return false
|
|
353
|
+
if (host === '' || host === '.' || host.includes('..')) return false
|
|
354
|
+
return /^[A-Za-z0-9.*_-]+$/.test(host)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Whether the harness can route through this address.
|
|
359
|
+
* @param value - proxy address as typed.
|
|
360
|
+
* @returns true for a parseable http: or https: URL.
|
|
361
|
+
*/
|
|
362
|
+
function supportedProxyUrl(value) {
|
|
363
|
+
try {
|
|
364
|
+
const url = new URL(value)
|
|
365
|
+
return url.protocol === 'http:' || url.protocol === 'https:'
|
|
366
|
+
} catch {
|
|
367
|
+
return false
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* The proxy address without credentials, for display.
|
|
373
|
+
* @param value - proxy address as typed.
|
|
374
|
+
* @returns scheme and authority only.
|
|
375
|
+
*/
|
|
376
|
+
function displayProxy(value) {
|
|
377
|
+
try {
|
|
378
|
+
const url = new URL(value)
|
|
379
|
+
return `${url.protocol}//${url.host}`
|
|
380
|
+
} catch {
|
|
381
|
+
return value
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Follow a snapshot source's replacements.
|
|
387
|
+
* @param source - observable with getSnapshot and subscribe.
|
|
388
|
+
* @returns the current snapshot.
|
|
389
|
+
*/
|
|
390
|
+
function useSnapshot(source) {
|
|
391
|
+
const [snapshot, setSnapshot] = React.useState(() => (source === undefined ? undefined : source.getSnapshot()))
|
|
392
|
+
React.useEffect(() => {
|
|
393
|
+
if (source === undefined) {
|
|
394
|
+
setSnapshot(undefined)
|
|
395
|
+
return undefined
|
|
396
|
+
}
|
|
397
|
+
setSnapshot(source.getSnapshot())
|
|
398
|
+
return source.subscribe(() => { setSnapshot(source.getSnapshot()) })
|
|
399
|
+
}, [source])
|
|
400
|
+
return snapshot
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Render the policy status summary from accepted values.
|
|
405
|
+
* @param props - translations and the current snapshot.
|
|
406
|
+
* @returns the status card.
|
|
407
|
+
*/
|
|
408
|
+
function StatusCard({ t, snapshot }) {
|
|
409
|
+
const ready = snapshot !== undefined && snapshot.status === 'ready'
|
|
410
|
+
const loading = snapshot === undefined || snapshot.status === 'loading'
|
|
411
|
+
const value = pick(ready ? snapshot.value : undefined)
|
|
412
|
+
const listed = splitDomains(value.domains).filter(isValidDomainEntry).length
|
|
413
|
+
const usable = supportedProxyUrl(value.proxyUrl)
|
|
414
|
+
const on = ready && value.enabled && usable && listed > 0
|
|
415
|
+
const broken = ready && value.enabled && !usable
|
|
416
|
+
const empty = ready && value.enabled && usable && listed === 0
|
|
417
|
+
const tone = on ? 'on' : broken || empty ? 'warn' : 'off'
|
|
418
|
+
return h('div', { className: 'dsh-np-card dsh-np-status' },
|
|
419
|
+
h('div', { className: 'dsh-np-statusRow' },
|
|
420
|
+
h('span', { className: 'dsh-np-pill', 'data-tone': tone }, t(on ? 'statusOn' : 'statusOff')),
|
|
421
|
+
h('span', { className: 'dsh-np-statusText' },
|
|
422
|
+
loading
|
|
423
|
+
? t('loading')
|
|
424
|
+
: !ready
|
|
425
|
+
? t('unavailable')
|
|
426
|
+
: on
|
|
427
|
+
? t('statusOnHint', { proxy: displayProxy(value.proxyUrl) })
|
|
428
|
+
: broken
|
|
429
|
+
? t('invalidEnabled')
|
|
430
|
+
: empty
|
|
431
|
+
? t('emptyDomains')
|
|
432
|
+
: t('statusIdleHint')),
|
|
433
|
+
),
|
|
434
|
+
h('div', { className: 'dsh-np-covers' },
|
|
435
|
+
h('span', { className: 'dsh-np-coversTitle' }, t('covers')),
|
|
436
|
+
h('ul', { className: 'dsh-np-list' },
|
|
437
|
+
h('li', null, t('coversHost')),
|
|
438
|
+
h('li', null, t('coversChild')),
|
|
439
|
+
h('li', null, t('directAlways')),
|
|
440
|
+
),
|
|
441
|
+
),
|
|
442
|
+
)
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* The editable settings card shared by the sidebar panel and the row page.
|
|
447
|
+
* @param props - translations, the current snapshot, and the write action.
|
|
448
|
+
* @returns the settings card.
|
|
449
|
+
*/
|
|
450
|
+
function SettingsCard({ t, snapshot, mutate }) {
|
|
451
|
+
const available = snapshot !== undefined && snapshot.status === 'ready' && typeof mutate === 'function'
|
|
452
|
+
const value = pick(available ? snapshot.value : undefined)
|
|
453
|
+
const revision = available ? snapshot.revision : undefined
|
|
454
|
+
const [draft, setDraft] = React.useState(() => value)
|
|
455
|
+
const [phase, setPhase] = React.useState('idle')
|
|
456
|
+
const [failure, setFailure] = React.useState(false)
|
|
457
|
+
const applied = React.useRef(revision)
|
|
458
|
+
const fieldId = React.useId()
|
|
459
|
+
const proxyHintId = `${fieldId}-proxy`
|
|
460
|
+
const domainsHintId = `${fieldId}-domains`
|
|
461
|
+
|
|
462
|
+
React.useEffect(() => {
|
|
463
|
+
if (revision === applied.current) return
|
|
464
|
+
applied.current = revision
|
|
465
|
+
const next = pick(snapshot !== undefined && snapshot.status === 'ready' ? snapshot.value : undefined)
|
|
466
|
+
setDraft(next)
|
|
467
|
+
}, [revision, snapshot])
|
|
468
|
+
|
|
469
|
+
if (snapshot === undefined || snapshot.status === 'loading') {
|
|
470
|
+
return h('div', { className: 'dsh-np-placeholder' }, t('loading'))
|
|
471
|
+
}
|
|
472
|
+
if (snapshot.status !== 'ready' || typeof mutate !== 'function') {
|
|
473
|
+
return h('div', { className: 'dsh-np-placeholder' }, t('unavailable'))
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const proxyText = draft.proxyUrl.trim()
|
|
477
|
+
const proxyInvalid = proxyText !== '' && !supportedProxyUrl(proxyText)
|
|
478
|
+
const proxyMissing = draft.enabled && proxyText === ''
|
|
479
|
+
const domainEntries = splitDomains(draft.domains)
|
|
480
|
+
const domainsInvalid = domainEntries.some(entry => !isValidDomainEntry(entry))
|
|
481
|
+
const domainListEmpty = domainEntries.length === 0
|
|
482
|
+
const dirty = draft.enabled !== value.enabled
|
|
483
|
+
|| proxyText !== value.proxyUrl
|
|
484
|
+
|| normalizeDomains(draft.domains) !== value.domains
|
|
485
|
+
const blocked = proxyInvalid || proxyMissing || domainsInvalid
|
|
486
|
+
const saving = phase === 'saving'
|
|
487
|
+
|
|
488
|
+
const edit = (patch) => {
|
|
489
|
+
setDraft((current) => ({ ...current, ...patch }))
|
|
490
|
+
setPhase('idle')
|
|
491
|
+
setFailure(false)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const write = async (ops, guarded) => {
|
|
495
|
+
if (saving) return
|
|
496
|
+
if (guarded === true && blocked) {
|
|
497
|
+
setPhase('failed')
|
|
498
|
+
setFailure(true)
|
|
499
|
+
return
|
|
500
|
+
}
|
|
501
|
+
setPhase('saving')
|
|
502
|
+
setFailure(false)
|
|
503
|
+
let accepted = false
|
|
504
|
+
try {
|
|
505
|
+
accepted = await mutate(ops, revision)
|
|
506
|
+
} catch (error) {
|
|
507
|
+
accepted = false
|
|
508
|
+
}
|
|
509
|
+
setPhase(accepted ? 'saved' : 'failed')
|
|
510
|
+
setFailure(!accepted)
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const save = () => write([
|
|
514
|
+
{ op: 'set', path: ['enabled'], value: draft.enabled },
|
|
515
|
+
{ op: 'set', path: ['proxyUrl'], value: proxyText },
|
|
516
|
+
{ op: 'set', path: ['domains'], value: normalizeDomains(draft.domains) },
|
|
517
|
+
], true)
|
|
518
|
+
|
|
519
|
+
const reset = () => write([
|
|
520
|
+
{ op: 'unset', path: ['enabled'] },
|
|
521
|
+
{ op: 'unset', path: ['proxyUrl'] },
|
|
522
|
+
{ op: 'unset', path: ['domains'] },
|
|
523
|
+
], false)
|
|
524
|
+
|
|
525
|
+
const feedback = failure
|
|
526
|
+
? { tone: 'error', text: proxyInvalid || proxyMissing ? t('proxyInvalid') : domainsInvalid ? t('domainsInvalid') : t('failed') }
|
|
527
|
+
: saving
|
|
528
|
+
? { tone: 'muted', text: t('saving') }
|
|
529
|
+
: phase === 'saved' && !dirty
|
|
530
|
+
? { tone: 'ok', text: t('saved') }
|
|
531
|
+
: dirty
|
|
532
|
+
? { tone: 'muted', text: t('unsaved') }
|
|
533
|
+
: null
|
|
534
|
+
|
|
535
|
+
return h('div', { className: 'dsh-np-card' },
|
|
536
|
+
h('div', { className: 'dsh-np-row' },
|
|
537
|
+
h('div', { className: 'dsh-np-rowText' },
|
|
538
|
+
h('span', { className: 'dsh-np-label' }, t('enabledLabel')),
|
|
539
|
+
h('span', { className: 'dsh-np-hint' }, t('enabledHint')),
|
|
540
|
+
),
|
|
541
|
+
h('button', {
|
|
542
|
+
type: 'button',
|
|
543
|
+
className: 'dsh-np-switch',
|
|
544
|
+
role: 'switch',
|
|
545
|
+
'aria-checked': draft.enabled,
|
|
546
|
+
'aria-label': t('enabledLabel'),
|
|
547
|
+
disabled: saving,
|
|
548
|
+
onClick: () => { edit({ enabled: !draft.enabled }) },
|
|
549
|
+
}),
|
|
550
|
+
),
|
|
551
|
+
h('label', { className: 'dsh-np-field' },
|
|
552
|
+
h('span', { className: 'dsh-np-label' }, t('proxyLabel')),
|
|
553
|
+
h('input', {
|
|
554
|
+
className: 'dsh-np-input dsh-np-mono',
|
|
555
|
+
type: 'text',
|
|
556
|
+
inputMode: 'url',
|
|
557
|
+
spellCheck: false,
|
|
558
|
+
autoComplete: 'off',
|
|
559
|
+
placeholder: 'http://127.0.0.1:7890',
|
|
560
|
+
value: draft.proxyUrl,
|
|
561
|
+
'aria-invalid': proxyInvalid,
|
|
562
|
+
'aria-describedby': proxyHintId,
|
|
563
|
+
disabled: saving,
|
|
564
|
+
onChange: (event) => { edit({ proxyUrl: event.target.value }) },
|
|
565
|
+
}),
|
|
566
|
+
h('span', { className: 'dsh-np-hint', id: proxyHintId, 'data-invalid': proxyInvalid }, proxyInvalid ? t('proxyInvalid') : t('proxyHint')),
|
|
567
|
+
),
|
|
568
|
+
h('label', { className: 'dsh-np-field' },
|
|
569
|
+
h('span', { className: 'dsh-np-label' }, t('domainsLabel')),
|
|
570
|
+
h('textarea', {
|
|
571
|
+
className: 'dsh-np-input dsh-np-textarea dsh-np-mono',
|
|
572
|
+
rows: 4,
|
|
573
|
+
spellCheck: false,
|
|
574
|
+
autoComplete: 'off',
|
|
575
|
+
placeholder: t('domainsPlaceholder'),
|
|
576
|
+
value: draft.domains,
|
|
577
|
+
'aria-invalid': domainsInvalid,
|
|
578
|
+
'aria-describedby': domainsHintId,
|
|
579
|
+
disabled: saving,
|
|
580
|
+
onChange: (event) => { edit({ domains: event.target.value }) },
|
|
581
|
+
}),
|
|
582
|
+
h('span', {
|
|
583
|
+
className: 'dsh-np-hint',
|
|
584
|
+
id: domainsHintId,
|
|
585
|
+
'data-invalid': domainsInvalid,
|
|
586
|
+
'data-warn': !domainsInvalid && domainListEmpty && draft.enabled,
|
|
587
|
+
}, domainsInvalid
|
|
588
|
+
? t('domainsInvalid')
|
|
589
|
+
: domainListEmpty && draft.enabled
|
|
590
|
+
? t('emptyDomains')
|
|
591
|
+
: t('domainsHint')),
|
|
592
|
+
),
|
|
593
|
+
h('div', { className: 'dsh-np-actions' },
|
|
594
|
+
h('button', {
|
|
595
|
+
type: 'button',
|
|
596
|
+
className: 'dsh-np-button',
|
|
597
|
+
'data-variant': 'primary',
|
|
598
|
+
disabled: saving || !dirty,
|
|
599
|
+
onClick: save,
|
|
600
|
+
}, saving ? t('saving') : t('save')),
|
|
601
|
+
h('button', {
|
|
602
|
+
type: 'button',
|
|
603
|
+
className: 'dsh-np-button',
|
|
604
|
+
'data-variant': 'ghost',
|
|
605
|
+
disabled: saving,
|
|
606
|
+
onClick: reset,
|
|
607
|
+
}, t('reset')),
|
|
608
|
+
),
|
|
609
|
+
feedback === null
|
|
610
|
+
? null
|
|
611
|
+
: h('span', {
|
|
612
|
+
className: 'dsh-np-feedback',
|
|
613
|
+
'data-tone': feedback.tone,
|
|
614
|
+
role: feedback.tone === 'error' ? 'alert' : 'status',
|
|
615
|
+
}, feedback.text),
|
|
616
|
+
)
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* The main-column panel the sidebar entry opens.
|
|
621
|
+
* @param props - the slot's translate seat plus this plugin's injection.
|
|
622
|
+
* @returns the panel.
|
|
623
|
+
*/
|
|
624
|
+
function ProxyPanel(props) {
|
|
625
|
+
const t = typeof props.t === 'function' ? props.t : props.translate
|
|
626
|
+
const source = props.proxyForm
|
|
627
|
+
const snapshot = useSnapshot(source)
|
|
628
|
+
const openManager = props.openPluginManager
|
|
629
|
+
return h('div', { className: 'dsh-np-page' },
|
|
630
|
+
h('style', null, CSS),
|
|
631
|
+
h('div', { className: 'dsh-np-inner' },
|
|
632
|
+
h('header', { className: 'dsh-np-head' },
|
|
633
|
+
h('h2', { className: 'dsh-np-title' }, t('title')),
|
|
634
|
+
h('p', { className: 'dsh-np-lead' }, t('lead')),
|
|
635
|
+
),
|
|
636
|
+
h(StatusCard, { t, snapshot }),
|
|
637
|
+
h(SettingsCard, {
|
|
638
|
+
t,
|
|
639
|
+
snapshot,
|
|
640
|
+
mutate: (ops, revision) => source.mutate(ops, revision),
|
|
641
|
+
}),
|
|
642
|
+
h('div', { className: 'dsh-np-actions' },
|
|
643
|
+
h('p', { className: 'dsh-np-note' }, t('managerHint')),
|
|
644
|
+
openManager === undefined
|
|
645
|
+
? null
|
|
646
|
+
: h('button', {
|
|
647
|
+
type: 'button',
|
|
648
|
+
className: 'dsh-np-button',
|
|
649
|
+
'data-variant': 'ghost',
|
|
650
|
+
onClick: openManager,
|
|
651
|
+
}, t('openManager')),
|
|
652
|
+
),
|
|
653
|
+
),
|
|
654
|
+
)
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* The row configuration page the Plugins manager renders for this bundle.
|
|
659
|
+
* @param props - the page's view, the configuration form, and the translate seat.
|
|
660
|
+
* @returns the summary line or the settings card.
|
|
661
|
+
*/
|
|
662
|
+
function ProxyRowConfig(props) {
|
|
663
|
+
const t = typeof props.t === 'function' ? props.t : props.translate
|
|
664
|
+
const form = props.form
|
|
665
|
+
const snapshot = form === undefined ? undefined : form.state
|
|
666
|
+
if (props.view === 'summary') {
|
|
667
|
+
const value = pick(snapshot !== undefined && snapshot.status === 'ready' ? snapshot.value : undefined)
|
|
668
|
+
if (!value.enabled) return h('span', null, t('summaryOff'))
|
|
669
|
+
if (!supportedProxyUrl(value.proxyUrl)) return h('span', null, `${t('summaryOn')} · ${t('summaryInvalidProxy')}`)
|
|
670
|
+
const count = splitDomains(value.domains).filter(isValidDomainEntry).length
|
|
671
|
+
if (count === 0) return h('span', null, `${t('summaryOn')} · ${t('summaryNone')}`)
|
|
672
|
+
return h('span', null, `${t('summaryOn')} · ${count === 1 ? t('summaryDomainOne') : t('summaryDomains', { count })}`)
|
|
673
|
+
}
|
|
674
|
+
return h(React.Fragment, null,
|
|
675
|
+
h('style', null, CSS),
|
|
676
|
+
h('div', { className: 'dsh-np-inner' },
|
|
677
|
+
h(SettingsCard, {
|
|
678
|
+
t,
|
|
679
|
+
snapshot,
|
|
680
|
+
mutate: form === undefined ? undefined : form.mutate,
|
|
681
|
+
}),
|
|
682
|
+
),
|
|
683
|
+
)
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Decorative glyph for the sidebar's panel row.
|
|
688
|
+
* @param props - the sidebar's requested edge.
|
|
689
|
+
* @returns the globe glyph element.
|
|
690
|
+
*/
|
|
691
|
+
function ProxyIcon(props) {
|
|
692
|
+
const size = typeof props.size === 'number' ? props.size : 20
|
|
693
|
+
return h('svg', {
|
|
694
|
+
width: size,
|
|
695
|
+
height: size,
|
|
696
|
+
viewBox: '0 0 24 24',
|
|
697
|
+
fill: 'none',
|
|
698
|
+
'aria-hidden': true,
|
|
699
|
+
focusable: false,
|
|
700
|
+
style: { display: 'block' },
|
|
701
|
+
},
|
|
702
|
+
h('circle', { cx: 12, cy: 12, r: 8.2, stroke: 'currentColor', strokeWidth: 1.6 }),
|
|
703
|
+
h('ellipse', { cx: 12, cy: 12, rx: 4, ry: 8.2, stroke: 'currentColor', strokeWidth: 1.6 }),
|
|
704
|
+
h('path', { d: 'M4.37 9h15.26M4.37 15h15.26', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round' }),
|
|
705
|
+
)
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
return {
|
|
709
|
+
inject: ['slots', 'locale', 'configForms'],
|
|
710
|
+
apply(ctx) {
|
|
711
|
+
ctx.effect(() => ctx.locale.register(NS, { en: EN, zh: ZH }), 'net-proxy: dictionaries')
|
|
712
|
+
const t = ctx.locale.bind(NS)
|
|
713
|
+
const form = ctx.configForms.get(ENTRY_ID)
|
|
714
|
+
|
|
715
|
+
ctx.slots.inject('main', () => ctx.slots.register({
|
|
716
|
+
name: 'main',
|
|
717
|
+
key: PANEL_ID,
|
|
718
|
+
locale: NS,
|
|
719
|
+
inject: () => ({
|
|
720
|
+
proxyForm: form,
|
|
721
|
+
translate: t,
|
|
722
|
+
openPluginManager: () => {
|
|
723
|
+
const navigation = ctx.get('pluginNavigation')
|
|
724
|
+
if (navigation === undefined || navigation === null || typeof navigation.openBundle !== 'function') return
|
|
725
|
+
navigation.openBundle('@lucky_zone/net-proxy')
|
|
726
|
+
},
|
|
727
|
+
}),
|
|
728
|
+
}, ProxyPanel))
|
|
729
|
+
|
|
730
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
731
|
+
name: 'sidebar.panellist',
|
|
732
|
+
id: PANEL_ID,
|
|
733
|
+
order: 5,
|
|
734
|
+
locale: NS,
|
|
735
|
+
label: () => t('panel'),
|
|
736
|
+
}, ProxyIcon))
|
|
737
|
+
|
|
738
|
+
ctx.slots.inject('plugins.row.config', () => ctx.slots.register({
|
|
739
|
+
name: 'plugins.row.config',
|
|
740
|
+
key: ROW_KEY,
|
|
741
|
+
locale: NS,
|
|
742
|
+
inject: () => ({ translate: t }),
|
|
743
|
+
}, ProxyRowConfig))
|
|
744
|
+
},
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
})
|