Skip to content

内置 DNS 缓存 TTL 的问题 #6359

Closed

Description

@yang05051

简述 / Description

Xray-core 内部 DNS 缓存无视上游服务器返回的真实 TTL(例如 3600s),其生存周期似乎被内部硬编码或截断为了最大 300 秒(5 分钟)。导致客户端在超过 5 分钟后再次请求相同域名时,Xray 必然发生内部缓存未命中(Cache Miss),从而被迫重新向分布式/本地上游发起 DoH 请求。

重现步骤 / Steps to Reproduce

  1. 配置上游 DoH 服务器(例如 AdGuardHome),设置最小 TTL 覆盖为 3600 秒。
  2. 配置 Xray 的 dns 模块,设置 "disableCache": false,路由使用 "domainStrategy": "IPOnDemand"。
  3. 客户端发起对某一域名(例如 www.example.com)的请求。
  4. 4 分钟时(未满 300 秒):再次请求该域名,Xray 日志正确显示 cache HIT,未触发上游查询。
  5. 5 分钟 30 秒时(超过 300 秒):再次请求该域名,Xray 缓存失效,日志显示重新开始 querying 上游。

预期行为 / Expected Behavior

Xray 的内部 DNS 缓存应当尊重并严格执行上游返回的有效 TTL 值(如 3600 秒),在 3600 秒内不应频繁在内部过期并重新向外(或向本地环回)发送 DoH 请求。或者,如果这是一个出于路由安全设计的硬上限截断(Clamp),应当在 config.json 中提供参数可供用户自行调节。

实际行为 / Actual Behavior

无论上游返回的 TTL 有多大(经测试 A 和 AAAA 纪录均返回 3600),Xray 内部只要超过 300 秒就会单方面蒸发该域名的缓存。

通过 Debug 日志可以清晰地看到:在 5 分钟之后重新触发的查询中,上游回包的 RTT 仅为 1.08ms。这绝对性地证明了上游 DNS(如 AdGuardHome)的缓存其实并没有过期(仍在 3600s 内),完全是 Xray 内部提前把该纪录删除了,才引发了这次不必要的重新查询。

日志证据 / Logs

// 05:32:08 初始触发查询,写入缓存
2026/06/22 05:32:08 [Info] app/dns: DOHL//adguardhome.dns.server querying: [www.example.com](https://www.example.com).
2026/06/22 05:32:08 [Info] app/dns: DOHL//adguardhome.dns.server got answer: [www.example.com](https://www.example.com). TypeA -> [...], rtt: 1.077709ms

// 05:36:37(过去 4 分 29 秒,未满 5 分钟) -> 正常命中 Xray 缓存
2026/06/22 05:36:37 [Debug] app/dns: DOHL//adguardhome.dns.server cache HIT [www.example.com](https://www.example.com).

// 05:38:04(过去 5 分 56 秒,超过 5 分钟) -> Xray 内部过期,重新查询上游
// 注意:此时上游响应仅用时 1.08ms,证明上游缓存依旧存在,完全是 Xray 内部单方面抛弃了缓存
2026/06/22 05:38:04 [Info] app/dns: DOHL//adguardhome.dns.server querying: [www.example.com](https://www.example.com).
2026/06/22 05:38:04 [Info] app/dns: DOHL//adguardhome.dns.server got answer: [www.example.com](https://www.example.com). TypeA -> [...], rtt: 1.089398ms

环境信息

