clash配置

记录 clash 的配置, 以及是如何使用的.一个文件就能搞定的东西, 就不折腾其他的方法了.

  • 为什么不用第三方订阅转换? 因为担心隐私.
  • 为什么不自建订阅转换? 因为觉得麻烦, 懒得维护.
  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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
port: 7890
socks-port: 7891
redir-port: 7892
mixed-port: 7893
# authentication:
#   - "usr1:pass1"
allow-lan: true
mode: Rule
log-level: info
ipv6: false
hosts:
external-controller: 0.0.0.0:9090
clash-for-android:
  append-system-dns: false
profile:
  tracing: true
dns:
  enable: true
  listen: 127.0.0.1:8853
  default-nameserver:
    - 223.5.5.5
    - 1.0.0.1
  ipv6: false
  enhanced-mode: fake-ip
  fake-ip-filter:
    - "*.lan"
    - stun.*.*.*
    - stun.*.*
    - time.windows.com
    - time.nist.gov
    - time.apple.com
    - time.asia.apple.com
    - "*.ntp.org.cn"
    - "*.openwrt.pool.ntp.org"
    - time1.cloud.tencent.com
    - time.ustc.edu.cn
    - pool.ntp.org
    - ntp.ubuntu.com
    - ntp.aliyun.com
    - ntp1.aliyun.com
    - ntp2.aliyun.com
    - ntp3.aliyun.com
    - ntp4.aliyun.com
    - ntp5.aliyun.com
    - ntp6.aliyun.com
    - ntp7.aliyun.com
    - time1.aliyun.com
    - time2.aliyun.com
    - time3.aliyun.com
    - time4.aliyun.com
    - time5.aliyun.com
    - time6.aliyun.com
    - time7.aliyun.com
    - "*.time.edu.cn"
    - time1.apple.com
    - time2.apple.com
    - time3.apple.com
    - time4.apple.com
    - time5.apple.com
    - time6.apple.com
    - time7.apple.com
    - time1.google.com
    - time2.google.com
    - time3.google.com
    - time4.google.com
    - music.163.com
    - "*.music.163.com"
    - "*.126.net"
    - musicapi.taihe.com
    - music.taihe.com
    - songsearch.kugou.com
    - trackercdn.kugou.com
    - "*.kuwo.cn"
    - api-jooxtt.sanook.com
    - api.joox.com
    - joox.com
    - y.qq.com
    - "*.y.qq.com"
    - streamoc.music.tc.qq.com
    - mobileoc.music.tc.qq.com
    - isure.stream.qqmusic.qq.com
    - dl.stream.qqmusic.qq.com
    - aqqmusic.tc.qq.com
    - amobile.music.tc.qq.com
    - "*.xiami.com"
    - "*.music.migu.cn"
    - music.migu.cn
    - "*.msftconnecttest.com"
    - "*.msftncsi.com"
    - localhost.ptlogin2.qq.com
    - "*.*.*.srv.nintendo.net"
    - "*.*.stun.playstation.net"
    - xbox.*.*.microsoft.com
    - "*.ipv6.microsoft.com"
    - "*.*.xboxlive.com"
    - speedtest.cros.wr.pvp.net
  nameserver:
    - https://223.6.6.6/dns-query
    - https://rubyfish.cn/dns-query
    - https://dns.pub/dns-query
  fallback:
    - https://dns.rubyfish.cn/dns-query
    - https://public.dns.iij.jp/dns-query
    - tls://8.8.4.4
  fallback-filter:
    geoip: true
    ipcidr:
      - 240.0.0.0/4
      - 0.0.0.0/32
      - 127.0.0.1/32
    domain:
      - +.google.com
      - +.facebook.com
      - +.twitter.com
      - +.youtube.com
      - +.xn--ngstr-lra8j.com
      - +.google.cn
      - +.googleapis.cn
      - +.googleapis.com
      - +.gvt1.com

