sing-box/experimental/v2rayapi/stats.go

223 lines
6.7 KiB
Go
Raw Normal View History

2022-09-26 19:37:06 +08:00
package v2rayapi
import (
"context"
"net"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
2023-04-14 20:55:05 +08:00
"github.com/sagernet/sing/common/atomic"
2023-04-24 19:01:10 +08:00
"github.com/sagernet/sing/common/bufio"
2022-09-26 19:37:06 +08:00
E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network"
)
func init() {
StatsService_ServiceDesc.ServiceName = "v2ray.core.app.stats.command.StatsService"
}
var (
2024-11-10 16:46:59 +08:00
_ adapter.ConnectionTracker = (*StatsService)(nil)
2022-09-26 19:37:06 +08:00
_ StatsServiceServer = (*StatsService)(nil)
)
type StatsService struct {
createdAt time.Time
inbounds map[string]bool
outbounds map[string]bool
2023-02-08 16:50:15 +08:00
users map[string]bool
2022-09-26 19:37:06 +08: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 16:50:15 +08:00
users := make(map[string]bool)
2022-09-26 19:37:06 +08:00
for _, inbound := range options.Inbounds {
inbounds[inbound] = true
}
for _, outbound := range options.Outbounds {
outbounds[outbound] = true
}
2023-02-08 16:50:15 +08:00
for _, user := range options.Users {
users[user] = true
}
2022-09-26 19:37:06 +08:00
return &StatsService{
createdAt: time.Now(),
inbounds: inbounds,
outbounds: outbounds,
2023-02-08 16:50:15 +08:00
users: users,
2022-09-26 19:37:06 +08:00
counters: make(map[string]*atomic.Int64),
}
}
2024-11-10 16:46:59 +08:00
func (s *StatsService) RoutedConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, matchedRule adapter.Rule, matchOutbound adapter.Outbound) net.Conn {
inbound := metadata.Inbound
user := metadata.User
outbound := matchOutbound.Tag()
2022-10-26 19:20:34 +08:00
var readCounter []*atomic.Int64
var writeCounter []*atomic.Int64
2022-09-26 19:37:06 +08:00
countInbound := inbound != "" && s.inbounds[inbound]
countOutbound := outbound != "" && s.outbounds[outbound]
2023-02-08 16:50:15 +08:00
countUser := user != "" && s.users[user]
if !countInbound && !countOutbound && !countUser {
2022-09-26 19:37:06 +08:00
return conn
}
s.access.Lock()
if countInbound {
2022-10-26 19:20:34 +08:00
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
2022-09-26 19:37:06 +08:00
}
if countOutbound {
2022-10-26 19:20:34 +08:00
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>downlink"))
2022-09-26 19:37:06 +08:00
}
2023-02-08 16:50:15 +08:00
if countUser {
readCounter = append(readCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>downlink"))
}
2022-09-26 19:37:06 +08:00
s.access.Unlock()
2023-04-24 19:01:10 +08:00
return bufio.NewInt64CounterConn(conn, readCounter, writeCounter)
2022-09-26 19:37:06 +08:00
}
2024-11-10 16:46:59 +08:00
func (s *StatsService) RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, matchedRule adapter.Rule, matchOutbound adapter.Outbound) N.PacketConn {
inbound := metadata.Inbound
user := metadata.User
outbound := matchOutbound.Tag()
2022-10-26 19:20:34 +08:00
var readCounter []*atomic.Int64
var writeCounter []*atomic.Int64
2022-09-26 19:37:06 +08:00
countInbound := inbound != "" && s.inbounds[inbound]
countOutbound := outbound != "" && s.outbounds[outbound]
2023-02-08 16:50:15 +08:00
countUser := user != "" && s.users[user]
if !countInbound && !countOutbound && !countUser {
2022-09-26 19:37:06 +08:00
return conn
}
s.access.Lock()
if countInbound {
2022-10-26 19:20:34 +08:00
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
2022-09-26 19:37:06 +08:00
}
if countOutbound {
2022-10-26 19:20:34 +08:00
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>downlink"))
2022-09-26 19:37:06 +08:00
}
2023-02-08 16:50:15 +08:00
if countUser {
readCounter = append(readCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>uplink"))
writeCounter = append(writeCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>downlink"))
}
2022-09-26 19:37:06 +08:00
s.access.Unlock()
2023-04-24 19:01:10 +08:00
return bufio.NewInt64CounterPacketConn(conn, readCounter, writeCounter)
2022-09-26 19:37:06 +08:00
}
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 19:20:34 +08: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 19:37:06 +08: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 22:27:16 +08:00
//nolint:staticcheck
2022-10-26 19:20:34 +08:00
func (s *StatsService) loadOrCreateCounter(name string) *atomic.Int64 {
2022-09-26 19:37:06 +08:00
counter, loaded := s.counters[name]
2022-10-26 19:20:34 +08:00
if loaded {
return counter
2022-09-26 19:37:06 +08:00
}
2023-04-14 20:55:05 +08:00
counter = &atomic.Int64{}
2022-10-26 19:20:34 +08:00
s.counters[name] = counter
2022-09-26 19:37:06 +08:00
return counter
}