server_test.go 6.9 KB

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