server_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package dns_test
  2. import (
  3. "testing"
  4. "time"
  5. "v2ray.com/core"
  6. "v2ray.com/core/app/dispatcher"
  7. . "v2ray.com/core/app/dns"
  8. "v2ray.com/core/app/policy"
  9. "v2ray.com/core/app/proxyman"
  10. _ "v2ray.com/core/app/proxyman/outbound"
  11. "v2ray.com/core/common"
  12. "v2ray.com/core/common/net"
  13. "v2ray.com/core/common/serial"
  14. "v2ray.com/core/proxy/freedom"
  15. "v2ray.com/core/testing/servers/udp"
  16. . "v2ray.com/ext/assert"
  17. "github.com/miekg/dns"
  18. )
  19. type staticHandler struct {
  20. }
  21. func (*staticHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
  22. ans := new(dns.Msg)
  23. ans.Id = r.Id
  24. for _, q := range r.Question {
  25. if q.Name == "google.com." && q.Qtype == dns.TypeA {
  26. rr, _ := dns.NewRR("google.com. IN A 8.8.8.8")
  27. ans.Answer = append(ans.Answer, rr)
  28. } else if q.Name == "facebook.com." && q.Qtype == dns.TypeA {
  29. rr, _ := dns.NewRR("facebook.com. IN A 9.9.9.9")
  30. ans.Answer = append(ans.Answer, rr)
  31. }
  32. }
  33. w.WriteMsg(ans)
  34. }
  35. func TestUDPServer(t *testing.T) {
  36. assert := With(t)
  37. port := udp.PickPort()
  38. listener, err := net.ListenUDP("udp4", &net.UDPAddr{
  39. IP: net.IP{127, 0, 0, 1},
  40. Port: int(port),
  41. })
  42. common.Must(err)
  43. dnsServer := dns.Server{
  44. PacketConn: listener,
  45. Handler: &staticHandler{},
  46. UDPSize: 1200,
  47. }
  48. go dnsServer.ActivateAndServe()
  49. time.Sleep(time.Second)
  50. config := &core.Config{
  51. App: []*serial.TypedMessage{
  52. serial.ToTypedMessage(&Config{
  53. NameServers: []*net.Endpoint{
  54. {
  55. Network: net.Network_UDP,
  56. Address: &net.IPOrDomain{
  57. Address: &net.IPOrDomain_Ip{
  58. Ip: []byte{127, 0, 0, 1},
  59. },
  60. },
  61. Port: uint32(port),
  62. },
  63. },
  64. }),
  65. serial.ToTypedMessage(&dispatcher.Config{}),
  66. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  67. serial.ToTypedMessage(&policy.Config{}),
  68. },
  69. Outbound: []*core.OutboundHandlerConfig{
  70. {
  71. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  72. },
  73. },
  74. }
  75. v, err := core.New(config)
  76. assert(err, IsNil)
  77. client := v.DNSClient()
  78. ips, err := client.LookupIP("google.com")
  79. assert(err, IsNil)
  80. assert(len(ips), Equals, 1)
  81. assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})
  82. ips, err = client.LookupIP("facebook.com")
  83. assert(err, IsNil)
  84. assert(len(ips), Equals, 1)
  85. assert([]byte(ips[0]), Equals, []byte{9, 9, 9, 9})
  86. dnsServer.Shutdown()
  87. ips, err = client.LookupIP("google.com")
  88. assert(err, IsNil)
  89. assert(len(ips), Equals, 1)
  90. assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})
  91. }
  92. func TestPrioritizedDomain(t *testing.T) {
  93. assert := With(t)
  94. port := udp.PickPort()
  95. listener, err := net.ListenUDP("udp4", &net.UDPAddr{
  96. IP: net.IP{127, 0, 0, 1},
  97. Port: int(port),
  98. })
  99. common.Must(err)
  100. dnsServer := dns.Server{
  101. PacketConn: listener,
  102. Handler: &staticHandler{},
  103. UDPSize: 1200,
  104. }
  105. go dnsServer.ActivateAndServe()
  106. time.Sleep(time.Second)
  107. config := &core.Config{
  108. App: []*serial.TypedMessage{
  109. serial.ToTypedMessage(&Config{
  110. NameServers: []*net.Endpoint{
  111. {
  112. Network: net.Network_UDP,
  113. Address: &net.IPOrDomain{
  114. Address: &net.IPOrDomain_Ip{
  115. Ip: []byte{127, 0, 0, 1},
  116. },
  117. },
  118. Port: 9999, /* unreachable */
  119. },
  120. },
  121. NameServer: []*NameServer{
  122. {
  123. Address: &net.Endpoint{
  124. Network: net.Network_UDP,
  125. Address: &net.IPOrDomain{
  126. Address: &net.IPOrDomain_Ip{
  127. Ip: []byte{127, 0, 0, 1},
  128. },
  129. },
  130. Port: uint32(port),
  131. },
  132. PrioritizedDomain: []*NameServer_PriorityDomain{
  133. {
  134. Type: DomainMatchingType_Full,
  135. Domain: "google.com",
  136. },
  137. },
  138. },
  139. },
  140. }),
  141. serial.ToTypedMessage(&dispatcher.Config{}),
  142. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  143. serial.ToTypedMessage(&policy.Config{}),
  144. },
  145. Outbound: []*core.OutboundHandlerConfig{
  146. {
  147. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  148. },
  149. },
  150. }
  151. v, err := core.New(config)
  152. assert(err, IsNil)
  153. client := v.DNSClient()
  154. startTime := time.Now()
  155. ips, err := client.LookupIP("google.com")
  156. assert(err, IsNil)
  157. assert(len(ips), Equals, 1)
  158. assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})
  159. endTime := time.Now()
  160. if startTime.After(endTime.Add(time.Second * 2)) {
  161. t.Error("DNS query doesn't finish in 2 seconds.")
  162. }
  163. }