router_dns.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package route
  2. import (
  3. "context"
  4. "net/netip"
  5. "strings"
  6. "github.com/sagernet/sing-box/adapter"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-dns"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. F "github.com/sagernet/sing/common/format"
  12. mDNS "github.com/miekg/dns"
  13. )
  14. func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport, dns.DomainStrategy) {
  15. metadata := adapter.ContextFrom(ctx)
  16. if metadata == nil {
  17. panic("no context")
  18. }
  19. for i, rule := range r.dnsRules {
  20. if rule.Match(metadata) {
  21. if rule.DisableCache() {
  22. ctx = dns.ContextWithDisableCache(ctx, true)
  23. }
  24. detour := rule.Outbound()
  25. r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
  26. if transport, loaded := r.transportMap[detour]; loaded {
  27. if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
  28. return ctx, transport, domainStrategy
  29. } else {
  30. return ctx, transport, r.defaultDomainStrategy
  31. }
  32. }
  33. r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
  34. }
  35. }
  36. if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {
  37. return ctx, r.defaultTransport, domainStrategy
  38. } else {
  39. return ctx, r.defaultTransport, r.defaultDomainStrategy
  40. }
  41. }
  42. func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
  43. if len(message.Question) > 0 {
  44. r.dnsLogger.DebugContext(ctx, "exchange ", formatQuestion(message.Question[0].String()))
  45. }
  46. ctx, metadata := adapter.AppendContext(ctx)
  47. if len(message.Question) > 0 {
  48. metadata.QueryType = message.Question[0].Qtype
  49. switch metadata.QueryType {
  50. case mDNS.TypeA:
  51. metadata.IPVersion = 4
  52. case mDNS.TypeAAAA:
  53. metadata.IPVersion = 6
  54. }
  55. metadata.Domain = fqdnToDomain(message.Question[0].Name)
  56. }
  57. ctx, transport, strategy := r.matchDNS(ctx)
  58. ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
  59. defer cancel()
  60. response, err := r.dnsClient.Exchange(ctx, transport, message, strategy)
  61. if err != nil && len(message.Question) > 0 {
  62. r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", formatQuestion(message.Question[0].String())))
  63. }
  64. if len(message.Question) > 0 && response != nil {
  65. LogDNSAnswers(r.dnsLogger, ctx, message.Question[0].Name, response.Answer)
  66. }
  67. return response, err
  68. }
  69. func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
  70. r.dnsLogger.DebugContext(ctx, "lookup domain ", domain)
  71. ctx, metadata := adapter.AppendContext(ctx)
  72. metadata.Domain = domain
  73. ctx, transport, transportStrategy := r.matchDNS(ctx)
  74. if strategy == dns.DomainStrategyAsIS {
  75. strategy = transportStrategy
  76. }
  77. ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
  78. defer cancel()
  79. addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy)
  80. if len(addrs) > 0 {
  81. r.dnsLogger.InfoContext(ctx, "lookup succeed for ", domain, ": ", strings.Join(F.MapToString(addrs), " "))
  82. } else {
  83. r.dnsLogger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain))
  84. if err == nil {
  85. err = dns.RCodeNameError
  86. }
  87. }
  88. return addrs, err
  89. }
  90. func (r *Router) LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error) {
  91. return r.Lookup(ctx, domain, dns.DomainStrategyAsIS)
  92. }
  93. func LogDNSAnswers(logger log.ContextLogger, ctx context.Context, domain string, answers []mDNS.RR) {
  94. for _, answer := range answers {
  95. logger.InfoContext(ctx, "exchanged ", domain, " ", mDNS.Type(answer.Header().Rrtype).String(), " ", formatQuestion(answer.String()))
  96. }
  97. }
  98. func fqdnToDomain(fqdn string) string {
  99. if mDNS.IsFqdn(fqdn) {
  100. return fqdn[:len(fqdn)-1]
  101. }
  102. return fqdn
  103. }
  104. func formatQuestion(string string) string {
  105. if strings.HasPrefix(string, ";") {
  106. string = string[1:]
  107. }
  108. string = strings.ReplaceAll(string, "\t", " ")
  109. for strings.Contains(string, " ") {
  110. string = strings.ReplaceAll(string, " ", " ")
  111. }
  112. return string
  113. }