server.go 3.3 KB

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