nameserver_local.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // +build !confonly
  2. package dns
  3. import (
  4. "context"
  5. "github.com/v2fly/v2ray-core/v4/common/net"
  6. "github.com/v2fly/v2ray-core/v4/features/dns/localdns"
  7. )
  8. // LocalNameServer is an wrapper over local DNS feature.
  9. type LocalNameServer struct {
  10. client *localdns.Client
  11. }
  12. // QueryIP implements Server.
  13. func (s *LocalNameServer) QueryIP(ctx context.Context, domain string, clientIP net.IP, option IPOption) ([]net.IP, error) {
  14. if option.IPv4Enable && option.IPv6Enable {
  15. return s.client.LookupIP(domain)
  16. }
  17. if option.IPv4Enable {
  18. return s.client.LookupIPv4(domain)
  19. }
  20. if option.IPv6Enable {
  21. return s.client.LookupIPv6(domain)
  22. }
  23. return nil, newError("neither IPv4 nor IPv6 is enabled")
  24. }
  25. // Name implements Server.
  26. func (s *LocalNameServer) Name() string {
  27. return "localhost"
  28. }
  29. // NewLocalNameServer creates localdns server object for directly lookup in system DNS.
  30. func NewLocalNameServer() *LocalNameServer {
  31. newError("DNS: created localhost client").AtInfo().WriteToLog()
  32. return &LocalNameServer{
  33. client: localdns.New(),
  34. }
  35. }
  36. // NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
  37. func NewLocalDNSClient() *Client {
  38. return &Client{server: NewLocalNameServer()}
  39. }