mux_test.go 3.6 KB

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