# 节点信息配置
# 从你的订阅地址下载节点信息,过滤掉不包含香港的节点
proxy-providers:
  AMY-HongKong:
    type: http
    path: ./ProxySet/HongKong.yaml
    url: "你的订阅地址"
    interval: 3600
    filter: "香港"
    health-check:
      enable: true
      url: http://www.gstatic.com/generate_204
      interval: 300
  AMY-US:
    type: http
    path: ./ProxySet/US.yaml
    url: "你的订阅地址"
    interval: 3600
    filter: "美国"
    health-check:
      enable: true
      url: http://www.gstatic.com/generate_204
      interval: 300
  AMY-Taiwan:
    type: http
    path: ./ProxySet/Taiwan.yaml
    url: "你的订阅地址"
    interval: 3600
    filter: "台湾"
    health-check:
      enable: true
      url: http://www.gstatic.com/generate_204
      interval: 300
  AMY-Japan:
    type: http
    path: ./ProxySet/Japan.yaml
    url: "你的订阅地址"
    interval: 3600
    filter: "日本"
    health-check:
      enable: true
      url: http://www.gstatic.com/generate_204
      interval: 300
  AMY-Singapore:
    type: http
    path: ./ProxySet/Singapore.yaml
    url: "你的订阅地址"
    interval: 3600
    filter: "新加坡"
    health-check:
      enable: true
      url: http://www.gstatic.com/generate_204
      interval: 300

# 策略组配置
# type字段
# relay代表链式,不支持UDP. 例如流量先经过机场,到达自建节点.出口ip在自建节点,因此ip不变
# url-test测速选择
# select手动选择
# fallback请求失败了才会切换
# load-balance负载均衡,ip可能一直变.
# tolerance字段,宽容毫秒数.如果少于100ms,就不切换节点
# interval字段,5*60秒尝试切换一次
proxy-groups:
  - name: 香港-auto
    type: url-test
    url: http://www.gstatic.com/generate_204
    interval: 600
    tolerance: 100
    use:
      - AMY-HongKong
  - name: 美国-auto
    type: url-test
    url: http://www.gstatic.com/generate_204
    interval: 600
    tolerance: 150
    use:
      - AMY-US
  - name: 所有-auto
    type: select
    url: http://www.gstatic.com/generate_204
    interval: 600
    use:
      - AMY-HongKong
      - AMY-US
      - AMY-Singapore
      - AMY-Japan
      - AMY-Taiwan

# 从github拿到规则集,用的时候注意behavior,一般readme文件会有写behavior的值
# behavior的含义参考 https://github.com/Dreamacro/clash/issues/1165#issuecomment-753739205
rule-providers:
  ChinaMax:
    type: http
    behavior: classical
    url: "https://mirror.ghproxy.com/https://github.com/blackmatrix7/ios_rule_script/blob/master/rule/Clash/ChinaMax/ChinaMax_Classical.yaml"
    path: ./RuleSet/ChinaMax.yaml
    interval: 86400
  OpenAI:
    type: http
    behavior: classical
    url: "https://mirror.ghproxy.com/https://github.com/blackmatrix7/ios_rule_script/blob/master/rule/Clash/OpenAI/OpenAI.yaml"
    path: ./RuleSet/OpenAI.yaml
    interval: 86400
  Microsoft:
    type: http
    behavior: classical
    url: "https://mirror.ghproxy.com/https://github.com/blackmatrix7/ios_rule_script/blob/master/rule/Clash/Microsoft/Microsoft.yaml"
    path: ./RuleSet/Microsoft.yaml
    interval: 86400
  GitLab:
    type: http
    behavior: classical
    url: "https://mirror.ghproxy.com/https://github.com/blackmatrix7/ios_rule_script/blob/master/rule/Clash/GitLab/GitLab.yaml"
    path: ./RuleSet/GitLab.yaml
    interval: 86400
  GitHub:
    type: http
    behavior: classical
    url: "https://mirror.ghproxy.com/https://github.com/blackmatrix7/ios_rule_script/blob/master/rule/Clash/GitHub/GitHub.yaml"
    path: ./RuleSet/GitHub.yaml
    interval: 86400
  Google:
    type: http
    behavior: classical
    url: "https://mirror.ghproxy.com/https://github.com/blackmatrix7/ios_rule_script/blob/master/rule/Clash/GitHub/GitHub.yaml"
    path: ./RuleSet/Google.yaml
    interval: 86400

