nameserver.go 942 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dns
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  5. "v2ray.com/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. type Client interface {
  13. Name() string
  14. QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
  15. }
  16. type localNameServer struct {
  17. client *localdns.Client
  18. }
  19. func (s *localNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
  20. if option.IPv4Enable && option.IPv6Enable {
  21. return s.client.LookupIP(domain)
  22. }
  23. if option.IPv4Enable {
  24. return s.client.LookupIPv4(domain)
  25. }
  26. if option.IPv6Enable {
  27. return s.client.LookupIPv6(domain)
  28. }
  29. return nil, newError("neither IPv4 nor IPv6 is enabled")
  30. }
  31. func (s *localNameServer) Name() string {
  32. return "localhost"
  33. }
  34. func NewLocalNameServer() *localNameServer {
  35. return &localNameServer{
  36. client: localdns.New(),
  37. }
  38. }