server_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package dns_test
  2. import (
  3. "context"
  4. "testing"
  5. "v2ray.com/core/app"
  6. "v2ray.com/core/app/dispatcher"
  7. _ "v2ray.com/core/app/dispatcher/impl"
  8. . "v2ray.com/core/app/dns"
  9. "v2ray.com/core/app/policy"
  10. _ "v2ray.com/core/app/policy/manager"
  11. "v2ray.com/core/app/proxyman"
  12. _ "v2ray.com/core/app/proxyman/outbound"
  13. "v2ray.com/core/common"
  14. "v2ray.com/core/common/net"
  15. "v2ray.com/core/common/serial"
  16. "v2ray.com/core/proxy/freedom"
  17. "v2ray.com/core/testing/servers/udp"
  18. . "v2ray.com/ext/assert"
  19. "github.com/miekg/dns"
  20. )
  21. type staticHandler struct {
  22. }
  23. func (*staticHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
  24. ans := new(dns.Msg)
  25. ans.Id = r.Id
  26. for _, q := range r.Question {
  27. if q.Name == "google.com." && q.Qtype == dns.TypeA {
  28. rr, _ := dns.NewRR("google.com. IN A 8.8.8.8")
  29. ans.Answer = append(ans.Answer, rr)
  30. } else if q.Name == "facebook.com." && q.Qtype == dns.TypeA {
  31. rr, _ := dns.NewRR("facebook.com. IN A 9.9.9.9")
  32. ans.Answer = append(ans.Answer, rr)
  33. }
  34. }
  35. w.WriteMsg(ans)
  36. }
  37. func TestUDPServer(t *testing.T) {
  38. assert := With(t)
  39. port := udp.PickPort()
  40. dnsServer := dns.Server{
  41. Addr: "127.0.0.1:" + port.String(),
  42. Net: "udp",
  43. Handler: &staticHandler{},
  44. UDPSize: 1200,
  45. }
  46. go dnsServer.ListenAndServe()
  47. config := &Config{
  48. NameServers: []*net.Endpoint{
  49. {
  50. Network: net.Network_UDP,
  51. Address: &net.IPOrDomain{
  52. Address: &net.IPOrDomain_Ip{
  53. Ip: []byte{127, 0, 0, 1},
  54. },
  55. },
  56. Port: uint32(port),
  57. },
  58. },
  59. }
  60. ctx := context.Background()
  61. space := app.NewSpace()
  62. ctx = app.ContextWithSpace(ctx, space)
  63. common.Must(app.AddApplicationToSpace(ctx, config))
  64. common.Must(app.AddApplicationToSpace(ctx, &dispatcher.Config{}))
  65. common.Must(app.AddApplicationToSpace(ctx, &proxyman.OutboundConfig{}))
  66. common.Must(app.AddApplicationToSpace(ctx, &policy.Config{}))
  67. om := proxyman.OutboundHandlerManagerFromSpace(space)
  68. om.AddHandler(ctx, &proxyman.OutboundHandlerConfig{
  69. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  70. })
  71. common.Must(space.Initialize())
  72. common.Must(space.Start())
  73. ips, err := net.LookupIP("google.com")
  74. assert(err, IsNil)
  75. assert(len(ips), Equals, 1)
  76. assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})
  77. ips, err = net.LookupIP("facebook.com")
  78. assert(err, IsNil)
  79. assert(len(ips), Equals, 1)
  80. assert([]byte(ips[0]), Equals, []byte{9, 9, 9, 9})
  81. dnsServer.Shutdown()
  82. ips, err = net.LookupIP("google.com")
  83. assert(err, IsNil)
  84. assert(len(ips), Equals, 1)
  85. assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})
  86. }