freedom_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/app/dispatcher"
  10. "github.com/v2ray/v2ray-core/common/alloc"
  11. v2io "github.com/v2ray/v2ray-core/common/io"
  12. v2net "github.com/v2ray/v2ray-core/common/net"
  13. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  14. v2proxy "github.com/v2ray/v2ray-core/proxy"
  15. _ "github.com/v2ray/v2ray-core/proxy/socks"
  16. proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
  17. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  18. "github.com/v2ray/v2ray-core/shell/point"
  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",
  44. func(space app.Space, config interface{}) (v2proxy.InboundHandler, error) {
  45. ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
  46. return ich, nil
  47. })
  48. assert.Error(err).IsNil()
  49. pointPort := v2nettesting.PickPort()
  50. config := &point.Config{
  51. Port: pointPort,
  52. InboundConfig: &point.ConnectionConfig{
  53. Protocol: protocol,
  54. Settings: nil,
  55. },
  56. OutboundConfig: &point.ConnectionConfig{
  57. Protocol: "freedom",
  58. Settings: nil,
  59. },
  60. }
  61. point, err := point.NewPoint(config)
  62. assert.Error(err).IsNil()
  63. err = point.Start()
  64. assert.Error(err).IsNil()
  65. data2SendBuffer := alloc.NewBuffer().Clear()
  66. data2SendBuffer.Append([]byte(data2Send))
  67. ich.Communicate(v2net.NewPacket(udpServerAddr, data2SendBuffer, false))
  68. assert.Bytes(connOutput.Bytes()).Equals([]byte("Processed: Data to be sent to remote"))
  69. }
  70. func TestSocksTcpConnect(t *testing.T) {
  71. v2testing.Current(t)
  72. port := v2nettesting.PickPort()
  73. data2Send := "Data to be sent to remote"
  74. tcpServer := &tcp.Server{
  75. Port: port,
  76. MsgProcessor: func(data []byte) []byte {
  77. buffer := make([]byte, 0, 2048)
  78. buffer = append(buffer, []byte("Processed: ")...)
  79. buffer = append(buffer, data...)
  80. return buffer
  81. },
  82. }
  83. _, err := tcpServer.Start()
  84. assert.Error(err).IsNil()
  85. pointPort := v2nettesting.PickPort()
  86. config := &point.Config{
  87. Port: pointPort,
  88. InboundConfig: &point.ConnectionConfig{
  89. Protocol: "socks",
  90. Settings: []byte(`{"auth": "noauth"}`),
  91. },
  92. OutboundConfig: &point.ConnectionConfig{
  93. Protocol: "freedom",
  94. Settings: nil,
  95. },
  96. }
  97. point, err := point.NewPoint(config)
  98. assert.Error(err).IsNil()
  99. err = point.Start()
  100. assert.Error(err).IsNil()
  101. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", pointPort), nil, proxy.Direct)
  102. assert.Error(err).IsNil()
  103. targetServer := fmt.Sprintf("127.0.0.1:%d", port)
  104. conn, err := socks5Client.Dial("tcp", targetServer)
  105. assert.Error(err).IsNil()
  106. conn.Write([]byte(data2Send))
  107. if tcpConn, ok := conn.(*net.TCPConn); ok {
  108. tcpConn.CloseWrite()
  109. }
  110. dataReturned, err := v2io.ReadFrom(conn, nil)
  111. assert.Error(err).IsNil()
  112. conn.Close()
  113. assert.Bytes(dataReturned.Value).Equals([]byte("Processed: Data to be sent to remote"))
  114. }