通过Parsers统一不同服务商的分流方案

2022-09-03 08:24 Saturday12403min
CC BY 4.0(除特别声明和转载)

[!info] 更新
版本 0.20.10 开始支持从远端获取

可用parsers

parsers:
  - reg: ^.*$
    remote:
      url: https://cdn.jsdelivr.net/gh/yaleiyale/yahosting@main/self-rules/parser.js
      cache: true # 默认为false,指示是否对重复下载此预处理代码使用缓存

原方案:
在有几个备选订阅时,各家不同的分流方案会有出入,可以通过parsers进行统一配置。
以下配置文件修改自Issue~Loyalsoldier/clash-rules

parsers: # array
  # - reg: ^.*$ 匹配所有订阅,或  - url: https://example.com/profile.yaml 指定订阅
  - 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:
            - DIRECT # 直连
            - PROXY # 代理

        - name: 🚀不时之需
          type: select
          proxies:
            - DIRECT # 直连
            - PROXY # 代理

        - name: PROXY
          type: select
          proxies:
            - 🤖自动节点
            - 🎯手动节点

        - name: 🤖自动节点
          type: url-test
          url: http://www.gstatic.com/generate_204 # 测试地址 非select类型分组必要
          interval: 300
          olerance: 50

        - name: 🎯手动节点
          type: select

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

      # 策略组示例
      # - name: 分组名
      # type: select       # 手动选点
      # url-test     # 自动节点延迟最低的节点
      # fallback     # 节点故障时自动切换下一个
      # laod-balance # 均衡使用分组内的节点
      # url: http://www.gstatic.com/generate_204 # 测试地址 非select类型分组必要
      # interval: 300 # 自动测试间隔时间,单位秒 非select类型分组必要
      # tolerance: 50 # 允许的偏差,节点之间延迟差小于该值不切换 非必要
      # proxies:
      # - 节点名称或其他分组套娃

      commands:
        - proxy-groups.🤖自动节点.proxies=[]proxyNames # 向指定策略组添加订阅中的节点名,可使用正则过滤
        - proxy-groups.🎯手动节点.proxies=[]proxyNames # 向指定策略组添加订阅中的节点名,可使用正则过滤

        # 一些可能用到的正则过滤节点示例,使分组更细致
        # []proxyNames|a                         # 包含a
        # []proxyNames|^(.*)(a|b)+(.*)$          # 包含a或b
        # []proxyNames|^(?=.*a)(?=.*b).*$        # 包含a和b
        # []proxyNames|^((?!b).)*a((?!b).)*$     # 包含a且不包含b
        # []proxyNames|^((?!b|c).)*a((?!b|c).)*$ # 包含a且不包含b或c

      # 添加规则

      prepend-rules: # 规则由上往下遍历,如上面规则已经命中,则不再往下处理
        - RULE-SET,reject,⛔广告拦截 #广告
        - RULE-SET,direct,DIRECT #直连
        - RULE-SET,cncidr,DIRECT #中国大陆 IP
        - RULE-SET,private,DIRECT #私有网络
        - RULE-SET,lancidr,DIRECT #局域网 IP 及保留 IP 地址
        - GEOIP,LAN,DIRECT
        - GEOIP,CN,DIRECT
        - RULE-SET,selfdomain,🚀不时之需 #自行配置可直连域名
        - RULE-SET,tld-not-cn,🚀不时之需 #非中国大陆使用的顶级域名
        - RULE-SET,selfapp,🚀不时之需 #个人配置需调控软件
        - RULE-SET,applications,🚀不时之需 #需要直连的常见软件
        - RULE-SET,google,🚀不时之需 #Google可直连
        - RULE-SET,icloud,🚀不时之需 #icloud
        - RULE-SET,apple,🚀不时之需 #苹果可直连
        - RULE-SET,gfw,PROXY #GFWList
        - RULE-SET,greatfire,PROXY #GreatFire
        - RULE-SET,telegramcidr,PROXY #Telegram IP
        - RULE-SET,proxy,PROXY #代理
        - MATCH,🐟漏网之鱼 # 规则之外的

      # 添加规则集
      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

        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

        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

        selfdomain:
          type: http
          behavior: domain
          url: "https://cdn.jsdelivr.net/gh/yaleiyale/yahosting@main/self-rules/selfdomain.txt"
          path: ./ruleset/selfdomain.yaml
          interval: 86400

        selfapp:
          type: http
          behavior: classical
          url: "https://cdn.jsdelivr.net/gh/yaleiyale/yahosting@main/self-rules/selfapp.txt"
          path: ./ruleset/selfapp.yaml
          interval: 86400
BuyMeACola