transport_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package scenarios
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "v2ray.com/core"
  7. v2net "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/protocol"
  9. "v2ray.com/core/common/serial"
  10. "v2ray.com/core/common/uuid"
  11. "v2ray.com/core/proxy/dokodemo"
  12. "v2ray.com/core/proxy/freedom"
  13. "v2ray.com/core/proxy/vmess"
  14. "v2ray.com/core/proxy/vmess/inbound"
  15. "v2ray.com/core/proxy/vmess/outbound"
  16. "v2ray.com/core/testing/assert"
  17. "v2ray.com/core/testing/servers/tcp"
  18. "v2ray.com/core/transport/internet"
  19. "v2ray.com/core/transport/internet/headers/http"
  20. tcptransport "v2ray.com/core/transport/internet/tcp"
  21. )
  22. func TestNoOpConnectionHeader(t *testing.T) {
  23. assert := assert.On(t)
  24. tcpServer := tcp.Server{
  25. MsgProcessor: xor,
  26. }
  27. dest, err := tcpServer.Start()
  28. assert.Error(err).IsNil()
  29. defer tcpServer.Close()
  30. userID := protocol.NewID(uuid.New())
  31. serverPort := pickPort()
  32. serverConfig := &core.Config{
  33. Inbound: []*core.InboundConnectionConfig{
  34. {
  35. PortRange: v2net.SinglePortRange(serverPort),
  36. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  37. Settings: serial.ToTypedMessage(&inbound.Config{
  38. User: []*protocol.User{
  39. {
  40. Account: serial.ToTypedMessage(&vmess.Account{
  41. Id: userID.String(),
  42. }),
  43. },
  44. },
  45. }),
  46. StreamSettings: &internet.StreamConfig{
  47. TransportSettings: []*internet.TransportConfig{
  48. {
  49. Protocol: internet.TransportProtocol_TCP,
  50. Settings: serial.ToTypedMessage(&tcptransport.Config{
  51. HeaderSettings: serial.ToTypedMessage(&http.Config{}),
  52. }),
  53. },
  54. },
  55. },
  56. },
  57. },
  58. Outbound: []*core.OutboundConnectionConfig{
  59. {
  60. Settings: serial.ToTypedMessage(&freedom.Config{}),
  61. },
  62. },
  63. }
  64. clientPort := pickPort()
  65. clientConfig := &core.Config{
  66. Inbound: []*core.InboundConnectionConfig{
  67. {
  68. PortRange: v2net.SinglePortRange(clientPort),
  69. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  70. Settings: serial.ToTypedMessage(&dokodemo.Config{
  71. Address: v2net.NewIPOrDomain(dest.Address),
  72. Port: uint32(dest.Port),
  73. NetworkList: &v2net.NetworkList{
  74. Network: []v2net.Network{v2net.Network_TCP},
  75. },
  76. }),
  77. },
  78. },
  79. Outbound: []*core.OutboundConnectionConfig{
  80. {
  81. Settings: serial.ToTypedMessage(&outbound.Config{
  82. Receiver: []*protocol.ServerEndpoint{
  83. {
  84. Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
  85. Port: uint32(serverPort),
  86. User: []*protocol.User{
  87. {
  88. Account: serial.ToTypedMessage(&vmess.Account{
  89. Id: userID.String(),
  90. }),
  91. },
  92. },
  93. },
  94. },
  95. }),
  96. StreamSettings: &internet.StreamConfig{
  97. TransportSettings: []*internet.TransportConfig{
  98. {
  99. Protocol: internet.TransportProtocol_TCP,
  100. Settings: serial.ToTypedMessage(&tcptransport.Config{
  101. HeaderSettings: serial.ToTypedMessage(&http.Config{}),
  102. }),
  103. },
  104. },
  105. },
  106. },
  107. },
  108. }
  109. assert.Error(InitializeServerConfig(serverConfig)).IsNil()
  110. assert.Error(InitializeServerConfig(clientConfig)).IsNil()
  111. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  112. IP: []byte{127, 0, 0, 1},
  113. Port: int(clientPort),
  114. })
  115. assert.Error(err).IsNil()
  116. payload := "dokodemo request."
  117. nBytes, err := conn.Write([]byte(payload))
  118. assert.Error(err).IsNil()
  119. assert.Int(nBytes).Equals(len(payload))
  120. response := readFrom(conn, time.Second*2, len(payload))
  121. assert.Bytes(response).Equals(xor([]byte(payload)))
  122. assert.Error(conn.Close()).IsNil()
  123. CloseAllServers()
  124. }