f
This commit is contained in:
@@ -189,21 +189,10 @@ func (s *SMSCodeService) GetCodeStatus(ctx context.Context, phone string, scene
|
||||
func (s *SMSCodeService) CheckRateLimit(ctx context.Context, phone string, scene entities.SMSScene, clientIP, userAgent string) error {
|
||||
// 设备标识(这里使用 User-Agent + IP 的组合做近似设备ID,可根据实际情况调整)
|
||||
deviceID := fmt.Sprintf("ua:%s|ip:%s", userAgent, clientIP)
|
||||
|
||||
// 1. 检查是否已被永久封禁(按手机号和设备双维度)
|
||||
phoneBanKey := fmt.Sprintf("sms:ban:phone:%s", phone)
|
||||
var phoneBanned bool
|
||||
if err := s.cache.Get(ctx, phoneBanKey, &phoneBanned); err == nil && phoneBanned {
|
||||
return fmt.Errorf("该手机号短信发送已被永久限制")
|
||||
}
|
||||
// deviceBanKey := fmt.Sprintf("sms:ban:device:%s", deviceID)
|
||||
|
||||
deviceBanKey := fmt.Sprintf("sms:ban:device:%s", deviceID)
|
||||
var deviceBanned bool
|
||||
if err := s.cache.Get(ctx, deviceBanKey, &deviceBanned); err == nil && deviceBanned {
|
||||
return fmt.Errorf("该设备短信发送已被永久限制")
|
||||
}
|
||||
|
||||
// 2. 按手机号的时间窗口限流
|
||||
// 1. 按手机号的时间窗口限流
|
||||
// 10分钟窗口
|
||||
if err := s.checkWindowLimit(ctx, fmt.Sprintf("sms:phone:%s:10m", phone), 10*time.Minute, 10); err != nil {
|
||||
return err
|
||||
@@ -239,21 +228,6 @@ func (s *SMSCodeService) CheckRateLimit(ctx context.Context, phone string, scene
|
||||
if err := s.checkWindowLimit(ctx, fmt.Sprintf("sms:device:%s:1h", deviceID), time.Hour, 20); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3.2 检查同一设备在短时间内是否出现在多个IP
|
||||
// 使用一个短期集合Key记录该设备最近使用过的IP数量
|
||||
deviceIPKey := fmt.Sprintf("sms:device:%s:ips", userAgent)
|
||||
var ipCount int
|
||||
if err := s.cache.Get(ctx, deviceIPKey, &ipCount); err == nil {
|
||||
// 如果一个设备短时间内出现多个不同IP(>3),认为存在异常,直接永久封禁设备
|
||||
if ipCount >= 3 {
|
||||
s.cache.Set(ctx, deviceBanKey, true, 0)
|
||||
return fmt.Errorf("该设备存在异常行为,短信发送已被永久限制")
|
||||
}
|
||||
s.cache.Set(ctx, deviceIPKey, ipCount+1, 30*time.Minute)
|
||||
} else {
|
||||
s.cache.Set(ctx, deviceIPKey, 1, 30*time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user