server_test.go 6.9 KB

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