Clash是一个强大的网络代理工具,其规则系统允许用户根据特定条件对流量进行管理。以下是详细的Clash规则配置教程,帮助您理解如何编写和应用规则
JSON规则文件结构
Clash的规则文件采用JSON格式,主要字段包括:
rules:流量匹配规则数组。proxies:代理规则数组。client:客户端设置。bandwidth:带宽限制。firewall:防火墙规则。dns:DNS解析规则。routing:路由规则。NAT:NAT规则。quota:流量配额。limit:速率限制。filter:过滤规则。redirect:重定向规则。strip:头字段处理。update:规则更新时间。delay:延迟设置。load:加载外部规则文件。static:静态IP和端口映射。port:端口转发。domain:域名匹配。ip:IP匹配。http:HTTP请求处理。https:HTTPS请求处理。socks:SOCKS代理。tfo:TLS流量优化。metric:规则优先级。description:规则说明。
规则编写示例
示例1:阻止访问Google
{
"rules": [
{
"match": {
"domain": "google.com"
},
"action": "block"
}
]
}
match:匹配google.com域名。action:执行block动作,阻止访问。
示例2:重定向到HTTPS
{
"rules": [
{
"match": {
"domain": "example.com",
"port": 80
},
"action": "redirect",
"redirect": {
"url": "https://example.com"
}
}
]
}
match:匹配域名example.com且端口80。action:执行redirect动作。redirect:重定向到HTTPS链接。
规则组合
您可以在一个规则文件中定义多个规则,使用逻辑或组合条件来匹配更复杂的流量。
{
"rules": [
{
"match": {
"domain": "youtube.com"
},
"action": "block"
},
{
"match": {
"domain": "google.com",
"port": 443
},
"action": "redirect",
"redirect": {
"url": "https://www.google.com"
}
}
]
}
用户组和组匹配
您可以针对不同的用户组应用不同的规则。
{
"rules": [
{
"match": {
"user": "admin",
"group": "admin_group"
},
"action": "passthrough"
},
{
"match": {
"user": "user",
"group": "default_group"
},
"action": "block",
"domain": "ads.com"
}
]
}
动态更新规则
规则文件可以动态更新,确保您始终使用最新的规则。
{
"update": {
"interval": "60m",
"enabled": true
}
}
规则优先级
通过设置metric字段控制规则执行顺序,优先级高的规则先执行。
{
"rules": [
{
"match": {
"domain": "example.com"
},
"action": "block",
"metric": 5
},
{
"match": {
"domain": "google.com"
},
"action": "redirect",
"metric": 10
}
]
}
加载外部规则文件
您可以引入外部的JSON文件,扩展规则。
{
"load": [
"filter.rules",
"proxy.rules"
]
}
防火墙规则
设置防火墙规则,限制非代理流量。
{
"firewall": [
{
"outbound": {
"port": 80,
"ip": ".../"
},
"action": "block"
}
]
}
DNS重定向
重定向DNS查询到指定服务器。
{
"dns": [
{
"match": {
"domain": "example.com"
},
"action": "rewrite",
"dns": {
"server": "127...1",
"port": 53
}
}
]
}
带宽限制
限制单个用户的带宽。
{
"quota": [
{
"ip": "192.168.1.1",
"downstream": "5M",
"upstream": "10M"
}
]
}
代理规则
设置代理规则,例如SOCKS代理。
{
"proxies": [
{
"listen": "...",
"port": 108,
"socks": true,
"auth": {
"username": "user",
"password": "pass"
}
}
]
}
端口转发
将特定端口转发到本地主机。
{
"port": [
{
"listen": "...",
"port": 808,
"forward": "127...1:80"
}
]
}
TLS流量优化(TFO)
启用TLS流量优化,提升连接速度。
{
"tfo": {
"enabled": true,
"version": "tls-1.3"
}
}
高级规则组合
结合多个字段进行复杂的流量管理。
{
"rules": [
{
"match": {
"ip": "192.168.1.1",
"port": 443,
"protocol": "tcp"
},
"action": "passthrough",
"metric": 1
},
{
"match": {
"domain": "example.com",
"port": 443,
"protocol": "tcp"
},
"action": "redirect",
"redirect": {
"url": "https://www.example.com"
},
"metric": 2
}
]
}
完整规则文件示例
以下是一个完整的Clash规则文件示例,包含多种规则:
{
"rules": [
{
"match": {
"domain": "ads.com"
},
"action": "block"
},
{
"match": {
"domain": "youtube.com"
},
"action": "redirect",
"redirect": {
"url": "https://www.youtube.com"
}
},
{
"match": {
"domain": "google.com",
"port": 443
},
"action": "redirect",
"redirect": {
"url": "https://www.google.com"
}
}
],
"proxies": [
{
"listen": "...",
"port": 108,
"socks": true
}
],
"client": {
"name": "Clash",
"version": "1.."
},
"firewall": [
{
"outbound": {
"port": 80,
"ip": ".../"
},
"action": "block"
}
],
"quota": [
{
"ip": "192.168.1.1",
"downstream": "5M",
"upstream": "10M"
}
],
如果没有特点说明,本站所有内容均由原子加速器官方网站|提供客户端版本、线路管理与节点选择功能,适配Windows、Android、iOS等设备,便于用户进行网络连接优化原创,转载请注明出处!