mux_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.MultiplexOptions{
  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.MultiplexOptions{
  25. Enabled: true,
  26. Protocol: protocol,
  27. })
  28. })
  29. }
  30. }
  31. func TestShadowsockH2Mux(t *testing.T) {
  32. testShadowsocksMux(t, option.MultiplexOptions{
  33. Enabled: true,
  34. Protocol: "h2mux",
  35. Padding: true,
  36. })
  37. }
  38. func TestShadowsockSMuxPadding(t *testing.T) {
  39. testShadowsocksMux(t, option.MultiplexOptions{
  40. Enabled: true,
  41. Protocol: "smux",
  42. Padding: true,
  43. })
  44. }
  45. func testShadowsocksMux(t *testing.T, options option.MultiplexOptions) {
  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. },
  70. },
  71. },
  72. Outbounds: []option.Outbound{
  73. {
  74. Type: C.TypeDirect,
  75. },
  76. {
  77. Type: C.TypeShadowsocks,
  78. Tag: "ss-out",
  79. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  80. ServerOptions: option.ServerOptions{
  81. Server: "127.0.0.1",
  82. ServerPort: serverPort,
  83. },
  84. Method: method,
  85. Password: password,
  86. MultiplexOptions: &options,
  87. },
  88. },
  89. },
  90. Route: &option.RouteOptions{
  91. Rules: []option.Rule{
  92. {
  93. DefaultOptions: option.DefaultRule{
  94. Inbound: []string{"mixed-in"},
  95. Outbound: "ss-out",
  96. },
  97. },
  98. },
  99. },
  100. })
  101. testSuit(t, clientPort, testPort)
  102. }
  103. func testVMessMux(t *testing.T, options option.MultiplexOptions) {
  104. user, _ := uuid.NewV4()
  105. startInstance(t, option.Options{
  106. Inbounds: []option.Inbound{
  107. {
  108. Type: C.TypeMixed,
  109. Tag: "mixed-in",
  110. MixedOptions: option.HTTPMixedInboundOptions{
  111. ListenOptions: option.ListenOptions{
  112. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  113. ListenPort: clientPort,
  114. },
  115. },
  116. },
  117. {
  118. Type: C.TypeVMess,
  119. VMessOptions: option.VMessInboundOptions{
  120. ListenOptions: option.ListenOptions{
  121. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  122. ListenPort: serverPort,
  123. },
  124. Users: []option.VMessUser{
  125. {
  126. UUID: user.String(),
  127. },
  128. },
  129. },
  130. },
  131. },
  132. Outbounds: []option.Outbound{
  133. {
  134. Type: C.TypeDirect,
  135. },
  136. {
  137. Type: C.TypeVMess,
  138. Tag: "vmess-out",
  139. VMessOptions: option.VMessOutboundOptions{
  140. ServerOptions: option.ServerOptions{
  141. Server: "127.0.0.1",
  142. ServerPort: serverPort,
  143. },
  144. Security: "auto",
  145. UUID: user.String(),
  146. Multiplex: &options,
  147. },
  148. },
  149. },
  150. Route: &option.RouteOptions{
  151. Rules: []option.Rule{
  152. {
  153. DefaultOptions: option.DefaultRule{
  154. Inbound: []string{"mixed-in"},
  155. Outbound: "vmess-out",
  156. },
  157. },
  158. },
  159. },
  160. })
  161. testSuit(t, clientPort, testPort)
  162. }