V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
LongLights
V2EX  ›  分享创造

分享一下个人自用的 clash 规则+对于“防 DNS 泄露”的看法

  •  
  •   LongLights · 98 天前 · 3613 次点击
    这是一个创建于 98 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是我自己使用的 yaml 配置文件,使用了 rule-providers 特性以进行规则分流,并使这些规则每 24hr 自动更新。规则来自Loyalsoldier/clash-rules

    适用于 PC 端的 clash-verge-rev 、OpenWrt 端的 openclash 、iOS 端的 stash ,其余未测试(最好尽量关闭这些客户端的 dns 覆写设置)

    未使用在 ip 规则后加 no-resolve ,或 nameserver-policy 等方式进行所谓的“防止 DNS 泄露”,如有需要可在配置底部最后 5 条“IP 地址规则”末尾添加“,no-resolve”即可,但个人并不建议这样操作,原因后面会说,这里先贴出完整配置(节点信息自己补充)

    port: 7890
    socks-port: 7891
    allow-lan: true
    mode: Rule
    log-level: info
    external-controller: :9090
    dns:
      enable: true
      prefer-h3: true
      default-nameserver:
        - 114.114.114.114
        - 119.29.29.29
      nameserver:
        - 114.114.114.114
        - 119.29.29.29
      fallback:
        - https://dns.google/dns-query
        - https://dns.cloudflare.com/dns-query
      fake-ip-filter:
        [
          "*.n.n.srv.nintendo.net",
          +.stun.playstation.net,
          xbox.*.*.microsoft.com,
          "*.msftncsi.com",
          "*.msftconnecttest.com",
          WORKGROUP,
          "*.lan",
          stun.*.*,
          stun.*.*.*,
          +.stun.*.*,
          +.stun.*.*.*,
          +.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,
          "*.*.xboxlive.com",
          speedtest.cros.wr.pvp.net,
        ]
    proxies: ~
    
    proxy-groups:
      - name: 节点选择
        type: select
        proxies:
          - DIRECT
          - 自动选择
    
      - name: 自动选择
        type: url-test
        url: http://www.gstatic.com/generate_204
        interval: 300
    
      - name: 全球直连
        type: select
        proxies:
          - DIRECT
          - 节点选择
          - 自动选择
    
      - name: 一堵高墙
        type: select
        proxies:
          - 节点选择
          - 自动选择
          - 全球直连
    
      - name: 谷歌
        type: select
        proxies:
          - 节点选择
          - 自动选择
          - 全球直连
    
      - name: 电报
        type: select
        proxies:
          - 节点选择
          - 自动选择
          - 全球直连
    
      - name: 苹果云同步
        type: select
        proxies:
          - 节点选择
          - 自动选择
          - 全球直连
      
      - name: 苹果
        type: select
        proxies:
          - 节点选择
          - 自动选择
          - 全球直连
    
      - name: 境外
        type: select
        proxies:
          - 节点选择
          - 自动选择
          - 全球直连
    
      - name: 私有
        type: select
        proxies:
          - 全球直连
          - 节点选择
          - 自动选择
    
      - name: 境内
        type: select
        proxies:
          - 全球直连
          - 节点选择
          - 自动选择
    
      - name: 常用软件
        type: select
        proxies:
          - 全球直连
          - 节点选择
          - 自动选择
    
      - name: 漏网之鱼
        type: select
        proxies:
          - 节点选择
          - 自动选择
          - 全球直连
          
    rule-providers:
      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
    
      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
    
    rules:
      # 普通规则
      - RULE-SET,private,私有
      - RULE-SET,applications,常用软件
      - RULE-SET,direct,境内
      - RULE-SET,gfw,一堵高墙
      - RULE-SET,icloud,苹果云同步
      - RULE-SET,apple,苹果
      - RULE-SET,google,谷歌
      - RULE-SET,proxy,境外
      # IP 地址规则
      - RULE-SET,telegramcidr,电报
      - RULE-SET,lancidr,私有
      - RULE-SET,cncidr,境内
      - GEOIP,LAN,私有
      - GEOIP,CN,境内
      # 兜底规则
      - MATCH,漏网之鱼
    

    下面开始说说 fakeip 模式下所谓的“防 DNS 泄露”

    无论是在 ip 规则后加 no-resolve ,还是使用 nameserver-policy 进行“dns 分流”,亦或是 openclash 配合 mosdns 勾选防泄漏,本质上运行方式都一样:在域名规则无法匹配的情况下,丢弃 ip 规则,使之匹配配置中的最后一行“match,proxy”

    但要注意,如果域名本身已经在“普通规则”中,无论你是否启用了这些“防 DNS 泄露”功能,他都不会经过运营商的 DNS 。在存在一条规则“RULE-SET,gfw,proxy”的情况下,这不就是我们想要的“防泄露”效果吗?即:我不在乎运营商知道我在访问 ipleak 这类非 gfw 网站,他不知道我在访问 google 这类 gfw 网站就行

    以“GEOIP,CN,DIRECT”这条 IP 地址规则为例,我们预期的目标是:当一个目标网站未匹配中任意一条网址规则时,境内境外两组 DNS 同时解析。若解析结果存在国外 ip ,走代理;若解析结果都是境内 ip ,走直连。但所谓的“防 DNS 泄露”会使之变成一条无用规则

    那么,有没有什么办法可以验证,访问 gfw 网站时有没有经过国内 DNS 留下痕迹呢?答案是有的:在 iOS 端的 stash 中选择“检视 DNS”即可,他应当只出现域名规则之外的域名

    第 1 条附言  ·  92 天前

    由于部分地区开始劫持公共dns的ip,优化dns部分如下:

      default-nameserver:
        - 114.114.114.114
        - 119.29.29.29
      nameserver:
        - https://doh.pub/dns-query
        - https://doh.360.cn/dns-query
      fallback:
        - tls://1.1.1.1
        - https://v.recipes/dns-query
    
    10 条回复    2025-06-12 08:46:34 +08:00
    lihaohenqiang
        1
    lihaohenqiang  
       98 天前 via iPhone   ❤️ 1
    说实话,这个“DNS 泄露”纯粹就是某些人制造焦虑,吧分流和解析分流做好就行,就像 GFW 吧表内域名直接丢给远程解析就行
    LongLights
        2
    LongLights  
    OP
       98 天前   ❤️ 1
    @lihaohenqiang 主要是存在普遍的误解误传,dns 的运行流程和“DNS 泄露”都是
    lihaohenqiang
        3
    lihaohenqiang  
       98 天前 via iPhone
    @LongLights 是的,就是一个 up 在那说,还有一群啥也不懂的小白底下起哄,最后越传越玄幻
    ewiglicht
        4
    ewiglicht  
       98 天前
    我自己写了一个单纯跑内核的配置文件,用的 mihomo 官方的规则,用了挺久了,也没处理“DNS 泄漏”之类的问题。

    https://github.com/ewigl/mihomo/blob/master/config.yaml

    其实主要是因为我确实不太清楚 DNS 泄漏是啥意思。😂
    juzisang
        5
    juzisang  
       98 天前
    感觉没有必要维护这么多常见 rules ,用 GeoSite 就行,rule-providers 我目前只在定制情况下才用。
    LongLights
        6
    LongLights  
    OP
       98 天前
    再贴一个美化版的 yaml 配置(其实只是填了 icon 字段),话说为啥流行的模板都用的 emoji 而不是 icon 呢
    ```yaml
    port: 7890
    socks-port: 7891
    allow-lan: true
    mode: Rule
    log-level: info
    external-controller: :9090
    dns:
    enable: true
    prefer-h3: true
    default-nameserver:
    - 114.114.114.114
    - 119.29.29.29
    nameserver:
    - 114.114.114.114
    - 119.29.29.29
    fallback:
    - https://dns.google/dns-query
    - https://dns.cloudflare.com/dns-query
    fake-ip-filter:
    [
    "*.n.n.srv.nintendo.net",
    +.stun.playstation.net,
    xbox.*.*.microsoft.com,
    "*.msftncsi.com",
    "*.msftconnecttest.com",
    WORKGROUP,
    "*.lan",
    stun.*.*,
    stun.*.*.*,
    +.stun.*.*,
    +.stun.*.*.*,
    +.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,
    "*.*.xboxlive.com",
    speedtest.cros.wr.pvp.net,
    ]
    proxies: ~

    proxy-groups:
    - name: 节点选择
    type: select
    proxies:
    - 自动选择
    - DIRECT
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Dinosaur.png

    - name: 自动选择
    type: url-test
    url: http://www.gstatic.com/generate_204
    interval: 300
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Speedtest.png

    - name: 全球直连
    type: select
    proxies:
    - DIRECT
    - 节点选择
    - 自动选择
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Bypass.png

    - name: 一堵高墙
    type: select
    proxies:
    - 节点选择
    - 自动选择
    - 全球直连
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Unlock.png

    - name: Google
    type: select
    proxies:
    - 节点选择
    - 自动选择
    - 全球直连
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Google_Search.png

    - name: Telegram
    type: select
    proxies:
    - 节点选择
    - 自动选择
    - 全球直连
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Telegram.png

    - name: iCloud
    type: select
    proxies:
    - 节点选择
    - 自动选择
    - 全球直连
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/iCloud.png

    - name: Apple
    type: select
    proxies:
    - 节点选择
    - 自动选择
    - 全球直连
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Apple_1.png

    - name: 境外
    type: select
    proxies:
    - 节点选择
    - 自动选择
    - 全球直连
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/United_States.png

    - name: 私有
    type: select
    proxies:
    - 全球直连
    - 节点选择
    - 自动选择
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Clubhouse_2.png

    - name: 境内
    type: select
    proxies:
    - 全球直连
    - 节点选择
    - 自动选择
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/China.png

    - name: 常用软件
    type: select
    proxies:
    - 全球直连
    - 节点选择
    - 自动选择
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/App_Store.png

    - name: 漏网之鱼
    type: select
    proxies:
    - 节点选择
    - 自动选择
    - 全球直连
    icon: https://raw.githubusercontent.com/Koolson/Qure/refs/heads/master/IconSet/Color/Global.png

    rule-providers:
    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

    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

    rules:
    # 普通规则
    - RULE-SET,private,私有
    - RULE-SET,applications,常用软件
    - RULE-SET,direct,境内
    - RULE-SET,gfw,一堵高墙
    - RULE-SET,icloud,iCloud
    - RULE-SET,apple,Apple
    - RULE-SET,google,Google
    - RULE-SET,proxy,境外
    # IP 地址规则
    - RULE-SET,telegramcidr,Telegram
    - RULE-SET,lancidr,私有
    - RULE-SET,cncidr,境内
    - GEOIP,LAN,私有
    - GEOIP,CN,境内
    # 兜底规则
    - MATCH,漏网之鱼
    ```
    laincat
        7
    laincat  
       98 天前 via iPhone
    没有用锚点写法?
    daisyfloor
        8
    daisyfloor  
       98 天前
    太复杂了。

    1 、用 tun 模式,并启用 tun 设置下的“严格路由”
    2 、dns 开启 fake-ip ,然后只设置运营商的 dns 就可以了,什么 policy ,fallback 都不用设
    3 、分流规则里所有设计 ip 类的规则都添加 no-resolve

    上面这样 2 分钟就搞好了,从来没见过泄露。

    简单来说就是一句话,不要在本地进行不必要的 dns 解析。
    peasant
        9
    peasant  
       98 天前   ❤️ 1
    所谓的 DNS 泄露就是凡是没有提前写在规则里的域名,都需要先进行本地解析才能知道到底要不要走代理,这时候被本地 DNS 解析过的域名会被 DNS 服务商知道,也就是所谓的泄露,解决办法就两种,一是完全可靠可信的本地 DNS ,也就是 DoH 之类,二就是维护一个庞大臃肿的需要分流的域名列表,当然你可以用别人维护好的,但是太多列表也会有性能方面的影响。
    lihaohenqiang
        10
    lihaohenqiang  
       97 天前 via iPhone
    @peasant 用到 doh 最大问题是,是不是最优解析结果,如果是在服务器节点城市还好,但是一般 DNS 节点只会在大城市有。还有如果是国内 doh 查询,本质上还是和泄露没有区别只是换人泄露,国外的很多域名不可避免会解析到境外服务器,ecs 国内的解析结果基本没什么参考价值,甚至结果还不如运营商(除污染)的准确,我认为目前唯一具有安全性和性能的还是基于域名表做 DNS 分流,比方把 cn 域名和国内白名单域名做直连解析,其他统一通过代理隧道走 dns 解析
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1623 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:19 · PVG 00:19 · LAX 09:19 · JFK 12:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.