mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
v2ray user stats
This commit is contained in:
parent
86ea035bdd
commit
21dac06f52
@ -45,6 +45,6 @@ type V2RayServer interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type V2RayStatsService interface {
|
type V2RayStatsService interface {
|
||||||
RoutedConnection(inbound string, outbound string, conn net.Conn) net.Conn
|
RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn
|
||||||
RoutedPacketConnection(inbound string, outbound string, conn N.PacketConn) N.PacketConn
|
RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ func NewStatsService(options option.V2RayStatsServiceOptions) *StatsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatsService) RoutedConnection(inbound string, outbound string, conn net.Conn) net.Conn {
|
func (s *StatsService) RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn {
|
||||||
var readCounter []*atomic.Int64
|
var readCounter []*atomic.Int64
|
||||||
var writeCounter []*atomic.Int64
|
var writeCounter []*atomic.Int64
|
||||||
countInbound := inbound != "" && s.inbounds[inbound]
|
countInbound := inbound != "" && s.inbounds[inbound]
|
||||||
@ -67,6 +67,10 @@ func (s *StatsService) RoutedConnection(inbound string, outbound string, conn ne
|
|||||||
if countInbound {
|
if countInbound {
|
||||||
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
|
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
|
||||||
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
|
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
|
||||||
|
if user != "" {
|
||||||
|
readCounter = append(readCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>uplink"))
|
||||||
|
writeCounter = append(writeCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>downlink"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if countOutbound {
|
if countOutbound {
|
||||||
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
|
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
|
||||||
@ -76,7 +80,7 @@ func (s *StatsService) RoutedConnection(inbound string, outbound string, conn ne
|
|||||||
return trackerconn.New(conn, readCounter, writeCounter)
|
return trackerconn.New(conn, readCounter, writeCounter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatsService) RoutedPacketConnection(inbound string, outbound string, conn N.PacketConn) N.PacketConn {
|
func (s *StatsService) RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn {
|
||||||
var readCounter []*atomic.Int64
|
var readCounter []*atomic.Int64
|
||||||
var writeCounter []*atomic.Int64
|
var writeCounter []*atomic.Int64
|
||||||
countInbound := inbound != "" && s.inbounds[inbound]
|
countInbound := inbound != "" && s.inbounds[inbound]
|
||||||
@ -88,6 +92,10 @@ func (s *StatsService) RoutedPacketConnection(inbound string, outbound string, c
|
|||||||
if countInbound {
|
if countInbound {
|
||||||
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
|
readCounter = append(readCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>uplink"))
|
||||||
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
|
writeCounter = append(writeCounter, s.loadOrCreateCounter("inbound>>>"+inbound+">>>traffic>>>downlink"))
|
||||||
|
if user != "" {
|
||||||
|
readCounter = append(readCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>uplink"))
|
||||||
|
writeCounter = append(writeCounter, s.loadOrCreateCounter("user>>>"+user+">>>traffic>>>downlink"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if countOutbound {
|
if countOutbound {
|
||||||
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
|
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink"))
|
||||||
|
@ -24,9 +24,9 @@ import (
|
|||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/sagernet/sing-dns"
|
dns "github.com/sagernet/sing-dns"
|
||||||
"github.com/sagernet/sing-tun"
|
tun "github.com/sagernet/sing-tun"
|
||||||
"github.com/sagernet/sing-vmess"
|
vmess "github.com/sagernet/sing-vmess"
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
@ -583,7 +583,7 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
|
|||||||
}
|
}
|
||||||
if r.v2rayServer != nil {
|
if r.v2rayServer != nil {
|
||||||
if statsService := r.v2rayServer.StatsService(); statsService != nil {
|
if statsService := r.v2rayServer.StatsService(); statsService != nil {
|
||||||
conn = statsService.RoutedConnection(metadata.Inbound, detour.Tag(), conn)
|
conn = statsService.RoutedConnection(metadata.Inbound, detour.Tag(), metadata.User, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return detour.NewConnection(ctx, conn, metadata)
|
return detour.NewConnection(ctx, conn, metadata)
|
||||||
@ -661,7 +661,7 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
|
|||||||
}
|
}
|
||||||
if r.v2rayServer != nil {
|
if r.v2rayServer != nil {
|
||||||
if statsService := r.v2rayServer.StatsService(); statsService != nil {
|
if statsService := r.v2rayServer.StatsService(); statsService != nil {
|
||||||
conn = statsService.RoutedPacketConnection(metadata.Inbound, detour.Tag(), conn)
|
conn = statsService.RoutedPacketConnection(metadata.Inbound, detour.Tag(), metadata.User, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return detour.NewPacketConnection(ctx, conn, metadata)
|
return detour.NewPacketConnection(ctx, conn, metadata)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user