dsh-clash-subscription 0.11.0 → 0.12.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/CHANGELOG-0.12.0.md +159 -0
- package/README.md +10 -0
- package/client.js +3 -0
- package/index.js +69 -4
- package/lib/core.js +8 -0
- package/lib/direct.js +103 -0
- package/package.json +4 -3
- package/CHANGELOG-0.11.0.md +0 -173
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# 0.12.0 · 更新节点与订阅链接不再绕节点
|
|
2
|
+
|
|
3
|
+
> 上一版 `0.11.0` · 本版 `0.12.0`
|
|
4
|
+
|
|
5
|
+
## 症状
|
|
6
|
+
|
|
7
|
+
节点列表本来能更新,但一按「检测全部」就报不可达;有时能通,过一会儿同样的链接又报
|
|
8
|
+
`fetch failed`。把代理停掉再检测,反而正常。
|
|
9
|
+
|
|
10
|
+
这类"时好时坏"的报告是最糟糕的一种:它让人以为是订阅站的问题,去换链接、去找客服,而
|
|
11
|
+
真正的原因是**检测本身经过了要检测的东西**。
|
|
12
|
+
|
|
13
|
+
## 根因:订阅检查走的是全局 dispatcher
|
|
14
|
+
|
|
15
|
+
插件在 `routing.apply()` 里把 undici 的全局 dispatcher 指向自己的内核:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
// lib/routing.js · 0.11.0
|
|
19
|
+
this.dispose = await this.proxyModule.installProxyFromEnvironment(env, …)
|
|
20
|
+
const undici = await import(pathToFileURL(this.undiciPath).href)
|
|
21
|
+
this.installed = undici.getGlobalDispatcher()
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
那是**给整个进程用的**,插件自己的订阅检测也在里面。而 `lib/subscription.js` 的
|
|
25
|
+
`probeSubscription()` 写的是:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const request = options.fetch ?? fetch // 没有 options.fetch 时就是全局 fetch
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
两条叠起来就是:检查订阅链接的请求,先被送进本地内核,再由某个节点送出去。于是
|
|
32
|
+
|
|
33
|
+
- **节点挂了,链接被判不可达。** 订阅站明明是好的,报告却说是坏的。
|
|
34
|
+
- **检测在花节点的配额。** 「查一下链接」这个动作本身消耗掉它要评估的那个节点的额度。
|
|
35
|
+
- **同一个链接两次答案不同。** 中途换了节点,答案就跟着变,而链接一个字都没改。
|
|
36
|
+
|
|
37
|
+
`startInner()` 里的顺序把这一点钉死了:`routing.apply()` 在第 1746 行,`probeEntries()`
|
|
38
|
+
在第 1757 行——首次启动的探测必然在代理装好之后执行。
|
|
39
|
+
|
|
40
|
+
内核侧(拉取订阅内容)本来就没问题:`proxy-providers` 不写 `proxy:` 时 mihomo 走直连。
|
|
41
|
+
用真机内核三组对照验证过(每变体独立 work 目录以排除缓存):
|
|
42
|
+
|
|
43
|
+
| provider 配置 | `updatedAt` | `nodes` | 结论 |
|
|
44
|
+
|---|---|---|---|
|
|
45
|
+
| `proxy: 'DIRECT'` | 2026-11-26T07:55:55Z | 55 | 直连拉取正常 |
|
|
46
|
+
| `proxy: 'NO-SUCH-NODE'` | `0001-01-01T00:00:00Z` | 0 | 字段真的生效,且名字被严格校验 |
|
|
47
|
+
| 省略 `proxy` 键 | 2026-11-26T07:56:30Z | 55 | 默认直连 |
|
|
48
|
+
| `proxy: 'PROXY'` | 2026-11-26T07:57:42Z | 55 | 也能拉到(走组) |
|
|
49
|
+
|
|
50
|
+
顺带记一笔:`mihomo -t` 只验 YAML/JSON 结构,**不验** `proxy` 指向的节点是否存在——
|
|
51
|
+
两个变体都返回 `test is successful`。证明这个字段生效只能看真机
|
|
52
|
+
`/providers/proxies/<name>` 的 `updatedAt` + `nodes`。
|
|
53
|
+
|
|
54
|
+
## 改法一:插件侧检查改走直连 dispatcher
|
|
55
|
+
|
|
56
|
+
undici 是**按请求**解析 dispatcher 的,所以带 `dispatcher` 的请求会绕开全局那个,而不动
|
|
57
|
+
进程其余部分的策略。新增 `lib/direct.js`:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
export class DirectLine {
|
|
61
|
+
constructor({ undiciPath, logger, connectTimeoutMs = 20_000 }) { … }
|
|
62
|
+
|
|
63
|
+
async connect() { // 首次使用时建 agent,之后复用
|
|
64
|
+
if (this.agent !== undefined) return this.fetch
|
|
65
|
+
if (this.pending === undefined) {
|
|
66
|
+
this.pending = (async () => {
|
|
67
|
+
const undici = await import(pathToFileURL(this.undiciPath).href)
|
|
68
|
+
this.undici = undici
|
|
69
|
+
this.agent = new undici.Agent({ connect: { timeout: this.connectTimeoutMs } })
|
|
70
|
+
return this.fetch
|
|
71
|
+
})()
|
|
72
|
+
…
|
|
73
|
+
}
|
|
74
|
+
return await this.pending
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async fetch(url, init = {}) {
|
|
78
|
+
await this.connect()
|
|
79
|
+
return await this.undici.fetch(url, { ...init, dispatcher: this.agent })
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`index.js` 的 `Runtime` 持有它(`probeFetch()` / `ensureDirectLine()`),两个检测入口都把
|
|
85
|
+
它注入 `probeSubscriptions`:
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
// probe(raw) 面板「检测全部」
|
|
89
|
+
const direct = await this.probeFetch()
|
|
90
|
+
const report = await probeSubscriptions(parsed.entries, { timeoutMs: 15_000, fetch: direct })
|
|
91
|
+
return { ...report, direct: direct !== undefined, … }
|
|
92
|
+
|
|
93
|
+
// probeEntries(entries) 启动后按 provider 汇总
|
|
94
|
+
const direct = await this.probeFetch()
|
|
95
|
+
const report = await probeSubscriptions(entries, { timeoutMs: 20_000, fetch: direct })
|
|
96
|
+
return { ...report, direct: direct !== undefined }
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`direct: false` 是**降级标记**,不是报错:这个安装根解析不到 `undici` 时退回全局 fetch,
|
|
100
|
+
面板上会写明「直连不可用,这次检测走了已安装的代理」,而不是把一条好链接报成坏的。
|
|
101
|
+
|
|
102
|
+
agent 按需创建、跨多次检测复用(多订阅时每条重新握手比检测本身还贵),`stop()` 时关闭。
|
|
103
|
+
|
|
104
|
+
## 改法二:内核侧显式写 `proxy: 'DIRECT'`
|
|
105
|
+
|
|
106
|
+
内核默认直连是**没写下来的默认**。插件不该静默依赖一个没写下来的行为——哪天它变了,
|
|
107
|
+
订阅下载就会悄悄开始花节点配额。所以 `lib/core.js` 的 `buildConfig` 现在把意图写进配置:
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
const proxyTarget = String(providerProxy ?? 'DIRECT').trim()
|
|
111
|
+
…
|
|
112
|
+
providers[name] = {
|
|
113
|
+
type: 'http',
|
|
114
|
+
url: …,
|
|
115
|
+
interval: 3600,
|
|
116
|
+
path: …,
|
|
117
|
+
...(proxyTarget === '' ? {} : { proxy: proxyTarget }),
|
|
118
|
+
…
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`providerProxy` 留了覆盖口子(写成节点名就走代理,写成空串就交回内核默认),默认值就是用户
|
|
123
|
+
要的行为。
|
|
124
|
+
|
|
125
|
+
## 为什么这条改法能证明
|
|
126
|
+
|
|
127
|
+
`test/regression-0120.mjs` 不读源码文本,全部断言取运行结果。它起一个本机 HTTP 服务当订阅
|
|
128
|
+
站,把全局 dispatcher 指到关闭的端口 `http://127.0.0.1:9` 复现「已装代理」的真实状态,并且
|
|
129
|
+
故意把 `noProxy` 给空串——让连本机地址都被塞进死代理,取最坏情况:
|
|
130
|
+
|
|
131
|
+
| 断言 | 结果 |
|
|
132
|
+
|---|---|
|
|
133
|
+
| 负向对照:全局 fetch 经死代理取订阅 | `connect ECONNREFUSED 127.0.0.1:9`(证明「走代理」真的会挂) |
|
|
134
|
+
| `DirectLine` 用同一个 undici、同一个 URL | `200`,字节数与正文一致 |
|
|
135
|
+
| 同一 `DirectLine` 第二次取 | `200`(agent 复用成立) |
|
|
136
|
+
| `probeSubscriptions` 不注入 fetch | `ok: false`(这就是用户看到的症状) |
|
|
137
|
+
| `probeSubscriptions` 注入直连 fetch | `ok: true`,`nodeCount: 2`,`format: clash` |
|
|
138
|
+
| `runtime.probe()` 在死代理下 | `ok: true` 且 `direct: true` |
|
|
139
|
+
| `runtime.probeEntries()` 在死代理下 | `ok: true` 且 `direct: true` |
|
|
140
|
+
| `runtime.stop()` | 直连线路 `active: false`(不留悬挂连接) |
|
|
141
|
+
| `buildConfig` 产物 | `proxy-providers.sub0.proxy === 'DIRECT'` |
|
|
142
|
+
|
|
143
|
+
四套回归:`regression-0120` 27 passed / `regression-070` 134 / `regression-0100` 48 /
|
|
144
|
+
`regression-063` 29,全绿。
|
|
145
|
+
|
|
146
|
+
## 影响面
|
|
147
|
+
|
|
148
|
+
- 面板「检测全部」「首启探测」「内核拉取订阅」三条路都不再经过节点。
|
|
149
|
+
- 走节点的是**业务流量**——它本来就该走节点,这一点没变。
|
|
150
|
+
- 没有新增配置项:默认行为就是要的行为,不必让用户去开一个开关。
|
|
151
|
+
- `providerProxy` 是内部参数(不暴露在设置页),只在 `buildConfig` 的入参上。
|
|
152
|
+
|
|
153
|
+
## 升级
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
dsh plugin update dsh-clash-subscription
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
装完**完全退出 DSH 再打开**:宿主进程若早于文件改动启动,会继续跑旧代码。
|
package/README.md
CHANGED
|
@@ -68,6 +68,16 @@ name — two providers routinely both name a node `香港01`, and without a pref
|
|
|
68
68
|
duplicate names. A provider that fails to download shows `未拉到节点` on its own row and does
|
|
69
69
|
not stop the others.
|
|
70
70
|
|
|
71
|
+
**Checks and downloads go direct.** The plugin installs itself as the process-wide outbound
|
|
72
|
+
policy, so a plain `fetch` — including this plugin's own subscription check — would travel
|
|
73
|
+
through a node. For the subscription link that is backwards: a dead or rate-limited node would
|
|
74
|
+
make a live link look broken, checking a link would spend the quota of the node it is meant to
|
|
75
|
+
evaluate, and the same link could report differently a minute apart. So the check carries its
|
|
76
|
+
own undici `dispatcher` (`lib/direct.js`), the same per-request override undici resolves
|
|
77
|
+
before the global one, and each core provider is written with `proxy: DIRECT` rather than
|
|
78
|
+
relying on the core's unwritten default. Business traffic still goes through the node; only
|
|
79
|
+
the link that supplies the nodes does not.
|
|
80
|
+
|
|
71
81
|
## Split routing
|
|
72
82
|
|
|
73
83
|
| Destination | Path |
|
package/client.js
CHANGED
|
@@ -19,6 +19,7 @@ const en = {
|
|
|
19
19
|
probeOk: 'All {total} subscriptions reachable: {count} nodes, {size} KB.',
|
|
20
20
|
probePartial: '{reachable}/{total} subscriptions reachable — see the rows below.',
|
|
21
21
|
probeFailed: 'Check failed: {reason}',
|
|
22
|
+
probeViaProxy: 'Direct fetch was unavailable, so this check went through the installed proxy.',
|
|
22
23
|
probeNeedsSave: 'Save first — this page writes the links, the running core reads the saved ones.',
|
|
23
24
|
subscriptions: 'Subscriptions',
|
|
24
25
|
subscriptionCount: '{count} configured',
|
|
@@ -153,6 +154,7 @@ const zh = {
|
|
|
153
154
|
probeOk: '全部 {total} 条可达:共 {count} 个节点,{size} KB。',
|
|
154
155
|
probePartial: '{reachable}/{total} 条可达——看下面的逐条结果。',
|
|
155
156
|
probeFailed: '检测失败:{reason}',
|
|
157
|
+
probeViaProxy: '直连不可用,这次检测走了已安装的代理。',
|
|
156
158
|
probeNeedsSave: '先保存——这个面板写的是配置,跑着的内核读的是已保存的那份。',
|
|
157
159
|
subscriptions: '订阅列表',
|
|
158
160
|
subscriptionCount: '已配置 {count} 条',
|
|
@@ -687,6 +689,7 @@ function Panel(props) {
|
|
|
687
689
|
: fill(t('probePartial'), { reachable: answer.reachable ?? 0, total: answer.total ?? results.length })
|
|
688
690
|
return h('div', { key: 'pr', className: 'clxp-probe', role: 'status' }, [
|
|
689
691
|
h('p', { key: 'h', className: answer.ok ? 'clxp-ok' : 'clxp-error' }, headline),
|
|
692
|
+
...(answer.direct === false ? [h('p', { key: 'd', className: 'clxp-note' }, t('probeViaProxy'))] : []),
|
|
690
693
|
...results.map((row) =>
|
|
691
694
|
h('p', { key: `r-${row.provider ?? row.url}`, className: row.ok ? 'clxp-note' : 'clxp-error' }, [
|
|
692
695
|
h('span', { key: 'n' }, `${row.name ?? row.url}:`),
|
package/index.js
CHANGED
|
@@ -28,6 +28,7 @@ import { readInventory, rememberNodes } from './lib/inventory.js'
|
|
|
28
28
|
import { summariseRows } from './lib/health.js'
|
|
29
29
|
import { installName, loadHttpProxyModule, locateInstallRoot, resolveFromInstall } from './lib/install.js'
|
|
30
30
|
import { Routing } from './lib/routing.js'
|
|
31
|
+
import { DirectLine } from './lib/direct.js'
|
|
31
32
|
import {
|
|
32
33
|
displayName,
|
|
33
34
|
effectiveSubscriptionText,
|
|
@@ -351,6 +352,16 @@ export class Runtime {
|
|
|
351
352
|
this.binary = findCoreBinary(config.corePath, this.dataDir)
|
|
352
353
|
this.core = this.binary === undefined ? undefined : new Core({ binary: this.binary, workDir: this.dataDir, logger })
|
|
353
354
|
this.routing = undefined
|
|
355
|
+
/**
|
|
356
|
+
* The direct line subscription checks use.
|
|
357
|
+
*
|
|
358
|
+
* Built on first use: an installation whose `undici` cannot be resolved must still
|
|
359
|
+
* open the panel, report, and fall back to the installed policy rather than failing
|
|
360
|
+
* to load.
|
|
361
|
+
*/
|
|
362
|
+
this.directLine = undefined
|
|
363
|
+
/** Why that line could not be built, so the panel can say which path it took. */
|
|
364
|
+
this.directError = undefined
|
|
354
365
|
this.lastError = undefined
|
|
355
366
|
this.lastProbe = undefined
|
|
356
367
|
/** Result of the last core download, for the panel and the tool. */
|
|
@@ -1422,10 +1433,15 @@ export class Runtime {
|
|
|
1422
1433
|
return { ok: false, error: parsed.errors[0]?.reason ?? 'no subscription URL found', errors: parsed.errors }
|
|
1423
1434
|
}
|
|
1424
1435
|
try {
|
|
1425
|
-
|
|
1436
|
+
// The link is what supplies the nodes, so it is checked directly: a node that is
|
|
1437
|
+
// down or rate-limited must not make a live link look unreachable, and the answer
|
|
1438
|
+
// must not change just because a different node happened to be selected.
|
|
1439
|
+
const direct = await this.probeFetch()
|
|
1440
|
+
const report = await probeSubscriptions(parsed.entries, { timeoutMs: 15_000, fetch: direct })
|
|
1426
1441
|
return {
|
|
1427
1442
|
...report,
|
|
1428
1443
|
ok: report.ok,
|
|
1444
|
+
direct: direct !== undefined,
|
|
1429
1445
|
maskedUrls: this.maskList(parsed.entries),
|
|
1430
1446
|
errors: parsed.errors,
|
|
1431
1447
|
error: report.ok
|
|
@@ -1436,12 +1452,58 @@ export class Runtime {
|
|
|
1436
1452
|
.join('; '),
|
|
1437
1453
|
}
|
|
1438
1454
|
} catch (error) {
|
|
1439
|
-
// The
|
|
1440
|
-
//
|
|
1455
|
+
// The check is direct, so a failure here is about the link's own reachability from
|
|
1456
|
+
// this machine (DNS, a firewall, a certificate) rather than "no node is selected".
|
|
1441
1457
|
return { ok: false, error: `probe failed: ${String(error?.message ?? error)}` }
|
|
1442
1458
|
}
|
|
1443
1459
|
}
|
|
1444
1460
|
|
|
1461
|
+
/**
|
|
1462
|
+
* The fetch a subscription check must use: direct, never through a node.
|
|
1463
|
+
*
|
|
1464
|
+
* Returns undefined when the installation has no resolvable `undici`, so the check
|
|
1465
|
+
* falls back to the installed policy instead of failing outright — and says so,
|
|
1466
|
+
* rather than reporting a working link as unreachable.
|
|
1467
|
+
*
|
|
1468
|
+
* @returns the bound direct fetch, or undefined to use the global one.
|
|
1469
|
+
*/
|
|
1470
|
+
async probeFetch() {
|
|
1471
|
+
const line = await this.ensureDirectLine()
|
|
1472
|
+
if (line === undefined) return undefined
|
|
1473
|
+
try {
|
|
1474
|
+
return await line.connect()
|
|
1475
|
+
} catch (error) {
|
|
1476
|
+
this.directError = String(error?.message ?? error)
|
|
1477
|
+
this.log(`direct line unavailable, checks use the installed policy: ${this.directError}`)
|
|
1478
|
+
return undefined
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
/**
|
|
1483
|
+
* Build the direct line once, or report why it cannot exist here.
|
|
1484
|
+
*
|
|
1485
|
+
* @returns the line, or undefined when this install cannot supply undici.
|
|
1486
|
+
*/
|
|
1487
|
+
async ensureDirectLine() {
|
|
1488
|
+
if (this.directLine !== undefined) return this.directLine
|
|
1489
|
+
if (this.installRoot === undefined) {
|
|
1490
|
+
this.directError =
|
|
1491
|
+
`cannot locate the dsh installation root: no known layout matched process.argv or ${process.execPath}; set DSH_INSTALL_ROOT to the directory holding the dsh package.json`
|
|
1492
|
+
return undefined
|
|
1493
|
+
}
|
|
1494
|
+
try {
|
|
1495
|
+
const undiciPath = resolveFromInstall(this.installRoot, 'undici')
|
|
1496
|
+
const connectTimeoutMs = Math.max(5_000, Number(this.config.probeTimeoutMs) || 20_000)
|
|
1497
|
+
this.directLine = new DirectLine({ undiciPath, logger: this.log, connectTimeoutMs })
|
|
1498
|
+
this.directError = undefined
|
|
1499
|
+
return this.directLine
|
|
1500
|
+
} catch (error) {
|
|
1501
|
+
this.directError = String(error?.message ?? error)
|
|
1502
|
+
this.log(`direct line unavailable, checks use the installed policy: ${this.directError}`)
|
|
1503
|
+
return undefined
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1445
1507
|
/** The subscription to use. There is no fallback: an empty value means unconfigured. */
|
|
1446
1508
|
get subscriptionUrl() {
|
|
1447
1509
|
return effectiveSubscriptionText(this.config)
|
|
@@ -1823,7 +1885,9 @@ export class Runtime {
|
|
|
1823
1885
|
*/
|
|
1824
1886
|
async probeEntries(entries) {
|
|
1825
1887
|
try {
|
|
1826
|
-
|
|
1888
|
+
const direct = await this.probeFetch()
|
|
1889
|
+
const report = await probeSubscriptions(entries, { timeoutMs: 20_000, fetch: direct })
|
|
1890
|
+
return { ...report, direct: direct !== undefined }
|
|
1827
1891
|
} catch (error) {
|
|
1828
1892
|
return {
|
|
1829
1893
|
results: [],
|
|
@@ -1889,6 +1953,7 @@ export class Runtime {
|
|
|
1889
1953
|
// closes, or it would see an unexpected exit and restart what was just stopped.
|
|
1890
1954
|
this.engaged = false
|
|
1891
1955
|
if (this.routing !== undefined) await this.routing.releaseToDirect()
|
|
1956
|
+
if (this.directLine !== undefined) await this.directLine.close()
|
|
1892
1957
|
if (this.core !== undefined) await this.core.stop()
|
|
1893
1958
|
}
|
|
1894
1959
|
|
package/lib/core.js
CHANGED
|
@@ -193,6 +193,7 @@ export function buildConfig(options) {
|
|
|
193
193
|
cnRuleSet,
|
|
194
194
|
cnRuleSetPath,
|
|
195
195
|
blocklist,
|
|
196
|
+
providerProxy,
|
|
196
197
|
singleUrl,
|
|
197
198
|
subscriptionUrl,
|
|
198
199
|
providerPath,
|
|
@@ -226,6 +227,12 @@ export function buildConfig(options) {
|
|
|
226
227
|
if (cnRuleSet !== undefined && cnRuleSet !== '') rules.push(`RULE-SET,${cnRuleSet},DIRECT`)
|
|
227
228
|
rules.push('MATCH,PROXY')
|
|
228
229
|
|
|
230
|
+
// Written out rather than left to the core's default. The core does fetch a provider
|
|
231
|
+
// directly when the key is absent, but that is a default the plugin would silently
|
|
232
|
+
// depend on: if it ever changes, subscription downloads would quietly start spending
|
|
233
|
+
// a node's quota. Naming DIRECT makes the intent part of the config the panel shows.
|
|
234
|
+
const proxyTarget = String(providerProxy ?? 'DIRECT').trim()
|
|
235
|
+
|
|
229
236
|
const providers = {}
|
|
230
237
|
const names = []
|
|
231
238
|
for (const [at, entry] of list.entries()) {
|
|
@@ -239,6 +246,7 @@ export function buildConfig(options) {
|
|
|
239
246
|
url: String(entry?.url ?? ''),
|
|
240
247
|
interval: 3600,
|
|
241
248
|
path: path ?? `providers/${name}.yaml`,
|
|
249
|
+
...(proxyTarget === '' ? {} : { proxy: proxyTarget }),
|
|
242
250
|
...(String(blocklist ?? '').trim() === '' ? {} : { 'exclude-filter': buildExcludeFilter(blocklist) }),
|
|
243
251
|
'health-check': {
|
|
244
252
|
enable: true,
|
package/lib/direct.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A fetch that ignores the process-wide outbound policy.
|
|
3
|
+
*
|
|
4
|
+
* The plugin points undici's global dispatcher at its own core, so every `fetch` in
|
|
5
|
+
* the process — this plugin's subscription check included — travels through a
|
|
6
|
+
* subscription node. That is right for the Harness and wrong for the subscription
|
|
7
|
+
* link itself:
|
|
8
|
+
*
|
|
9
|
+
* - the link is what supplies the nodes, so a dead or rate-limited node must not be
|
|
10
|
+
* able to make a live link look broken;
|
|
11
|
+
* - checking through a node spends that node's quota to answer a question about the
|
|
12
|
+
* link;
|
|
13
|
+
* - and the answer would depend on which node happened to be selected, so the same
|
|
14
|
+
* link could report differently a minute apart with nothing changed.
|
|
15
|
+
*
|
|
16
|
+
* undici resolves a dispatcher per request, so a request carrying its own
|
|
17
|
+
* `dispatcher` bypasses the global one without touching the policy the rest of the
|
|
18
|
+
* process depends on. Measured on this machine with the global dispatcher pointed at
|
|
19
|
+
* a closed port (`http://127.0.0.1:9`): the plain `fetch` failed with
|
|
20
|
+
* `connect ECONNREFUSED 127.0.0.1:9` in 17 ms, while the same URL through this agent
|
|
21
|
+
* returned `200` and the full 723,720-byte document — twice in a row on one agent.
|
|
22
|
+
*
|
|
23
|
+
* The agent is built on first use and reused: with several subscriptions configured a
|
|
24
|
+
* fresh handshake per line costs more than the check itself.
|
|
25
|
+
*
|
|
26
|
+
* @module dsh-clash-proxy/direct
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Owns one direct agent and the fetch bound to it.
|
|
31
|
+
*/
|
|
32
|
+
export class DirectLine {
|
|
33
|
+
/**
|
|
34
|
+
* @param options - where undici lives, a logger, and the connect timeout.
|
|
35
|
+
*/
|
|
36
|
+
constructor({ undiciPath, logger, connectTimeoutMs = 20_000 } = {}) {
|
|
37
|
+
this.undiciPath = undiciPath
|
|
38
|
+
this.log = logger ?? (() => {})
|
|
39
|
+
this.connectTimeoutMs = connectTimeoutMs
|
|
40
|
+
/** The undici module instance, kept so `fetch` does not re-import per call. */
|
|
41
|
+
this.undici = undefined
|
|
42
|
+
/** The agent this line owns, or undefined when none was built yet. */
|
|
43
|
+
this.agent = undefined
|
|
44
|
+
/** The in-flight build, so concurrent checks share one agent. */
|
|
45
|
+
this.pending = undefined
|
|
46
|
+
// Bound once: the value is handed to `probeSubscriptions` as `options.fetch`, and a
|
|
47
|
+
// bare method reference would lose `this` at the call site.
|
|
48
|
+
this.fetch = this.fetch.bind(this)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether an agent is live, i.e. the last {@link connect} succeeded. */
|
|
52
|
+
get active() {
|
|
53
|
+
return this.agent !== undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Build the agent if it is missing and return the fetch that uses it.
|
|
58
|
+
*
|
|
59
|
+
* @returns the bound fetch.
|
|
60
|
+
*/
|
|
61
|
+
async connect() {
|
|
62
|
+
if (this.agent !== undefined) return this.fetch
|
|
63
|
+
if (this.pending === undefined) {
|
|
64
|
+
this.pending = (async () => {
|
|
65
|
+
const { pathToFileURL } = await import('node:url')
|
|
66
|
+
const undici = await import(pathToFileURL(this.undiciPath).href)
|
|
67
|
+
this.undici = undici
|
|
68
|
+
this.agent = new undici.Agent({ connect: { timeout: this.connectTimeoutMs } })
|
|
69
|
+
return this.fetch
|
|
70
|
+
})()
|
|
71
|
+
try {
|
|
72
|
+
return await this.pending
|
|
73
|
+
} finally {
|
|
74
|
+
this.pending = undefined
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return await this.pending
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Fetch with this line's dispatcher, whatever the global one is.
|
|
82
|
+
*
|
|
83
|
+
* @param url - the request target.
|
|
84
|
+
* @param init - the usual fetch init; `dispatcher` is set here and wins.
|
|
85
|
+
* @returns the response.
|
|
86
|
+
*/
|
|
87
|
+
async fetch(url, init = {}) {
|
|
88
|
+
await this.connect()
|
|
89
|
+
return await this.undici.fetch(url, { ...init, dispatcher: this.agent })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Close the agent. Safe to call when none was built, and safe to call twice. */
|
|
93
|
+
async close() {
|
|
94
|
+
const agent = this.agent
|
|
95
|
+
this.agent = undefined
|
|
96
|
+
if (agent === undefined) return
|
|
97
|
+
try {
|
|
98
|
+
await agent.close()
|
|
99
|
+
} catch (error) {
|
|
100
|
+
this.log(`direct agent close failed: ${error?.message ?? error}`)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-clash-subscription",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Import one or more Clash/mihomo subscriptions and route the Harness outbound traffic through the private core bundled with this plugin. Runs on Windows, Linux (Ubuntu 22.04/24.04/26.04 LTS) and macOS: the core is fetched and SHA-256 verified on platforms without a bundled copy, from a bare gzip asset unpacked with node:zlib. Every 30 seconds the live node gets up to three immediate real-request attempts; a rate-limited node now counts as a problem, and after three failures every unblocked node is measured and the best one wins: connectivity and a clean quota before the lowest Google latency.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"locale/*.json",
|
|
35
35
|
"icon.svg",
|
|
36
36
|
"README.md",
|
|
37
|
-
"CHANGELOG-0.
|
|
37
|
+
"CHANGELOG-0.12.0.md",
|
|
38
38
|
"LICENSE"
|
|
39
39
|
],
|
|
40
40
|
"engines": {
|
|
@@ -67,7 +67,8 @@
|
|
|
67
67
|
},
|
|
68
68
|
"homepage": "https://github.com/lc297948801/dsh-clash-subscription#readme",
|
|
69
69
|
"scripts": {
|
|
70
|
-
"test": "node test/regression-070.mjs",
|
|
70
|
+
"test": "node test/regression-0120.mjs && node test/regression-070.mjs",
|
|
71
|
+
"test:direct": "node test/regression-0120.mjs",
|
|
71
72
|
"test:legacy": "node test/regression-063.mjs",
|
|
72
73
|
"test:linux": "node test/regression-0100.mjs",
|
|
73
74
|
"test:e2e": "node test/e2e-090.mjs"
|
package/CHANGELOG-0.11.0.md
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
# 0.11.0 · 不再选中被限流的节点
|
|
2
|
-
|
|
3
|
-
> 上一版 `0.10.0` · 本版 `0.11.0`
|
|
4
|
-
|
|
5
|
-
## 症状
|
|
6
|
-
|
|
7
|
-
订阅里有能用的节点,自动切换却停在一个**被限流**的节点上。面板上看得见:
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
health.lastSwitch: from 🇺🇸 美国02 → to 🇸🇬 新加坡03
|
|
11
|
-
failed ["latency"] · degraded true
|
|
12
|
-
health.lastRound: node 新加坡03 · usable true · degraded true · limited ["github"]
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
`新加坡03` 的 GitHub 配额已经耗尽,而当时存在配额干净的节点。切换还是选中了它。
|
|
16
|
-
|
|
17
|
-
## 根因:同一个问题有两套排序,其中一套不看限流
|
|
18
|
-
|
|
19
|
-
`lib/watch.js` 的 `rankHealth()` 是排序的定义,它知道限流要排在干净节点之后。但**运行期的切换路径自己又写了一套**:
|
|
20
|
-
|
|
21
|
-
```js
|
|
22
|
-
// 0.10.0 · index.js switchAfterStrikes 里的局部排序
|
|
23
|
-
const rank = (row) => [
|
|
24
|
-
row.verdict?.usable === true ? 0 : 1, // 可用
|
|
25
|
-
typeof row.latency === 'number' ? row.latency : Infinity,
|
|
26
|
-
(row.verdict?.failed ?? []).length,
|
|
27
|
-
(row.verdict?.blocked ?? []).length,
|
|
28
|
-
]
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
它从不读 `verdict.degraded`。而 `lib/realprobe.js` 里 `usable: blocked.length === 0` **只数 blocked,不数 limited** —— 所以一个被限流的节点 `usable` 仍是 `true`,排名靠的是延迟。限流节点往往更快(配额耗尽不耽误普通请求),于是它赢了。
|
|
32
|
-
|
|
33
|
-
启动时走的是 `lib/core.js` 的 `rankHealth(rows)[0]`,排序正确;运行期走的是这套局部排序,排序错误。**同一件事两个答案,取决于走哪条路**。
|
|
34
|
-
|
|
35
|
-
用真实运行期代码取证(不是手写模拟):
|
|
36
|
-
|
|
37
|
-
| 场景 | 切换路径选中 | `rankHealth` 选中 |
|
|
38
|
-
|---|---|---|
|
|
39
|
-
| `ok` 300 ms vs `limited` 150 ms | `limited-150` ✗ | `ok-300` ✓ |
|
|
40
|
-
| `ok` 280 ms vs `limited` 150 / 160 ms | `limited-150` ✗ | `ok-280` ✓ |
|
|
41
|
-
|
|
42
|
-
## 第二处:`rankHealth` 的最后一级不是延迟
|
|
43
|
-
|
|
44
|
-
修好第一处后还有一个洞:`rankHealth` 比到最后一级用的是 `verdict.total`(探针耗时之和),而 **`total` 里根本没有 Google 延迟** —— `lib/realprobe.js` 只累加 `gating` 探针,Google 基线是 `gating: false`。
|
|
45
|
-
|
|
46
|
-
```
|
|
47
|
-
A: google 100 ms · total 2300
|
|
48
|
-
B: google 300 ms · total 970
|
|
49
|
-
→ rankHealth 选 B
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
用户要的是「延迟最低」,而 `total` 被 git fetch 的 300–2100 ms 摆动支配,测的不是延迟。
|
|
53
|
-
|
|
54
|
-
## 第三处:限流节点一次就放行
|
|
55
|
-
|
|
56
|
-
`healthRound` 的接受条件是 `if (measured.verdict.usable === true) return`。因为限流不算 `blocked`,**限流节点第一次就通过,剩下两次重测和随后的全节点重选都不会发生**。实测:重启后 `lastRound` 是 `degraded / attempt 1/3 / failed ["github"]`,其余 8 个节点状态全是 `unknown` —— 压根没测。
|
|
57
|
-
|
|
58
|
-
## 第四处:名为「暖态中位数」的取值在 2 样本时取较大者
|
|
59
|
-
|
|
60
|
-
```js
|
|
61
|
-
// 0.10.0
|
|
62
|
-
const forMedian = samples.length > 1 ? samples.slice(1) : samples
|
|
63
|
-
const sorted = [...forMedian].sort((a, b) => a - b)
|
|
64
|
-
latency = sorted[Math.floor(sorted.length / 2)]
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
3 个样本丢掉第 1 个后剩 2 个,`floor(2 / 2) = 1` 取的是**较大者**。实测 `[513, 420, 380] → 420`(真中位 380)、`[513, 1500, 120] → 1500`。系统性把节点判慢,再把延迟上限当成硬失败,更多好节点被踢出候选。
|
|
68
|
-
|
|
69
|
-
## 改了什么
|
|
70
|
-
|
|
71
|
-
### 1. 只会有一套排序
|
|
72
|
-
|
|
73
|
-
`switchAfterStrikes` 里的局部 `rank` 整段删除,改为走 `rankHealth`:
|
|
74
|
-
|
|
75
|
-
```js
|
|
76
|
-
const ranked = rankHealth(rows)
|
|
77
|
-
rows.sort((a, b) => ranked.indexOf(a) - ranked.indexOf(b))
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### 2. `rankHealth` 在限流之后、耗时之前比真实延迟
|
|
81
|
-
|
|
82
|
-
```js
|
|
83
|
-
(a, b) =>
|
|
84
|
-
good(a) - good(b) ||
|
|
85
|
-
blockedCount(a) - blockedCount(b) ||
|
|
86
|
-
degraded(a) - degraded(b) || // 配额干净优先
|
|
87
|
-
latency(a) - latency(b) || // ← 新增:Google 暖态延迟
|
|
88
|
-
score(a) - score(b) ||
|
|
89
|
-
total(a) - total(b),
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
没有延迟的节点(启动矩阵里还没测过 Google)排在有延迟的之后 —— 不能靠一个不存在的数字取胜。
|
|
93
|
-
|
|
94
|
-
### 3. 限流算「有问题」
|
|
95
|
-
|
|
96
|
-
```js
|
|
97
|
-
const clean = measured.verdict.usable === true && measured.verdict.degraded !== true
|
|
98
|
-
if (clean === true) { /* 接受,结束本轮 */ }
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
限流节点现在也会连测满 3 次。**这里故意没有 `attempt === attempts` 的逃生口**:加过一次,结果第 3 次仍限流时也走进接受分支、永不切换,正是要修的缺陷原样复活,11 条测试变红后删除。
|
|
102
|
-
|
|
103
|
-
三次都不合格时按「是限流还是硬失败」分流:限流给 `state: 'limited'` 且 `giveUp: false`(不计 strike),硬失败照旧累计。
|
|
104
|
-
|
|
105
|
-
### 4. 找不到干净节点时不说谎
|
|
106
|
-
|
|
107
|
-
当前节点只是被限流、又没有配额干净的替代时,报告**不再说「不可用」**:
|
|
108
|
-
|
|
109
|
-
```js
|
|
110
|
-
return { ...report, state: liveUsable === true ? "limited" : "no-alternative", ... }
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
那条线还在承载流量。说它不可用,是在描述用户明明正在用的连接。`health.lastSwitch.state` 相应记为 `no-clean-alternative`,与真正的 `no-alternative` 分开。
|
|
114
|
-
|
|
115
|
-
### 5. 切换候选的门槛随之收紧
|
|
116
|
-
|
|
117
|
-
```js
|
|
118
|
-
const improves = (row) =>
|
|
119
|
-
row.verdict?.usable === true && (liveUsable !== true || row.verdict?.degraded !== true)
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
当前节点硬失败时,任何可用节点都算改善;当前节点仅限流时,**只有配额干净的才算** —— 换一个同样限流的节点,答复质量一样,却要中断所有在途连接。
|
|
123
|
-
|
|
124
|
-
### 6. 真中位数
|
|
125
|
-
|
|
126
|
-
```js
|
|
127
|
-
const mid = sorted.length >> 1
|
|
128
|
-
latency = sorted.length % 2 === 1 ? sorted[mid] : Math.round((sorted[mid - 1] + sorted[mid]) / 2)
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
### 7. 摘要说清是哪种情况
|
|
132
|
-
|
|
133
|
-
`describeHealth()` 从工具分支里抽成 `Runtime` 方法,测试才断言得到它的措辞。三种历史错法在 JSDoc 里写明:
|
|
134
|
-
|
|
135
|
-
- 把正在承载流量的限流节点说成「不可用」
|
|
136
|
-
- 把一轮内连测 3 次说成「连续 3 轮失败」
|
|
137
|
-
- 打印 `undefined`(`strikes`/`strikeLimit` 从未进过 report)
|
|
138
|
-
|
|
139
|
-
## 验收
|
|
140
|
-
|
|
141
|
-
```
|
|
142
|
-
node test/regression-070.mjs → 134 passed, 0 failed ← 新增 9b / 9c / 9d / 18 四节
|
|
143
|
-
node test/regression-0100.mjs → 48 passed, 0 failed
|
|
144
|
-
node test/regression-063.mjs → 29 passed, 0 failed
|
|
145
|
-
node test/e2e-063.mjs → 10 passed, 0 failed
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
负向对照(证明这些测试抓得住回归,不是装饰):
|
|
149
|
-
|
|
150
|
-
| 还原哪一处 | 报出的失败 |
|
|
151
|
-
|---|---|
|
|
152
|
-
| 切回旧接受条件 `usable === true` 即 return | 9c 报 7 条 FAIL |
|
|
153
|
-
| 从 comparator 删掉 `latency(a) - latency(b) \|\|` | 9d 报 2 条 FAIL |
|
|
154
|
-
| `state` 还原成一律 `"no-alternative"` | 18 报 4 条 FAIL |
|
|
155
|
-
|
|
156
|
-
真机数据验证(把 `/clash-proxy/status` 的 9 个节点喂新 `rankHealth`):
|
|
157
|
-
|
|
158
|
-
```
|
|
159
|
-
新加坡01 ok/123 → 新加坡03 ok/126 → 新加坡02 ok/127 → 台湾02 ok/187
|
|
160
|
-
→ 台湾01 ok/203 → 美国02 ok/231 → 美国03 ok/253 → 马来西亚01 ok/无延迟
|
|
161
|
-
→ 美国01 limited/247
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
`ok` 全部排在 `limited` 之前,同档内按 Google 延迟升序。
|
|
165
|
-
|
|
166
|
-
## 本版附带
|
|
167
|
-
|
|
168
|
-
- 包内补上 `vendor/`(`mihomo.exe.gz` + `SHA256SUMS.json`)。源码归档树一直缺这两份,所以 `e2e-070/080/090` 长期报 `CORE_MISSING` 跑不起来。
|
|
169
|
-
- `lib/skill.js` 的自述表同步:`limited` 行改为「不主动选;只有没有 `ok` 节点时才留」,切换规则改为「连通且不限流 → Google 延迟」排序,`lastRound.state` 取值列出 `ok / limited / slow / switched / bad`。
|
|
170
|
-
|
|
171
|
-
## 需要重启才生效
|
|
172
|
-
|
|
173
|
-
宿主进程在加载插件时把代码读进内存。装完本版后**要完全退出 DSH 再打开**,新逻辑才生效。
|