dsh-clash-subscription 0.6.4 → 0.8.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.8.0.md +120 -0
- package/client.js +120 -3
- package/index.js +585 -112
- package/lib/core.js +218 -1
- package/lib/health.js +7 -58
- package/lib/realprobe.js +629 -0
- package/lib/routes.js +36 -0
- package/lib/skill.js +153 -0
- package/lib/watch.js +148 -0
- package/package.json +6 -5
- package/CHANGELOG-0.6.4.md +0 -47
package/lib/core.js
CHANGED
|
@@ -18,6 +18,8 @@ import { join } from 'node:path'
|
|
|
18
18
|
import { CN_CIDR } from './cn-cidr.js'
|
|
19
19
|
import { buildExcludeFilter } from './blocklist.js'
|
|
20
20
|
import { findCoreBinary } from './discovery.js'
|
|
21
|
+
import { judgeNodeHealth, runProbes } from './realprobe.js'
|
|
22
|
+
import { rankHealth } from './watch.js'
|
|
21
23
|
|
|
22
24
|
/**
|
|
23
25
|
* GeoIP files a core can use, and the alternatives each satisfies.
|
|
@@ -180,6 +182,7 @@ export function buildConfig(options) {
|
|
|
180
182
|
const {
|
|
181
183
|
mixedPort,
|
|
182
184
|
controllerPort,
|
|
185
|
+
probePort,
|
|
183
186
|
secret,
|
|
184
187
|
subscriptions,
|
|
185
188
|
providerPaths,
|
|
@@ -250,6 +253,22 @@ export function buildConfig(options) {
|
|
|
250
253
|
|
|
251
254
|
return {
|
|
252
255
|
'mixed-port': mixedPort,
|
|
256
|
+
/**
|
|
257
|
+
* A second, dedicated inbound port per probe round.
|
|
258
|
+
*
|
|
259
|
+
* The core picks a node per *connection*, and it picks it from a selector group — so
|
|
260
|
+
* testing a node through the live `PROXY` port would mean pointing live traffic at the
|
|
261
|
+
* node under test. Measured on this machine, doing exactly that tore down in-flight
|
|
262
|
+
* streams and made every probe result suspect, because the answer could come from the
|
|
263
|
+
* previous node's established socket.
|
|
264
|
+
*
|
|
265
|
+
* With its own listener bound to its own group, a probe is one `PUT /proxies/PROBE`
|
|
266
|
+
* followed by real requests through `probePort` while live traffic keeps its node. The
|
|
267
|
+
* listener exists for the whole process and costs one loopback socket.
|
|
268
|
+
*/
|
|
269
|
+
...(typeof probePort === 'number' && probePort > 0
|
|
270
|
+
? { listeners: [{ name: 'probe-in', type: 'mixed', listen: '127.0.0.1', port: probePort, proxy: 'PROBE' }] }
|
|
271
|
+
: {}),
|
|
253
272
|
'allow-lan': false,
|
|
254
273
|
mode: mode === 'global' ? 'global' : 'rule',
|
|
255
274
|
'log-level': 'warning',
|
|
@@ -291,6 +310,18 @@ export function buildConfig(options) {
|
|
|
291
310
|
use: names,
|
|
292
311
|
proxies: ['DIRECT'],
|
|
293
312
|
},
|
|
313
|
+
/**
|
|
314
|
+
* The group a probe moves, kept apart from `PROXY` so measuring never changes where live
|
|
315
|
+
* traffic goes. It carries the same node table and starts on `DIRECT` for the same reason:
|
|
316
|
+
* a group with no selection would route the probe itself out unproxied, and a probe that
|
|
317
|
+
* never used the node would report the machine's own connectivity as the node's.
|
|
318
|
+
*/
|
|
319
|
+
{
|
|
320
|
+
name: 'PROBE',
|
|
321
|
+
type: 'select',
|
|
322
|
+
use: names,
|
|
323
|
+
proxies: ['DIRECT'],
|
|
324
|
+
},
|
|
294
325
|
],
|
|
295
326
|
rules,
|
|
296
327
|
}
|
|
@@ -324,6 +355,8 @@ export class Core {
|
|
|
324
355
|
this.providerNames = ['sub']
|
|
325
356
|
this.mixedPort = undefined
|
|
326
357
|
this.controllerPort = undefined
|
|
358
|
+
/** Loopback port of the dedicated probe listener; see `buildConfig`. */
|
|
359
|
+
this.probePort = undefined
|
|
327
360
|
this.secret = undefined
|
|
328
361
|
this.startedAt = undefined
|
|
329
362
|
this.lastError = undefined
|
|
@@ -357,6 +390,9 @@ export class Core {
|
|
|
357
390
|
this.cnRuleSet = writeCnRuleSet(this.workDir)
|
|
358
391
|
this.mixedPort = await freePort()
|
|
359
392
|
this.controllerPort = await freePort()
|
|
393
|
+
// A distinct port, so a probe round is never confused with live traffic even when a
|
|
394
|
+
// caller forgets which one it was handed.
|
|
395
|
+
this.probePort = await freePort()
|
|
360
396
|
this.secret = `dsh-${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`
|
|
361
397
|
|
|
362
398
|
const subscriptions = Array.isArray(options.subscriptions) ? options.subscriptions : []
|
|
@@ -367,6 +403,7 @@ export class Core {
|
|
|
367
403
|
...options,
|
|
368
404
|
mixedPort: this.mixedPort,
|
|
369
405
|
controllerPort: this.controllerPort,
|
|
406
|
+
probePort: this.probePort,
|
|
370
407
|
secret: this.secret,
|
|
371
408
|
providerPaths: this.providerNames.map((name) => join(this.workDir, 'providers', `${name}.yaml`)),
|
|
372
409
|
// The domestic address list is generated from the plugin's own data, so the
|
|
@@ -473,6 +510,30 @@ export class Core {
|
|
|
473
510
|
return { name: data?.name, now: data?.now, all: Array.isArray(data?.all) ? data.all : [] }
|
|
474
511
|
}
|
|
475
512
|
|
|
513
|
+
/**
|
|
514
|
+
* The `PROBE` group's current selection.
|
|
515
|
+
*
|
|
516
|
+
* Kept as its own call so a probe round reads the group it is about to move rather than
|
|
517
|
+
* assuming the two agree: they usually do not, which is the point of having two.
|
|
518
|
+
*
|
|
519
|
+
* @returns the group snapshot.
|
|
520
|
+
*/
|
|
521
|
+
async probeGroup() {
|
|
522
|
+
const data = await this.api('/proxies/PROBE')
|
|
523
|
+
return { name: data?.name, now: data?.now, all: Array.isArray(data?.all) ? data.all : [] }
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Point the probe group at one node, leaving live traffic where it is.
|
|
528
|
+
*
|
|
529
|
+
* @param name - node name from the group's `all` list.
|
|
530
|
+
* @returns the probe group snapshot after the switch.
|
|
531
|
+
*/
|
|
532
|
+
async selectProbe(name) {
|
|
533
|
+
await this.api('/proxies/PROBE', { method: 'PUT', body: JSON.stringify({ name }) })
|
|
534
|
+
return await this.probeGroup()
|
|
535
|
+
}
|
|
536
|
+
|
|
476
537
|
/**
|
|
477
538
|
* Select a node in the `PROXY` group.
|
|
478
539
|
*
|
|
@@ -630,6 +691,99 @@ export class Core {
|
|
|
630
691
|
return results
|
|
631
692
|
}
|
|
632
693
|
|
|
694
|
+
/**
|
|
695
|
+
* Measure one probe again through the current probe-group selection, without moving it.
|
|
696
|
+
*
|
|
697
|
+
* Used for latency, which must not be judged on a cold request. Every probe opens its own
|
|
698
|
+
* connection (`Connection: close`), so a core that has just started pays DNS resolution plus
|
|
699
|
+
* TCP and TLS setup on the first request for a host: measured on this machine, the same node
|
|
700
|
+
* answered `gstatic` in **513 ms** on the first request after a fresh core start and in
|
|
701
|
+
* **82 ms** on every request after, because only the first one had to resolve the name. Real
|
|
702
|
+
* traffic reuses both the resolver cache and its connections, so the cold number is not the
|
|
703
|
+
* node's latency — and judging a node on it marked every healthy node too slow.
|
|
704
|
+
*
|
|
705
|
+
* @param spec - the probe to repeat.
|
|
706
|
+
* @param options - `timeoutMs`, `attempts`.
|
|
707
|
+
* @returns the probe result, or undefined when the listener is not up.
|
|
708
|
+
*/
|
|
709
|
+
async remeasure(spec, options = {}) {
|
|
710
|
+
if (this.probePort === undefined || spec === undefined) return undefined
|
|
711
|
+
const results = await runProbes(this.probePort, [spec], options)
|
|
712
|
+
return results[spec.id]
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* Ask one node the real question, through the dedicated probe listener.
|
|
717
|
+
*
|
|
718
|
+
* The group is moved first, because the core decides the exit per connection: switching
|
|
719
|
+
* `PROBE` and then sending the request is the only way to know which node carried it. Live
|
|
720
|
+
* traffic stays on `PROXY` throughout, which is what makes it safe to do this on a schedule.
|
|
721
|
+
*
|
|
722
|
+
* @param name - node to test.
|
|
723
|
+
* @param specs - probe definitions from `lib/realprobe.js`.
|
|
724
|
+
* @param options - `concurrency`, `timeoutMs`.
|
|
725
|
+
* @returns probe id to result, or an empty object when the listener is not up.
|
|
726
|
+
*/
|
|
727
|
+
async probeNodeReal(name, specs, options = {}) {
|
|
728
|
+
if (this.probePort === undefined) return {}
|
|
729
|
+
try {
|
|
730
|
+
await this.selectProbe(name)
|
|
731
|
+
} catch (error) {
|
|
732
|
+
this.log(`probe group could not be moved to ${name}: ${error?.message ?? error}`)
|
|
733
|
+
return {}
|
|
734
|
+
}
|
|
735
|
+
return await runProbes(this.probePort, specs, options)
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Real-request matrix over a set of nodes, cheapest-first and budgeted.
|
|
740
|
+
*
|
|
741
|
+
* Latency remains a useful ordering signal (a node answering in 180 ms is worth testing
|
|
742
|
+
* before one answering in 1 500 ms), but it never decides usability: that comes only from
|
|
743
|
+
* the status codes and quotas the probes read.
|
|
744
|
+
*
|
|
745
|
+
* @param options - `nodes`, `specs`, `gating`, `budgetMs`, `onProgress`.
|
|
746
|
+
* @returns `{ rows, best, tested, skipped }`, where `rows` is `{ name, results, verdict }`.
|
|
747
|
+
*/
|
|
748
|
+
async realMatrix(options = {}) {
|
|
749
|
+
const specs = Array.isArray(options.specs) ? options.specs : []
|
|
750
|
+
const gating = Array.isArray(options.gating) ? options.gating : specs.map((spec) => spec.id)
|
|
751
|
+
const candidates = (Array.isArray(options.nodes) ? options.nodes : []).filter((name) => name !== 'DIRECT' && name !== 'REJECT')
|
|
752
|
+
const report = options.onProgress ?? (() => {})
|
|
753
|
+
const deadline = Date.now() + (options.budgetMs ?? 60_000)
|
|
754
|
+
const rows = []
|
|
755
|
+
const skipped = []
|
|
756
|
+
|
|
757
|
+
// Order by measured latency when it is known: the fastest half of a 54-node subscription
|
|
758
|
+
// usually contains every usable node, and the budget then covers it instead of dying
|
|
759
|
+
// halfway through a slow tail.
|
|
760
|
+
const delays = options.delays instanceof Map ? options.delays : new Map()
|
|
761
|
+
const ordered = [...candidates].sort((a, b) => {
|
|
762
|
+
const da = delays.get(a)
|
|
763
|
+
const db = delays.get(b)
|
|
764
|
+
return (typeof da === 'number' ? da : Number.POSITIVE_INFINITY) - (typeof db === 'number' ? db : Number.POSITIVE_INFINITY)
|
|
765
|
+
})
|
|
766
|
+
|
|
767
|
+
let tested = 0
|
|
768
|
+
for (const name of ordered) {
|
|
769
|
+
if (Date.now() > deadline) {
|
|
770
|
+
skipped.push(name)
|
|
771
|
+
continue
|
|
772
|
+
}
|
|
773
|
+
const results = await this.probeNodeReal(name, specs, options)
|
|
774
|
+
if (Object.keys(results).length === 0) {
|
|
775
|
+
skipped.push(name)
|
|
776
|
+
continue
|
|
777
|
+
}
|
|
778
|
+
tested += 1
|
|
779
|
+
const verdict = judgeNodeHealth(results, gating)
|
|
780
|
+
rows.push({ name, results, verdict })
|
|
781
|
+
report({ phase: 'probe', node: name, verdict, tested, total: ordered.length })
|
|
782
|
+
}
|
|
783
|
+
const ranked = rankHealth(rows)
|
|
784
|
+
return { rows, best: ranked[0], tested, skipped }
|
|
785
|
+
}
|
|
786
|
+
|
|
633
787
|
/**
|
|
634
788
|
* Build the capability matrix for a set of nodes.
|
|
635
789
|
*
|
|
@@ -714,10 +868,16 @@ export class Core {
|
|
|
714
868
|
* the preference is verified rather than trusted.
|
|
715
869
|
*
|
|
716
870
|
* @param preferred - a node name to honour when usable.
|
|
717
|
-
* @param options - probes, required ids, budgets.
|
|
871
|
+
* @param options - probes, required ids, budgets; with `specs` it runs real probes.
|
|
718
872
|
* @returns the chosen node and the matrix it was chosen from.
|
|
719
873
|
*/
|
|
720
874
|
async autoSelect(preferred, options = {}) {
|
|
875
|
+
// The real-probe path is the one that answers "does this node work", so it is preferred
|
|
876
|
+
// whenever the listener exists. The latency path below stays as the fallback for a core
|
|
877
|
+
// built without listeners (an older config, or a test stub).
|
|
878
|
+
if (Array.isArray(options.specs) && options.specs.length > 0 && this.probePort !== undefined) {
|
|
879
|
+
return await this.autoSelectReal(preferred, options)
|
|
880
|
+
}
|
|
721
881
|
const group = await this.group()
|
|
722
882
|
const candidates = group.all.filter((entry) => entry !== 'DIRECT')
|
|
723
883
|
if (candidates.length === 0) return { chosen: undefined, matrix: [] }
|
|
@@ -747,6 +907,63 @@ export class Core {
|
|
|
747
907
|
return { chosen: result.chosen, matrix: result.matrix }
|
|
748
908
|
}
|
|
749
909
|
|
|
910
|
+
/**
|
|
911
|
+
* Choose a working exit node using real requests.
|
|
912
|
+
*
|
|
913
|
+
* The pin is still honoured, but only after it passes the same probes: a pinned node that is
|
|
914
|
+
* out of GitHub quota is exactly the case that made the plugin pick a broken exit for hours.
|
|
915
|
+
* Every result carries `verdict`, so the caller can show *why* a node lost instead of just
|
|
916
|
+
* that it was not chosen.
|
|
917
|
+
*
|
|
918
|
+
* @param preferred - a node name to try first.
|
|
919
|
+
* @param options - `specs`, `gating`, `budgetMs`, `delays`, `exclude`.
|
|
920
|
+
* @returns `{ chosen, rows, best, tested }`.
|
|
921
|
+
*/
|
|
922
|
+
async autoSelectReal(preferred, options = {}) {
|
|
923
|
+
const group = await this.group()
|
|
924
|
+
const exclude = new Set(Array.isArray(options.exclude) ? options.exclude : [])
|
|
925
|
+
const candidates = group.all.filter((name) => name !== 'DIRECT' && name !== 'REJECT' && !exclude.has(name))
|
|
926
|
+
if (candidates.length === 0) return { chosen: undefined, rows: [], tested: 0 }
|
|
927
|
+
const specs = options.specs
|
|
928
|
+
const gating = Array.isArray(options.gating) ? options.gating : specs.map((spec) => spec.id)
|
|
929
|
+
|
|
930
|
+
const wanted = String(preferred ?? '').trim()
|
|
931
|
+
if (wanted !== '' && candidates.includes(wanted)) {
|
|
932
|
+
const results = await this.probeNodeReal(wanted, specs, options)
|
|
933
|
+
if (Object.keys(results).length > 0) {
|
|
934
|
+
const verdict = judgeNodeHealth(results, gating)
|
|
935
|
+
if (verdict.usable) {
|
|
936
|
+
await this.select(wanted)
|
|
937
|
+
return { chosen: wanted, rows: [{ name: wanted, results, verdict }], tested: 1, pinned: true }
|
|
938
|
+
}
|
|
939
|
+
this.log(`preferred node "${wanted}" failed (${verdict.failed.join(',') || 'no-answer'}); searching for another`)
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
const matrix = await this.realMatrix({ ...options, nodes: candidates, specs, gating })
|
|
944
|
+
const best = matrix.best
|
|
945
|
+
if (best === undefined || best.verdict?.usable !== true) {
|
|
946
|
+
this.log('no node passed the real probes; leaving the group on its current selection')
|
|
947
|
+
return { chosen: undefined, rows: matrix.rows, best, tested: matrix.tested }
|
|
948
|
+
}
|
|
949
|
+
await this.select(best.name)
|
|
950
|
+
return { chosen: best.name, rows: matrix.rows, best, tested: matrix.tested }
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* Check one node with real probes without touching the live selection.
|
|
955
|
+
*
|
|
956
|
+
* @param name - node to check.
|
|
957
|
+
* @param specs - probe definitions.
|
|
958
|
+
* @param gating - ids that must pass.
|
|
959
|
+
* @param options - `timeoutMs`, `concurrency`.
|
|
960
|
+
* @returns `{ results, verdict }`.
|
|
961
|
+
*/
|
|
962
|
+
async checkNodeReal(name, specs, gating, options = {}) {
|
|
963
|
+
const results = await this.probeNodeReal(name, specs, options)
|
|
964
|
+
return { results, verdict: judgeNodeHealth(results, gating) }
|
|
965
|
+
}
|
|
966
|
+
|
|
750
967
|
/**
|
|
751
968
|
* Every provider's fetch state: whether its subscription downloaded and when.
|
|
752
969
|
*
|
package/lib/health.js
CHANGED
|
@@ -1,63 +1,11 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Per-node health as one row the panel can render: delay plus usability.
|
|
3
|
-
*
|
|
4
|
-
* The panel used to show latency in the node list and usability in a separate capability
|
|
5
|
-
* block, which meant two lists of the same nodes and a reader had to join them by name. A
|
|
6
|
-
* single row carrying both is the whole point: "120 ms, usable" or "260 ms, OpenAI blocked"
|
|
7
|
-
* answers the question someone actually has when picking an exit.
|
|
8
|
-
*
|
|
9
|
-
* Usability is a three-state, not a boolean:
|
|
10
|
-
*
|
|
11
|
-
* - `unknown` — never probed, or probed before the current required set changed;
|
|
12
|
-
* - `ok` — every required service answered;
|
|
13
|
-
* - `blocked` — at least one did not.
|
|
14
|
-
*
|
|
15
|
-
* @module dsh-clash-proxy/health
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Build one row per node, joining the latency map with the capability matrix.
|
|
20
|
-
*
|
|
21
|
-
* @param names - node names the core currently carries.
|
|
22
|
-
* @param delays - node name to delay in ms, or null when it did not answer.
|
|
23
|
-
* @param capability - `{ required, nodes }` from the last selection, or undefined.
|
|
24
|
-
* @returns one row per node, in the order given.
|
|
25
|
-
*/
|
|
26
|
-
export function buildNodeRows(names, delays, capability) {
|
|
27
|
-
const list = Array.isArray(names) ? names : []
|
|
28
|
-
const delayMap = delays !== null && typeof delays === 'object' ? delays : {}
|
|
29
|
-
const required = Array.isArray(capability?.required) ? capability.required : []
|
|
30
|
-
const results = new Map()
|
|
31
|
-
for (const row of Array.isArray(capability?.nodes) ? capability.nodes : []) {
|
|
32
|
-
if (row !== null && typeof row === 'object' && typeof row.name === 'string') results.set(row.name, row)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return list.map((name) => {
|
|
36
|
-
const delay = Object.hasOwn(delayMap, name) ? delayMap[name] : undefined
|
|
37
|
-
const measured = results.get(name)
|
|
38
|
-
let usable = 'unknown'
|
|
39
|
-
let failed = []
|
|
40
|
-
if (measured !== undefined && required.length > 0) {
|
|
41
|
-
failed = required.filter((id) => typeof measured.results?.[id] !== 'number')
|
|
42
|
-
usable = failed.length === 0 ? 'ok' : 'blocked'
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
name,
|
|
46
|
-
/** ms, null for "did not answer", undefined for "not measured yet". */
|
|
47
|
-
delay: delay === undefined ? undefined : delay,
|
|
48
|
-
usable,
|
|
49
|
-
failed,
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
1
|
/**
|
|
55
2
|
* Summarise a row set for the panel header.
|
|
56
3
|
*
|
|
57
|
-
* `
|
|
58
|
-
*
|
|
4
|
+
* The rows now come from real probes, so `limited` is a state of its own: a node whose tunnel
|
|
5
|
+
* works but whose GitHub quota is spent is neither usable nor dead, and counting it as either
|
|
6
|
+
* is what made the panel disagree with reality.
|
|
59
7
|
*
|
|
60
|
-
* @param rows -
|
|
8
|
+
* @param rows - rows from `Runtime#buildRealRows`.
|
|
61
9
|
* @returns counts by state and the total.
|
|
62
10
|
*/
|
|
63
11
|
export function summariseRows(rows) {
|
|
@@ -65,6 +13,7 @@ export function summariseRows(rows) {
|
|
|
65
13
|
return {
|
|
66
14
|
total: list.length,
|
|
67
15
|
ok: list.filter((row) => row.usable === 'ok').length,
|
|
16
|
+
limited: list.filter((row) => row.usable === 'limited').length,
|
|
68
17
|
blocked: list.filter((row) => row.usable === 'blocked').length,
|
|
69
18
|
unknown: list.filter((row) => row.usable === 'unknown').length,
|
|
70
19
|
measured: list.filter((row) => typeof row.delay === 'number').length,
|
|
@@ -75,8 +24,8 @@ export function summariseRows(rows) {
|
|
|
75
24
|
* A short label for one row's usability, for the panel to translate.
|
|
76
25
|
*
|
|
77
26
|
* @param row - one output row.
|
|
78
|
-
* @returns `ok`, `blocked` or `unknown`.
|
|
27
|
+
* @returns `ok`, `limited`, `blocked` or `unknown`.
|
|
79
28
|
*/
|
|
80
29
|
export function usabilityLabel(row) {
|
|
81
|
-
return
|
|
30
|
+
return ['ok', 'limited', 'blocked'].includes(row?.usable) ? row.usable : 'unknown'
|
|
82
31
|
}
|