@jinngoo/dsh-model-proxy 0.1.0 → 0.1.1
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 +9 -1
- package/package.json +2 -1
- package/tests/local-check.mjs +84 -0
package/README.md
CHANGED
|
@@ -17,11 +17,19 @@ DSH 的代理策略是**进程级的一件事**:启动时读一次环境变量
|
|
|
17
17
|
|
|
18
18
|
## 安装
|
|
19
19
|
|
|
20
|
+
从 npm 安装(推荐):
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
dsh plugin --profile web add @jinngoo/dsh-model-proxy
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
也可以直接装 GitHub 仓库:
|
|
27
|
+
|
|
20
28
|
```sh
|
|
21
29
|
dsh plugin --profile web add https://github.com/Jinngoo/dsh-model-proxy
|
|
22
30
|
```
|
|
23
31
|
|
|
24
|
-
或在 Plugin Manager
|
|
32
|
+
或在 Plugin Manager 里搜索包名安装。
|
|
25
33
|
|
|
26
34
|
## 配置
|
|
27
35
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jinngoo/dsh-model-proxy",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "DSH 插件:只让名单内的主机经由 HTTP 代理发出请求,其余出站请求保持原有路由。用于「国外模型走代理、国内模型直连」这类分流,带设置界面,改完即时生效。",
|
|
5
5
|
"author": "Jinngoo <jinngoo@qq.com> (https://github.com/Jinngoo)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"cordis.patch.yml",
|
|
32
32
|
"locale/*.json",
|
|
33
33
|
"icon.svg",
|
|
34
|
+
"tests",
|
|
34
35
|
"README.md",
|
|
35
36
|
"LICENSE"
|
|
36
37
|
],
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// 本地验证 Host 半:假 ctx + 真 undici。
|
|
2
|
+
// 用「假代理」判定是否真的走了代理:把 proxyUrl 指向一个必然连不上的端口,
|
|
3
|
+
// 命中白名单的主机应当失败,未命中的主机应当照常成功。
|
|
4
|
+
import { getGlobalDispatcher } from 'undici'
|
|
5
|
+
import { apply, matchesHost, normalizeProxy } from '../index.js'
|
|
6
|
+
|
|
7
|
+
const REAL_PROXY = 'http://127.0.0.1:30080'
|
|
8
|
+
const DEAD_PROXY = 'http://127.0.0.1:9'
|
|
9
|
+
const ALLOWED = 'https://api.commandcode.ai/provider/v1/models'
|
|
10
|
+
const OTHER = 'https://registry.npmmirror.com/undici/latest'
|
|
11
|
+
|
|
12
|
+
let failures = 0
|
|
13
|
+
const check = (ok, label) => {
|
|
14
|
+
console.log(`${ok ? 'PASS' : 'FAIL'} ${label}`)
|
|
15
|
+
if (!ok) failures++
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// 纯函数
|
|
19
|
+
check(matchesHost('api.commandcode.ai', ['api.commandcode.ai']), 'exact host')
|
|
20
|
+
check(matchesHost('api.commandcode.ai', ['commandcode.ai']), 'suffix host')
|
|
21
|
+
check(!matchesHost('notcommandcode.ai', ['commandcode.ai']), 'no false suffix')
|
|
22
|
+
check(matchesHost('a.b.example.com', ['.example.com']), 'leading dot')
|
|
23
|
+
check(matchesHost('x.example.com', ['*.example.com']), 'leading star-dot')
|
|
24
|
+
check(normalizeProxy(' socks5://127.0.0.1:1080 ') === undefined, 'socks rejected')
|
|
25
|
+
check(normalizeProxy('http://127.0.0.1:30080') !== undefined, 'http accepted')
|
|
26
|
+
check(normalizeProxy(' ') === undefined, 'blank rejected')
|
|
27
|
+
|
|
28
|
+
const values = { enabled: true, proxyUrl: REAL_PROXY, hosts: ['api.commandcode.ai'] }
|
|
29
|
+
const config = {
|
|
30
|
+
enabled: { get: () => values.enabled },
|
|
31
|
+
proxyUrl: { get: () => values.proxyUrl },
|
|
32
|
+
hosts: { get: () => values.hosts },
|
|
33
|
+
}
|
|
34
|
+
const disposers = []
|
|
35
|
+
const ctx = {
|
|
36
|
+
logger: { warn: (...a) => console.log(' [warn]', ...a) },
|
|
37
|
+
effect: (fn) => { disposers.push(fn()) },
|
|
38
|
+
}
|
|
39
|
+
const attempt = async (url) => {
|
|
40
|
+
try {
|
|
41
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(20000) })
|
|
42
|
+
return res.status
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return `ERR ${error.cause?.code ?? error.message}`
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 1) 真代理:两个目标都应可达
|
|
49
|
+
const before = getGlobalDispatcher()
|
|
50
|
+
apply(ctx, config)
|
|
51
|
+
check(getGlobalDispatcher() !== before, 'global dispatcher replaced')
|
|
52
|
+
const okAllowed = await attempt(ALLOWED)
|
|
53
|
+
const okOther = await attempt(OTHER)
|
|
54
|
+
console.log(` allowed → ${okAllowed} | other → ${okOther}`)
|
|
55
|
+
check(okAllowed === 200, 'allowed host reachable through proxy')
|
|
56
|
+
check(okOther === 200, 'other host reachable directly')
|
|
57
|
+
for (const dispose of disposers) dispose()
|
|
58
|
+
await new Promise(r => setTimeout(r, 300))
|
|
59
|
+
check(getGlobalDispatcher() === before, 'dispatcher restored on dispose')
|
|
60
|
+
|
|
61
|
+
// 2) 死代理:命中白名单必须失败,未命中的必须照常成功 —— 这才证明分流真的生效
|
|
62
|
+
values.proxyUrl = DEAD_PROXY
|
|
63
|
+
const disposers2 = []
|
|
64
|
+
apply({ ...ctx, effect: (fn) => { disposers2.push(fn()) } }, config)
|
|
65
|
+
const deadAllowed = await attempt(ALLOWED)
|
|
66
|
+
const deadOther = await attempt(OTHER)
|
|
67
|
+
console.log(` dead-proxy allowed → ${deadAllowed} | other → ${deadOther}`)
|
|
68
|
+
check(deadAllowed !== 200, 'allowed host is really routed through the (dead) proxy')
|
|
69
|
+
check(deadOther === 200, 'other host is NOT routed through the proxy')
|
|
70
|
+
for (const dispose of disposers2) dispose()
|
|
71
|
+
await new Promise(r => setTimeout(r, 300))
|
|
72
|
+
|
|
73
|
+
// 3) 关闭开关后,命中白名单的主机也应恢复直连
|
|
74
|
+
values.proxyUrl = DEAD_PROXY
|
|
75
|
+
values.enabled = false
|
|
76
|
+
const disposers3 = []
|
|
77
|
+
apply({ ...ctx, effect: (fn) => { disposers3.push(fn()) } }, config)
|
|
78
|
+
const offAllowed = await attempt(ALLOWED)
|
|
79
|
+
console.log(` disabled allowed → ${offAllowed}`)
|
|
80
|
+
check(offAllowed === 200, 'disabled falls back to the previous route')
|
|
81
|
+
for (const dispose of disposers3) dispose()
|
|
82
|
+
|
|
83
|
+
console.log(failures === 0 ? '\nALL PASS' : `\n${failures} FAILURE(S)`)
|
|
84
|
+
process.exit(failures === 0 ? 0 : 1)
|