transport_test.go 4.1 KB

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