nameserver.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // Client is the interface for DNS client.
  14. type Client interface {
  15. // Name of the Client.
  16. Name() string
  17. // QueryIP sends IP queries to its configured server.
  18. QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
  19. }
  20. type localNameServer struct {
  21. client *localdns.Client
  22. }
  23. func (s *localNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
  24. if option.IPv4Enable && option.IPv6Enable {
  25. return s.client.LookupIP(domain)
  26. }
  27. if option.IPv4Enable {
  28. return s.client.LookupIPv4(domain)
  29. }
  30. if option.IPv6Enable {
  31. return s.client.LookupIPv6(domain)
  32. }
  33. return nil, newError("neither IPv4 nor IPv6 is enabled")
  34. }
  35. func (s *localNameServer) Name() string {
  36. return "localhost"
  37. }
  38. func NewLocalNameServer() *localNameServer {
  39. newError("DNS: created localhost client").AtInfo().WriteToLog()
  40. return &localNameServer{
  41. client: localdns.New(),
  42. }
  43. }