freedom_test.go 3.5 KB

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