nameserver_local.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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"
  7. "github.com/v2fly/v2ray-core/v4/features/dns/localdns"
  8. )
  9. // LocalNameServer is an wrapper over local DNS feature.
  10. type LocalNameServer struct {
  11. client *localdns.Client
  12. }
  13. // QueryIP implements Server.
  14. func (s *LocalNameServer) QueryIP(_ context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) ([]net.IP, error) {
  15. var ips []net.IP
  16. var err error
  17. switch {
  18. case option.IPv4Enable && option.IPv6Enable:
  19. ips, err = s.client.LookupIP(domain)
  20. case option.IPv4Enable:
  21. ips, err = s.client.LookupIPv4(domain)
  22. case option.IPv6Enable:
  23. ips, err = s.client.LookupIPv6(domain)
  24. }
  25. if len(ips) > 0 {
  26. newError("Localhost got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
  27. }
  28. return ips, err
  29. }
  30. // Name implements Server.
  31. func (s *LocalNameServer) Name() string {
  32. return "localhost"
  33. }
  34. // NewLocalNameServer creates localdns server object for directly lookup in system DNS.
  35. func NewLocalNameServer() *LocalNameServer {
  36. newError("DNS: created localhost client").AtInfo().WriteToLog()
  37. return &LocalNameServer{
  38. client: localdns.New(),
  39. }
  40. }
  41. // NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
  42. func NewLocalDNSClient() *Client {
  43. return &Client{server: NewLocalNameServer()}
  44. }