freedom_test.go 3.5 KB

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