dns.go 3.1 KB

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