2016-05-16 03:25:34 -04:00
|
|
|
package dns
|
2016-01-31 11:01:28 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2016-05-16 02:09:28 -04:00
|
|
|
"sync"
|
2016-01-31 11:01:28 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2016-05-16 02:09:28 -04:00
|
|
|
"github.com/v2ray/v2ray-core/app/dispatcher"
|
2016-05-16 14:53:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2016-05-16 02:09:28 -04:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2016-01-31 11:01:28 -05:00
|
|
|
)
|
|
|
|
|
2016-05-16 02:09:28 -04:00
|
|
|
const (
|
2016-05-16 14:53:18 -04:00
|
|
|
QueryTimeout = time.Second * 8
|
2016-05-16 02:09:28 -04:00
|
|
|
)
|
2016-01-31 11:01:28 -05:00
|
|
|
|
2016-05-16 02:09:28 -04:00
|
|
|
type DomainRecord struct {
|
|
|
|
A *ARecord
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:25:34 -04:00
|
|
|
type CacheServer struct {
|
2016-05-16 02:09:28 -04:00
|
|
|
sync.RWMutex
|
|
|
|
records map[string]*DomainRecord
|
|
|
|
servers []NameServer
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:25:34 -04:00
|
|
|
func NewCacheServer(space app.Space, config *Config) *CacheServer {
|
|
|
|
server := &CacheServer{
|
2016-05-16 02:09:28 -04:00
|
|
|
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 {
|
2016-05-16 12:05:01 -04:00
|
|
|
if ns.Address().IsDomain() && ns.Address().Domain() == "localhost" {
|
|
|
|
server.servers[idx] = &LocalNameServer{}
|
|
|
|
} else {
|
|
|
|
server.servers[idx] = NewUDPNameServer(ns, dispatcher)
|
|
|
|
}
|
2016-05-16 02:09:28 -04:00
|
|
|
}
|
|
|
|
return server
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 02:09:28 -04:00
|
|
|
//@Private
|
2016-05-16 03:25:34 -04:00
|
|
|
func (this *CacheServer) GetCached(domain string) []net.IP {
|
2016-05-16 02:09:28 -04:00
|
|
|
this.RLock()
|
|
|
|
defer this.RUnlock()
|
2016-01-31 11:01:28 -05:00
|
|
|
|
2016-05-16 02:09:28 -04:00
|
|
|
if record, found := this.records[domain]; found && record.A.Expire.After(time.Now()) {
|
|
|
|
return record.A.IPs
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
2016-05-16 02:09:28 -04:00
|
|
|
return nil
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 03:25:34 -04:00
|
|
|
func (this *CacheServer) Get(context app.Context, domain string) []net.IP {
|
2016-05-16 02:09:28 -04:00
|
|
|
domain = dns.Fqdn(domain)
|
|
|
|
ips := this.GetCached(domain)
|
|
|
|
if ips != nil {
|
|
|
|
return ips
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 02:09:28 -04:00
|
|
|
for _, server := range this.servers {
|
|
|
|
response := server.QueryA(domain)
|
|
|
|
select {
|
2016-05-16 14:53:18 -04:00
|
|
|
case a, open := <-response:
|
|
|
|
if !open || a == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-16 02:09:28 -04:00
|
|
|
this.Lock()
|
|
|
|
this.records[domain] = &DomainRecord{
|
|
|
|
A: a,
|
|
|
|
}
|
|
|
|
this.Unlock()
|
2016-05-16 14:53:18 -04:00
|
|
|
log.Debug("DNS: Returning ", len(a.IPs), " IPs for domain ", domain)
|
2016-05-16 02:09:28 -04:00
|
|
|
return a.IPs
|
2016-05-16 14:53:18 -04:00
|
|
|
case <-time.After(QueryTimeout):
|
2016-05-16 02:09:28 -04:00
|
|
|
}
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
2016-05-16 02:09:28 -04:00
|
|
|
|
2016-05-16 14:53:18 -04:00
|
|
|
log.Debug("DNS: Returning nil for domain ", domain)
|
2016-01-31 11:01:28 -05:00
|
|
|
return nil
|
|
|
|
}
|