freedom_test.go 3.3 KB

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