dns_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package scenarios
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/xtls/xray-core/app/dns"
  7. "github.com/xtls/xray-core/app/proxyman"
  8. "github.com/xtls/xray-core/app/router"
  9. "github.com/xtls/xray-core/common"
  10. "github.com/xtls/xray-core/common/net"
  11. "github.com/xtls/xray-core/common/serial"
  12. "github.com/xtls/xray-core/core"
  13. "github.com/xtls/xray-core/proxy/blackhole"
  14. "github.com/xtls/xray-core/proxy/freedom"
  15. "github.com/xtls/xray-core/proxy/socks"
  16. "github.com/xtls/xray-core/testing/servers/tcp"
  17. xproxy "golang.org/x/net/proxy"
  18. )
  19. func TestResolveIP(t *testing.T) {
  20. tcpServer := tcp.Server{
  21. MsgProcessor: xor,
  22. }
  23. dest, err := tcpServer.Start()
  24. common.Must(err)
  25. defer tcpServer.Close()
  26. serverPort := tcp.PickPort()
  27. serverConfig := &core.Config{
  28. App: []*serial.TypedMessage{
  29. serial.ToTypedMessage(&dns.Config{
  30. StaticHosts: []*dns.Config_HostMapping{
  31. {
  32. Type: dns.DomainMatchingType_Full,
  33. Domain: "google.com",
  34. Ip: [][]byte{dest.Address.IP()},
  35. },
  36. },
  37. }),
  38. serial.ToTypedMessage(&router.Config{
  39. DomainStrategy: router.Config_IpIfNonMatch,
  40. Rule: []*router.RoutingRule{
  41. {
  42. Geoip: []*router.GeoIP{
  43. {
  44. Cidr: []*router.CIDR{
  45. {
  46. Ip: []byte{127, 0, 0, 0},
  47. Prefix: 8,
  48. },
  49. },
  50. },
  51. },
  52. TargetTag: &router.RoutingRule_Tag{
  53. Tag: "direct",
  54. },
  55. },
  56. },
  57. }),
  58. },
  59. Inbound: []*core.InboundHandlerConfig{
  60. {
  61. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  62. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
  63. Listen: net.NewIPOrDomain(net.LocalHostIP),
  64. }),
  65. ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
  66. AuthType: socks.AuthType_NO_AUTH,
  67. Accounts: map[string]string{
  68. "Test Account": "Test Password",
  69. },
  70. Address: net.NewIPOrDomain(net.LocalHostIP),
  71. UdpEnabled: false,
  72. }),
  73. },
  74. },
  75. Outbound: []*core.OutboundHandlerConfig{
  76. {
  77. ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
  78. },
  79. {
  80. Tag: "direct",
  81. ProxySettings: serial.ToTypedMessage(&freedom.Config{
  82. DomainStrategy: freedom.Config_USE_IP,
  83. }),
  84. },
  85. },
  86. }
  87. servers, err := InitializeServerConfigs(serverConfig)
  88. common.Must(err)
  89. defer CloseAllServers(servers)
  90. {
  91. noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
  92. common.Must(err)
  93. conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port))
  94. common.Must(err)
  95. defer conn.Close()
  96. if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
  97. t.Error(err)
  98. }
  99. }
  100. }