socks_end_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package scenarios
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/app/point"
  6. "github.com/v2ray/v2ray-core/app/point/config/testing/mocks"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  9. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  10. _ "github.com/v2ray/v2ray-core/proxy/socks"
  11. socksjson "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  12. _ "github.com/v2ray/v2ray-core/proxy/vmess"
  13. "github.com/v2ray/v2ray-core/proxy/vmess/config"
  14. vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
  15. "github.com/v2ray/v2ray-core/testing/servers/tcp"
  16. "github.com/v2ray/v2ray-core/testing/unit"
  17. )
  18. func setUpV2Ray() (uint16, error) {
  19. id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
  20. if err != nil {
  21. return 0, err
  22. }
  23. id2, err := config.NewID("93ccfc71-b136-4015-ac85-e037bd1ead9e")
  24. if err != nil {
  25. return 0, err
  26. }
  27. users := []*vmessjson.ConfigUser{
  28. &vmessjson.ConfigUser{Id: id1},
  29. &vmessjson.ConfigUser{Id: id2},
  30. }
  31. portB := v2nettesting.PickPort()
  32. configB := mocks.Config{
  33. PortValue: portB,
  34. InboundConfigValue: &mocks.ConnectionConfig{
  35. ProtocolValue: "vmess",
  36. SettingsValue: &vmessjson.Inbound{
  37. AllowedClients: users,
  38. },
  39. },
  40. OutboundConfigValue: &mocks.ConnectionConfig{
  41. ProtocolValue: "freedom",
  42. SettingsValue: nil,
  43. },
  44. }
  45. pointB, err := point.NewPoint(&configB)
  46. if err != nil {
  47. return 0, err
  48. }
  49. err = pointB.Start()
  50. if err != nil {
  51. return 0, err
  52. }
  53. portA := v2nettesting.PickPort()
  54. configA := mocks.Config{
  55. PortValue: portA,
  56. InboundConfigValue: &mocks.ConnectionConfig{
  57. ProtocolValue: "socks",
  58. SettingsValue: &socksjson.SocksConfig{
  59. AuthMethod: "noauth",
  60. UDPEnabled: true,
  61. HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
  62. },
  63. },
  64. OutboundConfigValue: &mocks.ConnectionConfig{
  65. ProtocolValue: "vmess",
  66. SettingsValue: &vmessjson.Outbound{
  67. []*vmessjson.ConfigTarget{
  68. &vmessjson.ConfigTarget{
  69. Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
  70. Users: users,
  71. },
  72. },
  73. },
  74. },
  75. }
  76. pointA, err := point.NewPoint(&configA)
  77. if err != nil {
  78. return 0, err
  79. }
  80. err = pointA.Start()
  81. if err != nil {
  82. return 0, err
  83. }
  84. return portA, nil
  85. }
  86. func TestTCPConnection(t *testing.T) {
  87. assert := unit.Assert(t)
  88. targetPort := v2nettesting.PickPort()
  89. tcpServer := &tcp.Server{
  90. Port: targetPort,
  91. MsgProcessor: func(data []byte) []byte {
  92. buffer := make([]byte, 0, 2048)
  93. buffer = append(buffer, []byte("Processed: ")...)
  94. buffer = append(buffer, data...)
  95. return buffer
  96. },
  97. }
  98. _, err := tcpServer.Start()
  99. assert.Error(err).IsNil()
  100. v2rayPort, err := setUpV2Ray()
  101. assert.Error(err).IsNil()
  102. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  103. IP: []byte{127, 0, 0, 1},
  104. Port: int(v2rayPort),
  105. })
  106. authRequest := socks5AuthMethodRequest(byte(0))
  107. nBytes, err := conn.Write(authRequest)
  108. assert.Int(nBytes).Equals(len(authRequest))
  109. assert.Error(err).IsNil()
  110. authResponse := make([]byte, 1024)
  111. nBytes, err = conn.Read(authResponse)
  112. assert.Error(err).IsNil()
  113. assert.Bytes(authResponse[:nBytes]).Equals([]byte{socks5Version, 0})
  114. connectRequest := socks5Request(byte(1), v2net.IPAddress([]byte{127, 0, 0, 1}, targetPort))
  115. nBytes, err = conn.Write(connectRequest)
  116. assert.Int(nBytes).Equals(len(connectRequest))
  117. assert.Error(err).IsNil()
  118. connectResponse := make([]byte, 1024)
  119. nBytes, err = conn.Read(connectResponse)
  120. assert.Error(err).IsNil()
  121. assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 0, 0, 1, 0, 0, 0, 0, 6, 181})
  122. actualRequest := []byte("Request to target server.")
  123. nBytes, err = conn.Write(actualRequest)
  124. assert.Error(err).IsNil()
  125. assert.Int(nBytes).Equals(len(actualRequest))
  126. actualRequest = []byte("Request to target server again.")
  127. nBytes, err = conn.Write(actualRequest)
  128. assert.Error(err).IsNil()
  129. assert.Int(nBytes).Equals(len(actualRequest))
  130. conn.CloseWrite()
  131. actualResponse := make([]byte, 1024)
  132. nBytes, err = conn.Read(actualResponse)
  133. assert.Error(err).IsNil()
  134. assert.String(string(actualResponse[:nBytes])).Equals("Processed: Request to target server.Request to target server again.")
  135. conn.Close()
  136. }
  137. func TestTCPBind(t *testing.T) {
  138. assert := unit.Assert(t)
  139. targetPort := v2nettesting.PickPort()
  140. tcpServer := &tcp.Server{
  141. Port: targetPort,
  142. MsgProcessor: func(data []byte) []byte {
  143. buffer := make([]byte, 0, 2048)
  144. buffer = append(buffer, []byte("Processed: ")...)
  145. buffer = append(buffer, data...)
  146. return buffer
  147. },
  148. }
  149. _, err := tcpServer.Start()
  150. assert.Error(err).IsNil()
  151. v2rayPort, err := setUpV2Ray()
  152. assert.Error(err).IsNil()
  153. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  154. IP: []byte{127, 0, 0, 1},
  155. Port: int(v2rayPort),
  156. })
  157. authRequest := socks5AuthMethodRequest(byte(0))
  158. nBytes, err := conn.Write(authRequest)
  159. assert.Int(nBytes).Equals(len(authRequest))
  160. assert.Error(err).IsNil()
  161. authResponse := make([]byte, 1024)
  162. nBytes, err = conn.Read(authResponse)
  163. assert.Error(err).IsNil()
  164. assert.Bytes(authResponse[:nBytes]).Equals([]byte{socks5Version, 0})
  165. connectRequest := socks5Request(byte(2), v2net.IPAddress([]byte{127, 0, 0, 1}, targetPort))
  166. nBytes, err = conn.Write(connectRequest)
  167. assert.Int(nBytes).Equals(len(connectRequest))
  168. assert.Error(err).IsNil()
  169. connectResponse := make([]byte, 1024)
  170. nBytes, err = conn.Read(connectResponse)
  171. assert.Error(err).IsNil()
  172. assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 7, 0, 1, 0, 0, 0, 0, 0, 0})
  173. conn.Close()
  174. }