1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 10:56:06 -05:00
v2fly/app/dns/server.go

158 lines
3.4 KiB
Go
Raw Normal View History

2017-12-19 17:55:09 -05:00
package dns
2016-01-31 11:01:28 -05:00
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dns -path App,DNS
2017-04-08 19:43:25 -04:00
2016-01-31 11:01:28 -05:00
import (
2017-01-13 07:41:40 -05:00
"context"
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"
"v2ray.com/core"
2017-01-06 09:32:36 -05:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
2018-02-08 11:00:22 -05:00
"v2ray.com/core/common/signal"
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 {
2017-10-24 10:15:35 -04:00
IP []net.IP
Expire time.Time
LastAccess time.Time
}
2017-11-14 18:36:14 -05:00
func (r *DomainRecord) Expired() bool {
return r.Expire.Before(time.Now())
}
func (r *DomainRecord) Inactive() bool {
2017-10-24 10:15:35 -04:00
now := time.Now()
2017-11-14 18:36:14 -05:00
return r.Expire.Before(now) || r.LastAccess.Add(time.Minute*5).Before(now)
2017-10-24 10:15:35 -04:00
}
2017-12-28 17:19:41 -05:00
type Server struct {
2017-11-14 18:36:14 -05:00
sync.Mutex
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
2018-02-08 11:00:22 -05:00
task *signal.PeriodicTask
2016-01-31 11:01:28 -05:00
}
2017-12-28 17:19:41 -05:00
func New(ctx context.Context, config *Config) (*Server, error) {
server := &Server{
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
}
2018-02-08 11:00:22 -05:00
server.task = &signal.PeriodicTask{
Interval: time.Minute * 10,
Execute: func() error {
server.cleanup()
return nil
},
}
v := core.FromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}
if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
return nil, newError("unable to register DNSClient.").Base(err)
}
for idx, destPB := range config.NameServers {
address := destPB.Address.AsAddress()
if address.Family().IsDomain() && address.Domain() == "localhost" {
server.servers[idx] = &LocalNameServer{}
} else {
dest := destPB.AsDestination()
if dest.Network == net.Network_Unknown {
dest.Network = net.Network_UDP
}
if dest.Network == net.Network_UDP {
server.servers[idx] = NewUDPNameServer(dest, v.Dispatcher())
2016-05-18 11:12:04 -04:00
}
}
}
if len(config.NameServers) == 0 {
server.servers = append(server.servers, &LocalNameServer{})
}
2017-01-13 07:41:40 -05:00
return server, nil
}
2018-02-08 11:00:22 -05:00
// Start implements common.Runnable.
2017-12-28 17:19:41 -05:00
func (s *Server) Start() error {
2018-02-08 11:00:22 -05:00
return s.task.Start()
2017-02-01 15:35:40 -05:00
}
2018-02-08 11:00:22 -05:00
// Close implements common.Runnable.
func (s *Server) Close() error {
return s.task.Close()
2017-12-28 17:19:41 -05:00
}
2017-02-01 15:35:40 -05:00
2017-12-28 17:19:41 -05:00
func (s *Server) GetCached(domain string) []net.IP {
2017-11-14 18:36:14 -05:00
s.Lock()
defer s.Unlock()
2016-01-31 11:01:28 -05:00
2017-11-14 18:36:14 -05:00
if record, found := s.records[domain]; found && !record.Expired() {
record.LastAccess = time.Now()
return record.IP
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
}
2018-02-08 11:00:22 -05:00
func (s *Server) cleanup() {
2017-11-14 18:36:14 -05:00
s.Lock()
defer s.Unlock()
2018-02-08 11:00:22 -05:00
for d, r := range s.records {
if r.Expired() {
2017-11-14 18:36:14 -05:00
delete(s.records, d)
}
}
}
2017-12-28 17:19:41 -05:00
func (s *Server) LookupIP(domain string) ([]net.IP, error) {
2017-04-23 13:16:56 -04:00
if ip, found := s.hosts[domain]; found {
2017-12-19 17:55:09 -05:00
return []net.IP{ip}, nil
2016-05-22 16:30:08 -04:00
}
2016-12-16 10:20:12 -05:00
domain = dnsmsg.Fqdn(domain)
2017-04-23 13:16:56 -04:00
ips := s.GetCached(domain)
2016-05-16 02:09:28 -04:00
if ips != nil {
2017-12-19 17:55:09 -05:00
return ips, nil
2016-01-31 11:01:28 -05:00
}
2017-04-23 13:16:56 -04:00
for _, server := range s.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
}
2017-04-23 13:16:56 -04:00
s.Lock()
s.records[domain] = &DomainRecord{
2017-11-14 18:36:14 -05:00
IP: a.IPs,
Expire: a.Expire,
LastAccess: time.Now(),
2016-05-16 02:09:28 -04:00
}
2017-04-23 13:16:56 -04:00
s.Unlock()
2017-12-19 15:28:12 -05:00
newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug().WriteToLog()
2017-12-19 17:55:09 -05:00
return a.IPs, nil
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-12-19 17:55:09 -05:00
return nil, newError("returning nil for domain ", domain)
2016-01-31 11:01:28 -05:00
}
2016-10-16 10:04:30 -04:00
func init() {
2017-12-19 17:55:09 -05:00
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
2017-12-28 17:19:41 -05:00
return New(ctx, config.(*Config))
2017-01-13 07:41:40 -05:00
}))
2016-10-16 10:04:30 -04:00
}