socks_test.go 7.2 KB

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