freedom_test.go 3.0 KB

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