dsh-clash-subscription 0.2.3 → 0.4.2

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.
@@ -0,0 +1,167 @@
1
+ /**
2
+ * The core that ships inside this plugin.
3
+ *
4
+ * The core is what makes the plugin a working proxy, and the core itself can only be
5
+ * obtained from the internet. That makes it the wrong thing to download lazily: a machine
6
+ * that installs this plugin because it cannot reach the internet would then need the
7
+ * internet to become able to reach the internet. So the archive is vendored in
8
+ * `vendor/` and unpacked on first use.
9
+ *
10
+ * A vendored binary is not trusted just because it shipped here: its SHA-256 is recorded
11
+ * at build time, checked before unpacking, and the unpacked file is checked again after.
12
+ * If the recorded digest and the shipped archive ever disagree, the plugin refuses to use
13
+ * it rather than executing bytes it cannot account for.
14
+ *
15
+ * @module dsh-clash-proxy/vendored
16
+ */
17
+ import { createHash } from 'node:crypto'
18
+ import { spawn } from 'node:child_process'
19
+ import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, statSync } from 'node:fs'
20
+ import { dirname, join } from 'node:path'
21
+ import { fileURLToPath } from 'node:url'
22
+
23
+ /** This module's directory, so `vendor/` resolves regardless of the process cwd. */
24
+ const HERE = dirname(fileURLToPath(import.meta.url))
25
+
26
+ /**
27
+ * What ships in `vendor/`, per platform.
28
+ *
29
+ * The digests live in `vendor/SHA256SUMS.json`, which is generated when the plugin is
30
+ * packaged, so the shipped file and the code cannot drift apart without the manifest
31
+ * disagreeing with itself. The values here are the fallback used only when the manifest is
32
+ * absent — which means a broken package, and the checks then fail closed.
33
+ *
34
+ * Regenerating: gzip the release binary, hash both, rewrite the manifest.
35
+ *
36
+ * @type {Record<string, {archive: string, binary: string, digest: string, binaryDigest: string, size: number}>}
37
+ */
38
+ export const VENDORED = {
39
+ 'win32-x64': {
40
+ archive: 'mihomo.exe.gz',
41
+ binary: 'mihomo.exe',
42
+ // SHA-256 of vendor/mihomo.exe.gz — mihomo v1.19.34, windows-amd64-compatible build.
43
+ digest: '275a5ed94fb78ea57b49f3e7a7859fca65aed213171450b0a45b5e43c224f7f7',
44
+ // SHA-256 of the unpacked executable.
45
+ binaryDigest: '5a0231811a80b27477ebdee15684b41946d01afbd2114534e9a4b256125ca147',
46
+ size: 22332744,
47
+ },
48
+ }
49
+
50
+ /**
51
+ * The vendored entry for this machine.
52
+ *
53
+ * @param platform - `process.platform`.
54
+ * @param arch - `process.arch`.
55
+ * @returns the entry, or undefined when this build has no vendored core for the pair.
56
+ */
57
+ export function vendoredFor(platform = process.platform, arch = process.arch) {
58
+ return VENDORED[`${platform}-${arch}`]
59
+ }
60
+
61
+ /**
62
+ * Read the digest manifest that sits beside the vendored archive.
63
+ *
64
+ * The manifest is generated when the plugin is packaged, so the digests in code and in the
65
+ * shipped file cannot drift apart silently.
66
+ *
67
+ * @returns `{ archive, binary }` hex digests, or undefined when the manifest is absent.
68
+ */
69
+ export function readVendorManifest() {
70
+ try {
71
+ const text = readFileSync(join(HERE, '..', 'vendor', 'SHA256SUMS.json'), 'utf8')
72
+ const parsed = JSON.parse(text)
73
+ if (typeof parsed?.archive === 'string' && typeof parsed?.binary === 'string') return parsed
74
+ } catch {
75
+ return undefined
76
+ }
77
+ return undefined
78
+ }
79
+
80
+ /**
81
+ * Unpack one gzip archive.
82
+ *
83
+ * @param archivePath - the `.gz` file.
84
+ * @param destination - directory to unpack into.
85
+ * @returns nothing; throws on failure.
86
+ */
87
+ function gunzip(archivePath, destination) {
88
+ return new Promise((resolve, reject) => {
89
+ const child = spawn('tar', ['-xzf', archivePath, '-C', destination], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
90
+ let stderr = ''
91
+ child.stderr?.on('data', (chunk) => {
92
+ stderr += String(chunk)
93
+ })
94
+ child.once('error', reject)
95
+ child.once('exit', (code) => {
96
+ if (code === 0) resolve(undefined)
97
+ else reject(new Error(`tar exited ${code}: ${stderr.trim().slice(0, 200)}`))
98
+ })
99
+ })
100
+ }
101
+
102
+ /**
103
+ * Unpack the vendored core into `dataDir/bin`, verifying both hashes.
104
+ *
105
+ * Idempotent: an already-unpacked core whose digest matches is left alone, so this can run
106
+ * on every start without rewriting 60 MB.
107
+ *
108
+ * @param dataDir - the plugin's data directory.
109
+ * @param options - `platform`/`arch` overrides and a progress callback.
110
+ * @returns the installed path, its size and the digests used.
111
+ */
112
+ export async function installVendoredCore(dataDir, options = {}) {
113
+ const entry = vendoredFor(options.platform, options.arch)
114
+ if (entry === undefined) {
115
+ throw new Error(`no vendored core for ${options.platform ?? process.platform}-${options.arch ?? process.arch}`)
116
+ }
117
+ const report = options.onProgress ?? (() => {})
118
+ const vendorDir = join(HERE, '..', 'vendor')
119
+ const archivePath = join(vendorDir, entry.archive)
120
+ if (!existsSync(archivePath)) throw new Error(`the vendored archive is missing: ${archivePath}`)
121
+
122
+ const manifest = readVendorManifest()
123
+ const wantArchive = String(manifest?.archive ?? entry.digest)
124
+ const wantBinary = String(manifest?.binary ?? entry.binaryDigest)
125
+
126
+ const finalPath = join(dataDir, 'bin', entry.binary)
127
+ // Already unpacked and still the right bytes: nothing to do.
128
+ if (existsSync(finalPath)) {
129
+ const present = createHash('sha256').update(readFileSync(finalPath)).digest('hex')
130
+ if (present === wantBinary) {
131
+ report({ phase: 'already', path: finalPath })
132
+ return { path: finalPath, bytes: statSync(finalPath).size, digest: present, reused: true }
133
+ }
134
+ }
135
+
136
+ const archiveBytes = readFileSync(archivePath)
137
+ const archiveDigest = createHash('sha256').update(archiveBytes).digest('hex')
138
+ if (wantArchive !== '0000000000000000000000000000000000000000000000000000000000000000' && archiveDigest !== wantArchive) {
139
+ throw new Error(`the vendored archive does not match its manifest: expected ${wantArchive}, found ${archiveDigest}`)
140
+ }
141
+ report({ phase: 'unpacking', archiveDigest })
142
+
143
+ const stageDir = join(dataDir, 'bin-stage')
144
+ mkdirSync(stageDir, { recursive: true })
145
+ try {
146
+ await gunzip(archivePath, stageDir)
147
+ const unpacked = join(stageDir, entry.binary)
148
+ if (!existsSync(unpacked)) throw new Error(`the archive did not contain ${entry.binary}`)
149
+ const binaryDigest = createHash('sha256').update(readFileSync(unpacked)).digest('hex')
150
+ if (wantBinary !== '0000000000000000000000000000000000000000000000000000000000000000' && binaryDigest !== wantBinary) {
151
+ throw new Error(`the unpacked core does not match its manifest: expected ${wantBinary}, found ${binaryDigest}`)
152
+ }
153
+ mkdirSync(join(dataDir, 'bin'), { recursive: true })
154
+ if (existsSync(finalPath)) rmSync(finalPath, { force: true })
155
+ const { renameSync } = await import('node:fs')
156
+ renameSync(unpacked, finalPath)
157
+ if (process.platform !== 'win32') chmodSync(finalPath, 0o755)
158
+ report({ phase: 'installed', path: finalPath, bytes: statSync(finalPath).size })
159
+ return { path: finalPath, bytes: statSync(finalPath).size, digest: binaryDigest, archiveDigest, reused: false }
160
+ } finally {
161
+ try {
162
+ rmSync(stageDir, { recursive: true, force: true })
163
+ } catch {
164
+ // A locked file only costs disk space.
165
+ }
166
+ }
167
+ }
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Clash subscription proxy",
4
- "description": "Import a Clash/mihomo subscription, run a private core, and route only foreign outbound traffic through it."
4
+ "description": "Import one or more Clash/mihomo subscriptions, run a private core, and route only foreign outbound traffic through it."
5
5
  }
