socks_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package socks
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net"
  6. "testing"
  7. "golang.org/x/net/proxy"
  8. "github.com/v2ray/v2ray-core"
  9. "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  10. "github.com/v2ray/v2ray-core/testing/mocks"
  11. "github.com/v2ray/v2ray-core/testing/unit"
  12. )
  13. func TestSocksTcpConnect(t *testing.T) {
  14. assert := unit.Assert(t)
  15. port := uint16(12385)
  16. och := &mocks.OutboundConnectionHandler{
  17. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  18. Data2Return: []byte("The data to be returned to socks server."),
  19. }
  20. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  21. config := mocks.Config{
  22. PortValue: port,
  23. InboundConfigValue: &mocks.ConnectionConfig{
  24. ProtocolValue: "socks",
  25. SettingsValue: &json.SocksConfig{
  26. AuthMethod: "noauth",
  27. },
  28. },
  29. OutboundConfigValue: &mocks.ConnectionConfig{
  30. ProtocolValue: "mock_och",
  31. SettingsValue: nil,
  32. },
  33. }
  34. point, err := core.NewPoint(&config)
  35. assert.Error(err).IsNil()
  36. err = point.Start()
  37. assert.Error(err).IsNil()
  38. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12385", nil, proxy.Direct)
  39. assert.Error(err).IsNil()
  40. targetServer := "google.com:80"
  41. conn, err := socks5Client.Dial("tcp", targetServer)
  42. assert.Error(err).IsNil()
  43. data2Send := "The data to be sent to remote server."
  44. conn.Write([]byte(data2Send))
  45. if tcpConn, ok := conn.(*net.TCPConn); ok {
  46. tcpConn.CloseWrite()
  47. }
  48. dataReturned, err := ioutil.ReadAll(conn)
  49. assert.Error(err).IsNil()
  50. conn.Close()
  51. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  52. assert.Bytes(dataReturned).Equals(och.Data2Return)
  53. assert.String(targetServer).Equals(och.Destination.Address().String())
  54. }
  55. func TestSocksTcpConnectWithUserPass(t *testing.T) {
  56. assert := unit.Assert(t)
  57. port := uint16(12386)
  58. och := &mocks.OutboundConnectionHandler{
  59. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  60. Data2Return: []byte("The data to be returned to socks server."),
  61. }
  62. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  63. config := mocks.Config{
  64. PortValue: port,
  65. InboundConfigValue: &mocks.ConnectionConfig{
  66. ProtocolValue: "socks",
  67. SettingsValue: &json.SocksConfig{
  68. AuthMethod: "noauth",
  69. Username: "userx",
  70. Password: "passy",
  71. },
  72. },
  73. OutboundConfigValue: &mocks.ConnectionConfig{
  74. ProtocolValue: "mock_och",
  75. SettingsValue: nil,
  76. },
  77. }
  78. point, err := core.NewPoint(&config)
  79. assert.Error(err).IsNil()
  80. err = point.Start()
  81. assert.Error(err).IsNil()
  82. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12386", &proxy.Auth{"userx", "passy"}, proxy.Direct)
  83. assert.Error(err).IsNil()
  84. targetServer := "1.2.3.4:443"
  85. conn, err := socks5Client.Dial("tcp", targetServer)
  86. assert.Error(err).IsNil()
  87. data2Send := "The data to be sent to remote server."
  88. conn.Write([]byte(data2Send))
  89. if tcpConn, ok := conn.(*net.TCPConn); ok {
  90. tcpConn.CloseWrite()
  91. }
  92. dataReturned, err := ioutil.ReadAll(conn)
  93. assert.Error(err).IsNil()
  94. conn.Close()
  95. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  96. assert.Bytes(dataReturned).Equals(och.Data2Return)
  97. assert.String(targetServer).Equals(och.Destination.Address().String())
  98. }
  99. func TestSocksUdpSend(t *testing.T) {
  100. assert := unit.Assert(t)
  101. port := uint16(12372)
  102. och := &mocks.OutboundConnectionHandler{
  103. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  104. Data2Return: []byte("The data to be returned to socks server."),
  105. }
  106. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  107. config := mocks.Config{
  108. PortValue: port,
  109. InboundConfigValue: &mocks.ConnectionConfig{
  110. ProtocolValue: "socks",
  111. SettingsValue: &json.SocksConfig{
  112. AuthMethod: "noauth",
  113. UDPEnabled: true,
  114. },
  115. },
  116. OutboundConfigValue: &mocks.ConnectionConfig{
  117. ProtocolValue: "mock_och",
  118. SettingsValue: nil,
  119. },
  120. }
  121. point, err := core.NewPoint(&config)
  122. assert.Error(err).IsNil()
  123. err = point.Start()
  124. assert.Error(err).IsNil()
  125. conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
  126. IP: []byte{127, 0, 0, 1},
  127. Port: int(port),
  128. Zone: "",
  129. })
  130. assert.Error(err).IsNil()
  131. data2Send := []byte("Fake DNS request")
  132. buffer := make([]byte, 0, 1024)
  133. buffer = append(buffer, 0, 0, 0)
  134. buffer = append(buffer, 1, 8, 8, 4, 4, 0, 53)
  135. buffer = append(buffer, data2Send...)
  136. conn.Write(buffer)
  137. response := make([]byte, 1024)
  138. nBytes, err := conn.Read(response)
  139. assert.Error(err).IsNil()
  140. assert.Bytes(response[10:nBytes]).Equals(och.Data2Return)
  141. assert.Bytes(data2Send).Equals(och.Data2Send.Bytes())
  142. assert.String(och.Destination.String()).Equals("udp:8.8.4.4:53")
  143. }