1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-20 06:25:24 +00:00
v2fly/app/dns/server.go

106 lines
2.1 KiB
Go
Raw Normal View History

2016-05-16 07:25:34 +00:00
package dns
2016-01-31 16:01:28 +00:00
import (
"net"
2016-05-16 06:09:28 +00:00
"sync"
2016-01-31 16:01:28 +00:00
"time"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/log"
2016-05-16 06:09:28 +00:00
"github.com/miekg/dns"
2016-01-31 16:01:28 +00:00
)
2016-05-16 06:09:28 +00:00
const (
2016-05-16 18:53:18 +00:00
QueryTimeout = time.Second * 8
2016-05-16 06:09:28 +00:00
)
2016-01-31 16:01:28 +00:00
2016-05-16 06:09:28 +00:00
type DomainRecord struct {
A *ARecord
2016-01-31 16:01:28 +00:00
}
2016-05-16 07:25:34 +00:00
type CacheServer struct {
2016-05-16 06:09:28 +00:00
sync.RWMutex
2016-05-18 15:12:04 +00:00
space app.Space
2016-05-22 20:30:08 +00:00
hosts map[string]net.IP
2016-05-16 06:09:28 +00:00
records map[string]*DomainRecord
servers []NameServer
2016-01-31 16:01:28 +00:00
}
2016-05-16 07:25:34 +00:00
func NewCacheServer(space app.Space, config *Config) *CacheServer {
server := &CacheServer{
2016-05-16 06:09:28 +00:00
records: make(map[string]*DomainRecord),
servers: make([]NameServer, len(config.NameServers)),
2016-05-22 20:30:08 +00:00
hosts: config.Hosts,
2016-05-16 06:09:28 +00:00
}
2016-05-18 15:12:04 +00:00
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
log.Error("DNS: Dispatcher is not found in the space.")
2016-06-27 06:53:35 +00:00
return app.ErrMissingApplication
2016-05-16 16:05:01 +00:00
}
2016-05-18 15:12:04 +00:00
dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
for idx, ns := range config.NameServers {
2016-08-14 16:14:12 +00:00
if ns.Address().Family().IsDomain() && ns.Address().Domain() == "localhost" {
2016-05-18 15:12:04 +00:00
server.servers[idx] = &LocalNameServer{}
} else {
server.servers[idx] = NewUDPNameServer(ns, dispatcher)
}
}
2016-05-22 20:30:08 +00:00
if len(config.NameServers) == 0 {
server.servers = append(server.servers, &LocalNameServer{})
}
2016-05-18 15:12:04 +00:00
return nil
})
2016-05-16 06:09:28 +00:00
return server
2016-01-31 16:01:28 +00:00
}
2016-05-18 15:12:04 +00:00
func (this *CacheServer) Release() {
}
2016-05-16 06:09:28 +00:00
//@Private
2016-05-16 07:25:34 +00:00
func (this *CacheServer) GetCached(domain string) []net.IP {
2016-05-16 06:09:28 +00:00
this.RLock()
defer this.RUnlock()
2016-01-31 16:01:28 +00:00
2016-05-16 06:09:28 +00:00
if record, found := this.records[domain]; found && record.A.Expire.After(time.Now()) {
return record.A.IPs
2016-01-31 16:01:28 +00:00
}
2016-05-16 06:09:28 +00:00
return nil
2016-01-31 16:01:28 +00:00
}
2016-05-18 06:05:52 +00:00
func (this *CacheServer) Get(domain string) []net.IP {
2016-05-22 20:30:08 +00:00
if ip, found := this.hosts[domain]; found {
return []net.IP{ip}
}
2016-05-16 06:09:28 +00:00
domain = dns.Fqdn(domain)
ips := this.GetCached(domain)
if ips != nil {
return ips
2016-01-31 16:01:28 +00:00
}
2016-05-16 06:09:28 +00:00
for _, server := range this.servers {
response := server.QueryA(domain)
select {
2016-05-16 18:53:18 +00:00
case a, open := <-response:
if !open || a == nil {
continue
}
2016-05-16 06:09:28 +00:00
this.Lock()
this.records[domain] = &DomainRecord{
A: a,
}
this.Unlock()
2016-05-16 18:53:18 +00:00
log.Debug("DNS: Returning ", len(a.IPs), " IPs for domain ", domain)
2016-05-16 06:09:28 +00:00
return a.IPs
2016-05-16 18:53:18 +00:00
case <-time.After(QueryTimeout):
2016-05-16 06:09:28 +00:00
}
2016-01-31 16:01:28 +00:00
}
2016-05-16 06:09:28 +00:00
2016-05-16 18:53:18 +00:00
log.Debug("DNS: Returning nil for domain ", domain)
2016-01-31 16:01:28 +00:00
return nil
}