nameserver.go 908 B

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