2022-08-03 10:55:39 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/netip"
|
|
|
|
"strings"
|
2023-03-23 11:08:48 +00:00
|
|
|
"time"
|
2022-08-03 10:55:39 +00:00
|
|
|
|
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"
|
2023-03-23 11:08:48 +00:00
|
|
|
"github.com/sagernet/sing/common/cache"
|
2022-08-03 10:55:39 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
F "github.com/sagernet/sing/common/format"
|
2023-03-23 11:08:48 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-08-03 10:55:39 +00:00
|
|
|
|
2022-09-13 08:18:39 +00:00
|
|
|
mDNS "github.com/miekg/dns"
|
2022-08-03 10:55:39 +00:00
|
|
|
)
|
|
|
|
|
2023-03-23 11:08:48 +00:00
|
|
|
type DNSReverseMapping struct {
|
|
|
|
cache *cache.LruCache[netip.Addr, string]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDNSReverseMapping() *DNSReverseMapping {
|
|
|
|
return &DNSReverseMapping{
|
|
|
|
cache: cache.New[netip.Addr, string](),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DNSReverseMapping) Save(address netip.Addr, domain string, ttl int) {
|
|
|
|
m.cache.StoreWithExpire(address, domain, time.Now().Add(time.Duration(ttl)*time.Second))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DNSReverseMapping) Query(address netip.Addr) (string, bool) {
|
|
|
|
domain, loaded := m.cache.Load(address)
|
|
|
|
return domain, loaded
|
|
|
|
}
|
|
|
|
|
2022-08-22 04:01:50 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-11-19 14:39:30 +00:00
|
|
|
if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {
|
|
|
|
return ctx, r.defaultTransport, domainStrategy
|
|
|
|
} else {
|
|
|
|
return ctx, r.defaultTransport, r.defaultDomainStrategy
|
|
|
|
}
|
2022-08-22 04:01:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:18:39 +00:00
|
|
|
func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
|
|
|
if len(message.Question) > 0 {
|
|
|
|
r.dnsLogger.DebugContext(ctx, "exchange ", formatQuestion(message.Question[0].String()))
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
2022-08-16 15:46:05 +00:00
|
|
|
ctx, metadata := adapter.AppendContext(ctx)
|
2022-09-13 08:18:39 +00:00
|
|
|
if len(message.Question) > 0 {
|
2023-02-08 08:18:40 +00:00
|
|
|
metadata.QueryType = message.Question[0].Qtype
|
|
|
|
switch metadata.QueryType {
|
2022-09-13 08:18:39 +00:00
|
|
|
case mDNS.TypeA:
|
2022-08-16 15:46:05 +00:00
|
|
|
metadata.IPVersion = 4
|
2022-09-13 08:18:39 +00:00
|
|
|
case mDNS.TypeAAAA:
|
2022-08-16 15:46:05 +00:00
|
|
|
metadata.IPVersion = 6
|
|
|
|
}
|
2022-09-13 08:18:39 +00:00
|
|
|
metadata.Domain = fqdnToDomain(message.Question[0].Name)
|
2022-08-16 15:46:05 +00:00
|
|
|
}
|
2022-08-22 04:01:50 +00:00
|
|
|
ctx, transport, strategy := r.matchDNS(ctx)
|
2022-08-03 10:55:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
|
|
|
|
defer cancel()
|
2022-08-22 04:01:50 +00:00
|
|
|
response, err := r.dnsClient.Exchange(ctx, transport, message, strategy)
|
2022-09-13 08:18:39 +00:00
|
|
|
if err != nil && len(message.Question) > 0 {
|
|
|
|
r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", formatQuestion(message.Question[0].String())))
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
2022-09-13 08:18:39 +00:00
|
|
|
if len(message.Question) > 0 && response != nil {
|
|
|
|
LogDNSAnswers(r.dnsLogger, ctx, message.Question[0].Name, response.Answer)
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
2023-03-23 11:08:48 +00:00
|
|
|
if r.dnsReverseMapping != nil && len(message.Question) > 0 && response != nil && len(response.Answer) > 0 {
|
|
|
|
for _, answer := range response.Answer {
|
|
|
|
switch record := answer.(type) {
|
|
|
|
case *mDNS.A:
|
|
|
|
r.dnsReverseMapping.Save(M.AddrFromIP(record.A), fqdnToDomain(record.Hdr.Name), int(record.Hdr.Ttl))
|
|
|
|
case *mDNS.AAAA:
|
|
|
|
r.dnsReverseMapping.Save(M.AddrFromIP(record.AAAA), fqdnToDomain(record.Hdr.Name), int(record.Hdr.Ttl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 10:55:39 +00:00
|
|
|
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)
|
2022-09-04 04:39:43 +00:00
|
|
|
ctx, metadata := adapter.AppendContext(ctx)
|
|
|
|
metadata.Domain = domain
|
2022-08-22 04:01:50 +00:00
|
|
|
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-09-10 06:15:04 +00:00
|
|
|
if err == nil {
|
|
|
|
err = dns.RCodeNameError
|
|
|
|
}
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
|
|
|
return addrs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error) {
|
2022-08-22 04:01:50 +00:00
|
|
|
return r.Lookup(ctx, domain, dns.DomainStrategyAsIS)
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:18:39 +00:00
|
|
|
func LogDNSAnswers(logger log.ContextLogger, ctx context.Context, domain string, answers []mDNS.RR) {
|
|
|
|
for _, answer := range answers {
|
|
|
|
logger.InfoContext(ctx, "exchanged ", domain, " ", mDNS.Type(answer.Header().Rrtype).String(), " ", formatQuestion(answer.String()))
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:18:39 +00:00
|
|
|
func fqdnToDomain(fqdn string) string {
|
|
|
|
if mDNS.IsFqdn(fqdn) {
|
|
|
|
return fqdn[:len(fqdn)-1]
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
2022-09-13 08:18:39 +00:00
|
|
|
return fqdn
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:18:39 +00:00
|
|
|
func formatQuestion(string string) string {
|
|
|
|
if strings.HasPrefix(string, ";") {
|
|
|
|
string = string[1:]
|
|
|
|
}
|
|
|
|
string = strings.ReplaceAll(string, "\t", " ")
|
|
|
|
for strings.Contains(string, " ") {
|
|
|
|
string = strings.ReplaceAll(string, " ", " ")
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|
2022-09-13 08:18:39 +00:00
|
|
|
return string
|
2022-08-03 10:55:39 +00:00
|
|
|
}
|