2018-01-10 06:22:37 -05:00
|
|
|
package core
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
)
|
2018-01-10 06:22:37 -05:00
|
|
|
|
|
|
|
// DNSClient is a V2Ray feature for querying DNS information.
|
|
|
|
type DNSClient interface {
|
|
|
|
Feature
|
|
|
|
LookupIP(host string) ([]net.IP, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type syncDNSClient struct {
|
|
|
|
sync.RWMutex
|
|
|
|
DNSClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *syncDNSClient) LookupIP(host string) ([]net.IP, error) {
|
|
|
|
d.RLock()
|
|
|
|
defer d.RUnlock()
|
|
|
|
|
|
|
|
if d.DNSClient == nil {
|
|
|
|
return net.LookupIP(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.DNSClient.LookupIP(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *syncDNSClient) Start() error {
|
|
|
|
d.RLock()
|
|
|
|
defer d.RUnlock()
|
|
|
|
|
|
|
|
if d.DNSClient == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.DNSClient.Start()
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (d *syncDNSClient) Close() error {
|
2018-01-10 06:22:37 -05:00
|
|
|
d.RLock()
|
|
|
|
defer d.RUnlock()
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
return common.Close(d.DNSClient)
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *syncDNSClient) Set(client DNSClient) {
|
|
|
|
if client == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
2018-05-31 05:55:11 -04:00
|
|
|
common.Close(d.DNSClient) // nolint: errcheck
|
2018-01-10 06:22:37 -05:00
|
|
|
d.DNSClient = client
|
|
|
|
}
|