server_test.go 7.2 KB

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