| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 | package dnsimport (	"net"	"sync"	"time"	"github.com/v2ray/v2ray-core/app"	"github.com/v2ray/v2ray-core/app/dispatcher"	"github.com/v2ray/v2ray-core/common/log"	"github.com/miekg/dns")const (	QueryTimeout = time.Second * 8)type DomainRecord struct {	A *ARecord}type CacheServer struct {	sync.RWMutex	records map[string]*DomainRecord	servers []NameServer}func NewCacheServer(space app.Space, config *Config) *CacheServer {	server := &CacheServer{		records: make(map[string]*DomainRecord),		servers: make([]NameServer, len(config.NameServers)),	}	dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)	for idx, ns := range config.NameServers {		if ns.Address().IsDomain() && ns.Address().Domain() == "localhost" {			server.servers[idx] = &LocalNameServer{}		} else {			server.servers[idx] = NewUDPNameServer(ns, dispatcher)		}	}	return server}//@Privatefunc (this *CacheServer) GetCached(domain string) []net.IP {	this.RLock()	defer this.RUnlock()	if record, found := this.records[domain]; found && record.A.Expire.After(time.Now()) {		return record.A.IPs	}	return nil}func (this *CacheServer) Get(domain string) []net.IP {	domain = dns.Fqdn(domain)	ips := this.GetCached(domain)	if ips != nil {		return ips	}	for _, server := range this.servers {		response := server.QueryA(domain)		select {		case a, open := <-response:			if !open || a == nil {				continue			}			this.Lock()			this.records[domain] = &DomainRecord{				A: a,			}			this.Unlock()			log.Debug("DNS: Returning ", len(a.IPs), " IPs for domain ", domain)			return a.IPs		case <-time.After(QueryTimeout):		}	}	log.Debug("DNS: Returning nil for domain ", domain)	return nil}
 |