SagerNet / sing-box

The universal proxy platform
https://sing-box.sagernet.org/
Other
20.25k stars 2.41k forks source link

【BUG】切换clash_mode后DNS缓存大概率不会清除 #2277

Open ericsyj opened 1 week ago

ericsyj commented 1 week ago

操作系统

Android

系统版本

Android 14

安装类型

sing-box for Android 图形客户端程序

如果您使用图形客户端程序,请提供该程序版本。

最新稳定版1.10.1

版本

No response

描述

在1.10.1的SFA中,clash_mode切换后DNS缓存大概率不会清除,windows amd64命令行程序中也能复现。

重现方式

在SFA中使用如下配置

{
  "log": {
    "level": "trace",
    "timestamp": true
  },
  "dns": {
    "servers": [
      {
        "tag": "dns-v4",
        "address": "223.5.5.5",
        "detour": "direct",
        "strategy": "ipv4_only"
      },
      {
        "tag": "dns-all",
        "address": "223.5.5.5",
        "detour": "direct"
      }
    ],
    "rules": [
      {
        "clash_mode": "IPV6",
        "server": "dns-all"
      }
    ]
  },
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "mtu": 9000,
      "address": [
        "172.19.0.1/30",
        "fdfe:dcba:9876::1/126"
      ],
      "route_exclude_address": [
        "10.0.0.0/8",
        "192.168.0.0/16",
        "fe80::/10",
        "fc00::/7"
      ],
      "auto_route": true,
      "strict_route": true,
      "sniff": true
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    },
    {
      "type": "dns",
      "tag": "dns-out"
    }
  ],
  "route": {
    "auto_detect_interface": true,
    "rules": [
      {
        "protocol": "dns",
        "outbound": "dns-out"
      }
    ]
  },
  "experimental": {
    "clash_api": {
      "external_controller": "0.0.0.0:9999",
      "external_ui": "ui"
    },
    "cache_file": {
      "enabled": true
    }
  }
}

场景1:设置仅让Termux和任一浏览器APP经过VPN,启动sing-box后切换clash_mode IPV6,打开浏览器访问除www.baidu.com外任意内容,关闭浏览器,打开Termux使用dig @8.8.8.8 +noedns aaaa www.baidu.com查询dns,再切换clash_mode Rule->IPV6,再次以同样命令查询dns,日志中显示命中缓存,再切换clash_mode Rule,发现依然命中缓存,日志如下(时间最新的在上):

