server.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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() {
  72. }
  73. func (s *Server) GetCached(domain string) []net.IP {
  74. s.Lock()
  75. defer s.Unlock()
  76. if record, found := s.records[domain]; found && !record.Expired() {
  77. record.LastAccess = time.Now()
  78. return record.IP
  79. }
  80. return nil
  81. }
  82. func (s *Server) tryCleanup() {
  83. s.Lock()
  84. defer s.Unlock()
  85. if len(s.records) > 256 {
  86. domains := make([]string, 0, 256)
  87. for d, r := range s.records {
  88. if r.Expired() {
  89. domains = append(domains, d)
  90. }
  91. }
  92. for _, d := range domains {
  93. delete(s.records, d)
  94. }
  95. }
  96. }
  97. func (s *Server) LookupIP(domain string) ([]net.IP, error) {
  98. if ip, found := s.hosts[domain]; found {
  99. return []net.IP{ip}, nil
  100. }
  101. domain = dnsmsg.Fqdn(domain)
  102. ips := s.GetCached(domain)
  103. if ips != nil {
  104. return ips, nil
  105. }
  106. s.tryCleanup()
  107. for _, server := range s.servers {
  108. response := server.QueryA(domain)
  109. select {
  110. case a, open := <-response:
  111. if !open || a == nil {
  112. continue
  113. }
  114. s.Lock()
  115. s.records[domain] = &DomainRecord{
  116. IP: a.IPs,
  117. Expire: a.Expire,
  118. LastAccess: time.Now(),
  119. }
  120. s.Unlock()
  121. newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug().WriteToLog()
  122. return a.IPs, nil
  123. case <-time.After(QueryTimeout):
  124. }
  125. }
  126. return nil, newError("returning nil for domain ", domain)
  127. }
  128. func init() {
  129. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  130. return New(ctx, config.(*Config))
  131. }))
  132. }