mux_test.go 3.5 KB

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