V2bX/core/hy2/hook.go

63 lines
1.3 KiB
Go
Raw Normal View History

2024-02-05 20:59:37 -05:00
package hy2
import (
"sync"
"github.com/InazumaV/V2bX/common/counter"
"github.com/InazumaV/V2bX/common/format"
"github.com/InazumaV/V2bX/limiter"
2024-12-01 18:38:23 -05:00
"github.com/apernet/hysteria/core/v2/server"
quic "github.com/apernet/quic-go"
"go.uber.org/zap"
2024-02-05 20:59:37 -05:00
)
2024-12-01 18:38:23 -05:00
var _ server.TrafficLogger = (*HookServer)(nil)
2024-02-05 20:59:37 -05:00
type HookServer struct {
Tag string
logger *zap.Logger
2024-02-05 20:59:37 -05:00
Counter sync.Map
}
2024-12-01 18:38:23 -05:00
func (h *HookServer) TraceStream(stream quic.Stream, stats *server.StreamStats) {
}
func (h *HookServer) UntraceStream(stream quic.Stream) {
}
2024-06-12 11:06:22 -04:00
func (h *HookServer) LogTraffic(id string, tx, rx uint64) (ok bool) {
2024-02-16 07:46:23 -05:00
var c interface{}
var exists bool
limiterinfo, err := limiter.GetLimiter(h.Tag)
if err != nil {
h.logger.Error("Get limiter error", zap.String("tag", h.Tag), zap.Error(err))
return false
}
userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(h.Tag, id))
if ok {
userlimitInfo := userLimit.(*limiter.UserLimitInfo)
if userlimitInfo.OverLimit {
userlimitInfo.OverLimit = false
return false
}
}
2024-02-16 07:46:23 -05:00
if c, exists = h.Counter.Load(h.Tag); !exists {
c = counter.NewTrafficCounter()
2024-02-05 20:59:37 -05:00
h.Counter.Store(h.Tag, c)
2024-02-16 07:46:23 -05:00
}
if tc, ok := c.(*counter.TrafficCounter); ok {
tc.Rx(id, int(rx))
tc.Tx(id, int(tx))
2024-02-05 20:59:37 -05:00
return true
}
2024-02-16 07:46:23 -05:00
return false
2024-02-05 20:59:37 -05:00
}
2024-06-12 11:06:22 -04:00
func (s *HookServer) LogOnlineState(id string, online bool) {
}