From 79787a0176cd0b647c276a426a8d23a32936fee3 Mon Sep 17 00:00:00 2001 From: Meo597 <197331664+Meo597@users.noreply.github.com> Date: Sun, 19 Oct 2025 02:24:04 +0800 Subject: [PATCH] feat(dns): Implement optimistic caching --- app/dns/cache_controller.go | 33 +++--- app/dns/config.pb.go | 180 ++++++++++++++++++++------------ app/dns/config.proto | 4 + app/dns/dns.go | 7 +- app/dns/dnscommon.go | 14 +-- app/dns/nameserver.go | 22 ++-- app/dns/nameserver_doh.go | 42 ++++++-- app/dns/nameserver_doh_test.go | 8 +- app/dns/nameserver_quic.go | 62 ++++++++--- app/dns/nameserver_quic_test.go | 6 +- app/dns/nameserver_tcp.go | 70 +++++++++---- app/dns/nameserver_tcp_test.go | 8 +- app/dns/nameserver_udp.go | 26 +++-- infra/conf/dns.go | 64 +++++++----- 14 files changed, 352 insertions(+), 194 deletions(-) diff --git a/app/dns/cache_controller.go b/app/dns/cache_controller.go index f23c414d..fb3dcba3 100644 --- a/app/dns/cache_controller.go +++ b/app/dns/cache_controller.go @@ -3,6 +3,9 @@ package dns import ( "context" go_errors "errors" + "sync" + "time" + "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/net" @@ -10,25 +13,27 @@ import ( "github.com/xtls/xray-core/common/task" dns_feature "github.com/xtls/xray-core/features/dns" "golang.org/x/net/dns/dnsmessage" - "sync" - "time" ) type CacheController struct { sync.RWMutex - ips map[string]*record - pub *pubsub.Service - cacheCleanup *task.Periodic - name string - disableCache bool + ips map[string]*record + pub *pubsub.Service + cacheCleanup *task.Periodic + name string + disableCache bool + serveStale bool + serveExpiredTTL int32 } -func NewCacheController(name string, disableCache bool) *CacheController { +func NewCacheController(name string, disableCache bool, serveStale bool, serveExpiredTTL uint32) *CacheController { c := &CacheController{ - name: name, - disableCache: disableCache, - ips: make(map[string]*record), - pub: pubsub.NewService(), + name: name, + disableCache: disableCache, + serveStale: serveStale, + serveExpiredTTL: -int32(serveExpiredTTL), + ips: make(map[string]*record), + pub: pubsub.NewService(), } c.cacheCleanup = &task.Periodic{ @@ -113,7 +118,7 @@ func (c *CacheController) updateIP(req *dnsRequest, ipRec *IPRecord) { common.Must(c.cacheCleanup.Start()) } -func (c *CacheController) findIPsForDomain(domain string, option dns_feature.IPOption) ([]net.IP, uint32, error) { +func (c *CacheController) findIPsForDomain(domain string, option dns_feature.IPOption) ([]net.IP, int32, error) { c.RLock() record, found := c.ips[domain] c.RUnlock() @@ -124,7 +129,7 @@ func (c *CacheController) findIPsForDomain(domain string, option dns_feature.IPO var errs []error var allIPs []net.IP - var rTTL uint32 = dns_feature.DefaultTTL + var rTTL int32 = dns_feature.DefaultTTL mergeReq := option.IPv4Enable && option.IPv6Enable diff --git a/app/dns/config.pb.go b/app/dns/config.pb.go index d54142ce..9cc63986 100644 --- a/app/dns/config.pb.go +++ b/app/dns/config.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.35.1 -// protoc v5.28.2 +// protoc v5.29.4 // source: app/dns/config.proto package dns @@ -142,6 +142,8 @@ type NameServer struct { Tag string `protobuf:"bytes,9,opt,name=tag,proto3" json:"tag,omitempty"` TimeoutMs uint64 `protobuf:"varint,10,opt,name=timeoutMs,proto3" json:"timeoutMs,omitempty"` DisableCache bool `protobuf:"varint,11,opt,name=disableCache,proto3" json:"disableCache,omitempty"` + ServeStale bool `protobuf:"varint,15,opt,name=serveStale,proto3" json:"serveStale,omitempty"` + ServeExpiredTTL *uint32 `protobuf:"varint,16,opt,name=serveExpiredTTL,proto3,oneof" json:"serveExpiredTTL,omitempty"` FinalQuery bool `protobuf:"varint,12,opt,name=finalQuery,proto3" json:"finalQuery,omitempty"` UnexpectedGeoip []*router.GeoIP `protobuf:"bytes,13,rep,name=unexpected_geoip,json=unexpectedGeoip,proto3" json:"unexpected_geoip,omitempty"` ActUnprior bool `protobuf:"varint,14,opt,name=actUnprior,proto3" json:"actUnprior,omitempty"` @@ -254,6 +256,20 @@ func (x *NameServer) GetDisableCache() bool { return false } +func (x *NameServer) GetServeStale() bool { + if x != nil { + return x.ServeStale + } + return false +} + +func (x *NameServer) GetServeExpiredTTL() uint32 { + if x != nil && x.ServeExpiredTTL != nil { + return *x.ServeExpiredTTL + } + return 0 +} + func (x *NameServer) GetFinalQuery() bool { if x != nil { return x.FinalQuery @@ -291,6 +307,8 @@ type Config struct { Tag string `protobuf:"bytes,6,opt,name=tag,proto3" json:"tag,omitempty"` // DisableCache disables DNS cache DisableCache bool `protobuf:"varint,8,opt,name=disableCache,proto3" json:"disableCache,omitempty"` + ServeStale bool `protobuf:"varint,12,opt,name=serveStale,proto3" json:"serveStale,omitempty"` + ServeExpiredTTL uint32 `protobuf:"varint,13,opt,name=serveExpiredTTL,proto3" json:"serveExpiredTTL,omitempty"` QueryStrategy QueryStrategy `protobuf:"varint,9,opt,name=query_strategy,json=queryStrategy,proto3,enum=xray.app.dns.QueryStrategy" json:"query_strategy,omitempty"` DisableFallback bool `protobuf:"varint,10,opt,name=disableFallback,proto3" json:"disableFallback,omitempty"` DisableFallbackIfMatch bool `protobuf:"varint,11,opt,name=disableFallbackIfMatch,proto3" json:"disableFallbackIfMatch,omitempty"` @@ -361,6 +379,20 @@ func (x *Config) GetDisableCache() bool { return false } +func (x *Config) GetServeStale() bool { + if x != nil { + return x.ServeStale + } + return false +} + +func (x *Config) GetServeExpiredTTL() uint32 { + if x != nil { + return x.ServeExpiredTTL + } + return 0 +} + func (x *Config) GetQueryStrategy() QueryStrategy { if x != nil { return x.QueryStrategy @@ -567,7 +599,7 @@ var file_app_dns_config_proto_rawDesc = []byte{ 0x2e, 0x64, 0x6e, 0x73, 0x1a, 0x1c, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x6e, 0x65, 0x74, 0x2f, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x17, 0x61, 0x70, 0x70, 0x2f, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x2f, 0x63, - 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xb6, 0x06, 0x0a, 0x0a, + 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x99, 0x07, 0x0a, 0x0a, 0x4e, 0x61, 0x6d, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x33, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x6e, 0x65, 0x74, 0x2e, 0x45, 0x6e, @@ -601,72 +633,83 @@ var file_app_dns_config_proto_rawDesc = []byte{ 0x75, 0x74, 0x4d, 0x73, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x04, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x4d, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x43, 0x61, 0x63, 0x68, 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x64, 0x69, 0x73, - 0x61, 0x62, 0x6c, 0x65, 0x43, 0x61, 0x63, 0x68, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x66, 0x69, 0x6e, - 0x61, 0x6c, 0x51, 0x75, 0x65, 0x72, 0x79, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x66, - 0x69, 0x6e, 0x61, 0x6c, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x41, 0x0a, 0x10, 0x75, 0x6e, 0x65, - 0x78, 0x70, 0x65, 0x63, 0x74, 0x65, 0x64, 0x5f, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x18, 0x0d, 0x20, - 0x03, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x72, - 0x6f, 0x75, 0x74, 0x65, 0x72, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x52, 0x0f, 0x75, 0x6e, 0x65, - 0x78, 0x70, 0x65, 0x63, 0x74, 0x65, 0x64, 0x47, 0x65, 0x6f, 0x69, 0x70, 0x12, 0x1e, 0x0a, 0x0a, - 0x61, 0x63, 0x74, 0x55, 0x6e, 0x70, 0x72, 0x69, 0x6f, 0x72, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x0a, 0x61, 0x63, 0x74, 0x55, 0x6e, 0x70, 0x72, 0x69, 0x6f, 0x72, 0x1a, 0x5e, 0x0a, 0x0e, - 0x50, 0x72, 0x69, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x12, 0x34, - 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x20, 0x2e, 0x78, - 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, 0x44, 0x6f, 0x6d, 0x61, - 0x69, 0x6e, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, - 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x1a, 0x36, 0x0a, 0x0c, - 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x52, 0x75, 0x6c, 0x65, 0x12, 0x12, 0x0a, 0x04, - 0x72, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x72, 0x75, 0x6c, 0x65, - 0x12, 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, - 0x73, 0x69, 0x7a, 0x65, 0x22, 0x9c, 0x04, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, - 0x39, 0x0a, 0x0b, 0x6e, 0x61, 0x6d, 0x65, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x18, 0x05, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, - 0x64, 0x6e, 0x73, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x0a, - 0x6e, 0x61, 0x6d, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x1b, 0x0a, 0x09, 0x63, 0x6c, - 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, 0x63, - 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x70, 0x12, 0x43, 0x0a, 0x0c, 0x73, 0x74, 0x61, 0x74, 0x69, - 0x63, 0x5f, 0x68, 0x6f, 0x73, 0x74, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x20, 0x2e, - 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, 0x43, 0x6f, 0x6e, - 0x66, 0x69, 0x67, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x4d, 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x52, - 0x0b, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x48, 0x6f, 0x73, 0x74, 0x73, 0x12, 0x10, 0x0a, 0x03, - 0x74, 0x61, 0x67, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x12, 0x22, - 0x0a, 0x0c, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x43, 0x61, 0x63, 0x68, 0x65, 0x18, 0x08, - 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x43, 0x61, 0x63, - 0x68, 0x65, 0x12, 0x42, 0x0a, 0x0e, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x73, 0x74, 0x72, 0x61, - 0x74, 0x65, 0x67, 0x79, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1b, 0x2e, 0x78, 0x72, 0x61, - 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, - 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x52, 0x0d, 0x71, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, - 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x12, 0x28, 0x0a, 0x0f, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, - 0x65, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x08, 0x52, - 0x0f, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, - 0x12, 0x36, 0x0a, 0x16, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x61, 0x6c, 0x6c, 0x62, - 0x61, 0x63, 0x6b, 0x49, 0x66, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x16, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, - 0x6b, 0x49, 0x66, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x1a, 0x92, 0x01, 0x0a, 0x0b, 0x48, 0x6f, 0x73, - 0x74, 0x4d, 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x12, 0x34, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x20, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, - 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x4d, 0x61, 0x74, 0x63, - 0x68, 0x69, 0x6e, 0x67, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, - 0x0a, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, - 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x03, 0x20, 0x03, - 0x28, 0x0c, 0x52, 0x02, 0x69, 0x70, 0x12, 0x25, 0x0a, 0x0e, 0x70, 0x72, 0x6f, 0x78, 0x69, 0x65, - 0x64, 0x5f, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, - 0x70, 0x72, 0x6f, 0x78, 0x69, 0x65, 0x64, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x4a, 0x04, 0x08, - 0x07, 0x10, 0x08, 0x2a, 0x45, 0x0a, 0x12, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x4d, 0x61, 0x74, - 0x63, 0x68, 0x69, 0x6e, 0x67, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a, 0x04, 0x46, 0x75, 0x6c, - 0x6c, 0x10, 0x00, 0x12, 0x0d, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, - 0x10, 0x01, 0x12, 0x0b, 0x0a, 0x07, 0x4b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x10, 0x02, 0x12, - 0x09, 0x0a, 0x05, 0x52, 0x65, 0x67, 0x65, 0x78, 0x10, 0x03, 0x2a, 0x42, 0x0a, 0x0d, 0x51, 0x75, - 0x65, 0x72, 0x79, 0x53, 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x12, 0x0a, 0x0a, 0x06, 0x55, - 0x53, 0x45, 0x5f, 0x49, 0x50, 0x10, 0x00, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x53, 0x45, 0x5f, 0x49, - 0x50, 0x34, 0x10, 0x01, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x53, 0x45, 0x5f, 0x49, 0x50, 0x36, 0x10, - 0x02, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x53, 0x45, 0x5f, 0x53, 0x59, 0x53, 0x10, 0x03, 0x42, 0x46, - 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, - 0x6e, 0x73, 0x50, 0x01, 0x5a, 0x21, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, - 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, - 0x61, 0x70, 0x70, 0x2f, 0x64, 0x6e, 0x73, 0xaa, 0x02, 0x0c, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x41, - 0x70, 0x70, 0x2e, 0x44, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x62, 0x6c, 0x65, 0x43, 0x61, 0x63, 0x68, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x65, 0x72, + 0x76, 0x65, 0x53, 0x74, 0x61, 0x6c, 0x65, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x73, + 0x65, 0x72, 0x76, 0x65, 0x53, 0x74, 0x61, 0x6c, 0x65, 0x12, 0x2d, 0x0a, 0x0f, 0x73, 0x65, 0x72, + 0x76, 0x65, 0x45, 0x78, 0x70, 0x69, 0x72, 0x65, 0x64, 0x54, 0x54, 0x4c, 0x18, 0x10, 0x20, 0x01, + 0x28, 0x0d, 0x48, 0x00, 0x52, 0x0f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x45, 0x78, 0x70, 0x69, 0x72, + 0x65, 0x64, 0x54, 0x54, 0x4c, 0x88, 0x01, 0x01, 0x12, 0x1e, 0x0a, 0x0a, 0x66, 0x69, 0x6e, 0x61, + 0x6c, 0x51, 0x75, 0x65, 0x72, 0x79, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x66, 0x69, + 0x6e, 0x61, 0x6c, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x41, 0x0a, 0x10, 0x75, 0x6e, 0x65, 0x78, + 0x70, 0x65, 0x63, 0x74, 0x65, 0x64, 0x5f, 0x67, 0x65, 0x6f, 0x69, 0x70, 0x18, 0x0d, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x72, 0x6f, + 0x75, 0x74, 0x65, 0x72, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x52, 0x0f, 0x75, 0x6e, 0x65, 0x78, + 0x70, 0x65, 0x63, 0x74, 0x65, 0x64, 0x47, 0x65, 0x6f, 0x69, 0x70, 0x12, 0x1e, 0x0a, 0x0a, 0x61, + 0x63, 0x74, 0x55, 0x6e, 0x70, 0x72, 0x69, 0x6f, 0x72, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x08, 0x52, + 0x0a, 0x61, 0x63, 0x74, 0x55, 0x6e, 0x70, 0x72, 0x69, 0x6f, 0x72, 0x1a, 0x5e, 0x0a, 0x0e, 0x50, + 0x72, 0x69, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x12, 0x34, 0x0a, + 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x20, 0x2e, 0x78, 0x72, + 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, 0x44, 0x6f, 0x6d, 0x61, 0x69, + 0x6e, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, + 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x1a, 0x36, 0x0a, 0x0c, 0x4f, + 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, 0x52, 0x75, 0x6c, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x72, + 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x72, 0x75, 0x6c, 0x65, 0x12, + 0x12, 0x0a, 0x04, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x73, + 0x69, 0x7a, 0x65, 0x42, 0x12, 0x0a, 0x10, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x45, 0x78, 0x70, + 0x69, 0x72, 0x65, 0x64, 0x54, 0x54, 0x4c, 0x22, 0xe6, 0x04, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, + 0x69, 0x67, 0x12, 0x39, 0x0a, 0x0b, 0x6e, 0x61, 0x6d, 0x65, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, + 0x72, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, + 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, + 0x72, 0x52, 0x0a, 0x6e, 0x61, 0x6d, 0x65, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x1b, 0x0a, + 0x09, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x69, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, + 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x70, 0x12, 0x43, 0x0a, 0x0c, 0x73, 0x74, + 0x61, 0x74, 0x69, 0x63, 0x5f, 0x68, 0x6f, 0x73, 0x74, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x20, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, + 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x4d, 0x61, 0x70, 0x70, 0x69, + 0x6e, 0x67, 0x52, 0x0b, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x48, 0x6f, 0x73, 0x74, 0x73, 0x12, + 0x10, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, + 0x67, 0x12, 0x22, 0x0a, 0x0c, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x43, 0x61, 0x63, 0x68, + 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, + 0x43, 0x61, 0x63, 0x68, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x65, 0x72, 0x76, 0x65, 0x53, 0x74, + 0x61, 0x6c, 0x65, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x73, 0x65, 0x72, 0x76, 0x65, + 0x53, 0x74, 0x61, 0x6c, 0x65, 0x12, 0x28, 0x0a, 0x0f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x45, 0x78, + 0x70, 0x69, 0x72, 0x65, 0x64, 0x54, 0x54, 0x4c, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0f, + 0x73, 0x65, 0x72, 0x76, 0x65, 0x45, 0x78, 0x70, 0x69, 0x72, 0x65, 0x64, 0x54, 0x54, 0x4c, 0x12, + 0x42, 0x0a, 0x0e, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5f, 0x73, 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, + 0x79, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1b, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, + 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x72, 0x61, + 0x74, 0x65, 0x67, 0x79, 0x52, 0x0d, 0x71, 0x75, 0x65, 0x72, 0x79, 0x53, 0x74, 0x72, 0x61, 0x74, + 0x65, 0x67, 0x79, 0x12, 0x28, 0x0a, 0x0f, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x61, + 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0f, 0x64, 0x69, + 0x73, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x12, 0x36, 0x0a, + 0x16, 0x64, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, + 0x49, 0x66, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x08, 0x52, 0x16, 0x64, + 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x61, 0x6c, 0x6c, 0x62, 0x61, 0x63, 0x6b, 0x49, 0x66, + 0x4d, 0x61, 0x74, 0x63, 0x68, 0x1a, 0x92, 0x01, 0x0a, 0x0b, 0x48, 0x6f, 0x73, 0x74, 0x4d, 0x61, + 0x70, 0x70, 0x69, 0x6e, 0x67, 0x12, 0x34, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x0e, 0x32, 0x20, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, + 0x6e, 0x73, 0x2e, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x69, 0x6e, + 0x67, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x64, + 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x6f, 0x6d, + 0x61, 0x69, 0x6e, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0c, 0x52, + 0x02, 0x69, 0x70, 0x12, 0x25, 0x0a, 0x0e, 0x70, 0x72, 0x6f, 0x78, 0x69, 0x65, 0x64, 0x5f, 0x64, + 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x70, 0x72, 0x6f, + 0x78, 0x69, 0x65, 0x64, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x4a, 0x04, 0x08, 0x07, 0x10, 0x08, + 0x2a, 0x45, 0x0a, 0x12, 0x44, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x69, + 0x6e, 0x67, 0x54, 0x79, 0x70, 0x65, 0x12, 0x08, 0x0a, 0x04, 0x46, 0x75, 0x6c, 0x6c, 0x10, 0x00, + 0x12, 0x0d, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x64, 0x6f, 0x6d, 0x61, 0x69, 0x6e, 0x10, 0x01, 0x12, + 0x0b, 0x0a, 0x07, 0x4b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, + 0x52, 0x65, 0x67, 0x65, 0x78, 0x10, 0x03, 0x2a, 0x42, 0x0a, 0x0d, 0x51, 0x75, 0x65, 0x72, 0x79, + 0x53, 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x12, 0x0a, 0x0a, 0x06, 0x55, 0x53, 0x45, 0x5f, + 0x49, 0x50, 0x10, 0x00, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x53, 0x45, 0x5f, 0x49, 0x50, 0x34, 0x10, + 0x01, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x53, 0x45, 0x5f, 0x49, 0x50, 0x36, 0x10, 0x02, 0x12, 0x0b, + 0x0a, 0x07, 0x55, 0x53, 0x45, 0x5f, 0x53, 0x59, 0x53, 0x10, 0x03, 0x42, 0x46, 0x0a, 0x10, 0x63, + 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x64, 0x6e, 0x73, 0x50, + 0x01, 0x5a, 0x21, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, + 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x61, 0x70, 0x70, + 0x2f, 0x64, 0x6e, 0x73, 0xaa, 0x02, 0x0c, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x41, 0x70, 0x70, 0x2e, + 0x44, 0x6e, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -718,6 +761,7 @@ func file_app_dns_config_proto_init() { if File_app_dns_config_proto != nil { return } + file_app_dns_config_proto_msgTypes[0].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/app/dns/config.proto b/app/dns/config.proto index 4317d0d7..a87725a3 100644 --- a/app/dns/config.proto +++ b/app/dns/config.proto @@ -32,6 +32,8 @@ message NameServer { string tag = 9; uint64 timeoutMs = 10; bool disableCache = 11; + bool serveStale = 15; + optional uint32 serveExpiredTTL = 16; bool finalQuery = 12; repeated xray.app.router.GeoIP unexpected_geoip = 13; bool actUnprior = 14; @@ -80,6 +82,8 @@ message Config { // DisableCache disables DNS cache bool disableCache = 8; + bool serveStale = 12; + uint32 serveExpiredTTL = 13; QueryStrategy query_strategy = 9; diff --git a/app/dns/dns.go b/app/dns/dns.go index 37183aba..c9bf2ecc 100644 --- a/app/dns/dns.go +++ b/app/dns/dns.go @@ -118,6 +118,11 @@ func New(ctx context.Context, config *Config) (*DNS, error) { } disableCache := config.DisableCache || ns.DisableCache + serveStale := config.ServeStale || ns.ServeStale + serveExpiredTTL := config.ServeExpiredTTL + if ns.ServeExpiredTTL != nil { + serveExpiredTTL = *ns.ServeExpiredTTL + } var tag = defaultTag if len(ns.Tag) > 0 { @@ -128,7 +133,7 @@ func New(ctx context.Context, config *Config) (*DNS, error) { return nil, errors.New("no QueryStrategy available for ", ns.Address) } - client, err := NewClient(ctx, ns, myClientIP, disableCache, tag, clientIPOption, &matcherInfos, updateDomain) + client, err := NewClient(ctx, ns, myClientIP, disableCache, serveStale, serveExpiredTTL, tag, clientIPOption, &matcherInfos, updateDomain) if err != nil { return nil, errors.New("failed to create client").Base(err) } diff --git a/app/dns/dnscommon.go b/app/dns/dnscommon.go index 15ac2efe..f80464d4 100644 --- a/app/dns/dnscommon.go +++ b/app/dns/dnscommon.go @@ -3,6 +3,7 @@ package dns import ( "context" "encoding/binary" + "math" "strings" "time" @@ -38,19 +39,14 @@ type IPRecord struct { RawHeader *dnsmessage.Header } -func (r *IPRecord) getIPs() ([]net.IP, uint32, error) { +func (r *IPRecord) getIPs() ([]net.IP, int32, error) { if r == nil { return nil, 0, errRecordNotFound } - untilExpire := time.Until(r.Expire).Seconds() - if untilExpire <= 0 { - return nil, 0, errRecordNotFound - } - ttl := uint32(untilExpire) + 1 - if ttl == 1 { - r.Expire = time.Now().Add(time.Second) // To ensure that two consecutive requests get the same result - } + untilExpire := time.Until(r.Expire).Seconds() + ttl := int32(math.Ceil(untilExpire)) + if r.RCode != dnsmessage.RCodeSuccess { return nil, ttl, dns_feature.RCodeError(r.RCode) } diff --git a/app/dns/nameserver.go b/app/dns/nameserver.go index cf1b665b..20236e79 100644 --- a/app/dns/nameserver.go +++ b/app/dns/nameserver.go @@ -41,7 +41,7 @@ type Client struct { } // NewServer creates a name server object according to the network destination url. -func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dispatcher, disableCache bool, clientIP net.IP) (Server, error) { +func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dispatcher, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) (Server, error) { if address := dest.Address; address.Family().IsDomain() { u, err := url.Parse(address.Domain()) if err != nil { @@ -51,19 +51,19 @@ func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dis case strings.EqualFold(u.String(), "localhost"): return NewLocalNameServer(), nil case strings.EqualFold(u.Scheme, "https"): // DNS-over-HTTPS Remote mode - return NewDoHNameServer(u, dispatcher, false, disableCache, clientIP), nil + return NewDoHNameServer(u, dispatcher, false, disableCache, serveStale, serveExpiredTTL, clientIP), nil case strings.EqualFold(u.Scheme, "h2c"): // DNS-over-HTTPS h2c Remote mode - return NewDoHNameServer(u, dispatcher, true, disableCache, clientIP), nil + return NewDoHNameServer(u, dispatcher, true, disableCache, serveStale, serveExpiredTTL, clientIP), nil case strings.EqualFold(u.Scheme, "https+local"): // DNS-over-HTTPS Local mode - return NewDoHNameServer(u, nil, false, disableCache, clientIP), nil + return NewDoHNameServer(u, nil, false, disableCache, serveStale, serveExpiredTTL, clientIP), nil case strings.EqualFold(u.Scheme, "h2c+local"): // DNS-over-HTTPS h2c Local mode - return NewDoHNameServer(u, nil, true, disableCache, clientIP), nil + return NewDoHNameServer(u, nil, true, disableCache, serveStale, serveExpiredTTL, clientIP), nil case strings.EqualFold(u.Scheme, "quic+local"): // DNS-over-QUIC Local mode - return NewQUICNameServer(u, disableCache, clientIP) + return NewQUICNameServer(u, disableCache, serveStale, serveExpiredTTL, clientIP) case strings.EqualFold(u.Scheme, "tcp"): // DNS-over-TCP Remote mode - return NewTCPNameServer(u, dispatcher, disableCache, clientIP) + return NewTCPNameServer(u, dispatcher, disableCache, serveStale, serveExpiredTTL, clientIP) case strings.EqualFold(u.Scheme, "tcp+local"): // DNS-over-TCP Local mode - return NewTCPLocalNameServer(u, disableCache, clientIP) + return NewTCPLocalNameServer(u, disableCache, serveStale, serveExpiredTTL, clientIP) case strings.EqualFold(u.String(), "fakedns"): var fd dns.FakeDNSEngine err = core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) { @@ -79,7 +79,7 @@ func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dis dest.Network = net.Network_UDP } if dest.Network == net.Network_UDP { // UDP classic DNS mode - return NewClassicNameServer(dest, dispatcher, disableCache, clientIP), nil + return NewClassicNameServer(dest, dispatcher, disableCache, serveStale, serveExpiredTTL, clientIP), nil } return nil, errors.New("No available name server could be created from ", dest).AtWarning() } @@ -89,7 +89,7 @@ func NewClient( ctx context.Context, ns *NameServer, clientIP net.IP, - disableCache bool, + disableCache bool, serveStale bool, serveExpiredTTL uint32, tag string, ipOption dns.IPOption, matcherInfos *[]*DomainMatcherInfo, @@ -99,7 +99,7 @@ func NewClient( err := core.RequireFeatures(ctx, func(dispatcher routing.Dispatcher) error { // Create a new server for each client for now - server, err := NewServer(ctx, ns.Address.AsDestination(), dispatcher, disableCache, clientIP) + server, err := NewServer(ctx, ns.Address.AsDestination(), dispatcher, disableCache, serveStale, serveExpiredTTL, clientIP) if err != nil { return errors.New("failed to create nameserver").Base(err).AtWarning() } diff --git a/app/dns/nameserver_doh.go b/app/dns/nameserver_doh.go index cba59423..c5ef6bbe 100644 --- a/app/dns/nameserver_doh.go +++ b/app/dns/nameserver_doh.go @@ -38,7 +38,7 @@ type DoHNameServer struct { } // NewDoHNameServer creates DOH/DOHL client object for remote/local resolving. -func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, h2c bool, disableCache bool, clientIP net.IP) *DoHNameServer { +func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, h2c bool, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) *DoHNameServer { url.Scheme = "https" mode := "DOH" if dispatcher == nil { @@ -46,7 +46,7 @@ func NewDoHNameServer(url *url.URL, dispatcher routing.Dispatcher, h2c bool, dis } errors.LogInfo(context.Background(), "DNS: created ", mode, " client for ", url.String(), ", with h2c ", h2c) s := &DoHNameServer{ - cacheController: NewCacheController(mode+"//"+url.Host, disableCache), + cacheController: NewCacheController(mode+"//"+url.Host, disableCache, serveStale, serveExpiredTTL), dohURL: url.String(), clientIP: clientIP, } @@ -126,7 +126,9 @@ func (s *DoHNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- er if s.Name()+"." == "DOH//"+domain { errors.LogError(ctx, s.Name(), " tries to resolve itself! Use IP or set \"hosts\" instead.") - noResponseErrCh <- errors.New("tries to resolve itself!", s.Name()) + if noResponseErrCh != nil { + noResponseErrCh <- errors.New("tries to resolve itself!", s.Name()) + } return } @@ -167,19 +169,25 @@ func (s *DoHNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- er b, err := dns.PackMessage(r.msg) if err != nil { errors.LogErrorInner(ctx, err, "failed to pack dns query for ", domain) - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } resp, err := s.dohHTTPSContext(dnsCtx, b.Bytes()) if err != nil { errors.LogErrorInner(ctx, err, "failed to retrieve response for ", domain) - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } rec, err := parseResponse(resp) if err != nil { errors.LogErrorInner(ctx, err, "failed to handle DOH response for ", domain) - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } s.cacheController.updateIP(r, rec) @@ -226,9 +234,16 @@ func (s *DoHNameServer) QueryIP(ctx context.Context, domain string, option dns_f } else { ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) if !go_errors.Is(err, errRecordNotFound) { - 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 + if ttl > 0 { + 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, uint32(ttl), err + } + if s.cacheController.serveStale && (s.cacheController.serveExpiredTTL == 0 || s.cacheController.serveExpiredTTL < ttl) { + errors.LogDebugInner(ctx, err, s.Name(), " cache OPTIMISTE ", domain, " -> ", ips) + go s.sendQuery(ctx, nil, fqdn, option) + return ips, 1, err + } } } @@ -259,6 +274,11 @@ func (s *DoHNameServer) QueryIP(ctx context.Context, domain string, option dns_f ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err}) - return ips, ttl, err - + var rTTL uint32 + if ttl <= 0 { + rTTL = 1 + } else { + rTTL = uint32(ttl) + } + return ips, rTTL, err } diff --git a/app/dns/nameserver_doh_test.go b/app/dns/nameserver_doh_test.go index 96412c22..52387661 100644 --- a/app/dns/nameserver_doh_test.go +++ b/app/dns/nameserver_doh_test.go @@ -17,7 +17,7 @@ func TestDOHNameServer(t *testing.T) { url, err := url.Parse("https+local://1.1.1.1/dns-query") common.Must(err) - s := NewDoHNameServer(url, nil, false, false, net.IP(nil)) + s := NewDoHNameServer(url, nil, false, false, false, 0, net.IP(nil)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ IPv4Enable: true, @@ -34,7 +34,7 @@ func TestDOHNameServerWithCache(t *testing.T) { url, err := url.Parse("https+local://1.1.1.1/dns-query") common.Must(err) - s := NewDoHNameServer(url, nil, false, false, net.IP(nil)) + s := NewDoHNameServer(url, nil, false, false, false, 0, net.IP(nil)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ IPv4Enable: true, @@ -62,7 +62,7 @@ func TestDOHNameServerWithIPv4Override(t *testing.T) { url, err := url.Parse("https+local://1.1.1.1/dns-query") common.Must(err) - s := NewDoHNameServer(url, nil, false, false, net.IP(nil)) + s := NewDoHNameServer(url, nil, false, false, false, 0, net.IP(nil)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ IPv4Enable: true, @@ -85,7 +85,7 @@ func TestDOHNameServerWithIPv6Override(t *testing.T) { url, err := url.Parse("https+local://1.1.1.1/dns-query") common.Must(err) - s := NewDoHNameServer(url, nil, false, false, net.IP(nil)) + s := NewDoHNameServer(url, nil, false, false, false, 0, net.IP(nil)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ IPv4Enable: false, diff --git a/app/dns/nameserver_quic.go b/app/dns/nameserver_quic.go index 31af657e..c37fba42 100644 --- a/app/dns/nameserver_quic.go +++ b/app/dns/nameserver_quic.go @@ -37,7 +37,7 @@ type QUICNameServer struct { } // NewQUICNameServer creates DNS-over-QUIC client object for local resolving -func NewQUICNameServer(url *url.URL, disableCache bool, clientIP net.IP) (*QUICNameServer, error) { +func NewQUICNameServer(url *url.URL, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) (*QUICNameServer, error) { errors.LogInfo(context.Background(), "DNS: created Local DNS-over-QUIC client for ", url.String()) var err error @@ -51,7 +51,7 @@ func NewQUICNameServer(url *url.URL, disableCache bool, clientIP net.IP) (*QUICN dest := net.UDPDestination(net.ParseAddress(url.Hostname()), port) s := &QUICNameServer{ - cacheController: NewCacheController(url.String(), disableCache), + cacheController: NewCacheController(url.String(), disableCache, serveStale, serveExpiredTTL), destination: &dest, clientIP: clientIP, } @@ -103,7 +103,9 @@ func (s *QUICNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- e b, err := dns.PackMessage(r.msg) if err != nil { errors.LogErrorInner(ctx, err, "failed to pack dns query") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } @@ -111,13 +113,17 @@ func (s *QUICNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- e err = binary.Write(dnsReqBuf, binary.BigEndian, uint16(b.Len())) if err != nil { errors.LogErrorInner(ctx, err, "binary write failed") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } _, err = dnsReqBuf.Write(b.Bytes()) if err != nil { errors.LogErrorInner(ctx, err, "buffer write failed") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } b.Release() @@ -125,14 +131,18 @@ func (s *QUICNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- e conn, err := s.openStream(dnsCtx) if err != nil { errors.LogErrorInner(ctx, err, "failed to open quic connection") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } _, err = conn.Write(dnsReqBuf.Bytes()) if err != nil { errors.LogErrorInner(ctx, err, "failed to send query") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } @@ -143,28 +153,36 @@ func (s *QUICNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- e n, err := respBuf.ReadFullFrom(conn, 2) if err != nil && n == 0 { errors.LogErrorInner(ctx, err, "failed to read response length") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } var length int16 err = binary.Read(bytes.NewReader(respBuf.Bytes()), binary.BigEndian, &length) if err != nil { errors.LogErrorInner(ctx, err, "failed to parse response length") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } respBuf.Clear() n, err = respBuf.ReadFullFrom(conn, int32(length)) if err != nil && n == 0 { errors.LogErrorInner(ctx, err, "failed to read response length") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } rec, err := parseResponse(respBuf.Bytes()) if err != nil { errors.LogErrorInner(ctx, err, "failed to handle response") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } s.cacheController.updateIP(r, rec) @@ -183,9 +201,16 @@ func (s *QUICNameServer) QueryIP(ctx context.Context, domain string, option dns_ } else { ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) if !go_errors.Is(err, errRecordNotFound) { - 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 + if ttl > 0 { + 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, uint32(ttl), err + } + if s.cacheController.serveStale && (s.cacheController.serveExpiredTTL == 0 || s.cacheController.serveExpiredTTL < ttl) { + errors.LogDebugInner(ctx, err, s.Name(), " cache OPTIMISTE ", domain, " -> ", ips) + go s.sendQuery(ctx, nil, fqdn, option) + return ips, 1, err + } } } @@ -216,8 +241,13 @@ func (s *QUICNameServer) QueryIP(ctx context.Context, domain string, option dns_ ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err}) - return ips, ttl, err - + var rTTL uint32 + if ttl <= 0 { + rTTL = 1 + } else { + rTTL = uint32(ttl) + } + return ips, rTTL, err } func isActive(s *quic.Conn) bool { diff --git a/app/dns/nameserver_quic_test.go b/app/dns/nameserver_quic_test.go index fd11d2e6..83212dcb 100644 --- a/app/dns/nameserver_quic_test.go +++ b/app/dns/nameserver_quic_test.go @@ -16,7 +16,7 @@ import ( func TestQUICNameServer(t *testing.T) { url, err := url.Parse("quic://dns.adguard-dns.com") common.Must(err) - s, err := NewQUICNameServer(url, false, net.IP(nil)) + s, err := NewQUICNameServer(url, false, false, 0, net.IP(nil)) common.Must(err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) ips, _, err := s.QueryIP(ctx, "google.com", dns.IPOption{ @@ -43,7 +43,7 @@ func TestQUICNameServer(t *testing.T) { func TestQUICNameServerWithIPv4Override(t *testing.T) { url, err := url.Parse("quic://dns.adguard-dns.com") common.Must(err) - s, err := NewQUICNameServer(url, false, net.IP(nil)) + s, err := NewQUICNameServer(url, false, false, 0, net.IP(nil)) common.Must(err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) ips, _, err := s.QueryIP(ctx, "google.com", dns.IPOption{ @@ -66,7 +66,7 @@ func TestQUICNameServerWithIPv4Override(t *testing.T) { func TestQUICNameServerWithIPv6Override(t *testing.T) { url, err := url.Parse("quic://dns.adguard-dns.com") common.Must(err) - s, err := NewQUICNameServer(url, false, net.IP(nil)) + s, err := NewQUICNameServer(url, false, false, 0, net.IP(nil)) common.Must(err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) ips, _, err := s.QueryIP(ctx, "google.com", dns.IPOption{ diff --git a/app/dns/nameserver_tcp.go b/app/dns/nameserver_tcp.go index 1937c25c..129d1b26 100644 --- a/app/dns/nameserver_tcp.go +++ b/app/dns/nameserver_tcp.go @@ -34,10 +34,10 @@ type TCPNameServer struct { func NewTCPNameServer( url *url.URL, dispatcher routing.Dispatcher, - disableCache bool, + disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP, ) (*TCPNameServer, error) { - s, err := baseTCPNameServer(url, "TCP", disableCache, clientIP) + s, err := baseTCPNameServer(url, "TCP", disableCache, serveStale, serveExpiredTTL, clientIP) if err != nil { return nil, err } @@ -58,8 +58,8 @@ func NewTCPNameServer( } // NewTCPLocalNameServer creates DNS over TCP client object for local resolving -func NewTCPLocalNameServer(url *url.URL, disableCache bool, clientIP net.IP) (*TCPNameServer, error) { - s, err := baseTCPNameServer(url, "TCPL", disableCache, clientIP) +func NewTCPLocalNameServer(url *url.URL, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) (*TCPNameServer, error) { + s, err := baseTCPNameServer(url, "TCPL", disableCache, serveStale, serveExpiredTTL, clientIP) if err != nil { return nil, err } @@ -71,7 +71,7 @@ func NewTCPLocalNameServer(url *url.URL, disableCache bool, clientIP net.IP) (*T return s, nil } -func baseTCPNameServer(url *url.URL, prefix string, disableCache bool, clientIP net.IP) (*TCPNameServer, error) { +func baseTCPNameServer(url *url.URL, prefix string, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) (*TCPNameServer, error) { port := net.Port(53) if url.Port() != "" { var err error @@ -82,7 +82,7 @@ func baseTCPNameServer(url *url.URL, prefix string, disableCache bool, clientIP dest := net.TCPDestination(net.ParseAddress(url.Hostname()), port) s := &TCPNameServer{ - cacheController: NewCacheController(prefix+"//"+dest.NetAddr(), disableCache), + cacheController: NewCacheController(prefix+"//"+dest.NetAddr(), disableCache, serveStale, serveExpiredTTL), destination: &dest, clientIP: clientIP, } @@ -131,14 +131,18 @@ func (s *TCPNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- er b, err := dns.PackMessage(r.msg) if err != nil { errors.LogErrorInner(ctx, err, "failed to pack dns query") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } conn, err := s.dial(dnsCtx) if err != nil { errors.LogErrorInner(ctx, err, "failed to dial namesever") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } defer conn.Close() @@ -146,13 +150,17 @@ func (s *TCPNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- er err = binary.Write(dnsReqBuf, binary.BigEndian, uint16(b.Len())) if err != nil { errors.LogErrorInner(ctx, err, "binary write failed") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } _, err = dnsReqBuf.Write(b.Bytes()) if err != nil { errors.LogErrorInner(ctx, err, "buffer write failed") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } b.Release() @@ -160,7 +168,9 @@ func (s *TCPNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- er _, err = conn.Write(dnsReqBuf.Bytes()) if err != nil { errors.LogErrorInner(ctx, err, "failed to send query") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } dnsReqBuf.Release() @@ -170,28 +180,36 @@ func (s *TCPNameServer) sendQuery(ctx context.Context, noResponseErrCh chan<- er n, err := respBuf.ReadFullFrom(conn, 2) if err != nil && n == 0 { errors.LogErrorInner(ctx, err, "failed to read response length") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } var length int16 err = binary.Read(bytes.NewReader(respBuf.Bytes()), binary.BigEndian, &length) if err != nil { errors.LogErrorInner(ctx, err, "failed to parse response length") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } respBuf.Clear() n, err = respBuf.ReadFullFrom(conn, int32(length)) if err != nil && n == 0 { errors.LogErrorInner(ctx, err, "failed to read response length") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } rec, err := parseResponse(respBuf.Bytes()) if err != nil { errors.LogErrorInner(ctx, err, "failed to parse DNS over TCP response") - noResponseErrCh <- err + if noResponseErrCh != nil { + noResponseErrCh <- err + } return } @@ -211,9 +229,16 @@ func (s *TCPNameServer) QueryIP(ctx context.Context, domain string, option dns_f } else { ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) if !go_errors.Is(err, errRecordNotFound) { - 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 + if ttl > 0 { + 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, uint32(ttl), err + } + if s.cacheController.serveStale && (s.cacheController.serveExpiredTTL == 0 || s.cacheController.serveExpiredTTL < ttl) { + errors.LogDebugInner(ctx, err, s.Name(), " cache OPTIMISTE ", domain, " -> ", ips) + go s.sendQuery(ctx, nil, fqdn, option) + return ips, 1, err + } } } @@ -244,6 +269,11 @@ func (s *TCPNameServer) QueryIP(ctx context.Context, domain string, option dns_f ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err}) - return ips, ttl, err - + var rTTL uint32 + if ttl <= 0 { + rTTL = 1 + } else { + rTTL = uint32(ttl) + } + return ips, rTTL, err } diff --git a/app/dns/nameserver_tcp_test.go b/app/dns/nameserver_tcp_test.go index 074896ca..048e9477 100644 --- a/app/dns/nameserver_tcp_test.go +++ b/app/dns/nameserver_tcp_test.go @@ -16,7 +16,7 @@ import ( func TestTCPLocalNameServer(t *testing.T) { url, err := url.Parse("tcp+local://8.8.8.8") common.Must(err) - s, err := NewTCPLocalNameServer(url, false, net.IP(nil)) + s, err := NewTCPLocalNameServer(url, false, false, 0, net.IP(nil)) common.Must(err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ @@ -33,7 +33,7 @@ func TestTCPLocalNameServer(t *testing.T) { func TestTCPLocalNameServerWithCache(t *testing.T) { url, err := url.Parse("tcp+local://8.8.8.8") common.Must(err) - s, err := NewTCPLocalNameServer(url, false, net.IP(nil)) + s, err := NewTCPLocalNameServer(url, false, false, 0, net.IP(nil)) common.Must(err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ @@ -61,7 +61,7 @@ func TestTCPLocalNameServerWithCache(t *testing.T) { func TestTCPLocalNameServerWithIPv4Override(t *testing.T) { url, err := url.Parse("tcp+local://8.8.8.8") common.Must(err) - s, err := NewTCPLocalNameServer(url, false, net.IP(nil)) + s, err := NewTCPLocalNameServer(url, false, false, 0, net.IP(nil)) common.Must(err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ @@ -85,7 +85,7 @@ func TestTCPLocalNameServerWithIPv4Override(t *testing.T) { func TestTCPLocalNameServerWithIPv6Override(t *testing.T) { url, err := url.Parse("tcp+local://8.8.8.8") common.Must(err) - s, err := NewTCPLocalNameServer(url, false, net.IP(nil)) + s, err := NewTCPLocalNameServer(url, false, false, 0, net.IP(nil)) common.Must(err) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ips, _, err := s.QueryIP(ctx, "google.com", dns_feature.IPOption{ diff --git a/app/dns/nameserver_udp.go b/app/dns/nameserver_udp.go index e29f6e24..161e9b2d 100644 --- a/app/dns/nameserver_udp.go +++ b/app/dns/nameserver_udp.go @@ -39,14 +39,14 @@ type udpDnsRequest struct { } // NewClassicNameServer creates udp server object for remote resolving. -func NewClassicNameServer(address net.Destination, dispatcher routing.Dispatcher, disableCache bool, clientIP net.IP) *ClassicNameServer { +func NewClassicNameServer(address net.Destination, dispatcher routing.Dispatcher, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) *ClassicNameServer { // default to 53 if unspecific if address.Port == 0 { address.Port = net.Port(53) } s := &ClassicNameServer{ - cacheController: NewCacheController(strings.ToUpper(address.String()), disableCache), + cacheController: NewCacheController(strings.ToUpper(address.String()), disableCache, serveStale, serveExpiredTTL), address: &address, requests: make(map[uint16]*udpDnsRequest), clientIP: clientIP, @@ -179,9 +179,16 @@ func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string, option d } else { ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) if !go_errors.Is(err, errRecordNotFound) { - 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 + if ttl > 0 { + 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, uint32(ttl), err + } + if s.cacheController.serveStale && (s.cacheController.serveExpiredTTL == 0 || s.cacheController.serveExpiredTTL < ttl) { + errors.LogDebugInner(ctx, err, s.Name(), " cache OPTIMISTE ", domain, " -> ", ips) + go s.sendQuery(ctx, nil, fqdn, option) + return ips, 1, err + } } } @@ -212,6 +219,11 @@ func (s *ClassicNameServer) QueryIP(ctx context.Context, domain string, option d ips, ttl, err := s.cacheController.findIPsForDomain(fqdn, option) log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err}) - return ips, ttl, err - + var rTTL uint32 + if ttl <= 0 { + rTTL = 1 + } else { + rTTL = uint32(ttl) + } + return ips, rTTL, err } diff --git a/infra/conf/dns.go b/infra/conf/dns.go index 8a60bc00..282132fb 100644 --- a/infra/conf/dns.go +++ b/infra/conf/dns.go @@ -16,19 +16,21 @@ import ( ) type NameServerConfig struct { - Address *Address `json:"address"` - ClientIP *Address `json:"clientIp"` - Port uint16 `json:"port"` - SkipFallback bool `json:"skipFallback"` - Domains []string `json:"domains"` - ExpectedIPs StringList `json:"expectedIPs"` - ExpectIPs StringList `json:"expectIPs"` - QueryStrategy string `json:"queryStrategy"` - Tag string `json:"tag"` - TimeoutMs uint64 `json:"timeoutMs"` - DisableCache bool `json:"disableCache"` - FinalQuery bool `json:"finalQuery"` - UnexpectedIPs StringList `json:"unexpectedIPs"` + Address *Address `json:"address"` + ClientIP *Address `json:"clientIp"` + Port uint16 `json:"port"` + SkipFallback bool `json:"skipFallback"` + Domains []string `json:"domains"` + ExpectedIPs StringList `json:"expectedIPs"` + ExpectIPs StringList `json:"expectIPs"` + QueryStrategy string `json:"queryStrategy"` + Tag string `json:"tag"` + TimeoutMs uint64 `json:"timeoutMs"` + DisableCache bool `json:"disableCache"` + ServeStale bool `json:"serveStale"` + ServeExpiredTTL uint32 `json:"serveExpiredTTL"` + FinalQuery bool `json:"finalQuery"` + UnexpectedIPs StringList `json:"unexpectedIPs"` } // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON @@ -40,19 +42,21 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error { } var advanced struct { - Address *Address `json:"address"` - ClientIP *Address `json:"clientIp"` - Port uint16 `json:"port"` - SkipFallback bool `json:"skipFallback"` - Domains []string `json:"domains"` - ExpectedIPs StringList `json:"expectedIPs"` - ExpectIPs StringList `json:"expectIPs"` - QueryStrategy string `json:"queryStrategy"` - Tag string `json:"tag"` - TimeoutMs uint64 `json:"timeoutMs"` - DisableCache bool `json:"disableCache"` - FinalQuery bool `json:"finalQuery"` - UnexpectedIPs StringList `json:"unexpectedIPs"` + Address *Address `json:"address"` + ClientIP *Address `json:"clientIp"` + Port uint16 `json:"port"` + SkipFallback bool `json:"skipFallback"` + Domains []string `json:"domains"` + ExpectedIPs StringList `json:"expectedIPs"` + ExpectIPs StringList `json:"expectIPs"` + QueryStrategy string `json:"queryStrategy"` + Tag string `json:"tag"` + TimeoutMs uint64 `json:"timeoutMs"` + DisableCache bool `json:"disableCache"` + ServeStale bool `json:"serveStale"` + ServeExpiredTTL uint32 `json:"serveExpiredTTL"` + FinalQuery bool `json:"finalQuery"` + UnexpectedIPs StringList `json:"unexpectedIPs"` } if err := json.Unmarshal(data, &advanced); err == nil { c.Address = advanced.Address @@ -66,6 +70,8 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error { c.Tag = advanced.Tag c.TimeoutMs = advanced.TimeoutMs c.DisableCache = advanced.DisableCache + c.ServeStale = advanced.ServeStale + c.ServeExpiredTTL = advanced.ServeExpiredTTL c.FinalQuery = advanced.FinalQuery c.UnexpectedIPs = advanced.UnexpectedIPs return nil @@ -173,6 +179,8 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) { Tag: c.Tag, TimeoutMs: c.TimeoutMs, DisableCache: c.DisableCache, + ServeStale: c.ServeStale, + ServeExpiredTTL: &c.ServeExpiredTTL, FinalQuery: c.FinalQuery, UnexpectedGeoip: unexpectedGeoipList, ActUnprior: actUnprior, @@ -194,6 +202,8 @@ type DNSConfig struct { Tag string `json:"tag"` QueryStrategy string `json:"queryStrategy"` DisableCache bool `json:"disableCache"` + ServeStale bool `json:"serveStale"` + ServeExpiredTTL uint32 `json:"serveExpiredTTL"` DisableFallback bool `json:"disableFallback"` DisableFallbackIfMatch bool `json:"disableFallbackIfMatch"` UseSystemHosts bool `json:"useSystemHosts"` @@ -391,6 +401,8 @@ func (c *DNSConfig) Build() (*dns.Config, error) { config := &dns.Config{ Tag: c.Tag, DisableCache: c.DisableCache, + ServeStale: c.ServeStale, + ServeExpiredTTL: c.ServeExpiredTTL, DisableFallback: c.DisableFallback, DisableFallbackIfMatch: c.DisableFallbackIfMatch, QueryStrategy: resolveQueryStrategy(c.QueryStrategy),