Xray-Core: 26.3.27
系统: Ubuntu 22.04.5 LTS
Upstream DNS: AdGuardHome via DoH (⁠https+local://⁠)

Activity

  1. Fangliding commented on Jun 22, 2026

    @Fangliding
    Member

    似乎是实现的BUG 会修复

  2. Fangliding commented on Jun 22, 2026

    @Fangliding
    Member
  3. yang05051 commented on Jun 23, 2026

    @yang05051
    Author

    尝试一下 https://github.com/XTLS/Xray-core/actions/runs/27976400980

    十分感谢,能够确认这个问题被修复了。

    同一个配置下,您的修复版本不存在该问题,而最新 Release 的版本能够复现。至此,可以确认您的修复起作用了。十分感谢您的修复。

    附上测试时输出的日志(来自您的修复版本):

    2026/06/23 02:04:27.994741 [Info] app/dns: DNS: created UDP client initialized for 127.0.0.1:30053
    
    // 首次查询 www.example.com,AdGuardHome 日志显示 TTL 为 2662(约 45 分钟)
    2026/06/23 02:04:49.140145 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:04:49.140150 [Info] app/dns: UDP:127.0.0.1:30053 querying DNS for: example.com.
    2026/06/23 02:04:49.141290 [Info] app/dns: UDP:127.0.0.1:30053 got answer: example.com. TypeA -> [104.20.23.154 172.66.147.243], rtt: 1.169414ms, lock: 7.577µs
    2026/06/23 02:04:49.143288 [Info] app/dns: UDP:127.0.0.1:30053 got answer: example.com. TypeAAAA -> [2606:4700:10::ac42:93f3 2606:4700:10::6814:179a], rtt: 3.276534ms, lock: 2.831µs
    
    // 后续 45 分钟内,均正确 Cache Hit
    2026/06/23 02:06:29.045692 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:06:29.045704 [Debug] app/dns: UDP:127.0.0.1:30053 cache HIT example.com. -> [104.20.23.154 172.66.147.243 2606:4700:10::ac42:93f3 2606:4700:10::6814:179a]
    
    2026/06/23 02:08:09.454047 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:08:09.454058 [Debug] app/dns: UDP:127.0.0.1:30053 cache HIT example.com. -> [104.20.23.154 172.66.147.243 2606:4700:10::ac42:93f3 2606:4700:10::6814:179a]
    
    2026/06/23 02:08:52.283127 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:08:52.283141 [Debug] app/dns: UDP:127.0.0.1:30053 cache HIT example.com. -> [104.20.23.154 172.66.147.243 2606:4700:10::ac42:93f3 2606:4700:10::6814:179a]
    
    2026/06/23 02:10:11.982934 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:10:11.982947 [Debug] app/dns: UDP:127.0.0.1:30053 cache HIT example.com. -> [104.20.23.154 172.66.147.243 2606:4700:10::ac42:93f3 2606:4700:10::6814:179a]
    
    2026/06/23 02:14:14.550539 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:14:14.550551 [Debug] app/dns: UDP:127.0.0.1:30053 cache HIT example.com. -> [104.20.23.154 172.66.147.243 2606:4700:10::ac42:93f3 2606:4700:10::6814:179a]
    
    2026/06/23 02:24:52.578461 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:24:52.578474 [Debug] app/dns: UDP:127.0.0.1:30053 cache HIT example.com. -> [104.20.23.154 172.66.147.243 2606:4700:10::ac42:93f3 2606:4700:10::6814:179a]
    
    2026/06/23 02:43:05.308737 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:43:05.308751 [Debug] app/dns: UDP:127.0.0.1:30053 cache HIT example.com. -> [104.20.23.154 172.66.147.243 2606:4700:10::ac42:93f3 2606:4700:10::6814:179a]
    
    // 45 分钟之后,Cache 过期并被正确清除,成功执行查询
    2026/06/23 02:50:08.436395 [Warning] app/dns: domain example.com will use the first DNS: [UDP:127.0.0.1:30053]
    2026/06/23 02:50:08.436401 [Info] app/dns: UDP:127.0.0.1:30053 querying DNS for: example.com.
    2026/06/23 02:50:08.448138 [Info] app/dns: UDP:127.0.0.1:30053 got answer: example.com. TypeA -> [104.20.23.154 172.66.147.243], rtt: 11.847681ms, lock: 3.66µs
    2026/06/23 02:50:08.448206 [Info] app/dns: UDP:127.0.0.1:30053 got answer: example.com. TypeAAAA -> [2606:4700:10::ac42:93f3 2606:4700:10::6814:179a], rtt: 11.875087ms, lock: 940ns
    

    附上测试用的config:

    {
        "log": {
            "access": "/usr/local/etc/xray/log/access.log",
            "error": "/usr/local/etc/xray/log/error.log",
            "loglevel": "debug"
        },
        "dns": {
            "servers": [
                {
                    "address": "127.0.0.1",
                    "port": 30053
                }
            ],
            "disableFallback": true,
            "disableCache": false
        },
        "routing": {
            "domainStrategy": "IPOnDemand",
            "rules": [
                {
                    "type": "field",
                    "outboundTag": "direct_",
                    "network": "udp,tcp"
                }
            ]
        },
        "inbounds": [
            {
                "protocol": "socks",
                "listen": "127.0.0.1",
                "port": 10080,
                "settings": {
                    "auth": "noauth"
                }
            }
        ],
        "outbounds": [
            {
                "protocol": "freedom",
                "streamSettings": {
                    "sockopt": {
                        "domainStrategy": "UseIP",
                        "happyEyeballs": {
                            "tryDelayMs": 250,
                            "prioritizeIPv6": false
                        }
                    }
                },
                "tag": "direct_"
            }
        ]
    }
  4. added a commit that references this issue on Jun 23, 2026
  5. added a commit that references this issue on Jul 5, 2026
  6. added a commit that references this issue on Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions