mux_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.LegacyInbound{
  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. LegacyOutbounds: []option.LegacyOutbound{
  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. Type: C.RuleTypeDefault,
  97. DefaultOptions: option.DefaultRule{
  98. RawDefaultRule: option.RawDefaultRule{
  99. Inbound: []string{"mixed-in"},
  100. },
  101. RuleAction: option.RuleAction{
  102. Action: C.RuleActionTypeRoute,
  103. RouteOptions: option.RouteActionOptions{
  104. Outbound: "ss-out",
  105. },
  106. },
  107. },
  108. },
  109. },
  110. },
  111. })
  112. testSuit(t, clientPort, testPort)
  113. }
  114. func testVMessMux(t *testing.T, options option.OutboundMultiplexOptions) {
  115. user, _ := uuid.NewV4()
  116. startInstance(t, option.Options{
  117. Inbounds: []option.LegacyInbound{
  118. {
  119. Type: C.TypeMixed,
  120. Tag: "mixed-in",
  121. MixedOptions: option.HTTPMixedInboundOptions{
  122. ListenOptions: option.ListenOptions{
  123. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  124. ListenPort: clientPort,
  125. },
  126. },
  127. },
  128. {
  129. Type: C.TypeVMess,
  130. VMessOptions: option.VMessInboundOptions{
  131. ListenOptions: option.ListenOptions{
  132. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  133. ListenPort: serverPort,
  134. },
  135. Users: []option.VMessUser{
  136. {
  137. UUID: user.String(),
  138. },
  139. },
  140. Multiplex: &option.InboundMultiplexOptions{
  141. Enabled: true,
  142. },
  143. },
  144. },
  145. },
  146. LegacyOutbounds: []option.LegacyOutbound{
  147. {
  148. Type: C.TypeDirect,
  149. },
  150. {
  151. Type: C.TypeVMess,
  152. Tag: "vmess-out",
  153. VMessOptions: option.VMessOutboundOptions{
  154. ServerOptions: option.ServerOptions{
  155. Server: "127.0.0.1",
  156. ServerPort: serverPort,
  157. },
  158. Security: "auto",
  159. UUID: user.String(),
  160. Multiplex: &options,
  161. },
  162. },
  163. },
  164. Route: &option.RouteOptions{
  165. Rules: []option.Rule{
  166. {
  167. Type: C.RuleTypeDefault,
  168. DefaultOptions: option.DefaultRule{
  169. RawDefaultRule: option.RawDefaultRule{
  170. Inbound: []string{"mixed-in"},
  171. },
  172. RuleAction: option.RuleAction{
  173. Action: C.RuleActionTypeRoute,
  174. RouteOptions: option.RouteActionOptions{
  175. Outbound: "vmess-out",
  176. },
  177. },
  178. },
  179. },
  180. },
  181. },
  182. })
  183. testSuit(t, clientPort, testPort)
  184. }