server.go 2.8 KB

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