freedom_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package freedom
  2. import (
  3. "io/ioutil"
  4. "net"
  5. "sync"
  6. "testing"
  7. "golang.org/x/net/proxy"
  8. "github.com/v2ray/v2ray-core"
  9. _ "github.com/v2ray/v2ray-core/proxy/socks"
  10. "github.com/v2ray/v2ray-core/testing/mocks"
  11. "github.com/v2ray/v2ray-core/testing/unit"
  12. )
  13. func TestSocksTcpConnect(t *testing.T) {
  14. assert := unit.Assert(t)
  15. port := 48274
  16. data2Send := "Data to be sent to remote"
  17. data2Return := "Data to be returned to local"
  18. var serverReady sync.Mutex
  19. serverReady.Lock()
  20. go func() {
  21. listener, err := net.ListenTCP("tcp", &net.TCPAddr{
  22. IP: []byte{0, 0, 0, 0},
  23. Port: port,
  24. Zone: "",
  25. })
  26. assert.Error(err).IsNil()
  27. serverReady.Unlock()
  28. conn, err := listener.Accept()
  29. assert.Error(err).IsNil()
  30. buffer := make([]byte, 1024)
  31. nBytes, err := conn.Read(buffer)
  32. assert.Error(err).IsNil()
  33. if string(buffer[:nBytes]) == data2Send {
  34. _, err = conn.Write([]byte(data2Return))
  35. assert.Error(err).IsNil()
  36. }
  37. conn.Close()
  38. listener.Close()
  39. }()
  40. pointPort := uint16(38724)
  41. config := mocks.Config{
  42. PortValue: pointPort,
  43. InboundConfigValue: &mocks.ConnectionConfig{
  44. ProtocolValue: "socks",
  45. ContentValue: []byte("{\"auth\": \"noauth\"}"),
  46. },
  47. OutboundConfigValue: &mocks.ConnectionConfig{
  48. ProtocolValue: "freedom",
  49. ContentValue: nil,
  50. },
  51. }
  52. point, err := core.NewPoint(&config)
  53. assert.Error(err).IsNil()
  54. err = point.Start()
  55. assert.Error(err).IsNil()
  56. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38724", nil, proxy.Direct)
  57. assert.Error(err).IsNil()
  58. serverReady.Lock()
  59. targetServer := "127.0.0.1:48274"
  60. conn, err := socks5Client.Dial("tcp", targetServer)
  61. assert.Error(err).IsNil()
  62. conn.Write([]byte(data2Send))
  63. if tcpConn, ok := conn.(*net.TCPConn); ok {
  64. tcpConn.CloseWrite()
  65. }
  66. dataReturned, err := ioutil.ReadAll(conn)
  67. assert.Error(err).IsNil()
  68. conn.Close()
  69. assert.Bytes(dataReturned).Equals([]byte(data2Return))
  70. }