vless_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 TestVLESS(t *testing.T) {
  12. content, err := os.ReadFile("config/vless-server.json")
  13. require.NoError(t, err)
  14. config, err := ajson.Unmarshal(content)
  15. require.NoError(t, err)
  16. user := newUUID()
  17. inbound := config.MustKey("inbounds").MustIndex(0)
  18. inbound.MustKey("port").SetNumeric(float64(serverPort))
  19. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  20. content, err = ajson.Marshal(config)
  21. require.NoError(t, err)
  22. startDockerContainer(t, DockerOptions{
  23. Image: ImageV2RayCore,
  24. Ports: []uint16{serverPort, testPort},
  25. EntryPoint: "v2ray",
  26. Stdin: content,
  27. })
  28. startInstance(t, option.Options{
  29. Inbounds: []option.Inbound{
  30. {
  31. Type: C.TypeMixed,
  32. MixedOptions: option.HTTPMixedInboundOptions{
  33. ListenOptions: option.ListenOptions{
  34. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  35. ListenPort: clientPort,
  36. },
  37. },
  38. },
  39. },
  40. Outbounds: []option.Outbound{
  41. {
  42. Type: C.TypeVLESS,
  43. VLESSOptions: option.VLESSOutboundOptions{
  44. ServerOptions: option.ServerOptions{
  45. Server: "127.0.0.1",
  46. ServerPort: serverPort,
  47. },
  48. UUID: user.String(),
  49. },
  50. },
  51. },
  52. })
  53. testSuit(t, clientPort, testPort)
  54. }
  55. func TestVLESSXRay(t *testing.T) {
  56. testVLESSXray(t, "")
  57. }
  58. func TestVLESSXUDP(t *testing.T) {
  59. testVLESSXray(t, "xudp")
  60. }
  61. func testVLESSXray(t *testing.T, packetEncoding string) {
  62. content, err := os.ReadFile("config/vless-server.json")
  63. require.NoError(t, err)
  64. config, err := ajson.Unmarshal(content)
  65. require.NoError(t, err)
  66. user := newUUID()
  67. inbound := config.MustKey("inbounds").MustIndex(0)
  68. inbound.MustKey("port").SetNumeric(float64(serverPort))
  69. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  70. content, err = ajson.Marshal(config)
  71. require.NoError(t, err)
  72. startDockerContainer(t, DockerOptions{
  73. Image: ImageXRayCore,
  74. Ports: []uint16{serverPort, testPort},
  75. EntryPoint: "xray",
  76. Stdin: content,
  77. })
  78. startInstance(t, option.Options{
  79. Inbounds: []option.Inbound{
  80. {
  81. Type: C.TypeMixed,
  82. MixedOptions: option.HTTPMixedInboundOptions{
  83. ListenOptions: option.ListenOptions{
  84. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  85. ListenPort: clientPort,
  86. },
  87. },
  88. },
  89. },
  90. Outbounds: []option.Outbound{
  91. {
  92. Type: C.TypeVLESS,
  93. VLESSOptions: option.VLESSOutboundOptions{
  94. ServerOptions: option.ServerOptions{
  95. Server: "127.0.0.1",
  96. ServerPort: serverPort,
  97. },
  98. UUID: user.String(),
  99. PacketEncoding: packetEncoding,
  100. },
  101. },
  102. },
  103. })
  104. testSuit(t, clientPort, testPort)
  105. }