nameserver.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. this.Lock()
  96. request, found := this.requests[id]
  97. if !found {
  98. this.Unlock()
  99. return
  100. }
  101. delete(this.requests, id)
  102. this.Unlock()
  103. for _, rr := range msg.Answer {
  104. if a, ok := rr.(*dns.A); ok {
  105. record.IPs = append(record.IPs, a.A)
  106. if a.Hdr.Ttl < ttl {
  107. ttl = a.Hdr.Ttl
  108. }
  109. }
  110. }
  111. record.Expire = time.Now().Add(time.Second * time.Duration(ttl))
  112. request.response <- record
  113. close(request.response)
  114. }
  115. func (this *UDPNameServer) QueryA(domain string) <-chan *ARecord {
  116. response := make(chan *ARecord)
  117. buffer := alloc.NewBuffer()
  118. msg := new(dns.Msg)
  119. msg.Id = this.AssignUnusedID(response)
  120. msg.RecursionDesired = true
  121. msg.Question = []dns.Question{
  122. dns.Question{
  123. Name: dns.Fqdn(domain),
  124. Qtype: dns.TypeA,
  125. Qclass: dns.ClassINET,
  126. },
  127. dns.Question{
  128. Name: dns.Fqdn(domain),
  129. Qtype: dns.TypeAAAA,
  130. Qclass: dns.ClassINET,
  131. },
  132. }
  133. writtenBuffer, _ := msg.PackBuffer(buffer.Value)
  134. buffer.Slice(0, len(writtenBuffer))
  135. fakeDestination := v2net.UDPDestination(v2net.LocalHostIP, v2net.Port(53))
  136. this.udpServer.Dispatch(fakeDestination, this.address, buffer, this.HandleResponse)
  137. return response
  138. }