# 自定义规则
# 1自定义,2规则集,3国内,4兜底
rules:
  - DOMAIN-SUFFIX,at.alicdn.com,香港-auto
  - DOMAIN-SUFFIX,bet365.com,香港-auto
  - DOMAIN-SUFFIX,ip-api.com,美国-auto
  - IP-CIDR,10.0.0.0/8,DIRECT
  - IP-CIDR,172.16.0.0/12,DIRECT
  - IP-CIDR,192.168.0.0/16,DIRECT
  - RULE-SET,Google,香港-auto
  - RULE-SET,GitHub,香港-auto
  - RULE-SET,GitLab,香港-auto
  - RULE-SET,Microsoft,香港-auto
  - RULE-SET,OpenAI,美国-auto
  - RULE-SET,ChinaMax,DIRECT
  - GEOIP,CN,DIRECT
  - MATCH,所有-auto

修改订阅信息:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
proxy-providers:
  AMY-HongKong:
    type: http
    path: ./ProxySet/HongKong.yaml
    url: "你的订阅地址"
    interval: 3600
    # 你的香港节点包含"香港"两个字,就填香港.包含"HK",就填"HK"
    filter: "香港"
    health-check:
      enable: true
      url: http://www.gstatic.com/generate_204
      interval: 300

如果有自己特定的规则, 例如特定 ip, 特定网站需要走代理节点. 可以添加自定义规则:

  • DOMAIN-SUFFIX:域名后缀匹配
  • DOMAIN:域名匹配
  • DOMAIN-KEYWORD:域名关键字匹配
  • IP-CIDR:IP 段匹配
  • SRC-IP-CIDR:源 IP 段匹配
  • GEOIP:GEOIP 数据库(国家代码)匹配
  • DST-PORT:目标端口匹配
  • SRC-PORT:源端口匹配
  • PROCESS-NAME:源进程名匹配
  • RULE-SET:Rule Provider 规则匹配
  • MATCH:全匹配
  • 通过 ClashVerge=>配置=>新建, 类型 local,选择配置文件导入即可
  • 也可以使用类型 remote, 不过这里会用到我的 TestServer工具.
  • clash界面=>Profiles=>Import选择你的yml文件
  • 也可以使用类型 remote, 不过这里会用到我的 TestServer工具.

[!warning] clash 相关资源已经被删, 建议去 https://github.com/MetaCubeX/mihomo 下载替代下面的路径

linux 下的安装流程:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
mkdir clash ; cd clash
# 下载clash
wget https://github.com/Dreamacro/clash/releases/download/premium/clash-linux-amd64-2023.06.30.gz

# 配置
gunzip clash-linux-amd64-2023.06.30.gz
mv clash-linux-amd64-2023.06.30 clash
chmod +x clash

# 下载geo数据库
wget https://github.com/Loyalsoldier/geoip/releases/download/202307060123/Country.mmdb

# 贴入配置,建议加上用户名密码
vim config.yaml

Systemd 配置文件 /etc/systemd/system/clash.service

 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
[Unit]
Description=clash
# 启动区间30s内,尝试启动3次
StartLimitIntervalSec=30
StartLimitBurst=3

[Service]
# 环境变量 $MY_ENV1
# Environment=MY_ENV1=value1
# Environment="MY_ENV2=value2"
# 环境变量文件,文件内容"MY_ENV3=value3" $MY_ENV3
# EnvironmentFile=/path/to/environment/file1

WorkingDirectory=/root/clash
ExecStart=/root/clash/clash -d /root/clash
# 总是间隔30s重启,配合StartLimitIntervalSec实现无限重启
RestartSec=30s 
Restart=always
# 相关资源都发送term后,后发送kill
KillMode=mixed
# 最大文件打开数不限制
LimitNOFILE=infinity
# 子线程数量不限制
TasksMax=infinity


