dns.go 843 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package core
  2. import (
  3. "net"
  4. "sync"
  5. "v2ray.com/core/common"
  6. )
  7. // DNSClient is a V2Ray feature for querying DNS information.
  8. type DNSClient interface {
  9. Feature
  10. LookupIP(host string) ([]net.IP, error)
  11. }
  12. type syncDNSClient struct {
  13. sync.RWMutex
  14. DNSClient
  15. }
  16. func (d *syncDNSClient) LookupIP(host string) ([]net.IP, error) {
  17. d.RLock()
  18. defer d.RUnlock()
  19. if d.DNSClient == nil {
  20. return net.LookupIP(host)
  21. }
  22. return d.DNSClient.LookupIP(host)
  23. }
  24. func (d *syncDNSClient) Start() error {
  25. d.RLock()
  26. defer d.RUnlock()
  27. if d.DNSClient == nil {
  28. return nil
  29. }
  30. return d.DNSClient.Start()
  31. }
  32. func (d *syncDNSClient) Close() error {
  33. d.RLock()
  34. defer d.RUnlock()
  35. return common.Close(d.DNSClient)
  36. }
  37. func (d *syncDNSClient) Set(client DNSClient) {
  38. if client == nil {
  39. return
  40. }
  41. d.Lock()
  42. defer d.Unlock()
  43. d.DNSClient = client
  44. }