socks_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package socks
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "testing"
  8. "golang.org/x/net/proxy"
  9. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  10. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  11. "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  12. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  13. "github.com/v2ray/v2ray-core/shell/point"
  14. "github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
  15. "github.com/v2ray/v2ray-core/testing/unit"
  16. )
  17. func TestSocksTcpConnect(t *testing.T) {
  18. assert := unit.Assert(t)
  19. port := v2nettesting.PickPort()
  20. connInput := []byte("The data to be returned to socks server.")
  21. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  22. och := &proxymocks.OutboundConnectionHandler{
  23. ConnOutput: connOutput,
  24. ConnInput: bytes.NewReader(connInput),
  25. }
  26. connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  27. config := mocks.Config{
  28. PortValue: port,
  29. InboundConfigValue: &mocks.ConnectionConfig{
  30. ProtocolValue: "socks",
  31. SettingsValue: &json.SocksConfig{
  32. AuthMethod: "noauth",
  33. },
  34. },
  35. OutboundConfigValue: &mocks.ConnectionConfig{
  36. ProtocolValue: "mock_och",
  37. SettingsValue: nil,
  38. },
  39. }
  40. point, err := point.NewPoint(&config)
  41. assert.Error(err).IsNil()
  42. err = point.Start()
  43. assert.Error(err).IsNil()
  44. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct)
  45. assert.Error(err).IsNil()
  46. targetServer := "google.com:80"
  47. conn, err := socks5Client.Dial("tcp", targetServer)
  48. assert.Error(err).IsNil()
  49. data2Send := "The data to be sent to remote server."
  50. conn.Write([]byte(data2Send))
  51. if tcpConn, ok := conn.(*net.TCPConn); ok {
  52. tcpConn.CloseWrite()
  53. }
  54. dataReturned, err := ioutil.ReadAll(conn)
  55. assert.Error(err).IsNil()
  56. conn.Close()
  57. assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes())
  58. assert.Bytes(dataReturned).Equals(connInput)
  59. assert.String(targetServer).Equals(och.Destination.Address().String())
  60. }
  61. func TestSocksTcpConnectWithUserPass(t *testing.T) {
  62. assert := unit.Assert(t)
  63. port := v2nettesting.PickPort()
  64. connInput := []byte("The data to be returned to socks server.")
  65. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  66. och := &proxymocks.OutboundConnectionHandler{
  67. ConnInput: bytes.NewReader(connInput),
  68. ConnOutput: connOutput,
  69. }
  70. connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  71. config := mocks.Config{
  72. PortValue: port,
  73. InboundConfigValue: &mocks.ConnectionConfig{
  74. ProtocolValue: "socks",
  75. SettingsValue: &json.SocksConfig{
  76. AuthMethod: "password",
  77. Accounts: json.SocksAccountMap{
  78. "userx": "passy",
  79. },
  80. },
  81. },
  82. OutboundConfigValue: &mocks.ConnectionConfig{
  83. ProtocolValue: "mock_och",
  84. SettingsValue: nil,
  85. },
  86. }
  87. point, err := point.NewPoint(&config)
  88. assert.Error(err).IsNil()
  89. err = point.Start()
  90. assert.Error(err).IsNil()
  91. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passy"}, proxy.Direct)
  92. assert.Error(err).IsNil()
  93. targetServer := "1.2.3.4:443"
  94. conn, err := socks5Client.Dial("tcp", targetServer)
  95. assert.Error(err).IsNil()
  96. data2Send := "The data to be sent to remote server."
  97. conn.Write([]byte(data2Send))
  98. if tcpConn, ok := conn.(*net.TCPConn); ok {
  99. tcpConn.CloseWrite()
  100. }
  101. dataReturned, err := ioutil.ReadAll(conn)
  102. assert.Error(err).IsNil()
  103. conn.Close()
  104. assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes())
  105. assert.Bytes(dataReturned).Equals(connInput)
  106. assert.String(targetServer).Equals(och.Destination.Address().String())
  107. }
  108. func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
  109. assert := unit.Assert(t)
  110. port := v2nettesting.PickPort()
  111. connInput := []byte("The data to be returned to socks server.")
  112. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  113. och := &proxymocks.OutboundConnectionHandler{
  114. ConnInput: bytes.NewReader(connInput),
  115. ConnOutput: connOutput,
  116. }
  117. connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  118. config := mocks.Config{
  119. PortValue: port,
  120. InboundConfigValue: &mocks.ConnectionConfig{
  121. ProtocolValue: "socks",
  122. SettingsValue: &json.SocksConfig{
  123. AuthMethod: "password",
  124. Accounts: json.SocksAccountMap{
  125. "userx": "passy",
  126. },
  127. },
  128. },
  129. OutboundConfigValue: &mocks.ConnectionConfig{
  130. ProtocolValue: "mock_och",
  131. SettingsValue: nil,
  132. },
  133. }
  134. point, err := point.NewPoint(&config)
  135. assert.Error(err).IsNil()
  136. err = point.Start()
  137. assert.Error(err).IsNil()
  138. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passz"}, proxy.Direct)
  139. assert.Error(err).IsNil()
  140. targetServer := "1.2.3.4:443"
  141. _, err = socks5Client.Dial("tcp", targetServer)
  142. assert.Error(err).IsNotNil()
  143. }
  144. func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
  145. assert := unit.Assert(t)
  146. port := v2nettesting.PickPort()
  147. connInput := []byte("The data to be returned to socks server.")
  148. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  149. och := &proxymocks.OutboundConnectionHandler{
  150. ConnInput: bytes.NewReader(connInput),
  151. ConnOutput: connOutput,
  152. }
  153. connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  154. config := mocks.Config{
  155. PortValue: port,
  156. InboundConfigValue: &mocks.ConnectionConfig{
  157. ProtocolValue: "socks",
  158. SettingsValue: &json.SocksConfig{
  159. AuthMethod: "password",
  160. Accounts: json.SocksAccountMap{
  161. "userx": "passy",
  162. },
  163. },
  164. },
  165. OutboundConfigValue: &mocks.ConnectionConfig{
  166. ProtocolValue: "mock_och",
  167. SettingsValue: nil,
  168. },
  169. }
  170. point, err := point.NewPoint(&config)
  171. assert.Error(err).IsNil()
  172. err = point.Start()
  173. assert.Error(err).IsNil()
  174. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct)
  175. assert.Error(err).IsNil()
  176. targetServer := "1.2.3.4:443"
  177. _, err = socks5Client.Dial("tcp", targetServer)
  178. assert.Error(err).IsNotNil()
  179. }
  180. func TestSocksUdpSend(t *testing.T) {
  181. assert := unit.Assert(t)
  182. port := v2nettesting.PickPort()
  183. connInput := []byte("The data to be returned to socks server.")
  184. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  185. och := &proxymocks.OutboundConnectionHandler{
  186. ConnInput: bytes.NewReader(connInput),
  187. ConnOutput: connOutput,
  188. }
  189. connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  190. config := mocks.Config{
  191. PortValue: port,
  192. InboundConfigValue: &mocks.ConnectionConfig{
  193. ProtocolValue: "socks",
  194. SettingsValue: &json.SocksConfig{
  195. AuthMethod: "noauth",
  196. UDPEnabled: true,
  197. },
  198. },
  199. OutboundConfigValue: &mocks.ConnectionConfig{
  200. ProtocolValue: "mock_och",
  201. SettingsValue: nil,
  202. },
  203. }
  204. point, err := point.NewPoint(&config)
  205. assert.Error(err).IsNil()
  206. err = point.Start()
  207. assert.Error(err).IsNil()
  208. conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
  209. IP: []byte{127, 0, 0, 1},
  210. Port: int(port),
  211. Zone: "",
  212. })
  213. assert.Error(err).IsNil()
  214. data2Send := []byte("Fake DNS request")
  215. buffer := make([]byte, 0, 1024)
  216. buffer = append(buffer, 0, 0, 0)
  217. buffer = append(buffer, 1, 8, 8, 4, 4, 0, 53)
  218. buffer = append(buffer, data2Send...)
  219. conn.Write(buffer)
  220. response := make([]byte, 1024)
  221. nBytes, err := conn.Read(response)
  222. assert.Error(err).IsNil()
  223. assert.Bytes(response[10:nBytes]).Equals(connInput)
  224. assert.Bytes(data2Send).Equals(connOutput.Bytes())
  225. assert.String(och.Destination.String()).Equals("udp:8.8.4.4:53")
  226. }