freedom_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ich.Communicate(v2net.NewPacket(udpServerAddr, data2SendBuffer, false))
  61. assert.Bytes(connOutput.Bytes()).Equals([]byte("Processed: Data to be sent to remote"))
  62. }
  63. func TestSocksTcpConnect(t *testing.T) {
  64. v2testing.Current(t)
  65. port := v2nettesting.PickPort()
  66. data2Send := "Data to be sent to remote"
  67. tcpServer := &tcp.Server{
  68. Port: port,
  69. MsgProcessor: func(data []byte) []byte {
  70. buffer := make([]byte, 0, 2048)
  71. buffer = append(buffer, []byte("Processed: ")...)
  72. buffer = append(buffer, data...)
  73. return buffer
  74. },
  75. }
  76. _, err := tcpServer.Start()
  77. assert.Error(err).IsNil()
  78. pointPort := v2nettesting.PickPort()
  79. config := mocks.Config{
  80. PortValue: pointPort,
  81. InboundConfigValue: &mocks.ConnectionConfig{
  82. ProtocolValue: "socks",
  83. SettingsValue: &json.SocksConfig{
  84. AuthMethod: "auth",
  85. },
  86. },
  87. OutboundConfigValue: &mocks.ConnectionConfig{
  88. ProtocolValue: "freedom",
  89. SettingsValue: nil,
  90. },
  91. }
  92. point, err := point.NewPoint(&config)
  93. assert.Error(err).IsNil()
  94. err = point.Start()
  95. assert.Error(err).IsNil()
  96. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", pointPort), nil, proxy.Direct)
  97. assert.Error(err).IsNil()
  98. targetServer := fmt.Sprintf("127.0.0.1:%d", port)
  99. conn, err := socks5Client.Dial("tcp", targetServer)
  100. assert.Error(err).IsNil()
  101. conn.Write([]byte(data2Send))
  102. if tcpConn, ok := conn.(*net.TCPConn); ok {
  103. tcpConn.CloseWrite()
  104. }
  105. dataReturned, err := v2net.ReadFrom(conn, nil)
  106. assert.Error(err).IsNil()
  107. conn.Close()
  108. assert.Bytes(dataReturned.Value).Equals([]byte("Processed: Data to be sent to remote"))
  109. }