6
6
  }
package/locale/zh.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Clash 订阅代理",
4
- "description": "导入 Clash/mihomo 订阅,起一个私有内核,只把访问外网的流量走代理,国内网站保持直连。"
4
+ "description": "导入一条或多条 Clash/mihomo 订阅,起一个私有内核,只把访问外网的流量走代理,国内网站保持直连。"
5
5
  }
6
6
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-clash-subscription",
3
- "version": "0.2.3",
4
- "description": "Import a Clash/mihomo subscription and route the Harness' outbound traffic through a private core, so blocked sites work while domestic traffic stays direct.",
3
+ "version": "0.4.2",
4
+ "description": "Import one or more Clash/mihomo subscriptions and route the Harness outbound traffic through the private core bundled with this plugin, so blocked sites work while domestic traffic stays direct — no other software involved.",
5
5
  "keywords": [
6
6
  "dsh",
7
7
  "dsh-plugin",
@@ -10,7 +10,11 @@
10
10
  "mihomo",
11
11
  "proxy",
12
12
  "subscription",
13
- "split-tunnel"
13
+ "multi-subscription",
14
+ "split-tunnel",
15
+ "bundled-core",
16
+ "self-contained",
17
+ "geoip"
14
18
  ],
15
19
  "license": "MIT",
16
20
  "type": "module",
@@ -25,6 +29,7 @@
25
29
  "index.js",
26
30
  "client.js",
27
31
  "lib/*.js",
32
+ "vendor/*",
28
33
  "cordis.patch.yml",
29
34
  "locale/*.json",
30
35
  "icon.svg",
@@ -0,0 +1,7 @@
1
+ {
2
+ "archive": "275a5ed94fb78ea57b49f3e7a7859fca65aed213171450b0a45b5e43c224f7f7",
3
+ "binary": "5a0231811a80b27477ebdee15684b41946d01afbd2114534e9a4b256125ca147",
4
+ "archiveBytes": 22332744,
5
+ "binaryBytes": 61192704,
6
+ "core": "mihomo v1.19.34 windows-amd64-compatible"
7
+ }
Binary file