2016-12-16 10:20:12 -05:00
|
|
|
package server
|
2016-01-31 11:01:28 -05:00
|
|
|
|
|
|
|
import (
|
2017-01-13 07:41:40 -05:00
|
|
|
"context"
|
2016-01-31 11:01:28 -05:00
|
|
|
"net"
|
2016-05-16 02:09:28 -04:00
|
|
|
"sync"
|
2016-01-31 11:01:28 -05:00
|
|
|
"time"
|
|
|
|
|
2017-01-13 07:41:40 -05:00
|
|
|
dnsmsg "github.com/miekg/dns"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2016-12-16 10:20:12 -05:00
|
|
|
"v2ray.com/core/app/dns"
|
2017-02-01 15:35:40 -05:00
|
|
|
"v2ray.com/core/app/log"
|
2017-01-06 09:32:36 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-09-20 10:05:35 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
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
|
|
|
}
|
|
|
|
|
2017-01-13 07:41:40 -05:00
|
|
|
func NewCacheServer(ctx context.Context, config *dns.Config) (*CacheServer, error) {
|
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
|
|
|
return nil, errors.New("DNSCacheServer: No space in context.")
|
|
|
|
}
|
2016-05-16 03:25:34 -04:00
|
|
|
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
|
|
|
}
|
2017-01-06 09:32:36 -05:00
|
|
|
space.OnInitialize(func() error {
|
|
|
|
disp := dispatcher.FromSpace(space)
|
|
|
|
if disp == nil {
|
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-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 {
|
2017-01-06 09:32:36 -05:00
|
|
|
server.servers[idx] = NewUDPNameServer(dest, disp)
|
2016-09-20 10:05:35 -04:00
|
|
|
}
|
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
|
|
|
|
})
|
2017-01-13 07:41:40 -05:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (CacheServer) Interface() interface{} {
|
|
|
|
return (*dns.Server)(nil)
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2017-02-01 15:35:40 -05:00
|
|
|
func (CacheServer) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (CacheServer) Close() {}
|
|
|
|
|
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-12-16 10:20:12 -05:00
|
|
|
domain = dnsmsg.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()
|
2017-04-06 15:13:17 -04:00
|
|
|
log.Trace(errors.New("DNS: Returning ", len(a.IPs), " IPs for domain ", domain).AtDebug())
|
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
|
|
|
|
2017-04-06 15:13:17 -04:00
|
|
|
log.Trace(errors.New("DNS: Returning nil for domain ", domain).AtDebug())
|
2016-01-31 11:01:28 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-16 10:04:30 -04:00
|
|
|
|
|
|
|
func init() {
|
2017-01-13 07:41:40 -05: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 10:04:30 -04:00
|
|
|
}
|