mux_test.go 4.3 KB

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