dns.go 828 B

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