server_test.go 7.0 KB

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