Clash 的重写规则,一键切换黑白名单

为什么要折腾 Clash 的自定义规则

Clash 的订阅,很多机场都有自己的规则集。
一旦更新订阅,规则就会被覆盖掉。有时候自己想添加其他规则甚至弃用机场规则非常麻烦。

一个方式是使用 mixin,但是它会加载在现有规则集后面。
索性直接用 clash 提供的 parser 规则好了。

我这里使用的是 Loyalsoldier/clash-rules 的规则

如何完全弃用预定规则,使用更好的规则集

  1. 首先在 Clash 中” 配置” 找到自己的订阅
  2. 右键,点击” 配置文件预处理”
  3. 点击” 编辑解析器”

重点来了

在弹出的地方填入以下内容,然后确定即可。

这样,Clash 的规则界面就有黑白名单的选项了。
虽然丧失了根据不同服务选择不同节点,但对我来说清爽了非常多。

PS: 配置方式参考 Iridescent-me,感谢他的分享。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
parsers:
- reg: ^.*$
code: |
module.exports.parse = (raw, { yaml }) => {
const rawObj = yaml.parse(raw)
const groups = []
const rules = []
return yaml.stringify({ ...rawObj, 'proxy-groups': groups, rules })
}
yaml:
prepend-proxy-groups: # 建立策略组
- name: 🔯 代理模式
type: select
proxies:
- 绕过大陆丨黑名单(GFWlist) # 黑名单模式,意为「只有命中规则的网络流量,才使用代理」
- 绕过大陆丨白名单(Whitelist) # 白名单模式,意为「没有命中规则的网络流量,统统使用代理」

- name: 🔰 选择节点
type: select

- name: 🛑 广告拦截
type: select
proxies:
- DIRECT
- REJECT
- PROXY

- name: 绕过大陆丨黑名单(GFWlist)
type: url-test
url: http://www.gstatic.com/generate_204
interval: 86400
proxies:
- DIRECT

- name: 绕过大陆丨白名单(Whitelist)
type: url-test
url: http://www.gstatic.com/generate_204
interval: 86400
proxies:
- PROXY

- name: PROXY
type: url-test
url: http://www.gstatic.com/generate_204
interval: 86400
proxies:
- 🔰 选择节点
commands:
- proxy-groups.🔰 选择节点.proxies=[]proxyNames # 向指定策略组添加订阅中的节点名,可使用正则过滤
- proxy-groups.🔰 选择节点.proxies.0+DIRECT # 向指定分组第一个位置添加一个 DIRECT 节点名
prepend-rules:
- RULE-SET,applications,DIRECT
- DOMAIN,clash.razord.top,DIRECT
- DOMAIN,yacd.haishan.me,DIRECT
- RULE-SET,private,DIRECT
- RULE-SET,reject,REJECT
- RULE-SET,icloud,DIRECT
- RULE-SET,apple,DIRECT
- RULE-SET,google,DIRECT
- RULE-SET,proxy,PROXY
- RULE-SET,direct,DIRECT
- RULE-SET,lancidr,DIRECT
- RULE-SET,cncidr,DIRECT
- RULE-SET,telegramcidr,PROXY
- GEOIP,LAN,DIRECT
- GEOIP,CN,DIRECT
- MATCH,PROXY
mix-rule-providers:
reject:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/reject.txt"
path: ./ruleset/reject.yaml
interval: 86400

icloud:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/icloud.txt"
path: ./ruleset/icloud.yaml
interval: 86400

apple:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/apple.txt"
path: ./ruleset/apple.yaml
interval: 86400

google:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/google.txt"
path: ./ruleset/google.yaml
interval: 86400

proxy:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/proxy.txt"
path: ./ruleset/proxy.yaml
interval: 86400

direct:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/direct.txt"
path: ./ruleset/direct.yaml
interval: 86400

private:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/private.txt"
path: ./ruleset/private.yaml
interval: 86400

gfw:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/gfw.txt"
path: ./ruleset/gfw.yaml
interval: 86400

tld-not-cn:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/tld-not-cn.txt"
path: ./ruleset/tld-not-cn.yaml
interval: 86400

telegramcidr:
type: http
behavior: ipcidr
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/telegramcidr.txt"
path: ./ruleset/telegramcidr.yaml
interval: 86400

cncidr:
type: http
behavior: ipcidr
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/cncidr.txt"
path: ./ruleset/cncidr.yaml
interval: 86400

lancidr:
type: http
behavior: ipcidr
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/lancidr.txt"
path: ./ruleset/lancidr.yaml
interval: 86400

applications:
type: http
behavior: classical
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/applications.txt"
path: ./ruleset/applications.yaml
interval: 86400

Clash 的重写规则,一键切换黑白名单

https://robinxb.com/posts/2023/clash-rules-parser/

作者

薯条

发布于

2023-08-03

更新于

2024-01-15

许可协议

评论