socks_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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: "password",
  69. Accounts: []json.SocksAccount{
  70. json.SocksAccount{
  71. Username: "userx",
  72. Password: "passy",
  73. },
  74. },
  75. },
  76. },
  77. OutboundConfigValue: &mocks.ConnectionConfig{
  78. ProtocolValue: "mock_och",
  79. SettingsValue: nil,
  80. },
  81. }
  82. point, err := core.NewPoint(&config)
  83. assert.Error(err).IsNil()
  84. err = point.Start()
  85. assert.Error(err).IsNil()
  86. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12386", &proxy.Auth{"userx", "passy"}, proxy.Direct)
  87. assert.Error(err).IsNil()
  88. targetServer := "1.2.3.4:443"
  89. conn, err := socks5Client.Dial("tcp", targetServer)
  90. assert.Error(err).IsNil()
  91. data2Send := "The data to be sent to remote server."
  92. conn.Write([]byte(data2Send))
  93. if tcpConn, ok := conn.(*net.TCPConn); ok {
  94. tcpConn.CloseWrite()
  95. }
  96. dataReturned, err := ioutil.ReadAll(conn)
  97. assert.Error(err).IsNil()
  98. conn.Close()
  99. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  100. assert.Bytes(dataReturned).Equals(och.Data2Return)
  101. assert.String(targetServer).Equals(och.Destination.Address().String())
  102. }
  103. func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
  104. assert := unit.Assert(t)
  105. port := uint16(12389)
  106. och := &mocks.OutboundConnectionHandler{
  107. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  108. Data2Return: []byte("The data to be returned to socks server."),
  109. }
  110. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  111. config := mocks.Config{
  112. PortValue: port,
  113. InboundConfigValue: &mocks.ConnectionConfig{
  114. ProtocolValue: "socks",
  115. SettingsValue: &json.SocksConfig{
  116. AuthMethod: "password",
  117. Accounts: []json.SocksAccount{
  118. json.SocksAccount{
  119. Username: "userx",
  120. Password: "passy",
  121. },
  122. },
  123. },
  124. },
  125. OutboundConfigValue: &mocks.ConnectionConfig{
  126. ProtocolValue: "mock_och",
  127. SettingsValue: nil,
  128. },
  129. }
  130. point, err := core.NewPoint(&config)
  131. assert.Error(err).IsNil()
  132. err = point.Start()
  133. assert.Error(err).IsNil()
  134. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12389", &proxy.Auth{"userx", "passz"}, proxy.Direct)
  135. assert.Error(err).IsNil()
  136. targetServer := "1.2.3.4:443"
  137. _, err = socks5Client.Dial("tcp", targetServer)
  138. assert.Error(err).IsNotNil()
  139. }
  140. func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
  141. assert := unit.Assert(t)
  142. port := uint16(38405)
  143. och := &mocks.OutboundConnectionHandler{
  144. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  145. Data2Return: []byte("The data to be returned to socks server."),
  146. }
  147. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  148. config := mocks.Config{
  149. PortValue: port,
  150. InboundConfigValue: &mocks.ConnectionConfig{
  151. ProtocolValue: "socks",
  152. SettingsValue: &json.SocksConfig{
  153. AuthMethod: "password",
  154. Accounts: []json.SocksAccount{
  155. json.SocksAccount{
  156. Username: "userx",
  157. Password: "passy",
  158. },
  159. },
  160. },
  161. },
  162. OutboundConfigValue: &mocks.ConnectionConfig{
  163. ProtocolValue: "mock_och",
  164. SettingsValue: nil,
  165. },
  166. }
  167. point, err := core.NewPoint(&config)
  168. assert.Error(err).IsNil()
  169. err = point.Start()
  170. assert.Error(err).IsNil()
  171. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38405", nil, proxy.Direct)
  172. assert.Error(err).IsNil()
  173. targetServer := "1.2.3.4:443"
  174. _, err = socks5Client.Dial("tcp", targetServer)
  175. assert.Error(err).IsNotNil()
  176. }
  177. func TestSocksUdpSend(t *testing.T) {
  178. assert := unit.Assert(t)
  179. port := uint16(12372)
  180. och := &mocks.OutboundConnectionHandler{
  181. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  182. Data2Return: []byte("The data to be returned to socks server."),
  183. }
  184. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  185. config := mocks.Config{
  186. PortValue: port,
  187. InboundConfigValue: &mocks.ConnectionConfig{
  188. ProtocolValue: "socks",
  189. SettingsValue: &json.SocksConfig{
  190. AuthMethod: "noauth",
  191. UDPEnabled: true,
  192. },
  193. },
  194. OutboundConfigValue: &mocks.ConnectionConfig{
  195. ProtocolValue: "mock_och",
  196. SettingsValue: nil,
  197. },
  198. }
  199. point, err := core.NewPoint(&config)
  200. assert.Error(err).IsNil()
  201. err = point.Start()
  202. assert.Error(err).IsNil()
  203. conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
  204. IP: []byte{127, 0, 0, 1},
  205. Port: int(port),
  206. Zone: "",
  207. })
  208. assert.Error(err).IsNil()
  209. data2Send := []byte("Fake DNS request")
  210. buffer := make([]byte, 0, 1024)
  211. buffer = append(buffer, 0, 0, 0)
  212. buffer = append(buffer, 1, 8, 8, 4, 4, 0, 53)
  213. buffer = append(buffer, data2Send...)
  214. conn.Write(buffer)
  215. response := make([]byte, 1024)
  216. nBytes, err := conn.Read(response)
  217. assert.Error(err).IsNil()
  218. assert.Bytes(response[10:nBytes]).Equals(och.Data2Return)
  219. assert.Bytes(data2Send).Equals(och.Data2Send.Bytes())
  220. assert.String(och.Destination.String()).Equals("udp:8.8.4.4:53")
  221. }