Clash分流规则实践
参考
https://github.com/Kuingsmile/clash-core/blob/main/docs/premium/rule-providers.md#rule-providers
目标
实现clash配置分流规则, 以及快速修改规则.
为什么要使用clash来控制分流规则?
我们确实可以通过浏览器扩展SwitchyOmega, 在浏览器中修改当前网站的代理, 但只能实现一个代理, 无法实现不同的网站走不同的代理. 通过clash分流,可以实现以下效果:
- youtube.com 走香港代理,延迟低.
- openai.com 走英国代理,防止被屏蔽.
如何快速修改规则
在SwitchyOmega中, 可以快速修改浏览器的代理规则, 但是没有一种浏览器扩展可以与clash通信,来修改规则. 这里提供shell命令的方式来修改, 比修改config.yaml文件要方便些. 期待有人可以开发浏览器扩展与clash通信. shell命令可以与alfred等快速启动软件结合, 进而实现更方便的方式. 也可以在alfred中创建workflow来修改规则.
# 添加openai.com走ai代理
proxy -y "DOMAIN,openai.com,ai"
# 删除google.com走proxies代理
proxy -n "DOMAIN,google.com,proxies"
proxy命令的代码如下,使用方法:
- 复制以下代码, 在你的环境变量目录下新建文件名为
proxy
,并粘贴一下代码,保存. (环境变量目录如: /usr/local/bin) - terminal中执行
chmod +x proxy
- 重启terminal
#!/bin/bash
CONFIG_FILE="$HOME/.config/clash/config.yaml"
PORT=9090;
while getopts "y:n:" opt; do
case ${opt} in
y )
# Add a new rule
if [ -z "$2" ]; then
echo "Please provide a rule with the -y option."
exit 1
else
RULE=$OPTARG
sed -i "" "s/rules:/&\n - $RULE/" "$CONFIG_FILE"
echo "Added the following rule:"
echo "- $RULE"
RESPONSE=$(curl -X PUT http://127.0.0.1:9090/configs -H "Content-Type: application/json" -d '{"path": "~/.config/clash/config.yaml"}' -i)
echo $RESPONSE
fi
;;
n )
# Remove an existing rule
if [ -z "$2" ]; then
echo "Please provide a rule with the -n option."
exit 1
else
RULE=$OPTARG
sed -i "" "/$RULE/d" "$CONFIG_FILE"
echo "Removed the following rule:"
echo "- $RULE"
RESPONSE=$(curl -X PUT http://127.0.0.1:9090/configs -H "Content-Type: application/json" -d '{"path": "~/.config/clash/config.yaml"}' -i)
echo $RESPONSE
fi
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
: )
echo "Option -$OPTARG requires an argument." 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
配置分流规则
直接上配置, 配置项含义见注释. 把该配置文件复制到clash配置目录, mac的目录为~/.config/clash/config.yaml, 替换文件中sub1与sub2为你自己的订阅即可.
mixed-port: 7890
socks-port: 7891
# Linux 和 macOS 的 redir 代理端口
redir-port: 7892
# 允许局域网的连接
allow-lan: true
# 规则模式:Rule(规则) / Global(全局代理)/ Direct(全局直连)
mode: rule
# 设置日志输出级别 (默认级别:silent,即不输出任何内容,以避免因日志内容过大而导致程序内存溢出)。
# 5 个级别:silent / info / warning / error / debug。级别越高日志输出量越大,越倾向于调试,若需要请自行开启。
log-level: info
# Clash 的 RESTful API
external-controller: '0.0.0.0:9090'
# RESTful API 的口令
secret: ''
# === 你的代理订阅
proxy-providers:
sub1:
type: http
path: ./sub1.yaml
# 填写你得到的订阅地址
url: https://sub1.example.com
interval: 3600
health-check:
enable: true
url: http://www.gstatic.com/generate_204
interval: 3600
sub2:
type: http
path: ./sub2.yaml
# 填写你得到的订阅地址
url: https://sub2.example.com
interval: 3600
health-check:
enable: true
url: http://www.gstatic.com/generate_204
interval: 3600
# 配置代理组. 你可以自由组合任意代理,成为一个组. 例如下方的配置中, proxyGroup:ai 只使用了sub1作为代理, proxyGroup:copilot 使用了sub1和sub2作为代理.
proxy-groups:
# Proxies为Gloabl/规则 模式下, 可用的所有代理.
- name: Proxies
type: select
proxies:
- 自动切换
- 手动切换
- sub1
- sub2
# 下面配置的 手动切换/自动切换/sub1/sub2, 用于指明proxies中的所有代理.
- name: 手动切换
type: select
use:
- sub1
- sub2
- name: 自动切换
type: url-test
url: 'http://www.gstatic.com/generate_204'
interval: 2000
tolerance: 100
use:
- sub1
- sub2
- name: sub1
type: select
use:
- sub1
- name: sub2
type: select
use:
- sub2
- name: sub1-auto
type: url-test
interval: 2000
tolerance: 100
use:
- sub1
# 下面的 ai/copilot 用于根据rule-providers进行自动分流.
- name: ai
type: select
use:
- sub1
- name: copilot
type: select
use:
- sub2
- sub1
# 你订阅的由第三方提供的网站规则.
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 在中国大陆可直连的域名列表
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
greatfire:
type: http
behavior: domain
url: "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/greatfire.txt"
path: ./ruleset/greatfire.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
# 中国大陆 IP 地址列表
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
# ai
ai:
type: file
behavior: classical
path: ./ruleset/ai.yaml
# 分流规则. 把 proxy-groups 与 rule-provider 进行适配.
rules:
# 格式: 适配mode,适配规则,指定代理.
# 其中, 适配mode有4种. 根据配置mode不同, 适配规则的填写方式也不同, 可以是rule-provider name, 也可以是ip, 也可以是域名.
# 你的个人规则, 可以在此处添加.
- DOMAIN-SUFFIX,runwayml.com,Proxies
- DOMAIN,sudoku.liteneo.com,Proxies
- DOMAIN,clipdrop.co,Proxies
- DOMAIN,torrentmac.net,Proxies
- DOMAIN,copilot-proxy.githubusercontent.com,copilot
- DOMAIN,copilot-telemetry.githubusercontent.com,copilot
- DOMAIN-SUFFIX,mspider.cn,Proxies
- DOMAIN,sciter.com,Proxies
- DOMAIN,meilisearch.com,Proxies
- DOMAIN-SUFFIX,pencarrie.com, Proxies
- DOMAIN-SUFFIX,engage.cloudflareclient.com, Proxies
- DOMAIN,webmagic.io, Proxies
- DOMAIN-SUFFIX,gradio.live,Proxies
- DOMAIN-SUFFIX,civitai.com,Proxies
- DOMAIN,app.wombo.art,Proxies
- DOMAIN-SUFFIX,deepl.com,Proxies
- DOMAIN,bing.com,Proxies
- DOMAIN-SUFFIX,bing.com,Proxies
- DOMAIN,siskin.test.businessmatics.cn,Proxies
- DOMAIN,accounts.intuit.com,Proxies
- DOMAIN-SUFFIX,shuqian.com,DIRECT
- DOMAIN-SUFFIX,write.as,Proxies
- DOMAIN,yacd.haishan.me,DIRECT
- DOMAIN,siskin.businessmatics.io,Proxies
# 上面是自定义规则,想要的可以继续加。
# 下面的规则是通过rule-provider来确定的
- RULE-SET,applications,DIRECT
- RULE-SET,private,DIRECT
- RULE-SET,reject,REJECT
- RULE-SET,icloud,DIRECT
- RULE-SET,apple,DIRECT
# RULE-SET,google,DIRECT
- RULE-SET,ai,ai
- RULE-SET,proxy,Proxies
- RULE-SET,direct,DIRECT
- RULE-SET,lancidr,DIRECT
- RULE-SET,cncidr,DIRECT
- RULE-SET,telegramcidr,Proxies
- RULE-SET,gfw,Proxies
- GEOIP,,DIRECT
# IP属地为CN的, 走DIRECT
- GEOIP,CN,DIRECT
# 白名单模式,没有命中以上规则,则走Proxies
- MATCH,Proxies
# 黑名单模式,没有命中以上规则,则走DIRECT
#- MATCH,DIRECT