sing-box/experimental/v2rayapi/stats.go

218 lines
6.4 KiB
Go
Raw Permalink Normal View History

2022-09-26 11:37:06 +00:00
package v2rayapi
import (
"context"
"net"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/experimental/trackerconn"
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network"
"go.uber.org/atomic"
)
func init() {
StatsService_ServiceDesc.ServiceName = "v2ray.core.app.stats.command.StatsService"
}
var (
_ adapter.V2RayStatsService = (*StatsService)(nil)
_ StatsServiceServer = (*StatsService)(nil)
)
type StatsService struct {
createdAt time.Time
inbounds map[string]bool
outbounds map[string]bool
2023-02-08 08:50:15 +00:00
users map[string]bool
2022-09-26 11:37:06 +00:00
access sync.Mutex
counters map[string]*atomic.Int64
}
func NewStatsService(options option.V2RayStatsServiceOptions) *StatsService {
if !options.Enabled {
return nil
}
inbounds := make(map[string]bool)
outbounds := make(map[string]bool)
2023-02-08 08:50:15 +00:00
users := make(map[string]bool)
2022-09-26 11:37:06 +00:00
for _, inbound := range options.Inbounds {
inbounds[inbound] = true
}
for _, outbound := range options.Outbounds {
outbounds[outbound] = true
}
2023-02-08 08:50:15 +00:00
for _, user := range options.Users {
users[user] = true
}
2022-09-26 11:37:06 +00:00
return &StatsService{
createdAt: time.Now(),
inbounds: inbounds,
outbounds: outbounds,
2023-02-08 08:50:15 +00:00
users: users,
2022-09-26 11:37:06 +00:00
counters: make(map[string]*atomic.Int64),
}
}
2023-02-08 08:50:15 +00:00
func (s *StatsService) RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn {
2022-10-26 11:20:34 +00:00
var readCounter []*atomic.Int64
var writeCounter []*atomic.Int64
2022-09-26 11:37:06 +00:00
countInbound := inbound != "" && s.inbounds[inbound]
countOutbound := outbound != "" && s.outbounds[outbound]
2023-02-08 08:50:15 +00:00
countUser := user != "" && s.users[user]
if !countInbound && !countOutbound && !countUser {
2022-09-26 11:37:06 +00:00
return conn
}
s.access.Lock()
if countInbound {
2022-10-26 11:20:34 +00:00
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
2022-09-26 11:37:06 +00:00
}
if countOutbound {
2022-10-26 11:20:34 +00:00
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>downlink"))
2022-09-26 11:37:06 +00:00
}
2023-02-08 08:50:15 +00:00
if countUser {
readCounter = append(readCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>downlink"))
}
2022-09-26 11:37:06 +00:00
s.access.Unlock()
2022-11-24 04:37:29 +00:00
return trackerconn.New(conn, readCounter, writeCounter)
2022-09-26 11:37:06 +00:00
}
2023-02-08 08:50:15 +00:00
func (s *StatsService) RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn {
2022-10-26 11:20:34 +00:00
var readCounter []*atomic.Int64
var writeCounter []*atomic.Int64
2022-09-26 11:37:06 +00:00
countInbound := inbound != "" && s.inbounds[inbound]
countOutbound := outbound != "" && s.outbounds[outbound]
2023-02-08 08:50:15 +00:00
countUser := user != "" && s.users[user]
if !countInbound && !countOutbound && !countUser {
2022-09-26 11:37:06 +00:00
return conn
}
s.access.Lock()
if countInbound {
2022-10-26 11:20:34 +00:00
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
2022-09-26 11:37:06 +00:00
}
if countOutbound {
2022-10-26 11:20:34 +00:00
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>downlink"))
2022-09-26 11:37:06 +00:00
}
2023-02-08 08:50:15 +00:00
if countUser {
readCounter = append(readCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>downlink"))
}
2022-09-26 11:37:06 +00:00
s.access.Unlock()
return trackerconn.NewPacket(conn, readCounter, writeCounter)
}
func (s *StatsService) GetStats(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
s.access.Lock()
counter, loaded := s.counters[request.Name]
s.access.Unlock()
if !loaded {
return nil, E.New(request.Name, " not found.")
}
var value int64
if request.Reset_ {
value = counter.Swap(0)
} else {
value = counter.Load()
}
return &GetStatsResponse{Stat: &Stat{Name: request.Name, Value: value}}, nil
}
func (s *StatsService) QueryStats(ctx context.Context, request *QueryStatsRequest) (*QueryStatsResponse, error) {
var response QueryStatsResponse
s.access.Lock()
defer s.access.Unlock()
2022-10-26 11:20:34 +00:00
if len(request.Patterns) == 0 {
for name, counter := range s.counters {
var value int64
if request.Reset_ {
value = counter.Swap(0)
} else {
value = counter.Load()
}
response.Stat = append(response.Stat, &Stat{Name: name, Value: value})
}
} else if request.Regexp {
2022-09-26 11:37:06 +00:00
matchers := make([]*regexp.Regexp, 0, len(request.Patterns))
for _, pattern := range request.Patterns {
matcher, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
matchers = append(matchers, matcher)
}
for name, counter := range s.counters {
for _, matcher := range matchers {
if matcher.MatchString(name) {
var value int64
if request.Reset_ {
value = counter.Swap(0)
} else {
value = counter.Load()
}
response.Stat = append(response.Stat, &Stat{Name: name, Value: value})
}
}
}
} else {
for name, counter := range s.counters {
for _, matcher := range request.Patterns {
if strings.Contains(name, matcher) {
var value int64
if request.Reset_ {
value = counter.Swap(0)
} else {
value = counter.Load()
}
response.Stat = append(response.Stat, &Stat{Name: name, Value: value})
}
}
}
}
return &response, nil
}
func (s *StatsService) GetSysStats(ctx context.Context, request *SysStatsRequest) (*SysStatsResponse, error) {
var rtm runtime.MemStats
runtime.ReadMemStats(&rtm)
response := &SysStatsResponse{
Uptime: uint32(time.Now().Sub(s.createdAt).Seconds()),
NumGoroutine: uint32(runtime.NumGoroutine()),
Alloc: rtm.Alloc,
TotalAlloc: rtm.TotalAlloc,
Sys: rtm.Sys,
Mallocs: rtm.Mallocs,
Frees: rtm.Frees,
LiveObjects: rtm.Mallocs - rtm.Frees,
NumGC: rtm.NumGC,
PauseTotalNs: rtm.PauseTotalNs,
}
return response, nil
}
func (s *StatsService) mustEmbedUnimplementedStatsServiceServer() {
}
2022-09-29 14:27:16 +00:00
//nolint:staticcheck
2022-10-26 11:20:34 +00:00
func (s *StatsService) loadOrCreateCounter(name string) *atomic.Int64 {
2022-09-26 11:37:06 +00:00
counter, loaded := s.counters[name]
2022-10-26 11:20:34 +00:00
if loaded {
return counter
2022-09-26 11:37:06 +00:00
}
2022-10-26 11:20:34 +00:00
counter = atomic.NewInt64(0)
s.counters[name] = counter
2022-09-26 11:37:06 +00:00
return counter
}