server.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package server
  2. //go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg server -path App,DNS,Server
  3. import (
  4. "context"
  5. "net"
  6. "sync"
  7. "time"
  8. dnsmsg "github.com/miekg/dns"
  9. "v2ray.com/core/app"
  10. "v2ray.com/core/app/dispatcher"
  11. "v2ray.com/core/app/dns"
  12. "v2ray.com/core/app/log"
  13. "v2ray.com/core/common"
  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, newError("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 newError("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. func (CacheServer) Start() error {
  69. return nil
  70. }
  71. func (CacheServer) Close() {}
  72. // Private: Visible for testing.
  73. func (v *CacheServer) GetCached(domain string) []net.IP {
  74. v.RLock()
  75. defer v.RUnlock()
  76. if record, found := v.records[domain]; found && record.A.Expire.After(time.Now()) {
  77. return record.A.IPs
  78. }
  79. return nil
  80. }
  81. func (v *CacheServer) Get(domain string) []net.IP {
  82. if ip, found := v.hosts[domain]; found {
  83. return []net.IP{ip}
  84. }
  85. domain = dnsmsg.Fqdn(domain)
  86. ips := v.GetCached(domain)
  87. if ips != nil {
  88. return ips
  89. }
  90. for _, server := range v.servers {
  91. response := server.QueryA(domain)
  92. select {
  93. case a, open := <-response:
  94. if !open || a == nil {
  95. continue
  96. }
  97. v.Lock()
  98. v.records[domain] = &DomainRecord{
  99. A: a,
  100. }
  101. v.Unlock()
  102. log.Trace(newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug())
  103. return a.IPs
  104. case <-time.After(QueryTimeout):
  105. }
  106. }
  107. log.Trace(newError("returning nil for domain ", domain).AtDebug())
  108. return nil
  109. }
  110. func init() {
  111. common.Must(common.RegisterConfig((*dns.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  112. return NewCacheServer(ctx, config.(*dns.Config))
  113. }))
  114. }