24-11-13 20:02:35[ info ][3941641781 2ms] dns: cached www.baidu.com AAAA www.a.shifen.com. 80 IN AAAA 240e:ff:e020:966:0:ff:b042:f296
24-11-13 20:02:35[ info ][3941641781 1ms] dns: cached www.baidu.com AAAA www.a.shifen.com. 80 IN AAAA 240e:ff:e020:9ae:0:ff:b014:8e8b
24-11-13 20:02:35[ info ][3941641781 1ms] dns: cached www.baidu.com CNAME www.baidu.com. 80 IN CNAME www.a.shifen.com.
24-11-13 20:02:35[ debug ][3941641781 1ms] dns: cached www.baidu.com NOERROR 80
24-11-13 20:02:35[ debug ][3941641781 1ms] dns: exchange www.baidu.com. IN AAAA
24-11-13 20:02:35[ debug ][3941641781 1ms] router: match[0] protocol=dns => dns-out
24-11-13 20:02:35[ debug ][3941641781 1ms] router: sniffed packet protocol: dns
24-11-13 20:02:35[ info ][3941641781 0ms] inbound/tun[tun-in]: inbound packet connection to 8.8.8.8:53
24-11-13 20:02:35[ info ][3941641781 0ms] inbound/tun[tun-in]: inbound packet connection from 172.19.0.1:41976
24-11-13 20:02:33[ info ]clash-api: updated mode: Rule
24-11-13 20:02:23[ debug ][3107681114 10.0s] inbound/tun[tun-in]: connection closed: context canceled
24-11-13 20:02:17[ debug ][3695149815 10.2s] inbound/tun[tun-in]: connection closed: context canceled
24-11-13 20:02:13[ info ][3107681114 2ms] dns: cached www.baidu.com AAAA www.a.shifen.com. 103 IN AAAA 240e:ff:e020:966:0:ff:b042:f296
24-11-13 20:02:13[ info ][3107681114 1ms] dns: cached www.baidu.com AAAA www.a.shifen.com. 103 IN AAAA 240e:ff:e020:9ae:0:ff:b014:8e8b
24-11-13 20:02:13[ info ][3107681114 1ms] dns: cached www.baidu.com CNAME www.baidu.com. 103 IN CNAME www.a.shifen.com.
24-11-13 20:02:13[ debug ][3107681114 1ms] dns: cached www.baidu.com NOERROR 103
24-11-13 20:02:13[ debug ][3107681114 1ms] dns: exchange www.baidu.com. IN AAAA
24-11-13 20:02:13[ debug ][3107681114 0ms] router: match[0] protocol=dns => dns-out
24-11-13 20:02:13[ debug ][3107681114 0ms] router: sniffed packet protocol: dns
24-11-13 20:02:13[ info ][3107681114 0ms] inbound/tun[tun-in]: inbound packet connection to 8.8.8.8:53
24-11-13 20:02:13[ info ][3107681114 0ms] inbound/tun[tun-in]: inbound packet connection from 172.19.0.1:41367
24-11-13 20:02:13[ debug ][2125190175 10.0s] inbound/tun[tun-in]: connection closed: context canceled
24-11-13 20:02:11[ info ]clash-api: updated mode: IPV6
24-11-13 20:02:10[ info ]clash-api: updated mode: Rule
24-11-13 20:02:07[ info ][3695149815 23ms] dns: exchanged www.baidu.com AAAA www.a.shifen.com. 110 IN AAAA 240e:ff:e020:966:0:ff:b042:f296
24-11-13 20:02:07[ info ][3695149815 23ms] dns: exchanged www.baidu.com AAAA www.a.shifen.com. 110 IN AAAA 240e:ff:e020:9ae:0:ff:b014:8e8b
24-11-13 20:02:07[ info ][3695149815 22ms] dns: exchanged www.baidu.com CNAME www.baidu.com. 110 IN CNAME www.a.shifen.com.
24-11-13 20:02:07[ debug ][3695149815 22ms] dns: exchanged www.baidu.com NOERROR 110
24-11-13 20:02:07[ debug ][3695149815 1ms] dns: match[0] clash_mode=IPV6 => dns-all
24-11-13 20:02:07[ debug ][3695149815 1ms] dns: exchange www.baidu.com. IN AAAA
24-11-13 20:02:07[ debug ][3695149815 1ms] router: match[0] protocol=dns => dns-out
24-11-13 20:02:07[ debug ][3695149815 1ms] router: sniffed packet protocol: dns
24-11-13 20:02:07[ info ][3695149815 0ms] inbound/tun[tun-in]: inbound packet connection to 8.8.8.8:53
24-11-13 20:02:07[ info ][3695149815 0ms] inbound/tun[tun-in]: inbound packet connection from 172.19.0.1:45758

场景2:设置仅Termux经过VPN,进行场景1操作,则dns缓存能正常清除,切换clash_mode Rule->IPV6后日志中不会命中缓存,最后切回Rule时日志中会显示策略拒绝不会返回结果。

场景3:若在配置中加入"independent_cache": true,进行场景1操作,切换clash_mode Rule->IPV6后日志显示仍然命中缓存,最后切回Rule时日志中会显示策略拒绝不会返回结果符合独立缓存预期。

源码中确实是在切换clash_mode后有调用清除dns缓存的函数,不清楚是什么原因影响了清除缓存的操作,还是说本人理解有误。

日志

No response

支持我们

