mux_cool_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package main
  2. import (
  3. "net/netip"
  4. "os"
  5. "testing"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing-box/option"
  8. "github.com/spyzhov/ajson"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestMuxCoolServer(t *testing.T) {
  12. userId := newUUID()
  13. content, err := os.ReadFile("config/vmess-mux-client.json")
  14. require.NoError(t, err)
  15. config, err := ajson.Unmarshal(content)
  16. require.NoError(t, err)
  17. config.MustKey("inbounds").MustIndex(0).MustKey("port").SetNumeric(float64(clientPort))
  18. outbound := config.MustKey("outbounds").MustIndex(0).MustKey("settings").MustKey("vnext").MustIndex(0)
  19. outbound.MustKey("port").SetNumeric(float64(serverPort))
  20. user := outbound.MustKey("users").MustIndex(0)
  21. user.MustKey("id").SetString(userId.String())
  22. content, err = ajson.Marshal(config)
  23. require.NoError(t, err)
  24. startDockerContainer(t, DockerOptions{
  25. Image: ImageV2RayCore,
  26. Ports: []uint16{serverPort, testPort},
  27. EntryPoint: "v2ray",
  28. Cmd: []string{"run"},
  29. Stdin: content,
  30. })
  31. startInstance(t, option.Options{
  32. Inbounds: []option.Inbound{
  33. {
  34. Type: C.TypeVMess,
  35. VMessOptions: option.VMessInboundOptions{
  36. ListenOptions: option.ListenOptions{
  37. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  38. ListenPort: serverPort,
  39. },
  40. Users: []option.VMessUser{
  41. {
  42. Name: "sekai",
  43. UUID: userId.String(),
  44. },
  45. },
  46. },
  47. },
  48. },
  49. })
  50. testSuitSimple(t, clientPort, testPort)
  51. }
  52. func TestMuxCoolClient(t *testing.T) {
  53. user := newUUID()
  54. content, err := os.ReadFile("config/vmess-server.json")
  55. require.NoError(t, err)
  56. config, err := ajson.Unmarshal(content)
  57. require.NoError(t, err)
  58. inbound := config.MustKey("inbounds").MustIndex(0)
  59. inbound.MustKey("port").SetNumeric(float64(serverPort))
  60. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  61. content, err = ajson.Marshal(config)
  62. require.NoError(t, err)
  63. startDockerContainer(t, DockerOptions{
  64. Image: ImageXRayCore,
  65. Ports: []uint16{serverPort, testPort},
  66. EntryPoint: "xray",
  67. Stdin: content,
  68. })
  69. startInstance(t, option.Options{
  70. Inbounds: []option.Inbound{
  71. {
  72. Type: C.TypeMixed,
  73. MixedOptions: option.HTTPMixedInboundOptions{
  74. ListenOptions: option.ListenOptions{
  75. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  76. ListenPort: clientPort,
  77. },
  78. },
  79. },
  80. },
  81. Outbounds: []option.Outbound{
  82. {
  83. Type: C.TypeVMess,
  84. VMessOptions: option.VMessOutboundOptions{
  85. ServerOptions: option.ServerOptions{
  86. Server: "127.0.0.1",
  87. ServerPort: serverPort,
  88. },
  89. UUID: user.String(),
  90. PacketEncoding: "xudp",
  91. },
  92. },
  93. },
  94. })
  95. testSuitSimple(t, clientPort, testPort)
  96. }
  97. func TestMuxCoolSelf(t *testing.T) {
  98. user := newUUID()
  99. startInstance(t, option.Options{
  100. Inbounds: []option.Inbound{
  101. {
  102. Type: C.TypeMixed,
  103. Tag: "mixed-in",
  104. MixedOptions: option.HTTPMixedInboundOptions{
  105. ListenOptions: option.ListenOptions{
  106. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  107. ListenPort: clientPort,
  108. },
  109. },
  110. },
  111. {
  112. Type: C.TypeVMess,
  113. VMessOptions: option.VMessInboundOptions{
  114. ListenOptions: option.ListenOptions{
  115. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  116. ListenPort: serverPort,
  117. },
  118. Users: []option.VMessUser{
  119. {
  120. Name: "sekai",
  121. UUID: user.String(),
  122. },
  123. },
  124. },
  125. },
  126. },
  127. Outbounds: []option.Outbound{
  128. {
  129. Type: C.TypeDirect,
  130. },
  131. {
  132. Type: C.TypeVMess,
  133. Tag: "vmess-out",
  134. VMessOptions: option.VMessOutboundOptions{
  135. ServerOptions: option.ServerOptions{
  136. Server: "127.0.0.1",
  137. ServerPort: serverPort,
  138. },
  139. UUID: user.String(),
  140. PacketEncoding: "xudp",
  141. },
  142. },
  143. },
  144. Route: &option.RouteOptions{
  145. Rules: []option.Rule{
  146. {
  147. DefaultOptions: option.DefaultRule{
  148. Inbound: []string{"mixed-in"},
  149. Outbound: "vmess-out",
  150. },
  151. },
  152. },
  153. },
  154. })
  155. testSuitSimple(t, clientPort, testPort)
  156. }