diff --git a/dns/client.go b/dns/client.go index d44883b5..f456f878 100644 --- a/dns/client.go +++ b/dns/client.go @@ -537,7 +537,7 @@ func FixedResponse(id uint16, question dns.Question, addresses []netip.Addr, tim Question: []dns.Question{question}, } for _, address := range addresses { - if address.Is4() { + if address.Is4() && question.Qtype == dns.TypeA { response.Answer = append(response.Answer, &dns.A{ Hdr: dns.RR_Header{ Name: question.Name, @@ -547,7 +547,7 @@ func FixedResponse(id uint16, question dns.Question, addresses []netip.Addr, tim }, A: address.AsSlice(), }) - } else { + } else if address.Is6() && question.Qtype == dns.TypeAAAA { response.Answer = append(response.Answer, &dns.AAAA{ Hdr: dns.RR_Header{ Name: question.Name, diff --git a/dns/transport/hosts/hosts.go b/dns/transport/hosts/hosts.go index f13e85ae..0a1dd395 100644 --- a/dns/transport/hosts/hosts.go +++ b/dns/transport/hosts/hosts.go @@ -10,8 +10,6 @@ import ( "github.com/sagernet/sing-box/dns" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing/common/json/badjson" - "github.com/sagernet/sing/common/json/badoption" "github.com/sagernet/sing/service/filemanager" mDNS "github.com/miekg/dns" @@ -26,11 +24,14 @@ var _ adapter.DNSTransport = (*Transport)(nil) type Transport struct { dns.TransportAdapter files []*File - predefined badjson.TypedMap[string, badoption.Listable[netip.Addr]] + predefined map[string][]netip.Addr } func NewTransport(ctx context.Context, logger log.ContextLogger, tag string, options option.HostsDNSServerOptions) (adapter.DNSTransport, error) { - var files []*File + var ( + files []*File + predefined = make(map[string][]netip.Addr) + ) if len(options.Path) == 0 { files = append(files, NewFile(DefaultPath)) } else { @@ -38,10 +39,15 @@ func NewTransport(ctx context.Context, logger log.ContextLogger, tag string, opt files = append(files, NewFile(filemanager.BasePath(ctx, os.ExpandEnv(path)))) } } + if options.Predefined != nil { + for _, entry := range options.Predefined.Entries() { + predefined[mDNS.CanonicalName(entry.Key)] = entry.Value + } + } return &Transport{ TransportAdapter: dns.NewTransportAdapter(C.DNSTypeHosts, tag, nil), files: files, - predefined: options.Predefined, + predefined: predefined, }, nil } @@ -50,12 +56,11 @@ func (t *Transport) Reset() { func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) { question := message.Question[0] - domain := dns.FqdnToDomain(question.Name) + domain := mDNS.CanonicalName(question.Name) if question.Qtype == mDNS.TypeA || question.Qtype == mDNS.TypeAAAA { - if addresses, ok := t.predefined.Get(domain); ok { + if addresses, ok := t.predefined[domain]; ok { return dns.FixedResponse(message.Id, question, addresses, C.DefaultDNSTTL), nil } - for _, file := range t.files { addresses := file.Lookup(domain) if len(addresses) > 0 { diff --git a/dns/transport/hosts/hosts_file.go b/dns/transport/hosts/hosts_file.go index 84d7316c..ec384882 100644 --- a/dns/transport/hosts/hosts_file.go +++ b/dns/transport/hosts/hosts_file.go @@ -9,6 +9,8 @@ import ( "strings" "sync" "time" + + "github.com/miekg/dns" ) const cacheMaxAge = 5 * time.Second @@ -32,7 +34,7 @@ func (f *File) Lookup(name string) []netip.Addr { f.access.Lock() defer f.access.Unlock() f.update() - return f.byName[name] + return f.byName[dns.CanonicalName(name)] } func (f *File) update() { @@ -89,9 +91,8 @@ func (f *File) update() { continue } for index := 1; index < len(fields); index++ { - // canonicalName := dns.CanonicalName(fields[index]) - domain := fields[index] - byName[domain] = append(byName[domain], addr) + canonicalName := dns.CanonicalName(fields[index]) + byName[canonicalName] = append(byName[canonicalName], addr) } } f.expire = now.Add(cacheMaxAge) diff --git a/option/dns.go b/option/dns.go index 64dbea38..04eb442d 100644 --- a/option/dns.go +++ b/option/dns.go @@ -316,8 +316,8 @@ type LegacyDNSServerOptions struct { } type HostsDNSServerOptions struct { - Path badoption.Listable[string] `json:"path,omitempty"` - Predefined badjson.TypedMap[string, badoption.Listable[netip.Addr]] `json:"predefined,omitempty"` + Path badoption.Listable[string] `json:"path,omitempty"` + Predefined *badjson.TypedMap[string, badoption.Listable[netip.Addr]] `json:"predefined,omitempty"` } type LocalDNSServerOptions struct {