dsh-clash 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +114 -2
- package/README.zh.md +96 -0
- package/cordis.patch.yml +13 -0
- package/docs/DETAIL.md +103 -0
- package/icon.svg +15 -0
- package/index.js +833 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +49 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 赢容 (forkgo430)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,115 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-clash
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Let the AI operate the local Clash (mihomo) kernel — switch nodes, test latency, check which rule a domain hits, and see live connections. No more clicking through the Clash Verge panel to answer "which node am I on?".
|
|
4
|
+
|
|
5
|
+
[中文说明](./README.zh.md)
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
Eight tools over the mihomo REST API, so what you get is real state, not a guess:
|
|
10
|
+
|
|
11
|
+
- **List nodes and policy groups** — every group with its currently selected node
|
|
12
|
+
- **Switch nodes** — pick any member of any group
|
|
13
|
+
- **Test latency** — one node or a whole group, sorted fastest first
|
|
14
|
+
- **Switch mode** — `rule` (split by rules), `global` (everything via proxy), `direct` (bypass)
|
|
15
|
+
- **Inspect rules** — search ~10k routing rules, or temporarily disable one
|
|
16
|
+
- **Live connections** — destination, matched rule, node chain, bytes; drop one or all
|
|
17
|
+
|
|
18
|
+
## Setup (required)
|
|
19
|
+
|
|
20
|
+
Clash Verge ships with its API **disabled**. DSH needs it on TCP, but Clash Verge puts it on a Windows named pipe that only it can reach.
|
|
21
|
+
|
|
22
|
+
Edit `%APPDATA%\io.github.clash-verge-rev.clash-verge-rev\verge.yaml`:
|
|
23
|
+
|
|
24
|
+
```yaml
|
|
25
|
+
enable_external_controller: true
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then **fully quit Clash Verge (tray included) and reopen it**. Confirm:
|
|
29
|
+
|
|
30
|
+
```powershell
|
|
31
|
+
Test-NetConnection -ComputerName 127.0.0.1 -Port 9097
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`TcpTestSucceeded : True` means it worked.
|
|
35
|
+
|
|
36
|
+
The default secret is `set-your-secret`. If you changed it, update the plugin config to match.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
dsh plugin --profile desktop add github:forkgo430/dsh-clash
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or point **Plugins → Add plugin** at a local directory.
|
|
45
|
+
|
|
46
|
+
Restart DSH afterwards — the tool list is built at session start.
|
|
47
|
+
|
|
48
|
+
## The tools
|
|
49
|
+
|
|
50
|
+
| Tool | Purpose |
|
|
51
|
+
|---|---|
|
|
52
|
+
| `clash_status` | Version, mode, port, TUN state, global exit node, group/node counts |
|
|
53
|
+
| `clash_groups` | All policy groups with their selected node; filterable |
|
|
54
|
+
| `clash_nodes` | Members of one group, with last measured latency |
|
|
55
|
+
| `clash_select` | Switch a group to a node |
|
|
56
|
+
| `clash_delay` | Test one node or a whole group |
|
|
57
|
+
| `clash_mode` | Read or set `rule` / `global` / `direct` |
|
|
58
|
+
| `clash_rules` | Search rules, or toggle one off and back on |
|
|
59
|
+
| `clash_connections` | Live connections and traffic; close one or all |
|
|
60
|
+
|
|
61
|
+
## Typical flows
|
|
62
|
+
|
|
63
|
+
**A site won't load**
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
clash_status is the kernel alive, what mode
|
|
67
|
+
clash_delay <group> all timeouts means your subscription is dead
|
|
68
|
+
clash_connections which rule and node that domain takes
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Make a node switch take effect immediately**
|
|
72
|
+
|
|
73
|
+
Switching only affects new connections; established ones keep the old node.
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
clash_select <group> <node>
|
|
77
|
+
clash_connections closeAll=true
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Let one site bypass the proxy temporarily**
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
clash_rules filter=example.com find the rule index
|
|
84
|
+
clash_rules disableIndex=12 disable=true
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## What it deliberately does not do
|
|
88
|
+
|
|
89
|
+
- **System proxy switch** — that is a Windows registry write, outside the API. Use Clash Verge's toggle or Windows settings.
|
|
90
|
+
- **TUN mode** — needs admin rights to load the wintun adapter and a config change plus kernel restart. Not switchable over the API. Use the GUI.
|
|
91
|
+
- **Start or stop the kernel** — Clash Verge owns the process. This plugin only speaks HTTP and reports clearly when the kernel is down.
|
|
92
|
+
- **DSH's own outbound traffic** — Node's `fetch` does not read the Windows registry proxy, so flipping the system proxy does not affect DSH's own requests. Set `HTTPS_PROXY` separately if you need that.
|
|
93
|
+
|
|
94
|
+
## Volatility
|
|
95
|
+
|
|
96
|
+
| Action | After a kernel restart |
|
|
97
|
+
|---|---|
|
|
98
|
+
| Node switch | Reverts to the config default |
|
|
99
|
+
| Mode switch | Persists (with `store-selected`) |
|
|
100
|
+
| Rule disabled | Re-enabled |
|
|
101
|
+
| Dropped connections | No effect |
|
|
102
|
+
|
|
103
|
+
For persistence, change the Clash Verge config or the subscription itself.
|
|
104
|
+
|
|
105
|
+
## Errors you may see
|
|
106
|
+
|
|
107
|
+
**"Cannot reach the Clash kernel"** — Clash Verge is not running, `enable_external_controller` is still `false`, or the port is not 9097.
|
|
108
|
+
|
|
109
|
+
**"HTTP 401"** — secret mismatch. Check `secret:` at the top of `clash-verge.yaml`.
|
|
110
|
+
|
|
111
|
+
**"All latency tests timed out"** — the plugin is fine; the subscription nodes are dead. Update or replace the subscription in Clash Verge.
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT. It talks to mihomo over HTTP and bundles no GPL code.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Clash 代理控制
|
|
2
|
+
|
|
3
|
+
把本机 Clash(mihomo)内核的能力交给 AI:看节点、切节点、测延迟、切模式、查规则、看连接。
|
|
4
|
+
|
|
5
|
+
## 前置条件(必须先做)
|
|
6
|
+
|
|
7
|
+
DSH 通过 mihomo 的 REST API 工作,而这个 API 在 Clash Verge 里默认是关的。
|
|
8
|
+
|
|
9
|
+
编辑 `%APPDATA%\io.github.clash-verge-rev.clash-verge-rev\verge.yaml`,把
|
|
10
|
+
|
|
11
|
+
```yaml
|
|
12
|
+
enable_external_controller: false
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
改成
|
|
16
|
+
|
|
17
|
+
```yaml
|
|
18
|
+
enable_external_controller: true
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
然后**完全退出 Clash Verge 再重新打开**(托盘退出,只关窗口不够)。重启后 9097 端口开始监听,可以这样确认:
|
|
22
|
+
|
|
23
|
+
```powershell
|
|
24
|
+
Test-NetConnection -ComputerName 127.0.0.1 -Port 9097
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`TcpTestSucceeded : True` 就通了。
|
|
28
|
+
|
|
29
|
+
如果改过 secret,把它同步到本插件配置(默认是 Clash Verge 写的 `set-your-secret`)。
|
|
30
|
+
|
|
31
|
+
## 八个工具
|
|
32
|
+
|
|
33
|
+
| 工具 | 用途 |
|
|
34
|
+
|---|---|
|
|
35
|
+
| `clash_status` | 版本、模式、端口、TUN、全局出口、组数节点数。排查第一步 |
|
|
36
|
+
| `clash_groups` | 列出策略组及当前选中节点;可按名称过滤 |
|
|
37
|
+
| `clash_nodes` | 某个组的全部成员 + 最近延迟 |
|
|
38
|
+
| `clash_select` | 在某组内切换节点 |
|
|
39
|
+
| `clash_delay` | 测整组或单节点延迟 |
|
|
40
|
+
| `clash_mode` | 查看或切换 rule / global / direct |
|
|
41
|
+
| `clash_rules` | 查规则,或临时启用/禁用某条 |
|
|
42
|
+
| `clash_connections` | 看活动连接与流量;可断单条或全部 |
|
|
43
|
+
|
|
44
|
+
## 典型用法
|
|
45
|
+
|
|
46
|
+
**排查「某站点打不开」**
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
clash_status → 确认内核活着、当前什么模式
|
|
50
|
+
clash_delay 节点组 → 节点是否全超时(全超时 = 订阅失效)
|
|
51
|
+
clash_connections → 看那个域名走的哪条规则、哪个节点
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**换节点后让新配置立刻生效**
|
|
55
|
+
|
|
56
|
+
切换节点只影响新建连接,已建立的连接还在走旧节点。所以切完要断掉:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
clash_select 节点组 日本01
|
|
60
|
+
clash_connections closeAll=true
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**临时让某个站点直连**
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
clash_rules filter=example.com → 找到对应规则索引
|
|
67
|
+
clash_rules disableIndex=12 disable=true
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 边界
|
|
71
|
+
|
|
72
|
+
**这个插件不管的:**
|
|
73
|
+
|
|
74
|
+
- **系统代理开关** —— 那要改 Windows 注册表,不属于 API 范围。要关代理请用 Clash Verge 的开关,或者手动改系统设置。
|
|
75
|
+
- **TUN 模式** —— 需要管理员权限加载 wintun 网卡驱动,只能在配置里写死 `tun.enable` 后重启内核,API 无法动态切换。请用 Clash Verge 界面。
|
|
76
|
+
- **起停内核** —— 内核进程归 Clash Verge 管。插件只发 HTTP 请求,内核没开就报错告诉你。
|
|
77
|
+
- **DSH 自己的出站流量** —— 改系统代理对 DSH 主进程无效(Node 的 fetch 不读 Windows 注册表)。如果你想让 DSH 走代理,需要另外设 `HTTPS_PROXY` 环境变量。
|
|
78
|
+
|
|
79
|
+
**内存态 vs 持久态**
|
|
80
|
+
|
|
81
|
+
| 操作 | 重启内核后 |
|
|
82
|
+
|---|---|
|
|
83
|
+
| 切节点 | 回到配置文件默认 |
|
|
84
|
+
| 切模式 | 保持(若 `store-selected` 开启) |
|
|
85
|
+
| 禁用规则 | 恢复启用 |
|
|
86
|
+
| 断连接 | 无影响 |
|
|
87
|
+
|
|
88
|
+
想持久化,得改 Clash Verge 里的配置或订阅。
|
|
89
|
+
|
|
90
|
+
## 常见报错
|
|
91
|
+
|
|
92
|
+
**「连不上 Clash 内核」** —— Clash Verge 没运行,或 `enable_external_controller` 还是 false,或端口不是 9097。
|
|
93
|
+
|
|
94
|
+
**「HTTP 401」** —— secret 不匹配。看 `clash-verge.yaml` 顶部的 `secret:` 值,同步到插件配置。
|
|
95
|
+
|
|
96
|
+
**「测延迟全部超时」** —— 这不是插件问题,是订阅节点失效了。去 Clash Verge 里换订阅或更新节点。
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
- insert:
|
|
2
|
+
- id: clash
|
|
3
|
+
name: dsh-clash
|
|
4
|
+
config:
|
|
5
|
+
# mihomo 的 REST API 地址。Clash Verge 在 verge.yaml 里把
|
|
6
|
+
# enable_external_controller 打开后,就会在 9097 上监听(默认端口)。
|
|
7
|
+
# 换端口就改这里。
|
|
8
|
+
apiBase: http://127.0.0.1:9097
|
|
9
|
+
# mihomo 的 secret。Clash Verge 默认写的是 set-your-secret;
|
|
10
|
+
# 自己改过就同步到这里,留空表示不带鉴权头。
|
|
11
|
+
secret: set-your-secret
|
|
12
|
+
# 单次 HTTP 请求的超时。测延迟、拉连接列表都会用到。
|
|
13
|
+
timeoutMs: 10000
|
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Clash 代理控制
|
|
2
|
+
|
|
3
|
+
让 AI 直接操作本机的 Clash(mihomo)内核,不用你手动点 GUI。
|
|
4
|
+
|
|
5
|
+
## 它解决什么
|
|
6
|
+
|
|
7
|
+
以前想换个节点、看看哪个网站走了哪条规则、确认订阅是不是废了,都得自己开 Clash Verge 点面板。现在直接问 AI 就行:
|
|
8
|
+
|
|
9
|
+
- 「现在走的是哪个节点?」
|
|
10
|
+
- 「这个组的节点都测一下,哪个最快」
|
|
11
|
+
- 「切到日本」
|
|
12
|
+
- 「example.com 为什么打不开,走的是直连还是代理」
|
|
13
|
+
- 「临时让 example.com 直连」
|
|
14
|
+
|
|
15
|
+
底层是 mihomo 的 REST API,所以拿到的是真实状态,不是猜的。
|
|
16
|
+
|
|
17
|
+
## 前置条件
|
|
18
|
+
|
|
19
|
+
Clash Verge 默认把 API 关了。去
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
%APPDATA%\io.github.clash-verge-rev.clash-verge-rev\verge.yaml
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
把 `enable_external_controller: false` 改成 `true`,然后**托盘退出 Clash Verge 再重开**。
|
|
26
|
+
|
|
27
|
+
重启后这样确认:
|
|
28
|
+
|
|
29
|
+
```powershell
|
|
30
|
+
Test-NetConnection -ComputerName 127.0.0.1 -Port 9097
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
看到 `TcpTestSucceeded : True` 就成了。
|
|
34
|
+
|
|
35
|
+
secret 默认是 `set-your-secret`,改过就同步到插件配置里。
|
|
36
|
+
|
|
37
|
+
## 八个工具
|
|
38
|
+
|
|
39
|
+
| 工具 | 干什么 |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `clash_status` | 内核版本、出站模式、混合端口、TUN 状态、当前全局出口、组和节点数量 |
|
|
42
|
+
| `clash_groups` | 所有策略组及各自选中的节点,可按名称过滤 |
|
|
43
|
+
| `clash_nodes` | 某个组的全部成员,带最近一次延迟 |
|
|
44
|
+
| `clash_select` | 在组内切节点 |
|
|
45
|
+
| `clash_delay` | 测延迟,整组或单个都行 |
|
|
46
|
+
| `clash_mode` | 看/切 rule(规则分流)、global(全走代理)、direct(全直连) |
|
|
47
|
+
| `clash_rules` | 查分流规则,或临时禁用/恢复某条 |
|
|
48
|
+
| `clash_connections` | 看活动连接、命中规则、节点链、流量;可断单条或全部 |
|
|
49
|
+
|
|
50
|
+
## 三个实用场景
|
|
51
|
+
|
|
52
|
+
**排查某个站打不开**
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
clash_status 内核活着吗,什么模式
|
|
56
|
+
clash_delay 节点组 节点全超时就是订阅废了
|
|
57
|
+
clash_connections 那个域名走了哪条规则
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**切完节点要断连接**
|
|
61
|
+
|
|
62
|
+
切节点只影响新连接,老连接还在走旧节点。所以:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
clash_select 节点组 日本01
|
|
66
|
+
clash_connections closeAll=true
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**临时让某站直连**
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
clash_rules filter=example.com 找到规则索引
|
|
73
|
+
clash_rules disableIndex=12 disable=true
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 它不管什么
|
|
77
|
+
|
|
78
|
+
**系统代理开关** —— 那是 Windows 注册表的事,不在 API 范围内。要关代理还是得用 Clash Verge 的开关或者系统设置。
|
|
79
|
+
|
|
80
|
+
**TUN 模式** —— 要管理员权限加载网卡驱动,只能在配置里写死然后重启内核,API 切不了。去 GUI 点。
|
|
81
|
+
|
|
82
|
+
**起停内核** —— 内核归 Clash Verge 管。插件只发 HTTP,内核没开就直接报错告诉你。
|
|
83
|
+
|
|
84
|
+
**DSH 自己的流量** —— Node 的 fetch 不读 Windows 注册表代理,改系统代理对 DSH 主进程无效。想让 DSH 走代理得单独设 `HTTPS_PROXY`。
|
|
85
|
+
|
|
86
|
+
## 内存态提醒
|
|
87
|
+
|
|
88
|
+
| 操作 | 重启内核后 |
|
|
89
|
+
|---|---|
|
|
90
|
+
| 切节点 | 回到配置默认 |
|
|
91
|
+
| 切模式 | 保持 |
|
|
92
|
+
| 禁用规则 | 恢复启用 |
|
|
93
|
+
| 断连接 | 无影响 |
|
|
94
|
+
|
|
95
|
+
想持久化就去改 Clash Verge 的配置或订阅本身。
|
|
96
|
+
|
|
97
|
+
## 报错含义
|
|
98
|
+
|
|
99
|
+
**连不上内核** —— Clash Verge 没开,或 `enable_external_controller` 还是 false,或端口不是 9097。
|
|
100
|
+
|
|
101
|
+
**HTTP 401** —— secret 不对。看 `clash-verge.yaml` 顶部的 `secret:` 值。
|
|
102
|
+
|
|
103
|
+
**测延迟全超时** —— 插件没坏,是订阅节点死了。去 Clash Verge 换订阅。
|
package/icon.svg
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0%" stop-color="#3b82f6"/>
|
|
5
|
+
<stop offset="100%" stop-color="#8b5cf6"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect x="4" y="4" width="56" height="56" rx="14" fill="url(#g)"/>
|
|
9
|
+
<g fill="none" stroke="#ffffff" stroke-width="3" stroke-linecap="round" stroke-linejoin="round">
|
|
10
|
+
<circle cx="32" cy="32" r="13"/>
|
|
11
|
+
<ellipse cx="32" cy="32" rx="5.5" ry="13"/>
|
|
12
|
+
<path d="M19.6 26h24.8M19.6 38h24.8"/>
|
|
13
|
+
<path d="M44 14l6 6M50 14l-6 6" stroke-width="2.6"/>
|
|
14
|
+
</g>
|
|
15
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,833 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-clash —— 把 mihomo(Clash 内核)的 REST API 包装成 agent 可调用的工具。
|
|
3
|
+
*
|
|
4
|
+
* 设计边界(刻意为之):
|
|
5
|
+
* - 只发 HTTP 请求,**不**起停内核、**不**读写注册表、**不**改配置文件。
|
|
6
|
+
* 进程生命周期与系统代理开关仍归 Clash Verge 的 GUI 管,这里不越界。
|
|
7
|
+
* - 因此这里所有失败都是「请求失败」,不会把整机网络搞挂。
|
|
8
|
+
*
|
|
9
|
+
* 依赖的前置条件(由用户一次性配置):
|
|
10
|
+
* Clash Verge 的 verge.yaml 里 `enable_external_controller: true`,
|
|
11
|
+
* 内核会在 9097 上开 TCP 版 REST API。
|
|
12
|
+
*
|
|
13
|
+
* 为什么不用 `defineTool`:那个函数来自 `@deepseek-ai/dsh-tools`,是 dsh 安装
|
|
14
|
+
* 目录里的内置包,profile 里解析不到。第三方 bundle 装进 profile 后只能拿到
|
|
15
|
+
* `ctx.tools` 服务,所以这里直接产出 registry 需要的形状:
|
|
16
|
+
* `{ name, description, parameters, output: { schema, render }, execute }`。
|
|
17
|
+
* `parameters` 与 `output.schema` 都必须是**原生 JSON Schema 对象**,不是
|
|
18
|
+
* schemastery 的 spec —— registry 会用 `projectToolSchema` 直接快照它。
|
|
19
|
+
*
|
|
20
|
+
* 同理**不导出 `Config`**:那需要 `@deepseek-ai/schemastery`,而 profile 通过
|
|
21
|
+
* `link:` 装本地目录时,Node 的模块解析到不了 profile 的 node_modules
|
|
22
|
+
* (pnpm 也会警告 "The linked in dependency will not resolve the peer
|
|
23
|
+
* dependencies")。没有 `Config` 导出时 loader 跳过校验、把 row 的 config
|
|
24
|
+
* 原样传给 `apply`,这里就地做归一化与默认值,行为一致而依赖为零。
|
|
25
|
+
*
|
|
26
|
+
* @module dsh-clash
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export const name = 'clash'
|
|
30
|
+
export const inject = ['tools']
|
|
31
|
+
|
|
32
|
+
/** mihomo 合法的三种出站模式。 */
|
|
33
|
+
const MODES = ['rule', 'global', 'direct']
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 就地归一化 row 的 config。
|
|
37
|
+
*
|
|
38
|
+
* 缺省值与 `cordis.patch.yml` 里写的一致;类型不对的值一律回落到默认,
|
|
39
|
+
* 而不是让插件在第一次调用时才炸 —— 配置错误应该在激活时就表现为可用状态。
|
|
40
|
+
*/
|
|
41
|
+
function normalizeConfig(raw) {
|
|
42
|
+
const source = raw !== null && typeof raw === 'object' ? raw : {}
|
|
43
|
+
const apiBase =
|
|
44
|
+
typeof source.apiBase === 'string' && source.apiBase.trim().length > 0
|
|
45
|
+
? source.apiBase.trim().replace(/\/+$/, '')
|
|
46
|
+
: 'http://127.0.0.1:9097'
|
|
47
|
+
const secret = typeof source.secret === 'string' ? source.secret : ''
|
|
48
|
+
const rawTimeout = Number(source.timeoutMs)
|
|
49
|
+
const timeoutMs = Number.isFinite(rawTimeout) && rawTimeout > 0 ? rawTimeout : 10000
|
|
50
|
+
return { apiBase, secret, timeoutMs }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 把本模块的工具声明收敛成 registry 接受的形状。 */
|
|
54
|
+
function tool(spec) {
|
|
55
|
+
return {
|
|
56
|
+
name: spec.name,
|
|
57
|
+
description: spec.description,
|
|
58
|
+
parameters: spec.parameters ?? { type: 'object', properties: {}, additionalProperties: false },
|
|
59
|
+
output: {
|
|
60
|
+
schema: spec.outputSchema,
|
|
61
|
+
render: spec.render,
|
|
62
|
+
},
|
|
63
|
+
execute: spec.execute,
|
|
64
|
+
...(spec.presentCall === undefined ? {} : { presentCall: spec.presentCall }),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 调一次 mihomo 的 REST API。
|
|
70
|
+
*
|
|
71
|
+
* 统一的错误口径:网络不通、鉴权失败、HTTP 非 2xx、响应不是 JSON —— 四种都转成
|
|
72
|
+
* 带诊断线索的 Error,让模型能据此判断是「内核没开」「secret 不对」还是「接口没有」。
|
|
73
|
+
*
|
|
74
|
+
* @param {object} cfg - 插件配置。
|
|
75
|
+
* @param {string} path - 以 / 开头的 API 路径,含查询串。
|
|
76
|
+
* @param {object} [opts] - method 与 body。
|
|
77
|
+
* @returns {Promise<any>} 解析后的 JSON;204 无体时返回 null。
|
|
78
|
+
*/
|
|
79
|
+
async function api(cfg, path, opts = {}) {
|
|
80
|
+
const { method = 'GET', body, timeoutMs } = opts
|
|
81
|
+
const url = `${cfg.apiBase.replace(/\/+$/, '')}${path}`
|
|
82
|
+
const headers = { Accept: 'application/json' }
|
|
83
|
+
if (cfg.secret.length > 0) headers.Authorization = `Bearer ${cfg.secret}`
|
|
84
|
+
let payload
|
|
85
|
+
if (body !== undefined) {
|
|
86
|
+
headers['Content-Type'] = 'application/json'
|
|
87
|
+
payload = JSON.stringify(body)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let res
|
|
91
|
+
try {
|
|
92
|
+
res = await fetch(url, {
|
|
93
|
+
method,
|
|
94
|
+
headers,
|
|
95
|
+
...(payload === undefined ? {} : { body: payload }),
|
|
96
|
+
signal: AbortSignal.timeout(timeoutMs ?? cfg.timeoutMs),
|
|
97
|
+
})
|
|
98
|
+
} catch (error) {
|
|
99
|
+
const detail = error instanceof Error ? error.message : String(error)
|
|
100
|
+
throw new Error(
|
|
101
|
+
`连不上 Clash 内核(${cfg.apiBase}):${detail}。` +
|
|
102
|
+
'检查两件事:Clash Verge 是否在运行;verge.yaml 里 enable_external_controller 是否为 true。'
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (res.status === 401) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Clash 内核拒绝了鉴权(HTTP 401)。当前 secret 与内核不一致 —— ` +
|
|
109
|
+
'核对 clash-verge.yaml 顶部的 secret,并同步到本插件配置。'
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
if (res.status === 204) return null
|
|
113
|
+
if (!res.ok) {
|
|
114
|
+
const text = await res.text().catch(() => '')
|
|
115
|
+
throw new Error(`Clash API ${method} ${path} 返回 HTTP ${res.status}:${text.slice(0, 300)}`)
|
|
116
|
+
}
|
|
117
|
+
const text = await res.text()
|
|
118
|
+
if (text.trim().length === 0) return null
|
|
119
|
+
try {
|
|
120
|
+
return JSON.parse(text)
|
|
121
|
+
} catch {
|
|
122
|
+
throw new Error(`Clash API ${method} ${path} 的响应不是 JSON:${text.slice(0, 200)}`)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 把字节数换成易读单位,连接列表里用。 */
|
|
127
|
+
function humanBytes(n) {
|
|
128
|
+
if (typeof n !== 'number' || !Number.isFinite(n) || n <= 0) return '0 B'
|
|
129
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB']
|
|
130
|
+
let i = 0
|
|
131
|
+
let v = n
|
|
132
|
+
while (v >= 1024 && i < units.length - 1) {
|
|
133
|
+
v /= 1024
|
|
134
|
+
i++
|
|
135
|
+
}
|
|
136
|
+
return `${v.toFixed(i === 0 ? 0 : 1)} ${units[i]}`
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 从 /proxies 的全量结果里挑出策略组。
|
|
141
|
+
*
|
|
142
|
+
* 判据是「有 all 数组」—— 这是 mihomo 对 Selector/URLTest/Fallback/LoadBalance
|
|
143
|
+
* 四种组的共同标识,普通节点没有这个字段。
|
|
144
|
+
*/
|
|
145
|
+
function pickGroups(proxies) {
|
|
146
|
+
return Object.entries(proxies)
|
|
147
|
+
.filter(([, v]) => v !== null && typeof v === 'object' && Array.isArray(v.all))
|
|
148
|
+
.map(([groupName, v]) => ({
|
|
149
|
+
group: groupName,
|
|
150
|
+
type: v.type,
|
|
151
|
+
now: typeof v.now === 'string' ? v.now : null,
|
|
152
|
+
members: v.all.length,
|
|
153
|
+
}))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** 注册全部 clash_* 工具。 */
|
|
157
|
+
export function apply(ctx, rawConfig) {
|
|
158
|
+
const cfg = normalizeConfig(rawConfig)
|
|
159
|
+
|
|
160
|
+
const tools = []
|
|
161
|
+
|
|
162
|
+
tools.push(
|
|
163
|
+
tool({
|
|
164
|
+
name: 'clash_status',
|
|
165
|
+
description:
|
|
166
|
+
'查看本机 Clash 内核的整体状态:版本、出站模式、混合端口、TUN 是否开启、当前全局出口节点、策略组与节点数量。排查网络问题的第一步。',
|
|
167
|
+
parameters: { type: 'object', properties: {}, additionalProperties: false },
|
|
168
|
+
outputSchema: {
|
|
169
|
+
type: 'object',
|
|
170
|
+
additionalProperties: false,
|
|
171
|
+
properties: {
|
|
172
|
+
version: { type: 'string' },
|
|
173
|
+
mode: { type: 'string' },
|
|
174
|
+
mixedPort: { type: 'integer' },
|
|
175
|
+
tunEnabled: { type: 'boolean' },
|
|
176
|
+
globalNode: { type: 'string' },
|
|
177
|
+
groupCount: { type: 'integer' },
|
|
178
|
+
nodeCount: { type: 'integer' },
|
|
179
|
+
summary: { type: 'string' },
|
|
180
|
+
},
|
|
181
|
+
required: ['version', 'mode', 'mixedPort', 'tunEnabled', 'globalNode', 'groupCount', 'nodeCount', 'summary'],
|
|
182
|
+
},
|
|
183
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
184
|
+
async execute() {
|
|
185
|
+
const [version, conf, proxies] = await Promise.all([
|
|
186
|
+
api(cfg, '/version'),
|
|
187
|
+
api(cfg, '/configs'),
|
|
188
|
+
api(cfg, '/proxies'),
|
|
189
|
+
])
|
|
190
|
+
const groups = pickGroups(proxies.proxies)
|
|
191
|
+
const global = proxies.proxies.GLOBAL
|
|
192
|
+
const globalNode = global && typeof global.now === 'string' ? global.now : ''
|
|
193
|
+
const mixedPort = Number(conf['mixed-port'] ?? conf.port ?? 0)
|
|
194
|
+
const tunEnabled = conf.tun?.enable === true
|
|
195
|
+
|
|
196
|
+
const summary = [
|
|
197
|
+
`Clash ${version?.version ?? '(未知版本)'}`,
|
|
198
|
+
`模式 ${conf.mode}`,
|
|
199
|
+
`混合端口 ${mixedPort}`,
|
|
200
|
+
`TUN ${tunEnabled ? '已开启' : '未开启'}`,
|
|
201
|
+
`全局出口 ${globalNode || '(未选择)'}`,
|
|
202
|
+
`${groups.length} 个策略组 / ${Object.keys(proxies.proxies).length} 个条目`,
|
|
203
|
+
].join(' · ')
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
version: String(version?.version ?? ''),
|
|
207
|
+
mode: String(conf.mode ?? ''),
|
|
208
|
+
mixedPort,
|
|
209
|
+
tunEnabled,
|
|
210
|
+
globalNode,
|
|
211
|
+
groupCount: groups.length,
|
|
212
|
+
nodeCount: Object.keys(proxies.proxies).length,
|
|
213
|
+
summary,
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
presentCall: () => ({ card: 'generic', title: 'Clash 状态', kind: 'other' }),
|
|
217
|
+
})
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
tools.push(
|
|
221
|
+
tool({
|
|
222
|
+
name: 'clash_groups',
|
|
223
|
+
description:
|
|
224
|
+
'列出所有策略组(节点选择器)及其当前选中的节点。分组是 Clash 的调度单位:切换节点要针对具体分组,不同分组可以走不同出口。',
|
|
225
|
+
parameters: {
|
|
226
|
+
type: 'object',
|
|
227
|
+
additionalProperties: false,
|
|
228
|
+
properties: {
|
|
229
|
+
filter: { type: 'string', description: '只返回名称包含该子串的分组,空串表示不筛选。' },
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
outputSchema: {
|
|
233
|
+
type: 'object',
|
|
234
|
+
additionalProperties: false,
|
|
235
|
+
properties: {
|
|
236
|
+
groups: {
|
|
237
|
+
type: 'array',
|
|
238
|
+
items: {
|
|
239
|
+
type: 'object',
|
|
240
|
+
additionalProperties: false,
|
|
241
|
+
properties: {
|
|
242
|
+
group: { type: 'string' },
|
|
243
|
+
type: { type: 'string' },
|
|
244
|
+
now: { type: 'string' },
|
|
245
|
+
members: { type: 'integer' },
|
|
246
|
+
},
|
|
247
|
+
required: ['group', 'type', 'now', 'members'],
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
total: { type: 'integer' },
|
|
251
|
+
summary: { type: 'string' },
|
|
252
|
+
},
|
|
253
|
+
required: ['groups', 'total', 'summary'],
|
|
254
|
+
},
|
|
255
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
256
|
+
async execute(args) {
|
|
257
|
+
const proxies = await api(cfg, '/proxies')
|
|
258
|
+
const needle = (args.filter ?? '').trim()
|
|
259
|
+
let groups = pickGroups(proxies.proxies)
|
|
260
|
+
if (needle.length > 0) groups = groups.filter((g) => g.group.includes(needle))
|
|
261
|
+
|
|
262
|
+
const lines = groups.map(
|
|
263
|
+
(g) => `[${g.type}] ${g.group} → ${g.now || '(未选择)'} (${g.members} 个成员)`
|
|
264
|
+
)
|
|
265
|
+
return {
|
|
266
|
+
groups: groups.map((g) => ({
|
|
267
|
+
group: g.group,
|
|
268
|
+
type: g.type,
|
|
269
|
+
now: g.now ?? '',
|
|
270
|
+
members: g.members,
|
|
271
|
+
})),
|
|
272
|
+
total: groups.length,
|
|
273
|
+
summary: groups.length === 0 ? '没有匹配的策略组。' : lines.join('\n'),
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
presentCall: (args) => ({
|
|
277
|
+
card: 'generic',
|
|
278
|
+
title: 'Clash 策略组',
|
|
279
|
+
kind: 'other',
|
|
280
|
+
rawInput: args.filter ?? '',
|
|
281
|
+
}),
|
|
282
|
+
})
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
tools.push(
|
|
286
|
+
tool({
|
|
287
|
+
name: 'clash_nodes',
|
|
288
|
+
description:
|
|
289
|
+
'列出某个策略组的全部成员节点,附带最近一次测得的延迟。用于挑节点;不看分组直接列全部节点会很长,建议先 clash_groups 再查具体组。',
|
|
290
|
+
parameters: {
|
|
291
|
+
type: 'object',
|
|
292
|
+
additionalProperties: false,
|
|
293
|
+
properties: {
|
|
294
|
+
group: {
|
|
295
|
+
type: 'string',
|
|
296
|
+
description: '策略组名称,取自 clash_groups 的返回值,需完全一致(含 emoji)。',
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
required: ['group'],
|
|
300
|
+
},
|
|
301
|
+
outputSchema: {
|
|
302
|
+
type: 'object',
|
|
303
|
+
additionalProperties: false,
|
|
304
|
+
properties: {
|
|
305
|
+
group: { type: 'string' },
|
|
306
|
+
now: { type: 'string' },
|
|
307
|
+
nodes: {
|
|
308
|
+
type: 'array',
|
|
309
|
+
items: {
|
|
310
|
+
type: 'object',
|
|
311
|
+
additionalProperties: false,
|
|
312
|
+
properties: {
|
|
313
|
+
name: { type: 'string' },
|
|
314
|
+
type: { type: 'string' },
|
|
315
|
+
alive: { type: 'boolean' },
|
|
316
|
+
delayMs: { type: 'integer' },
|
|
317
|
+
selected: { type: 'boolean' },
|
|
318
|
+
},
|
|
319
|
+
required: ['name', 'type', 'alive', 'delayMs', 'selected'],
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
summary: { type: 'string' },
|
|
323
|
+
},
|
|
324
|
+
required: ['group', 'now', 'nodes', 'summary'],
|
|
325
|
+
},
|
|
326
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
327
|
+
async execute(args) {
|
|
328
|
+
const proxies = await api(cfg, '/proxies')
|
|
329
|
+
const target = proxies.proxies[args.group]
|
|
330
|
+
if (target === undefined) {
|
|
331
|
+
throw new Error(`没有名为 ${JSON.stringify(args.group)} 的策略组。先用 clash_groups 查可用名称。`)
|
|
332
|
+
}
|
|
333
|
+
const memberNames = Array.isArray(target.all) ? target.all : []
|
|
334
|
+
const now = typeof target.now === 'string' ? target.now : ''
|
|
335
|
+
|
|
336
|
+
const nodes = memberNames.map((memberName) => {
|
|
337
|
+
const member = proxies.proxies[memberName]
|
|
338
|
+
const history = Array.isArray(member?.history) ? member.history : []
|
|
339
|
+
const last = history.length > 0 ? history[history.length - 1] : undefined
|
|
340
|
+
const delay = last && typeof last.delay === 'number' ? last.delay : -1
|
|
341
|
+
return {
|
|
342
|
+
name: memberName,
|
|
343
|
+
type: String(member?.type ?? '(非节点)'),
|
|
344
|
+
alive: member?.alive !== false,
|
|
345
|
+
delayMs: delay,
|
|
346
|
+
selected: memberName === now,
|
|
347
|
+
}
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
const lines = nodes.map(
|
|
351
|
+
(n) =>
|
|
352
|
+
`${n.selected ? '✔' : ' '} ${n.name} [${n.type}] ${
|
|
353
|
+
n.delayMs < 0 ? '未测' : `${n.delayMs}ms`
|
|
354
|
+
}`
|
|
355
|
+
)
|
|
356
|
+
return {
|
|
357
|
+
group: args.group,
|
|
358
|
+
now,
|
|
359
|
+
nodes,
|
|
360
|
+
summary: `${args.group}(当前 ${now || '未选择'},共 ${nodes.length} 个成员)\n${lines.join(
|
|
361
|
+
'\n'
|
|
362
|
+
)}`,
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
presentCall: (args) => ({
|
|
366
|
+
card: 'generic',
|
|
367
|
+
title: 'Clash 节点',
|
|
368
|
+
kind: 'other',
|
|
369
|
+
rawInput: args.group,
|
|
370
|
+
}),
|
|
371
|
+
})
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
tools.push(
|
|
375
|
+
tool({
|
|
376
|
+
name: 'clash_select',
|
|
377
|
+
description:
|
|
378
|
+
'在指定策略组里切换到某个节点。切换是即时的、内存态的:内核重启或配置重载后会回到配置文件里的默认值。已建立的连接不受影响,要立刻生效请配合 clash_connections 关闭连接。',
|
|
379
|
+
parameters: {
|
|
380
|
+
type: 'object',
|
|
381
|
+
additionalProperties: false,
|
|
382
|
+
properties: {
|
|
383
|
+
group: { type: 'string', description: '策略组名称,取自 clash_groups。' },
|
|
384
|
+
node: {
|
|
385
|
+
type: 'string',
|
|
386
|
+
description: '该组内的成员名称,取自 clash_nodes,需完全一致(含 emoji)。',
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
required: ['group', 'node'],
|
|
390
|
+
},
|
|
391
|
+
outputSchema: {
|
|
392
|
+
type: 'object',
|
|
393
|
+
additionalProperties: false,
|
|
394
|
+
properties: {
|
|
395
|
+
group: { type: 'string' },
|
|
396
|
+
previous: { type: 'string' },
|
|
397
|
+
current: { type: 'string' },
|
|
398
|
+
summary: { type: 'string' },
|
|
399
|
+
},
|
|
400
|
+
required: ['group', 'previous', 'current', 'summary'],
|
|
401
|
+
},
|
|
402
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
403
|
+
async execute(args) {
|
|
404
|
+
const before = await api(cfg, '/proxies')
|
|
405
|
+
const target = before.proxies[args.group]
|
|
406
|
+
if (target === undefined) {
|
|
407
|
+
throw new Error(`没有名为 ${JSON.stringify(args.group)} 的策略组。先用 clash_groups 查可用名称。`)
|
|
408
|
+
}
|
|
409
|
+
const members = Array.isArray(target.all) ? target.all : []
|
|
410
|
+
if (!members.includes(args.node)) {
|
|
411
|
+
throw new Error(
|
|
412
|
+
`${JSON.stringify(args.node)} 不在策略组 ${JSON.stringify(args.group)} 里。` +
|
|
413
|
+
`该组成员:${members.slice(0, 12).join(' / ')}${members.length > 12 ? ' …' : ''}`
|
|
414
|
+
)
|
|
415
|
+
}
|
|
416
|
+
const previous = typeof target.now === 'string' ? target.now : ''
|
|
417
|
+
|
|
418
|
+
await api(cfg, `/proxies/${encodeURIComponent(args.group)}`, {
|
|
419
|
+
method: 'PUT',
|
|
420
|
+
body: { name: args.node },
|
|
421
|
+
})
|
|
422
|
+
|
|
423
|
+
const after = await api(cfg, '/proxies')
|
|
424
|
+
const current = after.proxies[args.group]?.now ?? args.node
|
|
425
|
+
|
|
426
|
+
return {
|
|
427
|
+
group: args.group,
|
|
428
|
+
previous,
|
|
429
|
+
current,
|
|
430
|
+
summary: `${args.group}:${previous || '(未选择)'} → ${current}`,
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
presentCall: (args) => ({
|
|
434
|
+
card: 'generic',
|
|
435
|
+
title: '切换节点',
|
|
436
|
+
kind: 'other',
|
|
437
|
+
rawInput: `${args.group} → ${args.node}`,
|
|
438
|
+
}),
|
|
439
|
+
})
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
tools.push(
|
|
443
|
+
tool({
|
|
444
|
+
name: 'clash_delay',
|
|
445
|
+
description:
|
|
446
|
+
'测延迟。给 group 就测该组全部成员(较慢),给 group + node 就测单个。返回毫秒数或超时 —— 全部超时通常说明订阅节点已失效,而不是配置写错了。',
|
|
447
|
+
parameters: {
|
|
448
|
+
type: 'object',
|
|
449
|
+
additionalProperties: false,
|
|
450
|
+
properties: {
|
|
451
|
+
group: {
|
|
452
|
+
type: 'string',
|
|
453
|
+
description: '策略组名称;测单个节点时也填它所属的组。',
|
|
454
|
+
},
|
|
455
|
+
node: { type: 'string', description: '只测这一个成员。省略则测整组。' },
|
|
456
|
+
url: {
|
|
457
|
+
type: 'string',
|
|
458
|
+
description: '测速目标地址,默认 http://www.gstatic.com/generate_204。',
|
|
459
|
+
},
|
|
460
|
+
timeoutMs: { type: 'integer', description: '单个节点的超时毫秒数,默认 5000。' },
|
|
461
|
+
},
|
|
462
|
+
required: ['group'],
|
|
463
|
+
},
|
|
464
|
+
outputSchema: {
|
|
465
|
+
type: 'object',
|
|
466
|
+
additionalProperties: false,
|
|
467
|
+
properties: {
|
|
468
|
+
results: {
|
|
469
|
+
type: 'array',
|
|
470
|
+
items: {
|
|
471
|
+
type: 'object',
|
|
472
|
+
additionalProperties: false,
|
|
473
|
+
properties: {
|
|
474
|
+
name: { type: 'string' },
|
|
475
|
+
delayMs: { type: 'integer' },
|
|
476
|
+
reachable: { type: 'boolean' },
|
|
477
|
+
},
|
|
478
|
+
required: ['name', 'delayMs', 'reachable'],
|
|
479
|
+
},
|
|
480
|
+
},
|
|
481
|
+
summary: { type: 'string' },
|
|
482
|
+
},
|
|
483
|
+
required: ['results', 'summary'],
|
|
484
|
+
},
|
|
485
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
486
|
+
async execute(args) {
|
|
487
|
+
const testUrl = (args.url ?? 'http://www.gstatic.com/generate_204').trim()
|
|
488
|
+
const perNodeTimeout = args.timeoutMs ?? 5000
|
|
489
|
+
const single = args.node !== undefined && args.node.length > 0
|
|
490
|
+
const path = single
|
|
491
|
+
? `/proxies/${encodeURIComponent(args.node)}/delay`
|
|
492
|
+
: `/group/${encodeURIComponent(args.group)}/delay`
|
|
493
|
+
const query = `?url=${encodeURIComponent(testUrl)}&timeout=${perNodeTimeout}`
|
|
494
|
+
|
|
495
|
+
let raw
|
|
496
|
+
try {
|
|
497
|
+
raw = await api(cfg, `${path}${query}`, {
|
|
498
|
+
timeoutMs: Math.max(cfg.timeoutMs, perNodeTimeout + 5000),
|
|
499
|
+
})
|
|
500
|
+
} catch (error) {
|
|
501
|
+
const detail = error instanceof Error ? error.message : String(error)
|
|
502
|
+
// 504 是内核告诉我们「这个节点在超时时间内没通」,不是接口坏了。
|
|
503
|
+
if (detail.includes('HTTP 504')) {
|
|
504
|
+
return {
|
|
505
|
+
results: [{ name: args.node ?? args.group, delayMs: -1, reachable: false }],
|
|
506
|
+
summary:
|
|
507
|
+
`${args.node ?? args.group} 测速超时(${perNodeTimeout}ms 内无响应)。\n` +
|
|
508
|
+
'这通常意味着该节点或整个订阅已失效 —— 去 Clash Verge 检查订阅,不是插件的问题。',
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
throw new Error(`测延迟失败:${detail}`)
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
let results
|
|
515
|
+
if (raw !== null && typeof raw === 'object' && typeof raw.delay === 'number') {
|
|
516
|
+
results = [{ name: args.node ?? args.group, delayMs: raw.delay, reachable: raw.delay > 0 }]
|
|
517
|
+
} else if (raw !== null && typeof raw === 'object') {
|
|
518
|
+
results = Object.entries(raw).map(([entryName, delay]) => ({
|
|
519
|
+
name: entryName,
|
|
520
|
+
delayMs: typeof delay === 'number' ? delay : -1,
|
|
521
|
+
reachable: typeof delay === 'number' && delay > 0,
|
|
522
|
+
}))
|
|
523
|
+
} else {
|
|
524
|
+
results = []
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
results.sort((a, b) => {
|
|
528
|
+
if (a.reachable !== b.reachable) return a.reachable ? -1 : 1
|
|
529
|
+
return a.delayMs - b.delayMs
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
const lines = results.map((r) => `${r.reachable ? `${r.delayMs}ms` : '超时'} ${r.name}`)
|
|
533
|
+
const okCount = results.filter((r) => r.reachable).length
|
|
534
|
+
const head =
|
|
535
|
+
results.length === 0 ? '没有拿到任何测速结果。' : `${okCount}/${results.length} 个节点可达。`
|
|
536
|
+
const tail =
|
|
537
|
+
results.length > 0 && okCount === 0
|
|
538
|
+
? '\n全部超时:通常是订阅节点已失效或上游不可用,先检查 Clash Verge 里的订阅。'
|
|
539
|
+
: ''
|
|
540
|
+
|
|
541
|
+
return { results, summary: head + '\n' + lines.join('\n') + tail }
|
|
542
|
+
},
|
|
543
|
+
presentCall: (args) => ({
|
|
544
|
+
card: 'generic',
|
|
545
|
+
title: '测延迟',
|
|
546
|
+
kind: 'other',
|
|
547
|
+
rawInput: args.node ?? args.group,
|
|
548
|
+
}),
|
|
549
|
+
})
|
|
550
|
+
)
|
|
551
|
+
|
|
552
|
+
tools.push(
|
|
553
|
+
tool({
|
|
554
|
+
name: 'clash_mode',
|
|
555
|
+
description:
|
|
556
|
+
'查看或切换出站模式。rule 按规则分流(默认,国内直连国外走节点);global 全部走代理;direct 全部直连(等于临时停用代理)。省略 mode 参数即为只查看。',
|
|
557
|
+
parameters: {
|
|
558
|
+
type: 'object',
|
|
559
|
+
additionalProperties: false,
|
|
560
|
+
properties: {
|
|
561
|
+
mode: { type: 'string', enum: MODES, description: '要切换到的模式;省略则只返回当前模式。' },
|
|
562
|
+
},
|
|
563
|
+
},
|
|
564
|
+
outputSchema: {
|
|
565
|
+
type: 'object',
|
|
566
|
+
additionalProperties: false,
|
|
567
|
+
properties: {
|
|
568
|
+
previous: { type: 'string' },
|
|
569
|
+
current: { type: 'string' },
|
|
570
|
+
changed: { type: 'boolean' },
|
|
571
|
+
summary: { type: 'string' },
|
|
572
|
+
},
|
|
573
|
+
required: ['previous', 'current', 'changed', 'summary'],
|
|
574
|
+
},
|
|
575
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
576
|
+
async execute(args) {
|
|
577
|
+
const conf = await api(cfg, '/configs')
|
|
578
|
+
const previous = String(conf.mode ?? '')
|
|
579
|
+
const wanted = args.mode
|
|
580
|
+
if (wanted === undefined || wanted.length === 0) {
|
|
581
|
+
return {
|
|
582
|
+
previous,
|
|
583
|
+
current: previous,
|
|
584
|
+
changed: false,
|
|
585
|
+
summary: `当前模式:${previous}。可选 rule / global / direct。`,
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (wanted === previous) {
|
|
589
|
+
return { previous, current: previous, changed: false, summary: `已经是 ${wanted},未做改动。` }
|
|
590
|
+
}
|
|
591
|
+
await api(cfg, '/configs', { method: 'PATCH', body: { mode: wanted } })
|
|
592
|
+
const after = await api(cfg, '/configs')
|
|
593
|
+
const current = String(after.mode ?? wanted)
|
|
594
|
+
const meaning =
|
|
595
|
+
{ rule: '按规则分流', global: '全部走代理', direct: '全部直连' }[current] ?? ''
|
|
596
|
+
return {
|
|
597
|
+
previous,
|
|
598
|
+
current,
|
|
599
|
+
changed: current !== previous,
|
|
600
|
+
summary: `模式:${previous} → ${current}(${meaning})`,
|
|
601
|
+
}
|
|
602
|
+
},
|
|
603
|
+
presentCall: (args) => ({
|
|
604
|
+
card: 'generic',
|
|
605
|
+
title: 'Clash 模式',
|
|
606
|
+
kind: 'other',
|
|
607
|
+
rawInput: args.mode ?? '(查看)',
|
|
608
|
+
}),
|
|
609
|
+
})
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
tools.push(
|
|
613
|
+
tool({
|
|
614
|
+
name: 'clash_rules',
|
|
615
|
+
description:
|
|
616
|
+
'查看分流规则,或临时启用/禁用某条规则。规则按索引从上往下匹配,第一条命中的决定出口。禁用是内存态的,内核重启后失效。规则可能有近万条,用 filter 缩小范围。',
|
|
617
|
+
parameters: {
|
|
618
|
+
type: 'object',
|
|
619
|
+
additionalProperties: false,
|
|
620
|
+
properties: {
|
|
621
|
+
filter: {
|
|
622
|
+
type: 'string',
|
|
623
|
+
description: '只返回 payload 或 proxy 包含该子串的规则,空串表示全部。',
|
|
624
|
+
},
|
|
625
|
+
limit: { type: 'integer', description: '最多返回多少条,默认 40,避免一次拉几千条规则。' },
|
|
626
|
+
disableIndex: { type: 'integer', description: '要改状态的规则索引(取自本工具的返回)。' },
|
|
627
|
+
disable: {
|
|
628
|
+
type: 'boolean',
|
|
629
|
+
description: '配合 disableIndex:true 禁用该规则,false 恢复启用。',
|
|
630
|
+
},
|
|
631
|
+
},
|
|
632
|
+
},
|
|
633
|
+
outputSchema: {
|
|
634
|
+
type: 'object',
|
|
635
|
+
additionalProperties: false,
|
|
636
|
+
properties: {
|
|
637
|
+
rules: {
|
|
638
|
+
type: 'array',
|
|
639
|
+
items: {
|
|
640
|
+
type: 'object',
|
|
641
|
+
additionalProperties: false,
|
|
642
|
+
properties: {
|
|
643
|
+
index: { type: 'integer' },
|
|
644
|
+
type: { type: 'string' },
|
|
645
|
+
payload: { type: 'string' },
|
|
646
|
+
proxy: { type: 'string' },
|
|
647
|
+
disabled: { type: 'boolean' },
|
|
648
|
+
},
|
|
649
|
+
required: ['index', 'type', 'payload', 'proxy', 'disabled'],
|
|
650
|
+
},
|
|
651
|
+
},
|
|
652
|
+
total: { type: 'integer' },
|
|
653
|
+
summary: { type: 'string' },
|
|
654
|
+
},
|
|
655
|
+
required: ['rules', 'total', 'summary'],
|
|
656
|
+
},
|
|
657
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
658
|
+
async execute(args) {
|
|
659
|
+
let toggleNote = ''
|
|
660
|
+
if (args.disableIndex !== undefined) {
|
|
661
|
+
const on = args.disable !== false
|
|
662
|
+
await api(cfg, '/rules/disable', {
|
|
663
|
+
method: 'PATCH',
|
|
664
|
+
body: { [String(args.disableIndex)]: on },
|
|
665
|
+
})
|
|
666
|
+
toggleNote = `规则 #${args.disableIndex} 已${on ? '禁用' : '恢复'}。\n`
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
const data = (await api(cfg, '/rules')) ?? {}
|
|
670
|
+
const all = Array.isArray(data.rules) ? data.rules : []
|
|
671
|
+
const needle = (args.filter ?? '').trim().toLowerCase()
|
|
672
|
+
const limit = args.limit ?? 40
|
|
673
|
+
|
|
674
|
+
let picked = all.map((r, i) => ({
|
|
675
|
+
index: typeof r.index === 'number' ? r.index : i,
|
|
676
|
+
type: String(r.type ?? ''),
|
|
677
|
+
payload: String(r.payload ?? ''),
|
|
678
|
+
proxy: String(r.proxy ?? ''),
|
|
679
|
+
disabled: r.extra?.disabled === true,
|
|
680
|
+
}))
|
|
681
|
+
if (needle.length > 0) {
|
|
682
|
+
picked = picked.filter(
|
|
683
|
+
(r) =>
|
|
684
|
+
r.payload.toLowerCase().includes(needle) || r.proxy.toLowerCase().includes(needle)
|
|
685
|
+
)
|
|
686
|
+
}
|
|
687
|
+
const shown = picked.slice(0, Math.max(1, limit))
|
|
688
|
+
const lines = shown.map(
|
|
689
|
+
(r) => `${r.disabled ? '✕' : ' '} #${r.index} ${r.type},${r.payload} → ${r.proxy}`
|
|
690
|
+
)
|
|
691
|
+
const omitted = picked.length - shown.length
|
|
692
|
+
const summary =
|
|
693
|
+
toggleNote +
|
|
694
|
+
`规则共 ${all.length} 条,匹配 ${picked.length} 条` +
|
|
695
|
+
(omitted > 0
|
|
696
|
+
? `,显示前 ${shown.length} 条(省略 ${omitted} 条,可调小 filter 或加大 limit)`
|
|
697
|
+
: '') +
|
|
698
|
+
'\n' +
|
|
699
|
+
lines.join('\n')
|
|
700
|
+
|
|
701
|
+
return { rules: shown, total: all.length, summary }
|
|
702
|
+
},
|
|
703
|
+
presentCall: (args) => ({
|
|
704
|
+
card: 'generic',
|
|
705
|
+
title: 'Clash 规则',
|
|
706
|
+
kind: 'other',
|
|
707
|
+
rawInput: args.filter ?? '',
|
|
708
|
+
}),
|
|
709
|
+
})
|
|
710
|
+
)
|
|
711
|
+
|
|
712
|
+
tools.push(
|
|
713
|
+
tool({
|
|
714
|
+
name: 'clash_connections',
|
|
715
|
+
description:
|
|
716
|
+
'查看当前活动的连接:每条的目标地址、命中规则、走的节点链、已传输字节。也可以关闭连接(全部或单条)—— 改完规则或切换节点后,旧连接不会自动重连,需要在这里断掉。',
|
|
717
|
+
parameters: {
|
|
718
|
+
type: 'object',
|
|
719
|
+
additionalProperties: false,
|
|
720
|
+
properties: {
|
|
721
|
+
limit: { type: 'integer', description: '最多返回多少条,默认 20。' },
|
|
722
|
+
closeAll: {
|
|
723
|
+
type: 'boolean',
|
|
724
|
+
description: '关闭全部连接。切换节点或改规则后想让新配置立刻生效时用。',
|
|
725
|
+
},
|
|
726
|
+
closeId: { type: 'string', description: '关闭指定连接,id 取自本工具的返回。' },
|
|
727
|
+
},
|
|
728
|
+
},
|
|
729
|
+
outputSchema: {
|
|
730
|
+
type: 'object',
|
|
731
|
+
additionalProperties: false,
|
|
732
|
+
properties: {
|
|
733
|
+
activeCount: { type: 'integer' },
|
|
734
|
+
downloadTotal: { type: 'string' },
|
|
735
|
+
uploadTotal: { type: 'string' },
|
|
736
|
+
connections: {
|
|
737
|
+
type: 'array',
|
|
738
|
+
items: {
|
|
739
|
+
type: 'object',
|
|
740
|
+
additionalProperties: false,
|
|
741
|
+
properties: {
|
|
742
|
+
id: { type: 'string' },
|
|
743
|
+
host: { type: 'string' },
|
|
744
|
+
destination: { type: 'string' },
|
|
745
|
+
rule: { type: 'string' },
|
|
746
|
+
chain: { type: 'string' },
|
|
747
|
+
download: { type: 'string' },
|
|
748
|
+
upload: { type: 'string' },
|
|
749
|
+
},
|
|
750
|
+
required: ['id', 'host', 'destination', 'rule', 'chain', 'download', 'upload'],
|
|
751
|
+
},
|
|
752
|
+
},
|
|
753
|
+
summary: { type: 'string' },
|
|
754
|
+
},
|
|
755
|
+
required: ['activeCount', 'downloadTotal', 'uploadTotal', 'connections', 'summary'],
|
|
756
|
+
},
|
|
757
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
758
|
+
async execute(args) {
|
|
759
|
+
let closeNote = ''
|
|
760
|
+
if (args.closeId !== undefined && args.closeId.length > 0) {
|
|
761
|
+
await api(cfg, `/connections/${encodeURIComponent(args.closeId)}`, { method: 'DELETE' })
|
|
762
|
+
closeNote = `已关闭连接 ${args.closeId}。\n`
|
|
763
|
+
} else if (args.closeAll === true) {
|
|
764
|
+
await api(cfg, '/connections', { method: 'DELETE' })
|
|
765
|
+
closeNote = '已关闭全部连接。\n'
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
const data = (await api(cfg, '/connections')) ?? {}
|
|
769
|
+
// 实测 mihomo v1.19.29 在没有活动连接时返回 connections: null,不是空数组。
|
|
770
|
+
const all = Array.isArray(data.connections) ? data.connections : []
|
|
771
|
+
const limit = args.limit ?? 20
|
|
772
|
+
|
|
773
|
+
const mapped = all.slice(0, Math.max(1, limit)).map((c) => {
|
|
774
|
+
const meta = c.metadata ?? {}
|
|
775
|
+
const host = String(meta.host ?? '')
|
|
776
|
+
const dest = `${meta.destinationIP ?? ''}:${meta.destinationPort ?? ''}`
|
|
777
|
+
const chain = Array.isArray(c.chains) ? c.chains.join(' ← ') : ''
|
|
778
|
+
return {
|
|
779
|
+
id: String(c.id ?? ''),
|
|
780
|
+
host: host.length > 0 ? host : dest,
|
|
781
|
+
destination: dest,
|
|
782
|
+
rule: `${c.rule ?? ''}${c.rulePayload ? `,${c.rulePayload}` : ''}`,
|
|
783
|
+
chain,
|
|
784
|
+
download: humanBytes(c.download),
|
|
785
|
+
upload: humanBytes(c.upload),
|
|
786
|
+
}
|
|
787
|
+
})
|
|
788
|
+
|
|
789
|
+
const lines = mapped.map(
|
|
790
|
+
(c) => `${c.host} [${c.rule || '-'}] ${c.chain || '-'} ↓${c.download} ↑${c.upload}`
|
|
791
|
+
)
|
|
792
|
+
const omitted = all.length - mapped.length
|
|
793
|
+
const summary =
|
|
794
|
+
closeNote +
|
|
795
|
+
`活动连接 ${all.length} 条,累计 ↓${humanBytes(data.downloadTotal)} ↑${humanBytes(
|
|
796
|
+
data.uploadTotal
|
|
797
|
+
)}` +
|
|
798
|
+
(omitted > 0 ? `(显示 ${mapped.length} 条,省略 ${omitted} 条)` : '') +
|
|
799
|
+
(all.length === 0 ? '' : '\n' + lines.join('\n'))
|
|
800
|
+
|
|
801
|
+
return {
|
|
802
|
+
activeCount: all.length,
|
|
803
|
+
downloadTotal: humanBytes(data.downloadTotal),
|
|
804
|
+
uploadTotal: humanBytes(data.uploadTotal),
|
|
805
|
+
connections: mapped,
|
|
806
|
+
summary,
|
|
807
|
+
}
|
|
808
|
+
},
|
|
809
|
+
presentCall: (args) => ({
|
|
810
|
+
card: 'generic',
|
|
811
|
+
title: args.closeAll === true ? '关闭全部连接' : 'Clash 连接',
|
|
812
|
+
kind: 'other',
|
|
813
|
+
}),
|
|
814
|
+
})
|
|
815
|
+
)
|
|
816
|
+
|
|
817
|
+
// 用 ctx.effect 登记,返回 disposer 的收集器;ctx.tools.register 自身返回 disposer。
|
|
818
|
+
const disposers = tools.map((definition) => ctx.tools.register(definition))
|
|
819
|
+
ctx.on?.('dispose', () => {
|
|
820
|
+
for (const dispose of disposers) {
|
|
821
|
+
try {
|
|
822
|
+
dispose?.()
|
|
823
|
+
} catch {
|
|
824
|
+
/* 卸载期忽略 */
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
})
|
|
828
|
+
|
|
829
|
+
ctx.logger?.info?.(
|
|
830
|
+
`[dsh-clash] 已注册 ${tools.length} 个工具,目标内核 ${cfg.apiBase}` +
|
|
831
|
+
(cfg.secret.length > 0 ? '(带 secret)' : '(无 secret)')
|
|
832
|
+
)
|
|
833
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Clash proxy control",
|
|
4
|
+
"description": "Let the AI operate the local Clash (mihomo) kernel: list nodes and policy groups, switch nodes, test latency, change rule/global/direct mode, inspect and temporarily toggle routing rules, read live connections and traffic, and drop connections so a new configuration takes effect immediately. Speaks the REST API; it neither owns the kernel process nor touches the system proxy switch.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-clash",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Let the AI operate the local Clash (mihomo) kernel: list nodes and policy groups, switch nodes, test latency, change rule/global/direct mode, inspect and temporarily toggle routing rules, read live connections and traffic. Speaks the mihomo REST API — it neither owns the kernel process nor touches the system proxy switch.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./locale/*.json": "./locale/*.json",
|
|
10
|
+
"./icon": "./icon.svg"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"cordis.patch.yml",
|
|
15
|
+
"locale/*.json",
|
|
16
|
+
"docs",
|
|
17
|
+
"icon.svg",
|
|
18
|
+
"README.md",
|
|
19
|
+
"README.zh.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22",
|
|
24
|
+
"dsh": ">=0.2.0 <0.3.0"
|
|
25
|
+
},
|
|
26
|
+
"dsh": {
|
|
27
|
+
"bundle": {
|
|
28
|
+
"patch": "./cordis.patch.yml"
|
|
29
|
+
},
|
|
30
|
+
"manifestVersion": 1
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"dsh",
|
|
34
|
+
"dsh-plugin",
|
|
35
|
+
"deepseek-harness",
|
|
36
|
+
"clash",
|
|
37
|
+
"mihomo",
|
|
38
|
+
"clash-verge",
|
|
39
|
+
"proxy",
|
|
40
|
+
"network"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"author": "赢容 (forkgo430)",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/forkgo430/dsh-clash.git"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/forkgo430/dsh-clash#readme",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/forkgo430/dsh-clash/issues"
|
|
51
|
+
}
|
|
6
52
|
}
|