[Install]
WantedBy=multi-user.target
Alias=clash.service

因为找了半天, 都没发现 clash 怎么配置 https, 所以通过 nginx 套一层 ssl 来保证安全性.

下面的配置通过 tcp 17890 端口代理出去.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
stream {
    upstream clash {
        hash   $remote_addr consistent;
        server 127.0.0.1:7890;
    }

    server {
        listen 17890 ssl;
        ssl_certificate /etc/nginx/ssl/kentxxq.cer;
        ssl_certificate_key /etc/nginx/ssl/kentxxq.key;
        proxy_connect_timeout   30s;
        proxy_timeout 300s;
        proxy_pass  clash;
    }
}

使用方法

1
2
3
4
# linux
export all_proxy=https://user1:pass1@a.kentxxq.com:17890; 
# windows
set all_proxy=https://user1:pass1@a.kentxxq.com:17890; 

参考网址

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 查看当前配置
curl -X GET http://127.0.0.1:9090/configs

# 切换成全局
curl -X PATCH http://127.0.0.1:9090/configs -d '{"mode":"GLOBAL"}'

# 查看可用,一般有一个global
curl -X GET http://127.0.0.1:9090/proxies

# 查看global的配置,一般它的type是selector。而api只支持切换selector
curl -X GET http://127.0.0.1:9090/proxies/GLOBAL

# 切换到global里面的美国a节点
curl -v -X PUT 'http://127.0.0.1:9090/proxies/GLOBAL'  -H "Content-Type: application/json" --data-raw '{"name": "美国 A"}'

虽然 ClashForWindows 已经被删, 但是网上还是有一些 fork/中文包版本存在.

因此此章节内容仍然具备可操作性. 同时也可以帮助理解 UWP Loopback, bypass 等相关概念.

参考链接 绕过系统代理 | Clash for Windows

Settings => System Proxy => Bypass Domain/IPNet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
bypass:
  - localhost
  - 127.*
  - 10.*
  - 172.16.*
  - 172.17.*
  - 172.18.*
  - 172.19.*
  - 172.20.*
  - 172.21.*
  - 172.22.*
  - 172.23.*
  - 172.24.*
  - 172.25.*
  - 172.26.*
  - 172.27.*
  - 172.28.*
  - 172.29.*``
  - 172.30.*
  - 172.31.*
  - 192.168.*
  - <local>

类似于 windows 的应用商店, 邮箱等应用开启代理后会无法访问.

可以通过 UWP Loopback 跳过.

配置文件预处理 适用于不想修改配置文件, 特定于当前机器的特殊配置

值类型操作
append-rules数组数组合并至原配置 rules 数组
prepend-rules数组数组合并至原配置 rules 数组
append-proxies数组数组合并至原配置 proxies 数组
prepend-proxies数组数组合并至原配置 proxies 数组
append-proxy-groups数组数组合并至原配置 proxy-groups 数组
prepend-proxy-groups数组数组合并至原配置 proxy-groups 数组
mix-proxy-providers对象对象合并至原配置 proxy-providers
mix-rule-providers对象对象合并至原配置 rule-providers
mix-object对象对象合并至原配置最外层中
commands数组在上面操作完成后执行简单命令操作配置文件

Settings=>Profiles=>Parsers=>edit 进入

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
parsers:
  - url: https://example.com/profile.yaml
    yaml:
      prepend-rules:
        - DOMAIN,test.com,DIRECT # rules最前面增加一个规则
      append-proxies:
        - name: test # proxies最后面增加一个服务
          type: http
          server: 123.123.123.123
          port: 456

#todo/笔记 快速验证代理走的什么网络!

即使配置了合适的分流规则, 京东, bilibili, 知乎等 app 兼容性还是有问题.

  • 推荐配置应用分流. 允许服务跳过白名单/黑名单模式. 让指定应用绕过代理.
  • 在 clash 中,关闭 <为 vpn service 附加 http 代理> 对我来说效果不好

相关内容: