dns.go 855 B

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