sing-box/route/router_dns.go

148 lines
4.6 KiB
Go
Raw Permalink Normal View History

2022-08-03 10:55:39 +00:00
package route
import (
"context"
"net/netip"
"strings"
2022-08-16 15:46:05 +00:00
"github.com/sagernet/sing-box/adapter"
2022-08-03 10:55:39 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-dns"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"golang.org/x/net/dns/dnsmessage"
)
func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport, dns.DomainStrategy) {
metadata := adapter.ContextFrom(ctx)
if metadata == nil {
panic("no context")
}
for i, rule := range r.dnsRules {
if rule.Match(metadata) {
if rule.DisableCache() {
ctx = dns.ContextWithDisableCache(ctx, true)
}
detour := rule.Outbound()
r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
if transport, loaded := r.transportMap[detour]; loaded {
if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
return ctx, transport, domainStrategy
} else {
return ctx, transport, r.defaultDomainStrategy
}
}
r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
}
}
return ctx, r.defaultTransport, r.defaultDomainStrategy
}
2022-08-03 10:55:39 +00:00
func (r *Router) Exchange(ctx context.Context, message *dnsmessage.Message) (*dnsmessage.Message, error) {
if len(message.Questions) > 0 {
r.dnsLogger.DebugContext(ctx, "exchange ", formatDNSQuestion(message.Questions[0]))
}
2022-08-16 15:46:05 +00:00
ctx, metadata := adapter.AppendContext(ctx)
if len(message.Questions) > 0 {
switch message.Questions[0].Type {
case dnsmessage.TypeA:
metadata.IPVersion = 4
case dnsmessage.TypeAAAA:
metadata.IPVersion = 6
}
}
ctx, transport, strategy := r.matchDNS(ctx)
2022-08-03 10:55:39 +00:00
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
defer cancel()
response, err := r.dnsClient.Exchange(ctx, transport, message, strategy)
2022-08-03 10:55:39 +00:00
if err != nil && len(message.Questions) > 0 {
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", message.Questions[0].Name.String()))
}
2022-08-05 08:55:46 +00:00
if len(message.Questions) > 0 && response != nil {
2022-08-03 10:55:39 +00:00
LogDNSAnswers(r.dnsLogger, ctx, message.Questions[0].Name.String(), response.Answers)
}
return response, err
}
func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
2022-08-03 13:51:34 +00:00
r.dnsLogger.DebugContext(ctx, "lookup domain ", domain)
ctx, transport, transportStrategy := r.matchDNS(ctx)
if strategy == dns.DomainStrategyAsIS {
strategy = transportStrategy
}
2022-08-03 10:55:39 +00:00
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
defer cancel()
addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy)
if len(addrs) > 0 {
2022-08-04 01:11:39 +00:00
r.dnsLogger.InfoContext(ctx, "lookup succeed for ", domain, ": ", strings.Join(F.MapToString(addrs), " "))
2022-08-03 10:55:39 +00:00
} else {
2022-08-04 01:11:39 +00:00
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain))
2022-08-03 10:55:39 +00:00
}
return addrs, err
}
func (r *Router) LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error) {
return r.Lookup(ctx, domain, dns.DomainStrategyAsIS)
2022-08-03 10:55:39 +00:00
}
func LogDNSAnswers(logger log.ContextLogger, ctx context.Context, domain string, answers []dnsmessage.Resource) {
for _, rawAnswer := range answers {
var content string
switch answer := rawAnswer.Body.(type) {
case *dnsmessage.AResource:
content = netip.AddrFrom4(answer.A).String()
case *dnsmessage.NSResource:
content = answer.NS.String()
case *dnsmessage.CNAMEResource:
content = answer.CNAME.String()
case *dnsmessage.SOAResource:
content = answer.MBox.String()
case *dnsmessage.PTRResource:
content = answer.PTR.String()
case *dnsmessage.MXResource:
content = answer.MX.String()
case *dnsmessage.TXTResource:
content = strings.Join(answer.TXT, " ")
case *dnsmessage.AAAAResource:
content = netip.AddrFrom16(answer.AAAA).String()
case *dnsmessage.SRVResource:
content = answer.Target.String()
case *dnsmessage.UnknownResource:
content = answer.Type.String()
default:
continue
}
rType := formatDNSType(rawAnswer.Header.Type)
if rType == "" {
logger.InfoContext(ctx, "exchanged ", domain, " ", rType)
} else {
logger.InfoContext(ctx, "exchanged ", domain, " ", rType, " ", content)
}
}
}
func formatDNSQuestion(question dnsmessage.Question) string {
var qType string
qType = question.Type.String()
if len(qType) > 4 {
qType = qType[4:]
}
var qClass string
qClass = question.Class.String()
if len(qClass) > 5 {
qClass = qClass[5:]
}
return string(question.Name.Data[:question.Name.Length-1]) + " " + qType + " " + qClass
}
func formatDNSType(qType dnsmessage.Type) string {
qTypeName := qType.String()
if len(qTypeName) > 4 {
return qTypeName[4:]
} else {
return F.ToString("unknown (type ", qTypeName, ")")
}
}