transport_test.go 4.2 KB

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