nameserver.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package dns
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common/net"
  5. "github.com/xtls/xray-core/features/dns/localdns"
  6. )
  7. // IPOption is an object for IP query options.
  8. type IPOption struct {
  9. IPv4Enable bool
  10. IPv6Enable bool
  11. }
  12. // Client is the interface for DNS client.
  13. type Client interface {
  14. // Name of the Client.
  15. Name() string
  16. // QueryIP sends IP queries to its configured server.
  17. QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
  18. }
  19. type LocalNameServer struct {
  20. client *localdns.Client
  21. }
  22. func (s *LocalNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
  23. if option.IPv4Enable && option.IPv6Enable {
  24. return s.client.LookupIP(domain)
  25. }
  26. if option.IPv4Enable {
  27. return s.client.LookupIPv4(domain)
  28. }
  29. if option.IPv6Enable {
  30. return s.client.LookupIPv6(domain)
  31. }
  32. return nil, newError("neither IPv4 nor IPv6 is enabled")
  33. }
  34. func (s *LocalNameServer) Name() string {
  35. return "localhost"
  36. }
  37. func NewLocalNameServer() *LocalNameServer {
  38. newError("DNS: created localhost client").AtInfo().WriteToLog()
  39. return &LocalNameServer{
  40. client: localdns.New(),
  41. }
  42. }