freedom_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package freedom
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net"
  6. "testing"
  7. "golang.org/x/net/proxy"
  8. "github.com/v2ray/v2ray-core/app"
  9. "github.com/v2ray/v2ray-core/common/alloc"
  10. v2net "github.com/v2ray/v2ray-core/common/net"
  11. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  12. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  13. _ "github.com/v2ray/v2ray-core/proxy/socks"
  14. "github.com/v2ray/v2ray-core/proxy/socks/json"
  15. proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
  16. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  17. "github.com/v2ray/v2ray-core/shell/point"
  18. "github.com/v2ray/v2ray-core/shell/point/testing/mocks"
  19. v2testing "github.com/v2ray/v2ray-core/testing"
  20. "github.com/v2ray/v2ray-core/testing/assert"
  21. "github.com/v2ray/v2ray-core/testing/servers/tcp"
  22. "github.com/v2ray/v2ray-core/testing/servers/udp"
  23. )
  24. func TestUDPSend(t *testing.T) {
  25. v2testing.Current(t)
  26. data2Send := "Data to be sent to remote"
  27. udpServer := &udp.Server{
  28. Port: 0,
  29. MsgProcessor: func(data []byte) []byte {
  30. buffer := make([]byte, 0, 2048)
  31. buffer = append(buffer, []byte("Processed: ")...)
  32. buffer = append(buffer, data...)
  33. return buffer
  34. },
  35. }
  36. udpServerAddr, err := udpServer.Start()
  37. assert.Error(err).IsNil()
  38. connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  39. ich := &proxymocks.InboundConnectionHandler{
  40. ConnInput: bytes.NewReader([]byte("Not Used")),
  41. ConnOutput: connOutput,
  42. }
  43. protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_ich", func(space app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) {
  44. ich.Space = space
  45. return ich, nil
  46. })
  47. assert.Error(err).IsNil()
  48. pointPort := v2nettesting.PickPort()
  49. config := mocks.Config{
  50. PortValue: pointPort,
  51. InboundConfigValue: &mocks.ConnectionConfig{
  52. ProtocolValue: protocol,
  53. SettingsValue: nil,
  54. },
  55. OutboundConfigValue: &mocks.ConnectionConfig{
  56. ProtocolValue: "freedom",
  57. SettingsValue: nil,
  58. },
  59. }
  60. point, err := point.NewPoint(&config)
  61. assert.Error(err).IsNil()
  62. err = point.Start()
  63. assert.Error(err).IsNil()
  64. data2SendBuffer := alloc.NewBuffer().Clear()
  65. data2SendBuffer.Append([]byte(data2Send))
  66. ich.Communicate(v2net.NewPacket(udpServerAddr, data2SendBuffer, false))
  67. assert.Bytes(connOutput.Bytes()).Equals([]byte("Processed: Data to be sent to remote"))
  68. }
  69. func TestSocksTcpConnect(t *testing.T) {
  70. v2testing.Current(t)
  71. port := v2nettesting.PickPort()
  72. data2Send := "Data to be sent to remote"
  73. tcpServer := &tcp.Server{
  74. Port: port,
  75. MsgProcessor: func(data []byte) []byte {
  76. buffer := make([]byte, 0, 2048)
  77. buffer = append(buffer, []byte("Processed: ")...)
  78. buffer = append(buffer, data...)
  79. return buffer
  80. },
  81. }
  82. _, err := tcpServer.Start()
  83. assert.Error(err).IsNil()
  84. pointPort := v2nettesting.PickPort()
  85. config := mocks.Config{
  86. PortValue: pointPort,
  87. InboundConfigValue: &mocks.ConnectionConfig{
  88. ProtocolValue: "socks",
  89. SettingsValue: &json.SocksConfig{
  90. AuthMethod: "auth",
  91. },
  92. },
  93. OutboundConfigValue: &mocks.ConnectionConfig{
  94. ProtocolValue: "freedom",
  95. SettingsValue: nil,
  96. },
  97. }
  98. point, err := point.NewPoint(&config)
  99. assert.Error(err).IsNil()
  100. err = point.Start()
  101. assert.Error(err).IsNil()
  102. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", pointPort), nil, proxy.Direct)
  103. assert.Error(err).IsNil()
  104. targetServer := fmt.Sprintf("127.0.0.1:%d", port)
  105. conn, err := socks5Client.Dial("tcp", targetServer)
  106. assert.Error(err).IsNil()
  107. conn.Write([]byte(data2Send))
  108. if tcpConn, ok := conn.(*net.TCPConn); ok {
  109. tcpConn.CloseWrite()
  110. }
  111. dataReturned, err := v2net.ReadFrom(conn, nil)
  112. assert.Error(err).IsNil()
  113. conn.Close()
  114. assert.Bytes(dataReturned.Value).Equals([]byte("Processed: Data to be sent to remote"))
  115. }