2016-12-16 15:20:12 +00:00
|
|
|
package server
|
2016-01-31 16:01:28 +00:00
|
|
|
|
2017-04-08 23:43:25 +00:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg server -path App,DNS,Server
|
|
|
|
|
2016-01-31 16:01:28 +00:00
|
|
|
import (
|
2017-01-13 12:41:40 +00:00
|
|
|
"context"
|
2016-05-16 06:09:28 +00:00
|
|
|
"sync"
|
2016-01-31 16:01:28 +00:00
|
|
|
"time"
|
|
|
|
|
2017-01-13 12:41:40 +00:00
|
|
|
dnsmsg "github.com/miekg/dns"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2016-12-16 15:20:12 +00:00
|
|
|
"v2ray.com/core/app/dns"
|
2017-02-01 20:35:40 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2017-01-06 14:32:36 +00:00
|
|
|
"v2ray.com/core/common"
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
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 {
|
2017-10-24 14:15:35 +00:00
|
|
|
IP []net.IP
|
|
|
|
Expire time.Time
|
|
|
|
LastAccess time.Time
|
|
|
|
}
|
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
func (r *DomainRecord) Expired() bool {
|
|
|
|
return r.Expire.Before(time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DomainRecord) Inactive() bool {
|
2017-10-24 14:15:35 +00:00
|
|
|
now := time.Now()
|
2017-11-14 23:36:14 +00:00
|
|
|
return r.Expire.Before(now) || r.LastAccess.Add(time.Minute*5).Before(now)
|
2017-10-24 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 07:25:34 +00:00
|
|
|
type CacheServer struct {
|
2017-11-14 23:36:14 +00:00
|
|
|
sync.Mutex
|
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
|
|
|
}
|
|
|
|
|
2017-01-13 12:41:40 +00:00
|
|
|
func NewCacheServer(ctx context.Context, config *dns.Config) (*CacheServer, error) {
|
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("no space in context")
|
2017-01-13 12:41:40 +00:00
|
|
|
}
|
2016-05-16 07:25:34 +00:00
|
|
|
server := &CacheServer{
|
2016-05-16 06:09:28 +00:00
|
|
|
records: make(map[string]*DomainRecord),
|
|
|
|
servers: make([]NameServer, len(config.NameServers)),
|
2016-09-20 14:05:35 +00:00
|
|
|
hosts: config.GetInternalHosts(),
|
2016-05-16 06:09:28 +00:00
|
|
|
}
|
2017-01-06 14:32:36 +00:00
|
|
|
space.OnInitialize(func() error {
|
|
|
|
disp := dispatcher.FromSpace(space)
|
|
|
|
if disp == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("dispatcher is not found in the space")
|
2016-05-16 16:05:01 +00:00
|
|
|
}
|
2016-09-20 14:05:35 +00:00
|
|
|
for idx, destPB := range config.NameServers {
|
|
|
|
address := destPB.Address.AsAddress()
|
|
|
|
if address.Family().IsDomain() && address.Domain() == "localhost" {
|
2016-05-18 15:12:04 +00:00
|
|
|
server.servers[idx] = &LocalNameServer{}
|
|
|
|
} else {
|
2016-09-20 14:05:35 +00:00
|
|
|
dest := destPB.AsDestination()
|
2017-08-29 10:56:57 +00:00
|
|
|
if dest.Network == net.Network_Unknown {
|
|
|
|
dest.Network = net.Network_UDP
|
2016-09-20 14:05:35 +00:00
|
|
|
}
|
2017-08-29 10:56:57 +00:00
|
|
|
if dest.Network == net.Network_UDP {
|
2017-01-06 14:32:36 +00:00
|
|
|
server.servers[idx] = NewUDPNameServer(dest, disp)
|
2016-09-20 14:05:35 +00:00
|
|
|
}
|
2016-05-18 15:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
})
|
2017-01-13 12:41:40 +00:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
2017-04-09 12:33:08 +00:00
|
|
|
func (*CacheServer) Interface() interface{} {
|
2017-01-13 12:41:40 +00:00
|
|
|
return (*dns.Server)(nil)
|
2016-01-31 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 12:33:08 +00:00
|
|
|
func (*CacheServer) Start() error {
|
2017-02-01 20:35:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-09 12:33:08 +00:00
|
|
|
func (*CacheServer) Close() {}
|
2017-02-01 20:35:40 +00:00
|
|
|
|
2017-04-23 17:16:56 +00:00
|
|
|
func (s *CacheServer) GetCached(domain string) []net.IP {
|
2017-11-14 23:36:14 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2016-01-31 16:01:28 +00:00
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
if record, found := s.records[domain]; found && !record.Expired() {
|
|
|
|
record.LastAccess = time.Now()
|
|
|
|
return record.IP
|
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
|
|
|
}
|
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
func (s *CacheServer) tryCleanup() {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
if len(s.records) > 256 {
|
|
|
|
domains := make([]string, 0, 256)
|
|
|
|
for d, r := range s.records {
|
|
|
|
if r.Expired() {
|
|
|
|
domains = append(domains, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, d := range domains {
|
|
|
|
delete(s.records, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:16:56 +00:00
|
|
|
func (s *CacheServer) Get(domain string) []net.IP {
|
|
|
|
if ip, found := s.hosts[domain]; found {
|
2016-05-22 20:30:08 +00:00
|
|
|
return []net.IP{ip}
|
|
|
|
}
|
|
|
|
|
2016-12-16 15:20:12 +00:00
|
|
|
domain = dnsmsg.Fqdn(domain)
|
2017-04-23 17:16:56 +00:00
|
|
|
ips := s.GetCached(domain)
|
2016-05-16 06:09:28 +00:00
|
|
|
if ips != nil {
|
|
|
|
return ips
|
2016-01-31 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 23:36:14 +00:00
|
|
|
s.tryCleanup()
|
|
|
|
|
2017-04-23 17:16:56 +00:00
|
|
|
for _, server := range s.servers {
|
2016-05-16 06:09:28 +00:00
|
|
|
response := server.QueryA(domain)
|
|
|
|
select {
|
2016-05-16 18:53:18 +00:00
|
|
|
case a, open := <-response:
|
|
|
|
if !open || a == nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-04-23 17:16:56 +00:00
|
|
|
s.Lock()
|
|
|
|
s.records[domain] = &DomainRecord{
|
2017-11-14 23:36:14 +00:00
|
|
|
IP: a.IPs,
|
|
|
|
Expire: a.Expire,
|
|
|
|
LastAccess: time.Now(),
|
2016-05-16 06:09:28 +00:00
|
|
|
}
|
2017-04-23 17:16:56 +00:00
|
|
|
s.Unlock()
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug())
|
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
|
|
|
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("returning nil for domain ", domain).AtDebug())
|
2016-01-31 16:01:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-16 14:04:30 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-01-13 12:41:40 +00:00
|
|
|
common.Must(common.RegisterConfig((*dns.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewCacheServer(ctx, config.(*dns.Config))
|
|
|
|
}))
|
2016-10-16 14:04:30 +00:00
|
|
|
}
|