完整性要求

ericsyj commented 1 week ago

经过测试在SFA中,如果将上面配置的所有dns.server.address均设置为local,按照场景1类似操作打开浏览器后再查询dns,即使开启独立缓存,切换clash_mode也能命中缓存,似乎独立缓存在此配置下也没有生效,独立缓存开启时日志如下(切换为rule时应该走的是默认的dns-v4然后被策略拒绝,但日志中并没有):

24-11-13 21:01:39[ debug ][741729599 10.0s] inbound/tun[tun-in]: connection closed: context canceled
24-11-13 21:01:30[ info ][741729599 1ms] dns: cached www.baidu.com AAAA www.a.shifen.com. 287 IN AAAA 240e:ff:e020:966:0:ff:b042:f296
24-11-13 21:01:30[ info ][741729599 1ms] dns: cached www.baidu.com AAAA www.a.shifen.com. 287 IN AAAA 240e:ff:e020:9ae:0:ff:b014:8e8b
24-11-13 21:01:30[ info ][741729599 1ms] dns: cached www.baidu.com CNAME www.baidu.com. 287 IN CNAME www.a.shifen.com.
24-11-13 21:01:30[ debug ][741729599 1ms] dns: cached www.baidu.com NOERROR 287
24-11-13 21:01:30[ debug ][741729599 1ms] dns: exchange www.baidu.com. IN AAAA
24-11-13 21:01:30[ debug ][741729599 0ms] router: match[0] protocol=dns => dns-out
24-11-13 21:01:30[ debug ][741729599 0ms] router: sniffed packet protocol: dns
24-11-13 21:01:30[ info ][741729599 0ms] inbound/tun[tun-in]: inbound packet connection to 8.8.8.8:53
24-11-13 21:01:30[ info ][741729599 0ms] inbound/tun[tun-in]: inbound packet connection from 172.19.0.1:43123
24-11-13 21:01:24[ info ]clash-api: updated mode: Rule
24-11-13 21:01:21[ debug ][1450432024 13.6s] inbound/tun[tun-in]: connection closed: context canceled
24-11-13 21:01:11[ info ][1450432024 3.6s] dns: exchanged www.baidu.com AAAA www.a.shifen.com. 306 IN AAAA 240e:ff:e020:966:0:ff:b042:f296
24-11-13 21:01:11[ info ][1450432024 3.6s] dns: exchanged www.baidu.com AAAA www.a.shifen.com. 306 IN AAAA 240e:ff:e020:9ae:0:ff:b014:8e8b
24-11-13 21:01:11[ info ][1450432024 3.6s] dns: exchanged www.baidu.com CNAME www.baidu.com. 306 IN CNAME www.a.shifen.com.
24-11-13 21:01:11[ debug ][1450432024 3.6s] dns: exchanged www.baidu.com NOERROR 306
24-11-13 21:01:08[ debug ][1450432024 1ms] dns: match[0] clash_mode=IPV6 => dns-all
24-11-13 21:01:08[ debug ][1450432024 1ms] dns: exchange www.baidu.com. IN AAAA
24-11-13 21:01:08[ debug ][1450432024 0ms] router: match[0] protocol=dns => dns-out
24-11-13 21:01:08[ debug ][1450432024 0ms] router: sniffed packet protocol: dns
24-11-13 21:01:08[ info ][1450432024 0ms] inbound/tun[tun-in]: inbound packet connection to 8.8.8.8:53
24-11-13 21:01:08[ info ][1450432024 0ms] inbound/tun[tun-in]: inbound packet connection from 172.19.0.1:47320
nekohasekai commented 1 week ago

Try 1.11.0-alpha.16

ericsyj commented 2 days ago

Try 1.11.0-alpha.16

刚刚尝试了一下1.11.0-alpha.20版本,问题还是存在,感觉基本上就是并发读写缓存的时候清缓存的时候复现的,但是代码里也加锁了,我这水平就搞不太懂了