v2ray_api_test.go 1.4 KB

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