freedom_test.go 3.5 KB

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