dns.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package adapter
  2. import (
  3. "context"
  4. "net/netip"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/log"
  7. "github.com/sagernet/sing-box/option"
  8. E "github.com/sagernet/sing/common/exceptions"
  9. "github.com/sagernet/sing/common/logger"
  10. "github.com/sagernet/sing/service"
  11. "github.com/miekg/dns"
  12. )
  13. type DNSRouter interface {
  14. Lifecycle
  15. Exchange(ctx context.Context, message *dns.Msg, options DNSQueryOptions) (*dns.Msg, error)
  16. Lookup(ctx context.Context, domain string, options DNSQueryOptions) ([]netip.Addr, error)
  17. ClearCache()
  18. LookupReverseMapping(ip netip.Addr) (string, bool)
  19. ResetNetwork()
  20. }
  21. type DNSClient interface {
  22. Start()
  23. Exchange(ctx context.Context, transport DNSTransport, message *dns.Msg, options DNSQueryOptions, responseChecker func(responseAddrs []netip.Addr) bool) (*dns.Msg, error)
  24. Lookup(ctx context.Context, transport DNSTransport, domain string, options DNSQueryOptions, responseChecker func(responseAddrs []netip.Addr) bool) ([]netip.Addr, error)
  25. LookupCache(domain string, strategy C.DomainStrategy) ([]netip.Addr, bool)
  26. ExchangeCache(ctx context.Context, message *dns.Msg) (*dns.Msg, bool)
  27. ClearCache()
  28. }
  29. type DNSQueryOptions struct {
  30. Transport DNSTransport
  31. Strategy C.DomainStrategy
  32. LookupStrategy C.DomainStrategy
  33. DisableCache bool
  34. RewriteTTL *uint32
  35. ClientSubnet netip.Prefix
  36. }
  37. func DNSQueryOptionsFrom(ctx context.Context, options *option.DomainResolveOptions) (*DNSQueryOptions, error) {
  38. if options == nil {
  39. return &DNSQueryOptions{}, nil
  40. }
  41. transportManager := service.FromContext[DNSTransportManager](ctx)
  42. transport, loaded := transportManager.Transport(options.Server)
  43. if !loaded {
  44. return nil, E.New("domain resolver not found: " + options.Server)
  45. }
  46. return &DNSQueryOptions{
  47. Transport: transport,
  48. Strategy: C.DomainStrategy(options.Strategy),
  49. DisableCache: options.DisableCache,
  50. RewriteTTL: options.RewriteTTL,
  51. ClientSubnet: options.ClientSubnet.Build(netip.Prefix{}),
  52. }, nil
  53. }
  54. type RDRCStore interface {
  55. LoadRDRC(transportName string, qName string, qType uint16) (rejected bool)
  56. SaveRDRC(transportName string, qName string, qType uint16) error
  57. SaveRDRCAsync(transportName string, qName string, qType uint16, logger logger.Logger)
  58. }
  59. type DNSTransport interface {
  60. Lifecycle
  61. Type() string
  62. Tag() string
  63. Dependencies() []string
  64. Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error)
  65. }
  66. type LegacyDNSTransport interface {
  67. LegacyStrategy() C.DomainStrategy
  68. LegacyClientSubnet() netip.Prefix
  69. }
  70. type DNSTransportRegistry interface {
  71. option.DNSTransportOptionsRegistry
  72. CreateDNSTransport(ctx context.Context, logger log.ContextLogger, tag string, transportType string, options any) (DNSTransport, error)
  73. }
  74. type DNSTransportManager interface {
  75. Lifecycle
  76. Transports() []DNSTransport
  77. Transport(tag string) (DNSTransport, bool)
  78. Default() DNSTransport
  79. FakeIP() FakeIPTransport
  80. Remove(tag string) error
  81. Create(ctx context.Context, logger log.ContextLogger, tag string, outboundType string, options any) error
  82. }