server.go 2.8 KB

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