socks_test.go 6.8 KB

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