client.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package localdns
  2. import (
  3. "v2ray.com/core/common/net"
  4. "v2ray.com/core/features/dns"
  5. )
  6. // Client is an implementation of dns.Client, which queries localhost for DNS.
  7. type Client struct{}
  8. // Type implements common.HasType.
  9. func (*Client) Type() interface{} {
  10. return dns.ClientType()
  11. }
  12. // Start implements common.Runnable.
  13. func (*Client) Start() error { return nil }
  14. // Close implements common.Closable.
  15. func (*Client) Close() error { return nil }
  16. // LookupIP implements Client.
  17. func (*Client) LookupIP(host string) ([]net.IP, error) {
  18. return net.LookupIP(host)
  19. }
  20. // LookupIPv4 implements IPv4Lookup.
  21. func (c *Client) LookupIPv4(host string) ([]net.IP, error) {
  22. ips, err := c.LookupIP(host)
  23. if err != nil {
  24. return nil, err
  25. }
  26. var ipv4 []net.IP
  27. for _, ip := range ips {
  28. parsed := net.IPAddress(ip)
  29. if parsed.Family().IsIPv4() {
  30. ipv4 = append(ipv4, parsed.IP())
  31. }
  32. }
  33. return ipv4, nil
  34. }
  35. // LookupIPv6 implements IPv6Lookup.
  36. func (c *Client) LookupIPv6(host string) ([]net.IP, error) {
  37. ips, err := c.LookupIP(host)
  38. if err != nil {
  39. return nil, err
  40. }
  41. var ipv6 []net.IP
  42. for _, ip := range ips {
  43. parsed := net.IPAddress(ip)
  44. if parsed.Family().IsIPv6() {
  45. ipv6 = append(ipv6, parsed.IP())
  46. }
  47. }
  48. return ipv6, nil
  49. }
  50. // New create a new dns.Client that queries localhost for DNS.
  51. func New() *Client {
  52. return &Client{}
  53. }