freedom_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package freedom
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "golang.org/x/net/proxy"
  7. v2io "github.com/v2ray/v2ray-core/common/io"
  8. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  9. _ "github.com/v2ray/v2ray-core/proxy/socks"
  10. "github.com/v2ray/v2ray-core/shell/point"
  11. v2testing "github.com/v2ray/v2ray-core/testing"
  12. "github.com/v2ray/v2ray-core/testing/assert"
  13. "github.com/v2ray/v2ray-core/testing/servers/tcp"
  14. )
  15. func TestSocksTcpConnect(t *testing.T) {
  16. v2testing.Current(t)
  17. port := v2nettesting.PickPort()
  18. data2Send := "Data to be sent to remote"
  19. tcpServer := &tcp.Server{
  20. Port: port,
  21. MsgProcessor: func(data []byte) []byte {
  22. buffer := make([]byte, 0, 2048)
  23. buffer = append(buffer, []byte("Processed: ")...)
  24. buffer = append(buffer, data...)
  25. return buffer
  26. },
  27. }
  28. _, err := tcpServer.Start()
  29. assert.Error(err).IsNil()
  30. pointPort := v2nettesting.PickPort()
  31. config := &point.Config{
  32. Port: pointPort,
  33. InboundConfig: &point.ConnectionConfig{
  34. Protocol: "socks",
  35. Settings: []byte(`{"auth": "noauth"}`),
  36. },
  37. OutboundConfig: &point.ConnectionConfig{
  38. Protocol: "freedom",
  39. Settings: nil,
  40. },
  41. }
  42. point, err := point.NewPoint(config)
  43. assert.Error(err).IsNil()
  44. err = point.Start()
  45. assert.Error(err).IsNil()
  46. socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", pointPort), nil, proxy.Direct)
  47. assert.Error(err).IsNil()
  48. targetServer := fmt.Sprintf("127.0.0.1:%d", port)
  49. conn, err := socks5Client.Dial("tcp", targetServer)
  50. assert.Error(err).IsNil()
  51. conn.Write([]byte(data2Send))
  52. if tcpConn, ok := conn.(*net.TCPConn); ok {
  53. tcpConn.CloseWrite()
  54. }
  55. dataReturned, err := v2io.ReadFrom(conn, nil)
  56. assert.Error(err).IsNil()
  57. conn.Close()
  58. assert.Bytes(dataReturned.Value).Equals([]byte("Processed: Data to be sent to remote"))
  59. }