transport_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package v4_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/v2fly/v2ray-core/v5/common/protocol"
  7. "github.com/v2fly/v2ray-core/v5/common/serial"
  8. "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/socketcfg"
  9. "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/testassist"
  10. v4 "github.com/v2fly/v2ray-core/v5/infra/conf/v4"
  11. "github.com/v2fly/v2ray-core/v5/transport"
  12. "github.com/v2fly/v2ray-core/v5/transport/internet"
  13. "github.com/v2fly/v2ray-core/v5/transport/internet/headers/http"
  14. "github.com/v2fly/v2ray-core/v5/transport/internet/headers/noop"
  15. "github.com/v2fly/v2ray-core/v5/transport/internet/headers/tls"
  16. "github.com/v2fly/v2ray-core/v5/transport/internet/kcp"
  17. "github.com/v2fly/v2ray-core/v5/transport/internet/quic"
  18. "github.com/v2fly/v2ray-core/v5/transport/internet/tcp"
  19. "github.com/v2fly/v2ray-core/v5/transport/internet/websocket"
  20. )
  21. func TestSocketConfig(t *testing.T) {
  22. createParser := func() func(string) (proto.Message, error) {
  23. return func(s string) (proto.Message, error) {
  24. config := new(socketcfg.SocketConfig)
  25. if err := json.Unmarshal([]byte(s), config); err != nil {
  26. return nil, err
  27. }
  28. return config.Build()
  29. }
  30. }
  31. testassist.RunMultiTestCase(t, []testassist.TestCase{
  32. {
  33. Input: `{
  34. "mark": 1,
  35. "tcpFastOpen": true,
  36. "tcpFastOpenQueueLength": 1024,
  37. "mptcp": true
  38. }`,
  39. Parser: createParser(),
  40. Output: &internet.SocketConfig{
  41. Mark: 1,
  42. Tfo: internet.SocketConfig_Enable,
  43. TfoQueueLength: 1024,
  44. Mptcp: internet.MPTCPState_Enable,
  45. },
  46. },
  47. })
  48. }
  49. func TestTransportConfig(t *testing.T) {
  50. createParser := func() func(string) (proto.Message, error) {
  51. return func(s string) (proto.Message, error) {
  52. config := new(v4.TransportConfig)
  53. if err := json.Unmarshal([]byte(s), config); err != nil {
  54. return nil, err
  55. }
  56. return config.Build()
  57. }
  58. }
  59. testassist.RunMultiTestCase(t, []testassist.TestCase{
  60. {
  61. Input: `{
  62. "tcpSettings": {
  63. "header": {
  64. "type": "http",
  65. "request": {
  66. "version": "1.1",
  67. "method": "GET",
  68. "path": "/b",
  69. "headers": {
  70. "a": "b",
  71. "c": "d"
  72. }
  73. },
  74. "response": {
  75. "version": "1.0",
  76. "status": "404",
  77. "reason": "Not Found"
  78. }
  79. }
  80. },
  81. "kcpSettings": {
  82. "mtu": 1200,
  83. "header": {
  84. "type": "none"
  85. }
  86. },
  87. "wsSettings": {
  88. "path": "/t"
  89. },
  90. "quicSettings": {
  91. "key": "abcd",
  92. "header": {
  93. "type": "dtls"
  94. }
  95. }
  96. }`,
  97. Parser: createParser(),
  98. Output: &transport.Config{
  99. TransportSettings: []*internet.TransportConfig{
  100. {
  101. ProtocolName: "tcp",
  102. Settings: serial.ToTypedMessage(&tcp.Config{
  103. HeaderSettings: serial.ToTypedMessage(&http.Config{
  104. Request: &http.RequestConfig{
  105. Version: &http.Version{Value: "1.1"},
  106. Method: &http.Method{Value: "GET"},
  107. Uri: []string{"/b"},
  108. Header: []*http.Header{
  109. {Name: "a", Value: []string{"b"}},
  110. {Name: "c", Value: []string{"d"}},
  111. },
  112. },
  113. Response: &http.ResponseConfig{
  114. Version: &http.Version{Value: "1.0"},
  115. Status: &http.Status{Code: "404", Reason: "Not Found"},
  116. Header: []*http.Header{
  117. {
  118. Name: "Content-Type",
  119. Value: []string{"application/octet-stream", "video/mpeg"},
  120. },
  121. {
  122. Name: "Transfer-Encoding",
  123. Value: []string{"chunked"},
  124. },
  125. {
  126. Name: "Connection",
  127. Value: []string{"keep-alive"},
  128. },
  129. {
  130. Name: "Pragma",
  131. Value: []string{"no-cache"},
  132. },
  133. {
  134. Name: "Cache-Control",
  135. Value: []string{"private", "no-cache"},
  136. },
  137. },
  138. },
  139. }),
  140. }),
  141. },
  142. {
  143. ProtocolName: "mkcp",
  144. Settings: serial.ToTypedMessage(&kcp.Config{
  145. Mtu: &kcp.MTU{Value: 1200},
  146. HeaderConfig: serial.ToTypedMessage(&noop.Config{}),
  147. }),
  148. },
  149. {
  150. ProtocolName: "websocket",
  151. Settings: serial.ToTypedMessage(&websocket.Config{
  152. Path: "/t",
  153. }),
  154. },
  155. {
  156. ProtocolName: "quic",
  157. Settings: serial.ToTypedMessage(&quic.Config{
  158. Key: "abcd",
  159. Security: &protocol.SecurityConfig{
  160. Type: protocol.SecurityType_NONE,
  161. },
  162. Header: serial.ToTypedMessage(&tls.PacketConfig{}),
  163. }),
  164. },
  165. },
  166. },
  167. },
  168. })
  169. }