@twinsearth/dsh-2xray 0.2.3 → 0.2.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.4 — 2026-10-29
4
+
5
+ ### 设置页与导入
6
+
7
+ - **设置页新增「2Xray」标签页**(`settings.section`):VLESS 链接输入、本地二维码图片选择、屏幕扫码、启用/takeover/xray 路径/端口,保存后重启生效。
8
+ - 插件页卡片 `plugins.item` 的 id 改为 settings namespace(`xray-proxy`),并按页面真实契约使用 `{ state, mutate }`;写冲突(mutate=false)会显式报错,不再假装保存成功。
9
+ - 左侧「2Xray」面板保持三卡片(配置/实时信息/状态)。
10
+
11
+ ### 兼容
12
+
13
+ - 路由直连规则从 `geoip:private` 改为显式私有网段(10/8、172.16/12、192.168/16、127/8、169.254/16、::1、fc00::/7、fe80::/10),不再依赖 xray 二进制旁的 `geoip.dat`——v2rayN 自带的 xray.exe 可 `-test` 通过。
14
+
3
15
  ## 0.2.3 — 2026-10-29
4
16
 
5
17
  ### 界面
package/lib/client.js CHANGED
@@ -6,7 +6,7 @@ window.__ModuleLoader__.load({
6
6
 
7
7
  const PANEL_ID = 'xray'
8
8
  const NS = 'xray-proxy'
9
- const inject = ['slots', 'locale']
9
+ const inject = ['slots']
10
10
 
11
11
  const C = {
12
12
  page: { maxWidth: 980, margin: '0 auto', padding: '20px 24px 16px', color: 'var(--dsw-alias-label-primary, #0f1115)' },
@@ -260,76 +260,220 @@ window.__ModuleLoader__.load({
260
260
  return h('div', null, h('label', { style: C.label }, label), children)
261
261
  }
262
262
 
263
- function readFormValue(form) {
263
+ function controllerValue(controller) {
264
264
  try {
265
- const snapshot = form?.getSnapshot?.()
266
- const candidate = snapshot?.value ?? snapshot?.state?.view?.value ?? snapshot?.view?.value
267
- return candidate && typeof candidate === 'object' ? candidate : {}
265
+ const snapshot = controller?.getSnapshot?.()
266
+ return snapshot?.value && typeof snapshot.value === 'object' ? snapshot.value : {}
268
267
  } catch {
269
268
  return {}
270
269
  }
271
270
  }
272
271
 
273
- async function setField(form, field, value) {
274
- if (typeof form?.set === 'function' && typeof field === 'string') return form.set(field, value)
275
- const snapshot = form?.getSnapshot?.()
276
- const revision = snapshot?.revision ?? snapshot?.state?.view?.revision
277
- if (typeof form?.mutate === 'function') {
278
- return form.mutate([{ op: 'set', path: Array.isArray(field) ? field : [field], value }], revision)
272
+ function pageValue(pageForm) {
273
+ const value = pageForm?.state?.value
274
+ return value && typeof value === 'object' ? value : {}
275
+ }
276
+
277
+ async function writeFields({ pageForm, controller }, fields) {
278
+ if (pageForm && typeof pageForm.mutate === 'function') {
279
+ const ops = fields.map(({ path, value }) => ({ op: 'set', path, value }))
280
+ const accepted = await pageForm.mutate(ops, pageForm.state?.revision)
281
+ if (accepted === false) throw new Error('写入被拒绝(revision 冲突)')
282
+ return
283
+ }
284
+ if (controller && typeof controller.mutate === 'function') {
285
+ for (const { path, value } of fields) {
286
+ if (path.length === 1 && typeof controller.set === 'function') {
287
+ const accepted = await controller.set(path[0], value)
288
+ if (accepted === false) throw new Error('写入被拒绝(revision 冲突)')
289
+ } else {
290
+ const revision = controller.getSnapshot?.()?.revision
291
+ const accepted = await controller.mutate([{ op: 'set', path, value }], revision)
292
+ if (accepted === false) throw new Error('写入被拒绝(revision 冲突)')
293
+ }
294
+ }
295
+ return
279
296
  }
280
297
  throw new Error('设置表单不可写')
281
298
  }
282
299
 
283
- function XraySettingsCard({ form }) {
284
- const value = readFormValue(form)
300
+ async function decodeQrFromCanvas(canvas) {
301
+ if (!('BarcodeDetector' in window)) return null
302
+ const detector = new window.BarcodeDetector({ formats: ['qr_code'] })
303
+ const codes = await detector.detect(canvas)
304
+ return codes && codes.length > 0 ? codes[0].rawValue : null
305
+ }
306
+
307
+ function XrayConfigForm({ controller, pageForm }) {
308
+ const value = controller ? controllerValue(controller) : pageValue(pageForm)
285
309
  const [shareLink, setShareLink] = React.useState(value.shareLink ?? '')
286
310
  const [binaryPath, setBinaryPath] = React.useState(value.binary?.path ?? '')
287
311
  const [port, setPort] = React.useState(String(value.inbound?.port ?? 20808))
288
312
  const [enabled, setEnabled] = React.useState(value.enabled === true)
289
313
  const [takeover, setTakeover] = React.useState(value.takeover ?? 'auto')
290
314
  const [message, setMessage] = React.useState('')
315
+ const [kind, setKind] = React.useState('')
291
316
  const [busy, setBusy] = React.useState(false)
317
+ const [scanning, setScanning] = React.useState(false)
318
+ const videoRef = React.useRef(null)
319
+ const streamRef = React.useRef(null)
320
+ const timerRef = React.useRef(null)
321
+
322
+ const say = (text, tone) => {
323
+ setMessage(text)
324
+ setKind(tone || '')
325
+ }
326
+
327
+ const applyDecoded = (text) => {
328
+ const trimmed = String(text || '').trim()
329
+ if (!trimmed) return
330
+ setShareLink(trimmed)
331
+ say('已识别二维码,确认后点保存。', 'ok')
332
+ }
333
+
334
+ const onFile = async (event) => {
335
+ const file = event.target.files && event.target.files[0]
336
+ if (!file) return
337
+ say('正在解析图片…')
338
+ try {
339
+ const bitmap = await createImageBitmap(file)
340
+ const scale = Math.min(1, 1600 / Math.max(bitmap.width, bitmap.height))
341
+ const canvas = document.createElement('canvas')
342
+ canvas.width = Math.max(1, Math.round(bitmap.width * scale))
343
+ canvas.height = Math.max(1, Math.round(bitmap.height * scale))
344
+ canvas.getContext('2d').drawImage(bitmap, 0, 0, canvas.width, canvas.height)
345
+ const decoded = await decodeQrFromCanvas(canvas)
346
+ if (!decoded) say('没有识别到二维码;当前浏览器不支持或图片不清晰,可改用插件目录 tools/scanner.html。', 'error')
347
+ else applyDecoded(decoded)
348
+ } catch (error) {
349
+ say('解析失败:' + (error.message || error), 'error')
350
+ }
351
+ }
352
+
353
+ const stopScan = () => {
354
+ if (timerRef.current) clearInterval(timerRef.current)
355
+ timerRef.current = null
356
+ if (streamRef.current) for (const track of streamRef.current.getTracks()) track.stop()
357
+ streamRef.current = null
358
+ if (videoRef.current) videoRef.current.srcObject = null
359
+ setScanning(false)
360
+ }
361
+
362
+ const tick = async () => {
363
+ const video = videoRef.current
364
+ if (!video || !streamRef.current || video.readyState < 2 || !video.videoWidth) return
365
+ const canvas = document.createElement('canvas')
366
+ canvas.width = video.videoWidth
367
+ canvas.height = video.videoHeight
368
+ canvas.getContext('2d').drawImage(video, 0, 0)
369
+ try {
370
+ const decoded = await decodeQrFromCanvas(canvas)
371
+ if (decoded) {
372
+ applyDecoded(decoded)
373
+ stopScan()
374
+ }
375
+ } catch {
376
+ // a frame without a readable code is normal
377
+ }
378
+ }
379
+
380
+ const startScan = async () => {
381
+ if (!navigator.mediaDevices || !navigator.mediaDevices.getDisplayMedia) {
382
+ say('当前浏览器不支持屏幕捕获;请使用「二维码图片」。', 'error')
383
+ return
384
+ }
385
+ if (!('BarcodeDetector' in window)) {
386
+ say('当前浏览器没有 BarcodeDetector;请使用插件目录 tools/scanner.html(内置 jsQR)。', 'error')
387
+ return
388
+ }
389
+ try {
390
+ const stream = await navigator.mediaDevices.getDisplayMedia({ video: { frameRate: 10 }, audio: false })
391
+ streamRef.current = stream
392
+ setScanning(true)
393
+ if (videoRef.current) {
394
+ videoRef.current.srcObject = stream
395
+ videoRef.current.style.display = 'block'
396
+ await videoRef.current.play()
397
+ }
398
+ stream.getVideoTracks()[0].addEventListener('ended', stopScan)
399
+ timerRef.current = setInterval(tick, 350)
400
+ say('请把二维码放到共享屏幕上…')
401
+ } catch (error) {
402
+ say('屏幕共享未开始:' + (error.message || error), 'error')
403
+ setScanning(false)
404
+ }
405
+ }
406
+
407
+ React.useEffect(() => () => stopScan(), [])
292
408
 
293
409
  const save = async () => {
294
410
  setBusy(true)
295
411
  try {
296
- await setField(form, 'shareLink', shareLink.trim())
297
- await setField(form, 'enabled', enabled)
298
- await setField(form, 'takeover', takeover)
412
+ const fields = [
413
+ { path: ['shareLink'], value: shareLink.trim() },
414
+ { path: ['enabled'], value: enabled },
415
+ { path: ['takeover'], value: takeover },
416
+ ]
417
+ if (binaryPath.trim()) fields.push({ path: ['binary', 'path'], value: binaryPath.trim() })
299
418
  const numericPort = Number(port)
300
- if (Number.isInteger(numericPort) && numericPort > 0) await setField(form, ['inbound', 'port'], numericPort)
301
- if (binaryPath.trim()) await setField(form, ['binary', 'path'], binaryPath.trim())
302
- setMessage('已保存,重启 DSH 后生效。')
419
+ if (Number.isInteger(numericPort) && numericPort > 0) fields.push({ path: ['inbound', 'port'], value: numericPort })
420
+ await writeFields({ controller, pageForm }, fields)
421
+ say('已保存。重启 DSH 后生效。', 'ok')
303
422
  } catch (error) {
304
- setMessage('保存失败:' + (error.message || error))
423
+ say('保存失败:' + (error.message || error), 'error')
305
424
  } finally {
306
425
  setBusy(false)
307
426
  }
308
427
  }
309
428
 
310
- return h('div', { style: { ...C.card, marginTop: 8 } },
311
- h('div', { style: C.cardHead }, h(GlobeIcon), '2Xray 连接'),
312
- h(Field, { label: 'VLESS 分享链接(粘贴或扫码)' },
429
+ return h('div', null,
430
+ h(Field, { label: '1. VLESS 分享链接(粘贴)' },
313
431
  h('textarea', {
314
432
  style: { ...C.input, minHeight: 64, fontFamily: 'ui-monospace, Consolas, monospace' },
315
433
  value: shareLink,
316
434
  placeholder: 'vless://uuid@host:443?security=reality&sni=...&pbk=...#备注',
317
435
  onChange: (event) => setShareLink(event.target.value),
318
436
  })),
437
+ h(Field, { label: '2. 本地二维码图片' },
438
+ h('input', { type: 'file', accept: 'image/*', onChange: onFile, disabled: scanning })),
439
+ h('div', { style: { display: 'flex', gap: 10, alignItems: 'center', marginTop: 10, flexWrap: 'wrap' } },
440
+ h('button', { type: 'button', style: C.btn, onClick: startScan, disabled: scanning }, scanning ? '扫描中…' : '扫描屏幕二维码'),
441
+ h('button', { type: 'button', style: C.btn, onClick: stopScan, disabled: !scanning }, '停止'),
442
+ h('span', { style: { ...C.muted, fontSize: 12 } }, '本地图片/屏幕扫码结果会填入上方链接')),
443
+ h('video', { ref: videoRef, playsInline: true, muted: true, style: { ...C.input, display: 'none', maxHeight: 240, background: '#000' } }),
319
444
  h('div', { style: { display: 'flex', gap: 12, flexWrap: 'wrap' } },
320
- h('label', { style: { fontSize: 12.5, display: 'flex', alignItems: 'center', gap: 6, marginTop: 8 } },
445
+ h('label', { style: { fontSize: 12.5, display: 'flex', alignItems: 'center', gap: 6, marginTop: 10 } },
321
446
  h('input', { type: 'checkbox', checked: enabled, onChange: (event) => setEnabled(event.target.checked) }), '启用代理'),
322
- h('label', { style: { fontSize: 12.5, display: 'flex', alignItems: 'center', gap: 6, marginTop: 8 } }, 'takeover',
447
+ h('label', { style: { fontSize: 12.5, display: 'flex', alignItems: 'center', gap: 6, marginTop: 10 } }, 'takeover',
323
448
  h('select', { value: takeover, onChange: (event) => setTakeover(event.target.value), style: { padding: '4px 8px', borderRadius: 6, background: 'transparent', color: 'inherit' } },
324
449
  h('option', { value: 'auto' }, 'auto'), h('option', { value: 'force' }, 'force'), h('option', { value: 'off' }, 'off')))),
325
450
  h(Field, { label: 'xray 可执行文件路径(留空自动查找)' },
326
- h('input', { style: { ...C.input, fontFamily: 'ui-monospace, Consolas, monospace' }, value: binaryPath, placeholder: '例如 D:\\v2rayN\\xray.exe', onChange: (event) => setBinaryPath(event.target.value) })),
451
+ h('input', { style: { ...C.input, fontFamily: 'ui-monospace, Consolas, monospace' }, value: binaryPath, placeholder: '例如 D:\\v2rayN\\...\\bin\\xray\\xray.exe', onChange: (event) => setBinaryPath(event.target.value) })),
327
452
  h(Field, { label: '本地 HTTP 入站端口' },
328
453
  h('input', { style: C.input, value: port, onChange: (event) => setPort(event.target.value) })),
329
454
  h('div', { style: { display: 'flex', gap: 10, alignItems: 'center', marginTop: 12 } },
330
455
  h('button', { type: 'button', style: C.primaryBtn, disabled: busy, onClick: save }, busy ? '保存中…' : '保存'),
331
- h('span', { style: { ...C.muted, fontSize: 12 } }, message)),
332
- h('p', { style: { ...C.muted, fontSize: 12, marginTop: 10 } }, '打开左侧「2Xray」面板可扫码导入、查看节点/日志/延迟/出口 IP。'))
456
+ h('span', { style: { fontSize: 12, color: kind === 'error' ? '#c81e1e' : kind === 'ok' ? '#15803d' : undefined } }, message)),
457
+ h('p', { style: { ...C.muted, fontSize: 12, marginTop: 10 } }, '保存后重启 DSH;左侧「2Xray」面板显示节点、日志、延迟与出口 IP。'))
458
+ }
459
+
460
+ function XraySettingsCard({ view, form }) {
461
+ if (view === 'summary') {
462
+ return h('div', { style: { ...C.muted, fontSize: 12.5 } }, 'VLESS 链接 / 本地二维码导入 · 保存后重启 DSH 生效')
463
+ }
464
+ return h('div', { style: { ...C.card, marginTop: 8 } },
465
+ h('div', { style: C.cardHead }, h(GlobeIcon), '2Xray 连接'),
466
+ h(XrayConfigForm, { pageForm: form }))
467
+ }
468
+
469
+ function SettingsSection({ controller }) {
470
+ return h('div', { style: C.page },
471
+ h('div', { style: C.header },
472
+ h('span', { style: C.globe }, h(GlobeIcon, { size: 22 })),
473
+ h('div', null,
474
+ h('h1', { style: C.title }, '2Xray'),
475
+ h('p', { style: C.sub }, '导入 VLESS 链接或本地二维码,保存后重启 DSH 生效。'))),
476
+ h('section', { style: C.card }, h(XrayConfigForm, { controller })))
333
477
  }
334
478
 
335
479
  function apply(ctx) {
@@ -350,26 +494,32 @@ window.__ModuleLoader__.load({
350
494
  if (typeof ctx.inject === 'function') {
351
495
  ctx.inject(['configForms'], (child) => {
352
496
  try {
497
+ // A dedicated Settings tab. It owns the ConfigFormController, so the
498
+ // full editor works even when the plugins page passes a narrow form.
499
+ const controller = child.configForms.get(NS)
500
+ child.effect(
501
+ () =>
502
+ child.slots.inject('settings.section', () =>
503
+ child.slots.register(
504
+ { name: 'settings.section', id: NS, order: 30, label: () => '2Xray' },
505
+ () => h(SettingsSection, { controller }),
506
+ ),
507
+ ),
508
+ 'dsh-2xray: settings section',
509
+ )
510
+ // The plugins page card: its id MUST equal the settings namespace,
511
+ // otherwise the page's formFor(id) returns undefined.
353
512
  child.effect(
354
513
  () =>
355
514
  child.configForms.whileServed([NS], () =>
356
515
  child.slots.inject('plugins.item', () =>
357
- child.slots.register(
358
- {
359
- name: 'plugins.item',
360
- id: PANEL_ID,
361
- order: 40,
362
- label: () => '2Xray 连接',
363
- inject: () => ({ form: child.configForms.get(NS) }),
364
- },
365
- XraySettingsCard,
366
- ),
516
+ child.slots.register({ name: 'plugins.item', id: NS, order: 40, label: () => '2Xray' }, XraySettingsCard),
367
517
  ),
368
518
  ),
369
- 'dsh-2xray: settings card',
519
+ 'dsh-2xray: plugins card',
370
520
  )
371
- } catch {
372
- // a client without the settings service keeps the sidebar panel
521
+ } catch (error) {
522
+ console.error('[dsh-2xray] client registration failed', error)
373
523
  }
374
524
  })
375
525
  }
@@ -17,7 +17,19 @@
17
17
  * `api` routing rule, and optionally the stats counters.
18
18
  */
19
19
 
20
- const PRIVATE_IP_RULES = ['geoip:private']
20
+ // Explicit RFC1918/loopback/link-local ranges instead of geoip:private: the
21
+ // latter needs geoip.dat beside the binary, and v2rayN's xray.exe keeps it one
22
+ // directory up, which fails -test with "failed to open geoip.dat".
23
+ const PRIVATE_IP_RULES = [
24
+ '10.0.0.0/8',
25
+ '172.16.0.0/12',
26
+ '192.168.0.0/16',
27
+ '127.0.0.0/8',
28
+ '169.254.0.0/16',
29
+ '::1/128',
30
+ 'fc00::/7',
31
+ 'fe80::/10',
32
+ ]
21
33
  const API_TAG = 'api'
22
34
 
23
35
  function escapeRegExp(value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twinsearth/dsh-2xray",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "private": false,
5
5
  "description": "DSH-2Xray: supervises xray-core and routes DSH process-wide HTTP(S) traffic through its local inbound, with optional multi-node gRPC control plane",
6
6
  "type": "module",
@@ -1,7 +1,7 @@
1
1
  import test from 'node:test'
2
2
  import assert from 'node:assert/strict'
3
3
 
4
- test('the client bundle registers a main panel and a sidebar entry', async () => {
4
+ test('the client bundle registers sidebar, main panel and settings entries', async () => {
5
5
  let captured
6
6
  globalThis.window = { __ModuleLoader__: { load(module) { captured = module } } }
7
7
  try {
@@ -24,10 +24,11 @@ test('the client bundle registers a main panel and a sidebar entry', async () =>
24
24
  })
25
25
 
26
26
  assert.equal(typeof mod.apply, 'function')
27
- assert.deepEqual(mod.inject, ['slots', 'locale'])
27
+ assert.deepEqual(mod.inject, ['slots'])
28
28
  assert.equal(mod.PANEL_ID, 'xray')
29
+ assert.equal(mod.NS, 'xray-proxy')
29
30
 
30
- const registrations = []
31
+ const base = []
31
32
  const ctx = {
32
33
  effect(fn) {
33
34
  fn()
@@ -37,20 +38,64 @@ test('the client bundle registers a main panel and a sidebar entry', async () =>
37
38
  callback()
38
39
  },
39
40
  register(definition, component) {
40
- registrations.push({ definition, component })
41
+ base.push({ definition, component })
41
42
  },
42
43
  },
43
44
  }
44
45
  mod.apply(ctx)
45
46
 
46
47
  assert.deepEqual(
47
- registrations.map((entry) => entry.definition.name),
48
+ base.map((entry) => entry.definition.name),
48
49
  ['sidebar.panellist', 'main'],
49
50
  )
50
- assert.equal(registrations[0].definition.id, 'xray')
51
- assert.equal(registrations[0].definition.order, 1)
52
- assert.equal(registrations[0].definition.label(), '2Xray')
53
- const icon = registrations[0].component()
54
- assert.equal(icon.type, 'svg')
55
- assert.equal(registrations[1].definition.key, 'xray')
51
+ assert.equal(base[0].definition.id, 'xray')
52
+ assert.equal(base[0].definition.order, 1)
53
+ assert.equal(base[0].definition.label(), '2Xray')
54
+ assert.equal(base[0].component().type, 'svg')
55
+ assert.equal(base[1].definition.key, 'xray')
56
+
57
+ const settings = []
58
+ const controller = {
59
+ getSnapshot: () => ({ status: 'ready', value: { shareLink: '' }, revision: 0, writable: true }),
60
+ set: async () => true,
61
+ mutate: async () => true,
62
+ }
63
+ const child = {
64
+ effect(fn) {
65
+ fn()
66
+ },
67
+ slots: {
68
+ inject(_name, callback) {
69
+ callback()
70
+ },
71
+ register(definition, component) {
72
+ settings.push({ definition, component })
73
+ },
74
+ },
75
+ configForms: {
76
+ get: () => controller,
77
+ whileServed(namespaces, register) {
78
+ return register(new Set(namespaces))
79
+ },
80
+ },
81
+ }
82
+ mod.apply({
83
+ effect(fn) {
84
+ fn()
85
+ },
86
+ slots: { inject() {}, register() {} },
87
+ inject(services, callback) {
88
+ assert.deepEqual(services, ['configForms'])
89
+ callback(child)
90
+ },
91
+ })
92
+
93
+ assert.deepEqual(
94
+ settings.map((entry) => entry.definition.name),
95
+ ['settings.section', 'plugins.item'],
96
+ )
97
+ // The plugins page looks the form up by the settings namespace, not the panel id.
98
+ assert.equal(settings[0].definition.id, 'xray-proxy')
99
+ assert.equal(settings[1].definition.id, 'xray-proxy')
100
+ assert.equal(settings[1].definition.label(), '2Xray')
56
101
  })
@@ -82,7 +82,9 @@ test('direct and block outbounds exist and the catch-all goes to proxy', () => {
82
82
  test('private addresses and internal suffixes are routed direct', () => {
83
83
  const built = buildXrayConfig(config({ root: { routing: { bypassDomains: ['lan', 'corp'] } } }))
84
84
  const ipRule = built.routing.rules.find((rule) => rule.ip)
85
- assert.deepEqual(ipRule.ip, ['geoip:private'])
85
+ assert.ok(ipRule.ip.includes('10.0.0.0/8'))
86
+ assert.ok(ipRule.ip.includes('192.168.0.0/16'))
87
+ assert.equal(ipRule.ip.includes('geoip:private'), false)
86
88
  const domainRule = built.routing.rules.find((rule) => rule.domain && rule.domain.some((entry) => entry.startsWith('domain:lan')))
87
89
  assert.deepEqual(domainRule.domain, ['domain:lan', 'domain:corp'])
88
90
  })