From 8757b955a6b96763cebd7c9bb0690d5cdb21916f Mon Sep 17 00:00:00 2001 From: wyx2685 Date: Mon, 8 Jul 2024 20:32:52 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhy2=E5=86=85=E6=A0=B8?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=95=B0=E7=BB=9F=E8=AE=A1=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/hy2/logger.go | 37 ++++++++++++++++++++++++++++--------- limiter/limiter.go | 3 +++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/core/hy2/logger.go b/core/hy2/logger.go index 1165feb..3a40ff0 100644 --- a/core/hy2/logger.go +++ b/core/hy2/logger.go @@ -5,6 +5,7 @@ import ( "net" "strings" + "github.com/InazumaV/V2bX/common/format" "github.com/InazumaV/V2bX/limiter" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -46,12 +47,18 @@ var logFormatMap = map[string]zapcore.EncoderConfig{ } func (l *serverLogger) Connect(addr net.Addr, uuid string, tx uint64) { - limiter, err := limiter.GetLimiter(l.Tag) + limiterinfo, err := limiter.GetLimiter(l.Tag) if err != nil { l.logger.Panic("Get limiter error", zap.String("tag", l.Tag), zap.Error(err)) } - if _, r := limiter.CheckLimit(uuid, extractIPFromAddr(addr), addr.Network() == "tcp"); r { - l.logger.Warn("Need Reject", zap.String("addr", addr.String()), zap.String("uuid", uuid)) + if _, r := limiterinfo.CheckLimit(format.UserTag(l.Tag, uuid), extractIPFromAddr(addr), addr.Network() == "tcp"); r { + if userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(l.Tag, uuid)); ok { + userLimit.(*limiter.UserLimitInfo).OverLimit = true + } + } else { + if userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(l.Tag, uuid)); ok { + userLimit.(*limiter.UserLimitInfo).OverLimit = false + } } l.logger.Info("client connected", zap.String("addr", addr.String()), zap.String("uuid", uuid), zap.Uint64("tx", tx)) } @@ -61,12 +68,18 @@ func (l *serverLogger) Disconnect(addr net.Addr, uuid string, err error) { } func (l *serverLogger) TCPRequest(addr net.Addr, uuid, reqAddr string) { - limiter, err := limiter.GetLimiter(l.Tag) + limiterinfo, err := limiter.GetLimiter(l.Tag) if err != nil { l.logger.Panic("Get limiter error", zap.String("tag", l.Tag), zap.Error(err)) } - if _, r := limiter.CheckLimit(uuid, extractIPFromAddr(addr), addr.Network() == "tcp"); r { - l.logger.Warn("Need Reject", zap.String("addr", addr.String()), zap.String("uuid", uuid)) + if _, r := limiterinfo.CheckLimit(format.UserTag(l.Tag, uuid), extractIPFromAddr(addr), addr.Network() == "tcp"); r { + if userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(l.Tag, uuid)); ok { + userLimit.(*limiter.UserLimitInfo).OverLimit = true + } + } else { + if userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(l.Tag, uuid)); ok { + userLimit.(*limiter.UserLimitInfo).OverLimit = false + } } l.logger.Debug("TCP request", zap.String("addr", addr.String()), zap.String("uuid", uuid), zap.String("reqAddr", reqAddr)) } @@ -80,12 +93,18 @@ func (l *serverLogger) TCPError(addr net.Addr, uuid, reqAddr string, err error) } func (l *serverLogger) UDPRequest(addr net.Addr, uuid string, sessionId uint32, reqAddr string) { - limiter, err := limiter.GetLimiter(l.Tag) + limiterinfo, err := limiter.GetLimiter(l.Tag) if err != nil { l.logger.Panic("Get limiter error", zap.String("tag", l.Tag), zap.Error(err)) } - if _, r := limiter.CheckLimit(uuid, extractIPFromAddr(addr), addr.Network() == "tcp"); r { - l.logger.Warn("Need Reject", zap.String("addr", addr.String()), zap.String("uuid", uuid)) + if _, r := limiterinfo.CheckLimit(format.UserTag(l.Tag, uuid), extractIPFromAddr(addr), addr.Network() == "tcp"); r { + if userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(l.Tag, uuid)); ok { + userLimit.(*limiter.UserLimitInfo).OverLimit = true + } + } else { + if userLimit, ok := limiterinfo.UserLimitInfo.Load(format.UserTag(l.Tag, uuid)); ok { + userLimit.(*limiter.UserLimitInfo).OverLimit = false + } } l.logger.Debug("UDP request", zap.String("addr", addr.String()), zap.String("uuid", uuid), zap.Uint32("sessionId", sessionId), zap.String("reqAddr", reqAddr)) } diff --git a/limiter/limiter.go b/limiter/limiter.go index b13a6a0..e130995 100644 --- a/limiter/limiter.go +++ b/limiter/limiter.go @@ -48,6 +48,7 @@ type UserLimitInfo struct { DeviceLimit int DynamicSpeedLimit int ExpireTime int64 + OverLimit bool } func AddLimiter(tag string, l *conf.LimitConfig, users []panel.UserInfo) *Limiter { @@ -69,6 +70,7 @@ func AddLimiter(tag string, l *conf.LimitConfig, users []panel.UserInfo) *Limite if users[i].DeviceLimit != 0 { userLimit.DeviceLimit = users[i].DeviceLimit } + userLimit.OverLimit = false info.UserLimitInfo.Store(format.UserTag(tag, users[i].Uuid), userLimit) } info.UUIDtoUID = uuidmap @@ -110,6 +112,7 @@ func (l *Limiter) UpdateUser(tag string, added []panel.UserInfo, deleted []panel if added[i].DeviceLimit != 0 { userLimit.DeviceLimit = added[i].DeviceLimit } + userLimit.OverLimit = false l.UserLimitInfo.Store(format.UserTag(tag, added[i].Uuid), userLimit) l.UUIDtoUID[added[i].Uuid] = added[i].Id }