socks_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package socks
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net"
  6. "testing"
  7. "golang.org/x/net/proxy"
  8. "github.com/v2ray/v2ray-core"
  9. "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  10. "github.com/v2ray/v2ray-core/testing/mocks"
  11. "github.com/v2ray/v2ray-core/testing/unit"
  12. )
  13. func TestSocksTcpConnect(t *testing.T) {
  14. assert := unit.Assert(t)
  15. port := uint16(12385)
  16. och := &mocks.OutboundConnectionHandler{
  17. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  18. Data2Return: []byte("The data to be returned to socks server."),
  19. }
  20. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  21. config := mocks.Config{
  22. PortValue: port,
  23. InboundConfigValue: &mocks.ConnectionConfig{
  24. ProtocolValue: "socks",
  25. SettingsValue: &json.SocksConfig{
  26. AuthMethod: "noauth",
  27. },
  28. },
  29. OutboundConfigValue: &mocks.ConnectionConfig{
  30. ProtocolValue: "mock_och",
  31. SettingsValue: nil,
  32. },
  33. }
  34. point, err := core.NewPoint(&config)
  35. assert.Error(err).IsNil()
  36. err = point.Start()
  37. assert.Error(err).IsNil()
  38. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12385", nil, proxy.Direct)
  39. assert.Error(err).IsNil()
  40. targetServer := "google.com:80"
  41. conn, err := socks5Client.Dial("tcp", targetServer)
  42. assert.Error(err).IsNil()
  43. data2Send := "The data to be sent to remote server."
  44. conn.Write([]byte(data2Send))
  45. if tcpConn, ok := conn.(*net.TCPConn); ok {
  46. tcpConn.CloseWrite()
  47. }
  48. dataReturned, err := ioutil.ReadAll(conn)
  49. assert.Error(err).IsNil()
  50. conn.Close()
  51. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  52. assert.Bytes(dataReturned).Equals(och.Data2Return)
  53. assert.String(targetServer).Equals(och.Destination.Address().String())
  54. }
  55. func TestSocksTcpConnectWithUserPass(t *testing.T) {
  56. assert := unit.Assert(t)
  57. port := uint16(12386)
  58. och := &mocks.OutboundConnectionHandler{
  59. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  60. Data2Return: []byte("The data to be returned to socks server."),
  61. }
  62. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  63. config := mocks.Config{
  64. PortValue: port,
  65. InboundConfigValue: &mocks.ConnectionConfig{
  66. ProtocolValue: "socks",
  67. SettingsValue: &json.SocksConfig{
  68. AuthMethod: "password",
  69. Accounts: []json.SocksAccount{
  70. json.SocksAccount{
  71. Username: "userx",
  72. Password: "passy",
  73. },
  74. },
  75. },
  76. },
  77. OutboundConfigValue: &mocks.ConnectionConfig{
  78. ProtocolValue: "mock_och",
  79. SettingsValue: nil,
  80. },
  81. }
  82. point, err := core.NewPoint(&config)
  83. assert.Error(err).IsNil()
  84. err = point.Start()
  85. assert.Error(err).IsNil()
  86. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12386", &proxy.Auth{"userx", "passy"}, proxy.Direct)
  87. assert.Error(err).IsNil()
  88. targetServer := "1.2.3.4:443"
  89. conn, err := socks5Client.Dial("tcp", targetServer)
  90. assert.Error(err).IsNil()
  91. data2Send := "The data to be sent to remote server."
  92. conn.Write([]byte(data2Send))
  93. if tcpConn, ok := conn.(*net.TCPConn); ok {
  94. tcpConn.CloseWrite()
  95. }
  96. dataReturned, err := ioutil.ReadAll(conn)
  97. assert.Error(err).IsNil()
  98. conn.Close()
  99. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  100. assert.Bytes(dataReturned).Equals(och.Data2Return)
  101. assert.String(targetServer).Equals(och.Destination.Address().String())
  102. }
  103. func TestSocksUdpSend(t *testing.T) {
  104. assert := unit.Assert(t)
  105. port := uint16(12372)
  106. och := &mocks.OutboundConnectionHandler{
  107. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  108. Data2Return: []byte("The data to be returned to socks server."),
  109. }
  110. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  111. config := mocks.Config{
  112. PortValue: port,
  113. InboundConfigValue: &mocks.ConnectionConfig{
  114. ProtocolValue: "socks",
  115. SettingsValue: &json.SocksConfig{
  116. AuthMethod: "noauth",
  117. UDPEnabled: true,
  118. },
  119. },
  120. OutboundConfigValue: &mocks.ConnectionConfig{
  121. ProtocolValue: "mock_och",
  122. SettingsValue: nil,
  123. },
  124. }
  125. point, err := core.NewPoint(&config)
  126. assert.Error(err).IsNil()
  127. err = point.Start()
  128. assert.Error(err).IsNil()
  129. conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
  130. IP: []byte{127, 0, 0, 1},
  131. Port: int(port),
  132. Zone: "",
  133. })
  134. assert.Error(err).IsNil()
  135. data2Send := []byte("Fake DNS request")
  136. buffer := make([]byte, 0, 1024)
  137. buffer = append(buffer, 0, 0, 0)
  138. buffer = append(buffer, 1, 8, 8, 4, 4, 0, 53)
  139. buffer = append(buffer, data2Send...)
  140. conn.Write(buffer)
  141. response := make([]byte, 1024)
  142. nBytes, err := conn.Read(response)
  143. assert.Error(err).IsNil()
  144. assert.Bytes(response[10:nBytes]).Equals(och.Data2Return)
  145. assert.Bytes(data2Send).Equals(och.Data2Send.Bytes())
  146. assert.String(och.Destination.String()).Equals("udp:8.8.4.4:53")
  147. }