socks_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package socks_test
  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. v2proxy "github.com/v2ray/v2ray-core/proxy"
  12. proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
  13. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  14. "github.com/v2ray/v2ray-core/shell/point"
  15. v2testing "github.com/v2ray/v2ray-core/testing"
  16. "github.com/v2ray/v2ray-core/testing/assert"
  17. )
  18. func TestSocksTcpConnect(t *testing.T) {
  19. v2testing.Current(t)
  20. port := v2nettesting.PickPort()
  21. connInput := []byte("The data to be returned to socks server.")
  22. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  23. och := &proxymocks.OutboundConnectionHandler{
  24. ConnOutput: connOutput,
  25. ConnInput: bytes.NewReader(connInput),
  26. }
  27. protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) {
  28. return och, nil
  29. })
  30. assert.Error(err).IsNil()
  31. config := &point.Config{
  32. Port: port,
  33. InboundConfig: &point.ConnectionConfig{
  34. Protocol: "socks",
  35. Settings: []byte(`
  36. {
  37. "auth": "noauth"
  38. }`),
  39. },
  40. OutboundConfig: &point.ConnectionConfig{
  41. Protocol: protocol,
  42. Settings: nil,
  43. },
  44. }
  45. point, err := point.NewPoint(config)
  46. assert.Error(err).IsNil()
  47. err = point.Start()
  48. assert.Error(err).IsNil()
  49. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct)
  50. assert.Error(err).IsNil()
  51. targetServer := "google.com:80"
  52. conn, err := socks5Client.Dial("tcp", targetServer)
  53. assert.Error(err).IsNil()
  54. data2Send := "The data to be sent to remote server."
  55. conn.Write([]byte(data2Send))
  56. if tcpConn, ok := conn.(*net.TCPConn); ok {
  57. tcpConn.CloseWrite()
  58. }
  59. dataReturned, err := ioutil.ReadAll(conn)
  60. assert.Error(err).IsNil()
  61. conn.Close()
  62. assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes())
  63. assert.Bytes(dataReturned).Equals(connInput)
  64. assert.StringLiteral(targetServer).Equals(och.Destination.NetAddr())
  65. }
  66. func TestSocksTcpConnectWithUserPass(t *testing.T) {
  67. v2testing.Current(t)
  68. port := v2nettesting.PickPort()
  69. connInput := []byte("The data to be returned to socks server.")
  70. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  71. och := &proxymocks.OutboundConnectionHandler{
  72. ConnInput: bytes.NewReader(connInput),
  73. ConnOutput: connOutput,
  74. }
  75. protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) {
  76. return och, nil
  77. })
  78. assert.Error(err).IsNil()
  79. config := &point.Config{
  80. Port: port,
  81. InboundConfig: &point.ConnectionConfig{
  82. Protocol: "socks",
  83. Settings: []byte(`
  84. {
  85. "auth": "password",
  86. "accounts": [
  87. {"user": "userx", "pass": "passy"}
  88. ]
  89. }`),
  90. },
  91. OutboundConfig: &point.ConnectionConfig{
  92. Protocol: protocol,
  93. Settings: 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{}) (v2proxy.OutboundConnectionHandler, error) {
  127. return och, nil
  128. })
  129. assert.Error(err).IsNil()
  130. config := &point.Config{
  131. Port: port,
  132. InboundConfig: &point.ConnectionConfig{
  133. Protocol: "socks",
  134. Settings: []byte(`
  135. {
  136. "auth": "password",
  137. "accounts": [
  138. {"user": "userx", "pass": "passy"}
  139. ]
  140. }`),
  141. },
  142. OutboundConfig: &point.ConnectionConfig{
  143. Protocol: protocol,
  144. Settings: nil,
  145. },
  146. }
  147. point, err := point.NewPoint(config)
  148. assert.Error(err).IsNil()
  149. err = point.Start()
  150. assert.Error(err).IsNil()
  151. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passz"}, proxy.Direct)
  152. assert.Error(err).IsNil()
  153. targetServer := "1.2.3.4:443"
  154. _, err = socks5Client.Dial("tcp", targetServer)
  155. assert.Error(err).IsNotNil()
  156. }
  157. func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
  158. v2testing.Current(t)
  159. port := v2nettesting.PickPort()
  160. connInput := []byte("The data to be returned to socks server.")
  161. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  162. och := &proxymocks.OutboundConnectionHandler{
  163. ConnInput: bytes.NewReader(connInput),
  164. ConnOutput: connOutput,
  165. }
  166. protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) {
  167. return och, nil
  168. })
  169. assert.Error(err).IsNil()
  170. config := &point.Config{
  171. Port: port,
  172. InboundConfig: &point.ConnectionConfig{
  173. Protocol: "socks",
  174. Settings: []byte(`
  175. {
  176. "auth": "password",
  177. "accounts": [
  178. {"user": "userx", "pass": "passy"}
  179. ]
  180. }`),
  181. },
  182. OutboundConfig: &point.ConnectionConfig{
  183. Protocol: protocol,
  184. Settings: nil,
  185. },
  186. }
  187. point, err := point.NewPoint(config)
  188. assert.Error(err).IsNil()
  189. err = point.Start()
  190. assert.Error(err).IsNil()
  191. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct)
  192. assert.Error(err).IsNil()
  193. targetServer := "1.2.3.4:443"
  194. _, err = socks5Client.Dial("tcp", targetServer)
  195. assert.Error(err).IsNotNil()
  196. }
  197. func TestSocksUdpSend(t *testing.T) {
  198. v2testing.Current(t)
  199. port := v2nettesting.PickPort()
  200. connInput := []byte("The data to be returned to socks server.")
  201. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  202. och := &proxymocks.OutboundConnectionHandler{
  203. ConnInput: bytes.NewReader(connInput),
  204. ConnOutput: connOutput,
  205. }
  206. protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
  207. func(space app.Space, config interface{}) (v2proxy.OutboundConnectionHandler, error) {
  208. return och, nil
  209. })
  210. assert.Error(err).IsNil()
  211. config := &point.Config{
  212. Port: port,
  213. InboundConfig: &point.ConnectionConfig{
  214. Protocol: "socks",
  215. Settings: []byte(`{"auth": "noauth", "udp": true}`),
  216. },
  217. OutboundConfig: &point.ConnectionConfig{
  218. Protocol: protocol,
  219. Settings: 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. }