dsh-clash-subscription 0.2.3 → 0.4.2-probe.5
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/README.md +2 -223
- package/index.js +1 -710
- package/package.json +6 -62
- package/LICENSE +0 -21
- package/client.js +0 -786
- package/cordis.patch.yml +0 -5
- package/icon.svg +0 -14
- package/lib/core.js +0 -496
- package/lib/discovery.js +0 -94
- package/lib/install.js +0 -171
- package/lib/routes.js +0 -262
- package/lib/routing.js +0 -173
- package/lib/subscription.js +0 -138
- package/locale/en.json +0 -6
- package/locale/zh.json +0 -6
package/lib/subscription.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Subscription intake.
|
|
3
|
-
*
|
|
4
|
-
* A subscription URL is fetched exactly once here, only to answer three questions
|
|
5
|
-
* the plugin needs before it can route: is this URL reachable, does it return a
|
|
6
|
-
* Clash document or a share-link list, and how many nodes does it carry. The core
|
|
7
|
-
* then re-fetches it through its own `proxy-providers` entry, which is where the
|
|
8
|
-
* real parsing happens — including share-link formats (`ss://`, `vmess://`,
|
|
9
|
-
* `vless://`, `trojan://`, `anytls://`, `hysteria2://`) that a Clash document
|
|
10
|
-
* cannot express and no plugin-side parser should try to re-implement.
|
|
11
|
-
*
|
|
12
|
-
* @module dsh-clash-proxy/subscription
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/** Markers that identify a Clash/mihomo YAML document. */
|
|
16
|
-
const CLASH_MARKERS = ['proxy-groups:', 'proxy-providers:', 'proxies:', 'rules:', 'mixed-port:']
|
|
17
|
-
|
|
18
|
-
/** Share-link schemes a mihomo provider accepts. */
|
|
19
|
-
const SHARE_SCHEMES = ['ss://', 'ssr://', 'vmess://', 'vless://', 'trojan://', 'anytls://', 'hysteria://', 'hysteria2://', 'tuic://', 'wireguard://']
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Convert a Clash Verge import link into the raw subscription URL.
|
|
23
|
-
*
|
|
24
|
-
* Clash Verge and most one-click importers share this `clash://install-config?url=`
|
|
25
|
-
* form, so a URL copied from such a site works without editing.
|
|
26
|
-
*
|
|
27
|
-
* @param input - the URL as the user pasted it.
|
|
28
|
-
* @returns the subscription URL to fetch.
|
|
29
|
-
*/
|
|
30
|
-
export function normalizeSubscriptionUrl(input) {
|
|
31
|
-
const raw = String(input ?? '').trim()
|
|
32
|
-
if (raw === '') throw new Error('subscription URL is empty')
|
|
33
|
-
const nested = /^clash:\/\/install-config\?/i.test(raw) || /^clash:\/\/install-config\//i.test(raw)
|
|
34
|
-
if (nested) {
|
|
35
|
-
const query = raw.slice(raw.indexOf('?') + 1)
|
|
36
|
-
const url = new URLSearchParams(query).get('url')
|
|
37
|
-
if (url === null || url === '') throw new Error('import link carries no url parameter')
|
|
38
|
-
return url
|
|
39
|
-
}
|
|
40
|
-
let parsed
|
|
41
|
-
try {
|
|
42
|
-
parsed = new URL(raw)
|
|
43
|
-
} catch {
|
|
44
|
-
throw new Error(`not an absolute URL: ${raw.slice(0, 60)}`)
|
|
45
|
-
}
|
|
46
|
-
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
47
|
-
throw new Error(`unsupported subscription scheme "${parsed.protocol}"`)
|
|
48
|
-
}
|
|
49
|
-
return parsed.toString()
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Classify a subscription response body.
|
|
54
|
-
*
|
|
55
|
-
* @param text - response text.
|
|
56
|
-
* @returns the detected format and its node count where countable.
|
|
57
|
-
*/
|
|
58
|
-
export function classifyBody(text) {
|
|
59
|
-
const head = text.slice(0, 4000)
|
|
60
|
-
const clash = CLASH_MARKERS.filter((marker) => head.includes(marker) || text.includes(marker))
|
|
61
|
-
if (clash.length >= 2) return { format: 'clash', nodeCount: countClashNodes(text) }
|
|
62
|
-
const schemes = SHARE_SCHEMES.filter((scheme) => text.includes(scheme))
|
|
63
|
-
if (schemes.length > 0) return { format: 'share-links', nodeCount: countShareLinks(text), schemes }
|
|
64
|
-
if (text.trimStart().startsWith('<')) return { format: 'html', nodeCount: 0 }
|
|
65
|
-
return { format: 'unknown', nodeCount: 0 }
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Count entries in a Clash document's `proxies:` block by indentation.
|
|
70
|
-
*
|
|
71
|
-
* @param text - Clash document text.
|
|
72
|
-
* @returns the count, or 0 when the block is absent.
|
|
73
|
-
*/
|
|
74
|
-
function countClashNodes(text) {
|
|
75
|
-
const lines = text.split(/\r?\n/)
|
|
76
|
-
const start = lines.findIndex((line) => /^proxies:\s*$/.test(line))
|
|
77
|
-
if (start < 0) {
|
|
78
|
-
if (/^proxies:\s*\[\s*\]/m.test(text)) return 0
|
|
79
|
-
return 0
|
|
80
|
-
}
|
|
81
|
-
let count = 0
|
|
82
|
-
for (let index = start + 1; index < lines.length; index += 1) {
|
|
83
|
-
const line = lines[index]
|
|
84
|
-
if (/^\S/.test(line)) break
|
|
85
|
-
if (/^\s*-\s/.test(line)) count += 1
|
|
86
|
-
}
|
|
87
|
-
return count
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Count non-empty share-link lines.
|
|
92
|
-
*
|
|
93
|
-
* @param text - response text.
|
|
94
|
-
* @returns the count.
|
|
95
|
-
*/
|
|
96
|
-
function countShareLinks(text) {
|
|
97
|
-
return text
|
|
98
|
-
.split(/\r?\n/)
|
|
99
|
-
.map((line) => line.trim())
|
|
100
|
-
.filter((line) => SHARE_SCHEMES.some((scheme) => line.toLowerCase().startsWith(scheme))).length
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* The user agent a subscription endpoint expects.
|
|
105
|
-
*
|
|
106
|
-
* Subscription services gate their response on client identity: a generic agent is
|
|
107
|
-
* answered with 403 or a marketing page, while a Clash core name is answered with the
|
|
108
|
-
* node list. Measured against a live provider — `clash-verge/2.0` returns 403,
|
|
109
|
-
* `ClashforWindows` returns 421, and `clash.meta` returns the full 723 KB document.
|
|
110
|
-
* Sending the core's own identity makes the probe see exactly what the core fetches.
|
|
111
|
-
*/
|
|
112
|
-
const SUBSCRIPTION_USER_AGENT = 'clash.meta'
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Fetch a subscription once and report what it carries.
|
|
116
|
-
*
|
|
117
|
-
* @param url - normalized subscription URL.
|
|
118
|
-
* @param options - timeout and an optional proxy-free fetch implementation.
|
|
119
|
-
* @returns the probe result.
|
|
120
|
-
*/
|
|
121
|
-
export async function probeSubscription(url, options = {}) {
|
|
122
|
-
const timeoutMs = options.timeoutMs ?? 20_000
|
|
123
|
-
const request = options.fetch ?? fetch
|
|
124
|
-
const response = await request(url, {
|
|
125
|
-
redirect: 'follow',
|
|
126
|
-
headers: { 'user-agent': SUBSCRIPTION_USER_AGENT },
|
|
127
|
-
signal: AbortSignal.timeout(timeoutMs),
|
|
128
|
-
})
|
|
129
|
-
const text = await response.text()
|
|
130
|
-
const classified = classifyBody(text)
|
|
131
|
-
return {
|
|
132
|
-
status: response.status,
|
|
133
|
-
ok: response.ok,
|
|
134
|
-
bytes: Buffer.byteLength(text, 'utf8'),
|
|
135
|
-
...classified,
|
|
136
|
-
hints: response.headers.get('subscription-userinfo') ?? response.headers.get('profile-update-interval') ?? '',
|
|
137
|
-
}
|
|
138
|
-
}
|
package/locale/en.json
DELETED