freedom_test.go 1.8 KB

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