server.go 2.8 KB

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