nameserver.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package dns
  2. import (
  3. "math/rand"
  4. "net"
  5. "sync"
  6. "time"
  7. "github.com/v2ray/v2ray-core/app/dispatcher"
  8. "github.com/v2ray/v2ray-core/common/alloc"
  9. "github.com/v2ray/v2ray-core/common/log"
  10. v2net "github.com/v2ray/v2ray-core/common/net"
  11. "github.com/v2ray/v2ray-core/transport/hub"
  12. "github.com/miekg/dns"
  13. )
  14. const (
  15. DefaultTTL = uint32(3600)
  16. )
  17. type ARecord struct {
  18. IPs []net.IP
  19. Expire time.Time
  20. }
  21. type NameServer interface {
  22. QueryA(domain string) <-chan *ARecord
  23. }
  24. type PendingRequest struct {
  25. expire time.Time
  26. response chan<- *ARecord
  27. }
  28. type UDPNameServer struct {
  29. sync.Mutex
  30. address v2net.Destination
  31. requests map[uint16]*PendingRequest
  32. udpServer *hub.UDPServer
  33. }
  34. func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDispatcher) *UDPNameServer {
  35. s := &UDPNameServer{
  36. address: address,
  37. requests: make(map[uint16]*PendingRequest),
  38. udpServer: hub.NewUDPServer(dispatcher),
  39. }
  40. go s.Cleanup()
  41. return s
  42. }
  43. // @Private
  44. func (this *UDPNameServer) Cleanup() {
  45. for {
  46. time.Sleep(time.Second * 60)
  47. expiredRequests := make([]uint16, 0, 16)
  48. now := time.Now()
  49. this.Lock()
  50. for id, r := range this.requests {
  51. if r.expire.Before(now) {
  52. expiredRequests = append(expiredRequests, id)
  53. close(r.response)
  54. }
  55. }
  56. for _, id := range expiredRequests {
  57. delete(this.requests, id)
  58. }
  59. this.Unlock()
  60. expiredRequests = nil
  61. }
  62. }
  63. // @Private
  64. func (this *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
  65. var id uint16
  66. this.Lock()
  67. for {
  68. id = uint16(rand.Intn(65536))
  69. if _, found := this.requests[id]; found {
  70. continue
  71. }
  72. log.Debug("DNS: Add pending request id ", id)
  73. this.requests[id] = &PendingRequest{
  74. expire: time.Now().Add(time.Second * 16),
  75. response: response,
  76. }
  77. break
  78. }
  79. this.Unlock()
  80. return id
  81. }
  82. // @Private
  83. func (this *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Buffer) {
  84. msg := new(dns.Msg)
  85. err := msg.Unpack(payload.Value)
  86. if err != nil {
  87. log.Warning("DNS: Failed to parse DNS response: ", err)
  88. return
  89. }
  90. record := &ARecord{
  91. IPs: make([]net.IP, 0, 16),
  92. }
  93. id := msg.Id
  94. ttl := DefaultTTL
  95. log.Debug("DNS: Handling response for id ", id, " content: ", msg.String())
  96. this.Lock()
  97. request, found := this.requests[id]
  98. if !found {
  99. this.Unlock()
  100. return
  101. }
  102. delete(this.requests, id)
  103. this.Unlock()
  104. for _, rr := range msg.Answer {
  105. switch rr := rr.(type) {
  106. case *dns.A:
  107. record.IPs = append(record.IPs, rr.A)
  108. if rr.Hdr.Ttl < ttl {
  109. ttl = rr.Hdr.Ttl
  110. }
  111. case *dns.AAAA:
  112. record.IPs = append(record.IPs, rr.AAAA)
  113. if rr.Hdr.Ttl < ttl {
  114. ttl = rr.Hdr.Ttl
  115. }
  116. }
  117. }
  118. record.Expire = time.Now().Add(time.Second * time.Duration(ttl))
  119. request.response <- record
  120. close(request.response)
  121. }
  122. func (this *UDPNameServer) QueryA(domain string) <-chan *ARecord {
  123. response := make(chan *ARecord, 1)
  124. buffer := alloc.NewBuffer()
  125. msg := new(dns.Msg)
  126. msg.Id = this.AssignUnusedID(response)
  127. msg.RecursionDesired = true
  128. msg.Question = []dns.Question{
  129. dns.Question{
  130. Name: dns.Fqdn(domain),
  131. Qtype: dns.TypeA,
  132. Qclass: dns.ClassINET,
  133. }}
  134. writtenBuffer, _ := msg.PackBuffer(buffer.Value)
  135. buffer.Slice(0, len(writtenBuffer))
  136. fakeDestination := v2net.UDPDestination(v2net.LocalHostIP, v2net.Port(53))
  137. this.udpServer.Dispatch(fakeDestination, this.address, buffer, this.HandleResponse)
  138. return response
  139. }
  140. type LocalNameServer struct {
  141. }
  142. func (this *LocalNameServer) QueryA(domain string) <-chan *ARecord {
  143. response := make(chan *ARecord, 1)
  144. go func() {
  145. defer close(response)
  146. ips, err := net.LookupIP(domain)
  147. if err != nil {
  148. log.Info("DNS: Failed to lookup IPs for domain ", domain)
  149. return
  150. }
  151. response <- &ARecord{
  152. IPs: ips,
  153. Expire: time.Now().Add(time.Second * time.Duration(DefaultTTL)),
  154. }
  155. }()
  156. return response
  157. }