1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-08-06 20:44:31 -04:00
v2fly/app/dns/server.go

130 lines
2.8 KiB
Go
Raw Normal View History

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"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-10-16 10:04:30 -04:00
"v2ray.com/core/common/loader"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
2016-09-20 10:05:35 -04:00
v2net "v2ray.com/core/common/net"
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
2016-05-18 11:12:04 -04:00
space app.Space
2016-05-22 16:30:08 -04:00
hosts map[string]net.IP
2016-05-16 02:09:28 -04:00
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)),
2016-09-20 10:05:35 -04:00
hosts: config.GetInternalHosts(),
2016-05-16 02:09:28 -04:00
}
2016-05-18 11:12:04 -04:00
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
2016-11-21 15:13:01 -05:00
return errors.New("DNS: Dispatcher is not found in the space.")
2016-05-16 12:05:01 -04:00
}
2016-05-18 11:12:04 -04:00
dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
2016-09-20 10:05:35 -04:00
for idx, destPB := range config.NameServers {
address := destPB.Address.AsAddress()
if address.Family().IsDomain() && address.Domain() == "localhost" {
2016-05-18 11:12:04 -04:00
server.servers[idx] = &LocalNameServer{}
} else {
2016-09-20 10:05:35 -04:00
dest := destPB.AsDestination()
if dest.Network == v2net.Network_Unknown {
dest.Network = v2net.Network_UDP
}
if dest.Network == v2net.Network_UDP {
server.servers[idx] = NewUDPNameServer(dest, dispatcher)
}
2016-05-18 11:12:04 -04:00
}
}
2016-05-22 16:30:08 -04:00
if len(config.NameServers) == 0 {
server.servers = append(server.servers, &LocalNameServer{})
}
2016-05-18 11:12:04 -04:00
return nil
})
2016-05-16 02:09:28 -04:00
return server
2016-01-31 11:01:28 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *CacheServer) Release() {
2016-05-18 11:12:04 -04:00
}
2016-08-24 05:17:42 -04:00
// Private: Visible for testing.
2016-11-27 15:39:09 -05:00
func (v *CacheServer) GetCached(domain string) []net.IP {
v.RLock()
defer v.RUnlock()
2016-01-31 11:01:28 -05:00
2016-11-27 15:39:09 -05:00
if record, found := v.records[domain]; found && record.A.Expire.After(time.Now()) {
2016-05-16 02:09:28 -04:00
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-11-27 15:39:09 -05:00
func (v *CacheServer) Get(domain string) []net.IP {
if ip, found := v.hosts[domain]; found {
2016-05-22 16:30:08 -04:00
return []net.IP{ip}
}
2016-05-16 02:09:28 -04:00
domain = dns.Fqdn(domain)
2016-11-27 15:39:09 -05:00
ips := v.GetCached(domain)
2016-05-16 02:09:28 -04:00
if ips != nil {
return ips
2016-01-31 11:01:28 -05:00
}
2016-11-27 15:39:09 -05:00
for _, server := range v.servers {
2016-05-16 02:09:28 -04:00
response := server.QueryA(domain)
select {
2016-05-16 14:53:18 -04:00
case a, open := <-response:
if !open || a == nil {
continue
}
2016-11-27 15:39:09 -05:00
v.Lock()
v.records[domain] = &DomainRecord{
2016-05-16 02:09:28 -04:00
A: a,
}
2016-11-27 15:39:09 -05:00
v.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
}
2016-10-16 10:04:30 -04:00
type CacheServerFactory struct{}
2016-11-27 15:39:09 -05:00
func (v CacheServerFactory) Create(space app.Space, config interface{}) (app.Application, error) {
2016-10-16 10:04:30 -04:00
server := NewCacheServer(space, config.(*Config))
return server, nil
}
2016-11-27 15:39:09 -05:00
func (v CacheServerFactory) AppId() app.ID {
2016-10-16 10:04:30 -04:00
return APP_ID
}
func init() {
app.RegisterApplicationFactory(loader.GetType(new(Config)), CacheServerFactory{})
}