socks_test.go 8.1 KB

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