nameserver.go 828 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
  13. }
  14. type localNameServer struct {
  15. client *localdns.Client
  16. }
  17. func (s *localNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
  18. if option.IPv4Enable && option.IPv6Enable {
  19. return s.client.LookupIP(domain)
  20. }
  21. if option.IPv4Enable {
  22. return s.client.LookupIPv4(domain)
  23. }
  24. if option.IPv6Enable {
  25. return s.client.LookupIPv6(domain)
  26. }
  27. return nil, newError("neither IPv4 nor IPv6 is enabled")
  28. }
  29. func NewLocalNameServer() *localNameServer {
  30. return &localNameServer{
  31. client: localdns.New(),
  32. }
  33. }