test: 增加MinReportTraffic最低流量上报阈值

This commit is contained in:
wyx2685
2025-08-07 14:18:12 +09:00
parent 9be082ede6
commit f7b588fb45
18 changed files with 238 additions and 128 deletions

View File

@@ -14,9 +14,10 @@ import (
var _ server.TrafficLogger = (*HookServer)(nil)
type HookServer struct {
Tag string
logger *zap.Logger
Counter sync.Map
Tag string
logger *zap.Logger
Counter sync.Map
ReportMinTrafficBytes int64
}
func (h *HookServer) TraceStream(stream quic.Stream, stats *server.StreamStats) {

View File

@@ -40,8 +40,9 @@ func (h *Hysteria2) AddNode(tag string, info *panel.NodeInfo, config *conf.Optio
logger: h.Logger,
},
TrafficLogger: &HookServer{
Tag: tag,
logger: h.Logger,
Tag: tag,
logger: h.Logger,
ReportMinTrafficBytes: config.ReportMinTraffic * 1024,
},
}

View File

@@ -14,12 +14,12 @@ var _ server.Authenticator = &V2bX{}
type V2bX struct {
usersMap map[string]int
mutex sync.Mutex
mutex sync.RWMutex
}
func (v *V2bX) Authenticate(addr net.Addr, auth string, tx uint64) (ok bool, id string) {
v.mutex.Lock()
defer v.mutex.Unlock()
v.mutex.RLock()
defer v.mutex.RUnlock()
if _, exists := v.usersMap[auth]; exists {
return true, auth
}
@@ -56,15 +56,38 @@ func (h *Hysteria2) DelUsers(users []panel.UserInfo, tag string, _ *panel.NodeIn
return nil
}
func (h *Hysteria2) GetUserTraffic(tag string, uuid string, reset bool) (up int64, down int64) {
if v, ok := h.Hy2nodes[tag].TrafficLogger.(*HookServer).Counter.Load(tag); ok {
c := v.(*counter.TrafficCounter)
up = c.GetUpCount(uuid)
down = c.GetDownCount(uuid)
if reset {
c.Reset(uuid)
}
return up, down
func (h *Hysteria2) GetUserTrafficSlice(tag string, reset bool) ([]panel.UserTraffic, error) {
trafficSlice := make([]panel.UserTraffic, 0)
h.Auth.mutex.RLock()
defer h.Auth.mutex.RUnlock()
if _, ok := h.Hy2nodes[tag]; !ok {
return nil, nil
}
return 0, 0
hook := h.Hy2nodes[tag].TrafficLogger.(*HookServer)
if v, ok := hook.Counter.Load(tag); ok {
c := v.(*counter.TrafficCounter)
c.Counters.Range(func(key, value interface{}) bool {
uuid := key.(string)
traffic := value.(*counter.TrafficStorage)
up := traffic.UpCounter.Load()
down := traffic.DownCounter.Load()
if up+down >= hook.ReportMinTrafficBytes {
if reset {
traffic.UpCounter.Store(0)
traffic.DownCounter.Store(0)
}
trafficSlice = append(trafficSlice, panel.UserTraffic{
UID: h.Auth.usersMap[uuid],
Upload: up,
Download: down,
})
}
return true
})
if len(trafficSlice) == 0 {
return nil, nil
}
return trafficSlice, nil
}
return nil, nil
}