nameserver.go 963 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // +build !confonly
  2. package dns
  3. import (
  4. "context"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/features/dns/localdns"
  7. )
  8. // IPOption is an object for IP query options.
  9. type IPOption struct {
  10. IPv4Enable bool
  11. IPv6Enable bool
  12. }
  13. type Client interface {
  14. Name() string
  15. QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
  16. }
  17. type localNameServer struct {
  18. client *localdns.Client
  19. }
  20. func (s *localNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
  21. if option.IPv4Enable && option.IPv6Enable {
  22. return s.client.LookupIP(domain)
  23. }
  24. if option.IPv4Enable {
  25. return s.client.LookupIPv4(domain)
  26. }
  27. if option.IPv6Enable {
  28. return s.client.LookupIPv6(domain)
  29. }
  30. return nil, newError("neither IPv4 nor IPv6 is enabled")
  31. }
  32. func (s *localNameServer) Name() string {
  33. return "localhost"
  34. }
  35. func NewLocalNameServer() *localNameServer {
  36. return &localNameServer{
  37. client: localdns.New(),
  38. }
  39. }