mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54: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},
|
Question: []dns.Question{question},
|
||||||
}
|
}
|
||||||
for _, address := range addresses {
|
for _, address := range addresses {
|
||||||
if address.Is4() {
|
if address.Is4() && question.Qtype == dns.TypeA {
|
||||||
response.Answer = append(response.Answer, &dns.A{
|
response.Answer = append(response.Answer, &dns.A{
|
||||||
Hdr: dns.RR_Header{
|
Hdr: dns.RR_Header{
|
||||||
Name: question.Name,
|
Name: question.Name,
|
||||||
@ -547,7 +547,7 @@ func FixedResponse(id uint16, question dns.Question, addresses []netip.Addr, tim
|
|||||||
},
|
},
|
||||||
A: address.AsSlice(),
|
A: address.AsSlice(),
|
||||||
})
|
})
|
||||||
} else {
|
} else if address.Is6() && question.Qtype == dns.TypeAAAA {
|
||||||
response.Answer = append(response.Answer, &dns.AAAA{
|
response.Answer = append(response.Answer, &dns.AAAA{
|
||||||
Hdr: dns.RR_Header{
|
Hdr: dns.RR_Header{
|
||||||
Name: question.Name,
|
Name: question.Name,
|
||||||
|
@ -10,8 +10,6 @@ import (
|
|||||||
"github.com/sagernet/sing-box/dns"
|
"github.com/sagernet/sing-box/dns"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
"github.com/sagernet/sing-box/option"
|
"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"
|
"github.com/sagernet/sing/service/filemanager"
|
||||||
|
|
||||||
mDNS "github.com/miekg/dns"
|
mDNS "github.com/miekg/dns"
|
||||||
@ -26,11 +24,14 @@ var _ adapter.DNSTransport = (*Transport)(nil)
|
|||||||
type Transport struct {
|
type Transport struct {
|
||||||
dns.TransportAdapter
|
dns.TransportAdapter
|
||||||
files []*File
|
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) {
|
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 {
|
if len(options.Path) == 0 {
|
||||||
files = append(files, NewFile(DefaultPath))
|
files = append(files, NewFile(DefaultPath))
|
||||||
} else {
|
} 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))))
|
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{
|
return &Transport{
|
||||||
TransportAdapter: dns.NewTransportAdapter(C.DNSTypeHosts, tag, nil),
|
TransportAdapter: dns.NewTransportAdapter(C.DNSTypeHosts, tag, nil),
|
||||||
files: files,
|
files: files,
|
||||||
predefined: options.Predefined,
|
predefined: predefined,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,12 +56,11 @@ func (t *Transport) Reset() {
|
|||||||
|
|
||||||
func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
||||||
question := message.Question[0]
|
question := message.Question[0]
|
||||||
domain := dns.FqdnToDomain(question.Name)
|
domain := mDNS.CanonicalName(question.Name)
|
||||||
if question.Qtype == mDNS.TypeA || question.Qtype == mDNS.TypeAAAA {
|
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
|
return dns.FixedResponse(message.Id, question, addresses, C.DefaultDNSTTL), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range t.files {
|
for _, file := range t.files {
|
||||||
addresses := file.Lookup(domain)
|
addresses := file.Lookup(domain)
|
||||||
if len(addresses) > 0 {
|
if len(addresses) > 0 {
|
||||||
|
@ -9,6 +9,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
const cacheMaxAge = 5 * time.Second
|
const cacheMaxAge = 5 * time.Second
|
||||||
@ -32,7 +34,7 @@ func (f *File) Lookup(name string) []netip.Addr {
|
|||||||
f.access.Lock()
|
f.access.Lock()
|
||||||
defer f.access.Unlock()
|
defer f.access.Unlock()
|
||||||
f.update()
|
f.update()
|
||||||
return f.byName[name]
|
return f.byName[dns.CanonicalName(name)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) update() {
|
func (f *File) update() {
|
||||||
@ -89,9 +91,8 @@ func (f *File) update() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for index := 1; index < len(fields); index++ {
|
for index := 1; index < len(fields); index++ {
|
||||||
// canonicalName := dns.CanonicalName(fields[index])
|
canonicalName := dns.CanonicalName(fields[index])
|
||||||
domain := fields[index]
|
byName[canonicalName] = append(byName[canonicalName], addr)
|
||||||
byName[domain] = append(byName[domain], addr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.expire = now.Add(cacheMaxAge)
|
f.expire = now.Add(cacheMaxAge)
|
||||||
|
@ -316,8 +316,8 @@ type LegacyDNSServerOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type HostsDNSServerOptions struct {
|
type HostsDNSServerOptions struct {
|
||||||
Path badoption.Listable[string] `json:"path,omitempty"`
|
Path badoption.Listable[string] `json:"path,omitempty"`
|
||||||
Predefined badjson.TypedMap[string, badoption.Listable[netip.Addr]] `json:"predefined,omitempty"`
|
Predefined *badjson.TypedMap[string, badoption.Listable[netip.Addr]] `json:"predefined,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LocalDNSServerOptions struct {
|
type LocalDNSServerOptions struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user