server.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dns
  2. import (
  3. "net"
  4. "sync"
  5. "time"
  6. "v2ray.com/core/app"
  7. "v2ray.com/core/app/dispatcher"
  8. "v2ray.com/core/common/log"
  9. v2net "v2ray.com/core/common/net"
  10. "github.com/miekg/dns"
  11. )
  12. const (
  13. QueryTimeout = time.Second * 8
  14. )
  15. type DomainRecord struct {
  16. A *ARecord
  17. }
  18. type CacheServer struct {
  19. sync.RWMutex
  20. space app.Space
  21. hosts map[string]net.IP
  22. records map[string]*DomainRecord
  23. servers []NameServer
  24. }
  25. func NewCacheServer(space app.Space, config *Config) *CacheServer {
  26. server := &CacheServer{
  27. records: make(map[string]*DomainRecord),
  28. servers: make([]NameServer, len(config.NameServers)),
  29. hosts: config.GetInternalHosts(),
  30. }
  31. space.InitializeApplication(func() error {
  32. if !space.HasApp(dispatcher.APP_ID) {
  33. log.Error("DNS: Dispatcher is not found in the space.")
  34. return app.ErrMissingApplication
  35. }
  36. dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
  37. for idx, destPB := range config.NameServers {
  38. address := destPB.Address.AsAddress()
  39. if address.Family().IsDomain() && address.Domain() == "localhost" {
  40. server.servers[idx] = &LocalNameServer{}
  41. } else {
  42. dest := destPB.AsDestination()
  43. if dest.Network == v2net.Network_Unknown {
  44. dest.Network = v2net.Network_UDP
  45. }
  46. if dest.Network == v2net.Network_UDP {
  47. server.servers[idx] = NewUDPNameServer(dest, dispatcher)
  48. }
  49. }
  50. }
  51. if len(config.NameServers) == 0 {
  52. server.servers = append(server.servers, &LocalNameServer{})
  53. }
  54. return nil
  55. })
  56. return server
  57. }
  58. func (this *CacheServer) Release() {
  59. }
  60. // Private: Visible for testing.
  61. func (this *CacheServer) GetCached(domain string) []net.IP {
  62. this.RLock()
  63. defer this.RUnlock()
  64. if record, found := this.records[domain]; found && record.A.Expire.After(time.Now()) {
  65. return record.A.IPs
  66. }
  67. return nil
  68. }
  69. func (this *CacheServer) Get(domain string) []net.IP {
  70. if ip, found := this.hosts[domain]; found {
  71. return []net.IP{ip}
  72. }
  73. domain = dns.Fqdn(domain)
  74. ips := this.GetCached(domain)
  75. if ips != nil {
  76. return ips
  77. }
  78. for _, server := range this.servers {
  79. response := server.QueryA(domain)
  80. select {
  81. case a, open := <-response:
  82. if !open || a == nil {
  83. continue
  84. }
  85. this.Lock()
  86. this.records[domain] = &DomainRecord{
  87. A: a,
  88. }
  89. this.Unlock()
  90. log.Debug("DNS: Returning ", len(a.IPs), " IPs for domain ", domain)
  91. return a.IPs
  92. case <-time.After(QueryTimeout):
  93. }
  94. }
  95. log.Debug("DNS: Returning nil for domain ", domain)
  96. return nil
  97. }