server.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package dns
  2. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dns -path App,DNS
  3. import (
  4. "context"
  5. "sync"
  6. "time"
  7. dnsmsg "github.com/miekg/dns"
  8. "v2ray.com/core"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/task"
  12. )
  13. const (
  14. QueryTimeout = time.Second * 8
  15. )
  16. type DomainRecord struct {
  17. IP []net.IP
  18. Expire time.Time
  19. LastAccess time.Time
  20. }
  21. func (r *DomainRecord) Expired() bool {
  22. return r.Expire.Before(time.Now())
  23. }
  24. type Server struct {
  25. sync.Mutex
  26. hosts map[string]net.IP
  27. records map[string]*DomainRecord
  28. servers []NameServer
  29. task *task.Periodic
  30. }
  31. func New(ctx context.Context, config *Config) (*Server, error) {
  32. server := &Server{
  33. records: make(map[string]*DomainRecord),
  34. servers: make([]NameServer, len(config.NameServers)),
  35. hosts: config.GetInternalHosts(),
  36. }
  37. server.task = &task.Periodic{
  38. Interval: time.Minute * 10,
  39. Execute: func() error {
  40. server.cleanup()
  41. return nil
  42. },
  43. }
  44. v := core.MustFromContext(ctx)
  45. if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
  46. return nil, newError("unable to register DNSClient.").Base(err)
  47. }
  48. for idx, destPB := range config.NameServers {
  49. address := destPB.Address.AsAddress()
  50. if address.Family().IsDomain() && address.Domain() == "localhost" {
  51. server.servers[idx] = &LocalNameServer{}
  52. } else {
  53. dest := destPB.AsDestination()
  54. if dest.Network == net.Network_Unknown {
  55. dest.Network = net.Network_UDP
  56. }
  57. if dest.Network == net.Network_UDP {
  58. server.servers[idx] = NewUDPNameServer(dest, v.Dispatcher())
  59. }
  60. }
  61. }
  62. if len(config.NameServers) == 0 {
  63. server.servers = append(server.servers, &LocalNameServer{})
  64. }
  65. return server, nil
  66. }
  67. // Start implements common.Runnable.
  68. func (s *Server) Start() error {
  69. return s.task.Start()
  70. }
  71. // Close implements common.Closable.
  72. func (s *Server) Close() error {
  73. return s.task.Close()
  74. }
  75. func (s *Server) GetCached(domain string) []net.IP {
  76. s.Lock()
  77. defer s.Unlock()
  78. if record, found := s.records[domain]; found && !record.Expired() {
  79. record.LastAccess = time.Now()
  80. return record.IP
  81. }
  82. return nil
  83. }
  84. func (s *Server) cleanup() {
  85. s.Lock()
  86. defer s.Unlock()
  87. for d, r := range s.records {
  88. if r.Expired() {
  89. delete(s.records, d)
  90. }
  91. }
  92. if len(s.records) == 0 {
  93. s.records = make(map[string]*DomainRecord)
  94. }
  95. }
  96. func (s *Server) LookupIP(domain string) ([]net.IP, error) {
  97. if ip, found := s.hosts[domain]; found {
  98. return []net.IP{ip}, nil
  99. }
  100. domain = dnsmsg.Fqdn(domain)
  101. ips := s.GetCached(domain)
  102. if ips != nil {
  103. return ips, nil
  104. }
  105. for _, server := range s.servers {
  106. response := server.QueryA(domain)
  107. select {
  108. case a, open := <-response:
  109. if !open || a == nil {
  110. continue
  111. }
  112. s.Lock()
  113. s.records[domain] = &DomainRecord{
  114. IP: a.IPs,
  115. Expire: a.Expire,
  116. LastAccess: time.Now(),
  117. }
  118. s.Unlock()
  119. newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug().WriteToLog()
  120. return a.IPs, nil
  121. case <-time.After(QueryTimeout):
  122. }
  123. }
  124. return nil, newError("returning nil for domain ", domain)
  125. }
  126. func init() {
  127. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  128. return New(ctx, config.(*Config))
  129. }))
  130. }