mux_cool_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. Stdin: content,
  29. })
  30. startInstance(t, option.Options{
  31. Inbounds: []option.Inbound{
  32. {
  33. Type: C.TypeVMess,
  34. VMessOptions: option.VMessInboundOptions{
  35. ListenOptions: option.ListenOptions{
  36. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  37. ListenPort: serverPort,
  38. },
  39. Users: []option.VMessUser{
  40. {
  41. Name: "sekai",
  42. UUID: userId.String(),
  43. },
  44. },
  45. },
  46. },
  47. },
  48. })
  49. testSuitSimple(t, clientPort, testPort)
  50. }
  51. func TestMuxCoolClient(t *testing.T) {
  52. user := newUUID()
  53. content, err := os.ReadFile("config/vmess-server.json")
  54. require.NoError(t, err)
  55. config, err := ajson.Unmarshal(content)
  56. require.NoError(t, err)
  57. inbound := config.MustKey("inbounds").MustIndex(0)
  58. inbound.MustKey("port").SetNumeric(float64(serverPort))
  59. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  60. content, err = ajson.Marshal(config)
  61. require.NoError(t, err)
  62. startDockerContainer(t, DockerOptions{
  63. Image: ImageXRayCore,
  64. Ports: []uint16{serverPort, testPort},
  65. EntryPoint: "xray",
  66. Stdin: content,
  67. })
  68. startInstance(t, option.Options{
  69. Inbounds: []option.Inbound{
  70. {
  71. Type: C.TypeMixed,
  72. MixedOptions: option.HTTPMixedInboundOptions{
  73. ListenOptions: option.ListenOptions{
  74. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  75. ListenPort: clientPort,
  76. },
  77. },
  78. },
  79. },
  80. Outbounds: []option.Outbound{
  81. {
  82. Type: C.TypeVMess,
  83. VMessOptions: option.VMessOutboundOptions{
  84. ServerOptions: option.ServerOptions{
  85. Server: "127.0.0.1",
  86. ServerPort: serverPort,
  87. },
  88. UUID: user.String(),
  89. PacketEncoding: "xudp",
  90. },
  91. },
  92. },
  93. })
  94. testSuitSimple(t, clientPort, testPort)
  95. }
  96. func TestMuxCoolSelf(t *testing.T) {
  97. user := newUUID()
  98. startInstance(t, option.Options{
  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. Name: "sekai",
  120. UUID: user.String(),
  121. },
  122. },
  123. },
  124. },
  125. },
  126. Outbounds: []option.Outbound{
  127. {
  128. Type: C.TypeDirect,
  129. },
  130. {
  131. Type: C.TypeVMess,
  132. Tag: "vmess-out",
  133. VMessOptions: option.VMessOutboundOptions{
  134. ServerOptions: option.ServerOptions{
  135. Server: "127.0.0.1",
  136. ServerPort: serverPort,
  137. },
  138. UUID: user.String(),
  139. PacketEncoding: "xudp",
  140. },
  141. },
  142. },
  143. Route: &option.RouteOptions{
  144. Rules: []option.Rule{
  145. {
  146. DefaultOptions: option.DefaultRule{
  147. Inbound: []string{"mixed-in"},
  148. Outbound: "vmess-out",
  149. },
  150. },
  151. },
  152. },
  153. })
  154. testSuitSimple(t, clientPort, testPort)
  155. }