v2ray_api_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. /*
  3. import (
  4. "context"
  5. "net/netip"
  6. "testing"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/experimental/v2rayapi"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing/common"
  11. "github.com/sagernet/sing/common/json/badoption"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestV2RayAPI(t *testing.T) {
  15. i := startInstance(t, option.Options{
  16. Inbounds: []option.Inbound{
  17. {
  18. Type: C.TypeMixed,
  19. Tag: "in",
  20. Options: &option.HTTPMixedInboundOptions{
  21. ListenOptions: option.ListenOptions{
  22. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  23. ListenPort: clientPort,
  24. },
  25. },
  26. },
  27. },
  28. Outbounds: []option.Outbound{
  29. {
  30. Type: C.TypeDirect,
  31. Tag: "out",
  32. },
  33. },
  34. Experimental: &option.ExperimentalOptions{
  35. V2RayAPI: &option.V2RayAPIOptions{
  36. Listen: "127.0.0.1:8080",
  37. Stats: &option.V2RayStatsServiceOptions{
  38. Enabled: true,
  39. Inbounds: []string{"in"},
  40. Outbounds: []string{"out"},
  41. },
  42. },
  43. },
  44. })
  45. testSuit(t, clientPort, testPort)
  46. statsService := i.Router().V2RayServer().StatsService()
  47. require.NotNil(t, statsService)
  48. response, err := statsService.(v2rayapi.StatsServiceServer).QueryStats(context.Background(), &v2rayapi.QueryStatsRequest{Regexp: true, Patterns: []string{".*"}})
  49. require.NoError(t, err)
  50. count := response.Stat[0].Value
  51. require.Equal(t, len(response.Stat), 4)
  52. for _, stat := range response.Stat {
  53. require.Equal(t, count, stat.Value)
  54. }
  55. }
  56. */