mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-11 21:04:13 +08:00
fixes
This commit is contained in:
parent
c7ba57b6a4
commit
93020193f6
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user