目录

Clash 是一个非常强大的网络代理工具,支持通过配置文件来定义复杂的代理规则。以下是 Clash代理规则的基本语法和常见应用示例

Clash 代理规则语法 Clash 的规则文件采用 JSON 格式,规则由 match 和 replace 两个部分组成: match: 用于匹配符合条件的请求。 replace: 用于指定替代规则。 规则可以通过多个 match 和 replace 组合成一个完整的代理规则。 基本规则结构 { "rules": [ { "match": { "ip": ["192.168.1.1"], "port": [80, 443], "domain": "example.com" }, "replace": { "host": "proxy.example.com", "port": 808, "next": [ { "host": "target.example.com", "port": 80 } ] } } ] } 规则字段说明 match: ip: 匹配特定 IP 地址。 port: 匹配特定端口。 domain: 匹配特定域名。 path: 匹配特定路径。 scheme: 匹配特定协议(如 http, https)。 query: 匹配特定查询参数。 header: 匹配特定 HTTP 头。 replace: host: 指定代理的目标主机。 port: 指定代理的目标端口。 next: 指定后续的代理规则(支持链式代理)。 remove-header: 去掉特定 HTTP 头。 add-header: 添加特定 HTTP 头。 body: 修改或添加请求体。...

Clash 代理规则语法

Clash 的规则文件采用 JSON 格式,规则由 matchreplace 两个部分组成:

  • match: 用于匹配符合条件的请求。
  • replace: 用于指定替代规则。

规则可以通过多个 matchreplace 组合成一个完整的代理规则。

基本规则结构

{
  "rules": [
    {
      "match": {
        "ip": ["192.168.1.1"],
        "port": [80, 443],
        "domain": "example.com"
      },
      "replace": {
        "host": "proxy.example.com",
        "port": 808,
        "next": [
          {
            "host": "target.example.com",
            "port": 80
          }
        ]
      }
    }
  ]
}

规则字段说明

  1. match:

    • ip: 匹配特定 IP 地址。
    • port: 匹配特定端口。
    • domain: 匹配特定域名。
    • path: 匹配特定路径。
    • scheme: 匹配特定协议(如 http, https)。
    • query: 匹配特定查询参数。
    • header: 匹配特定 HTTP 头。
  2. replace:

    • host: 指定代理的目标主机。
    • port: 指定代理的目标端口。
    • next: 指定后续的代理规则(支持链式代理)。
    • remove-header: 去掉特定 HTTP 头。
    • add-header: 添加特定 HTTP 头。
    • body: 修改或添加请求体。

常见代理规则示例

domain 过滤(域名过滤)

禁止访问某些域名:

{
  "rules": [
    {
      "match": {
        "domain": "ads.example.com"
      },
      "replace": {
        "next": []
      }
    }
  ]
}

效果:访问 ads.example.com 被代理到空白页面。


端口转发

将局域网端口转发到远程服务器:

{
  "rules": [
    {
      "match": {
        "ip": "192.168.1.1",
        "port": [80, 443]
      },
      "replace": {
        "host": "remote.example.com",
        "port": 808
      }
    }
  ]
}

效果:局域网内的 168.1.1 端口 80 和 443 转发到 remote.example.com 的 808 端口。


限制访问某个网络

限制某个子网的访问:

{
  "rules": [
    {
      "match": {
        "ip": ["192.168.1./24"]
      },
      "replace": {
        "next": []
      }
    }
  ]
}

效果:阻止对 168.1./24 子网的访问。


限速

限制某个域名的下载速度:

{
  "rules": [
    {
      "match": {
        "domain": "example.com"
      },
      "replace": {
        "限速": 100M
      }
    }
  ]
}

效果:访问 example.com 的速度被限制为 100Mbps。


黑名单规则

禁止访问某些网站:

{
  "rules": [
    {
      "match": {
        "domain": ["blackhole1.com", "blackhole2.com"]
      },
      "replace": {
        "next": []
      }
    }
  ]
}

效果:访问 blackhole1.comblackhole2.com 被代理到空白页面。


重写域名

将某个域名重写为另一个域名:

{
  "rules": [
    {
      "match": {
        "domain": "example.com"
      },
      "replace": {
        "host": "proxy.example.com"
      }
    }
  ]
}

效果:访问 example.com 被代理到 proxy.example.com


链式代理

多级代理规则:

{
  "rules": [
    {
      "match": {
        "domain": "origin.example.com"
      },
      "replace": {
        "host": "first.proxy.example.com",
        "port": 808,
        "next": [
          {
            "host": "second.proxy.example.com",
            "port": 808
          }
        ]
      }
    }
  ]
}

效果:origin.example.comfirst.proxy.example.comsecond.proxy.example.com


高级规则应用

Clash 支持更复杂的规则组合,

  • 正则表达式匹配
    {
      "match": {
        "domain": "(?:\\.com|\\.net)"
      }
    }

    匹配所有 .com.net 域名。

  • 路径匹配
    {
      "match": {
        "path": "/api/(.*)"
      },
      "replace": {
        "next": [
          {
            "host": "internalapi.example.com",
            "path": "/$1"
          }
        ]
      }
    }

    /api/ 路径的请求转发到 internalapi.example.com

  • 查询参数匹配
    {
      "match": {
        "query": "param=value"
      }
    }

    匹配带有 param=value 查询参数的请求。


Clash 配置文件结构

Clash 的配置文件通常位于 /etc/clash/clash.json 或用户目录下的 .clash.json 文件中,配置文件包含以下主要部分:

{
  "port": 789, // 代理端口
  "listen": "...", // 监听地址
  "max_connections": 100, // 最大连接数
  "timeout": 10000, // 代理超时
  "DNS": "192.168.1.1", // DNS 服务器
  "HTTP": "http://localhost:789", // HTTP 代理
  "HTTPS": "http://localhost:789", // HTTPS 代理
  "rules": [ // 代理规则
    ...
  ]
}

注意事项

  1. 规则优先级:Clash 的规则是按顺序执行的,先匹配的规则优先生效。
  2. 链式代理replace 中的 next 可以链式调用多个代理规则。
  3. 正则表达式:Clash 支持使用正则表达式,需要在 matchreplace 中使用 regex 字段。
  4. 测试规则:建议在测试环境中验证规则是否正确,避免误删或误改重要规则。

Clash 是一个非常强大的网络代理工具,支持通过配置文件来定义复杂的代理规则。以下是 Clash代理规则的基本语法和常见应用示例

扫描二维码推送至手机访问。

本文转载自互联网,如有侵权,联系删除。

本文链接:https://shandian-vpn.com/post/9495.html

扫描二维码手机访问

文章目录
网站地图