mux_test.go 3.4 KB

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