mux_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package main
  2. import (
  3. "net/netip"
  4. "testing"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/option"
  7. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  8. "github.com/gofrs/uuid/v5"
  9. )
  10. var muxProtocols = []string{
  11. "h2mux",
  12. "smux",
  13. "yamux",
  14. }
  15. func TestVMessSMux(t *testing.T) {
  16. testVMessMux(t, option.OutboundMultiplexOptions{
  17. Enabled: true,
  18. Protocol: "smux",
  19. })
  20. }
  21. func TestShadowsocksMux(t *testing.T) {
  22. for _, protocol := range muxProtocols {
  23. t.Run(protocol, func(t *testing.T) {
  24. testShadowsocksMux(t, option.OutboundMultiplexOptions{
  25. Enabled: true,
  26. Protocol: protocol,
  27. })
  28. })
  29. }
  30. }
  31. func TestShadowsockH2Mux(t *testing.T) {
  32. testShadowsocksMux(t, option.OutboundMultiplexOptions{
  33. Enabled: true,
  34. Protocol: "h2mux",
  35. Padding: true,
  36. })
  37. }
  38. func TestShadowsockSMuxPadding(t *testing.T) {
  39. testShadowsocksMux(t, option.OutboundMultiplexOptions{
  40. Enabled: true,
  41. Protocol: "smux",
  42. Padding: true,
  43. })
  44. }
  45. func testShadowsocksMux(t *testing.T, options option.OutboundMultiplexOptions) {
  46. method := shadowaead_2022.List[0]
  47. password := mkBase64(t, 16)
  48. startInstance(t, option.Options{
  49. Inbounds: []option.Inbound{
  50. {
  51. Type: C.TypeMixed,
  52. Tag: "mixed-in",
  53. MixedOptions: option.HTTPMixedInboundOptions{
  54. ListenOptions: option.ListenOptions{
  55. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  56. ListenPort: clientPort,
  57. },
  58. },
  59. },
  60. {
  61. Type: C.TypeShadowsocks,
  62. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  63. ListenOptions: option.ListenOptions{
  64. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  65. ListenPort: serverPort,
  66. },
  67. Method: method,
  68. Password: password,
  69. Multiplex: &option.InboundMultiplexOptions{
  70. Enabled: true,
  71. },
  72. },
  73. },
  74. },
  75. Outbounds: []option.Outbound{
  76. {
  77. Type: C.TypeDirect,
  78. },
  79. {
  80. Type: C.TypeShadowsocks,
  81. Tag: "ss-out",
  82. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  83. ServerOptions: option.ServerOptions{
  84. Server: "127.0.0.1",
  85. ServerPort: serverPort,
  86. },
  87. Method: method,
  88. Password: password,
  89. Multiplex: &options,
  90. },
  91. },
  92. },
  93. Route: &option.RouteOptions{
  94. Rules: []option.Rule{
  95. {
  96. DefaultOptions: option.DefaultRule{
  97. Inbound: []string{"mixed-in"},
  98. Outbound: "ss-out",
  99. },
  100. },
  101. },
  102. },
  103. })
  104. testSuit(t, clientPort, testPort)
  105. }
  106. func testVMessMux(t *testing.T, options option.OutboundMultiplexOptions) {
  107. user, _ := uuid.NewV4()
  108. startInstance(t, option.Options{
  109. Inbounds: []option.Inbound{
  110. {
  111. Type: C.TypeMixed,
  112. Tag: "mixed-in",
  113. MixedOptions: option.HTTPMixedInboundOptions{
  114. ListenOptions: option.ListenOptions{
  115. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  116. ListenPort: clientPort,
  117. },
  118. },
  119. },
  120. {
  121. Type: C.TypeVMess,
  122. VMessOptions: option.VMessInboundOptions{
  123. ListenOptions: option.ListenOptions{
  124. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  125. ListenPort: serverPort,
  126. },
  127. Users: []option.VMessUser{
  128. {
  129. UUID: user.String(),
  130. },
  131. },
  132. Multiplex: &option.InboundMultiplexOptions{
  133. Enabled: true,
  134. },
  135. },
  136. },
  137. },
  138. Outbounds: []option.Outbound{
  139. {
  140. Type: C.TypeDirect,
  141. },
  142. {
  143. Type: C.TypeVMess,
  144. Tag: "vmess-out",
  145. VMessOptions: option.VMessOutboundOptions{
  146. ServerOptions: option.ServerOptions{
  147. Server: "127.0.0.1",
  148. ServerPort: serverPort,
  149. },
  150. Security: "auto",
  151. UUID: user.String(),
  152. Multiplex: &options,
  153. },
  154. },
  155. },
  156. Route: &option.RouteOptions{
  157. Rules: []option.Rule{
  158. {
  159. DefaultOptions: option.DefaultRule{
  160. Inbound: []string{"mixed-in"},
  161. Outbound: "vmess-out",
  162. },
  163. },
  164. },
  165. },
  166. })
  167. testSuit(t, clientPort, testPort)
  168. }