From 1685c61e442ece0da357254f69c6b264d13c193d Mon Sep 17 00:00:00 2001 From: patterniha <71074308+patterniha@users.noreply.github.com> Date: Sat, 29 Mar 2025 17:35:27 +0330 Subject: [PATCH] DNS: Use cache for NXDOMAIN (rcode 3 error) (#4560) https://github.com/XTLS/Xray-core/pull/4558#issuecomment-2759007356 --- app/dns/nameserver_doh.go | 2 +- app/dns/nameserver_quic.go | 2 +- app/dns/nameserver_tcp.go | 2 +- app/dns/nameserver_udp.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/dns/nameserver_doh.go b/app/dns/nameserver_doh.go index 8a23e0c3..6cdb8ee7 100644 --- a/app/dns/nameserver_doh.go +++ b/app/dns/nameserver_doh.go @@ -357,7 +357,7 @@ func (s *DoHNameServer) QueryIP(ctx context.Context, domain string, clientIP net errors.LogDebug(ctx, "DNS cache is disabled. Querying IP for ", domain, " at ", s.name) } else { ips, ttl, err := s.findIPsForDomain(fqdn, option) - if err == nil || err == dns_feature.ErrEmptyResponse { + if err == nil || err == dns_feature.ErrEmptyResponse || dns_feature.RCodeFromError(err) == 3 { errors.LogDebugInner(ctx, err, s.name, " cache HIT ", domain, " -> ", ips) log.Record(&log.DNSLog{Server: s.name, Domain: domain, Result: ips, Status: log.DNSCacheHit, Elapsed: 0, Error: err}) return ips, ttl, err diff --git a/app/dns/nameserver_quic.go b/app/dns/nameserver_quic.go index 2b0b7b66..6ce5809b 100644 --- a/app/dns/nameserver_quic.go +++ b/app/dns/nameserver_quic.go @@ -300,7 +300,7 @@ func (s *QUICNameServer) QueryIP(ctx context.Context, domain string, clientIP ne errors.LogDebug(ctx, "DNS cache is disabled. Querying IP for ", domain, " at ", s.name) } else { ips, ttl, err := s.findIPsForDomain(fqdn, option) - if err == nil || err == dns_feature.ErrEmptyResponse { + if err == nil || err == dns_feature.ErrEmptyResponse || dns_feature.RCodeFromError(err) == 3 { errors.LogDebugInner(ctx, err, s.name, " cache HIT ", domain, " -> ", ips) log.Record(&log.DNSLog{Server: s.name, Domain: domain, Result: ips, Status: log.DNSCacheHit, Elapsed: 0, Error: err}) return ips, ttl, err diff --git a/app/dns/nameserver_tcp.go b/app/dns/nameserver_tcp.go index d84974b0..49854312 100644 --- a/app/dns/nameserver_tcp.go +++ b/app/dns/nameserver_tcp.go @@ -325,7 +325,7 @@ func (s *TCPNameServer) QueryIP(ctx context.Context, domain string, clientIP net errors.LogDebug(ctx, "DNS cache is disabled. Querying IP for ", domain, " at ", s.name) } else { ips, ttl, err := s.findIPsForDomain(fqdn, option) - if err == nil || err == dns_feature.ErrEmptyResponse { + if err == nil || err == dns_feature.ErrEmptyResponse || dns_feature.RCodeFromError(err) == 3 { errors.LogDebugInner(ctx, err, s.name, " cache HIT ", domain, " -> ", ips) log.Record(&log.DNSLog{Server: s.name, Domain: domain, Result: ips, Status: log.DNSCacheHit, Elapsed: 0, Error: err}) return ips, ttl, err diff --git a/app/dns/nameserver_udp.go b/app/dns/nameserver_udp.go index 79df14ed..23803efa 100644 --- a/app/dns/nameserver_udp.go +++ b/app/dns/nameserver_udp.go @@ -282,7 +282,7 @@ func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string, clientIP errors.LogDebug(ctx, "DNS cache is disabled. Querying IP for ", domain, " at ", s.name) } else { ips, ttl, err := s.findIPsForDomain(fqdn, option) - if err == nil || err == dns_feature.ErrEmptyResponse { + if err == nil || err == dns_feature.ErrEmptyResponse || dns_feature.RCodeFromError(err) == 3 { errors.LogDebugInner(ctx, err, s.name, " cache HIT ", domain, " -> ", ips) log.Record(&log.DNSLog{Server: s.name, Domain: domain, Result: ips, Status: log.DNSCacheHit, Elapsed: 0, Error: err}) return ips, ttl, err