Переглянути джерело

feat(waf): 添加全球限制功能并优化日志记录

- 在 globalLimit.go 中添加 LastLoginIp 字段
- 修改 waflog.go 以使用用户的最后登录 IP 作为请求 IP
- 优化 globallimit.go 中的日志记录逻辑,使用异步方式记录- 更新 UserInfo 查询,包含 lastloginip 字段
fusu 5 днів тому
батько
коміт
6e7c964d3e

+ 1 - 0
api/v1/globalLimit.go

@@ -63,4 +63,5 @@ type UserInfo struct {
 	Username string `json:"username" form:"username"`
 	Email    string `json:"email" form:"email"`
 	PhoneNumber    string `json:"phonenumber" form:"phonenumber"`
+	LastLoginIp    string `json:"lastloginip" form:"lastloginip" gorm:"column:lastloginip"`
 }

+ 8 - 16
internal/handler/api/waf/globallimit.go

@@ -45,18 +45,14 @@ func (h *GlobalLimitHandler) AddGlobalLimit(ctx *gin.Context) {
 	}
 
 
-	err = h.wafLogService.AddWafLog(ctx, adminApi.WafLog{
+	go  h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
 		Uid:        req.Uid,
-		RequestIp:  ctx.ClientIP(),
-		UserAgent:  ctx.Request.UserAgent(),
-		Api:        ctx.Request.URL.Path,
+		RequestIp:  ctx.ClientIP(), // 复制 ClientIP
+		UserAgent:  ctx.Request.UserAgent(), // 复制 UserAgent
+		Api:        ctx.Request.URL.Path, // 复制 Path
 		HostId:     req.HostId,
 		ExtraData:  req,
 	})
-	if err != nil {
-		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
-		return
-	}
 	v1.HandleSuccess(ctx, nil)
 }
 
@@ -100,18 +96,14 @@ func (h *GlobalLimitHandler) DeleteGlobalLimit(ctx *gin.Context) {
 
 
 
-	err = h.wafLogService.AddWafLog(ctx, adminApi.WafLog{
+	go  h.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
 		Uid:        req.Uid,
-		RequestIp:  ctx.ClientIP(),
-		UserAgent:  ctx.Request.UserAgent(),
-		Api:        ctx.Request.URL.Path,
+		RequestIp:  ctx.ClientIP(), // 复制 ClientIP
+		UserAgent:  ctx.Request.UserAgent(), // 复制 UserAgent
+		Api:        ctx.Request.URL.Path, // 复制 Path
 		HostId:     req.HostId,
 		ExtraData:  req,
 	})
-	if err != nil {
-		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
-		return
-	}
 
 	v1.HandleSuccess(ctx, nil)
 }

+ 1 - 1
internal/repository/api/waf/globallimit.go

@@ -125,7 +125,7 @@ func (r *globalLimitRepository) GetUserInfo(ctx context.Context, uid int64) (v1.
 	var res v1.UserInfo
 	if err := r.DB(ctx).Table("shd_clients").
 		Where("id = ?", uid).
-		Select("username", "email", "phonenumber").
+		Select("username", "email", "phonenumber", "lastloginip").
 		Find(&res).Error; err != nil {
 		return v1.UserInfo{}, err
 	}

+ 4 - 0
internal/service/admin/waflog.go

@@ -139,6 +139,9 @@ func (s *wafLogService) AddWafLog(ctx context.Context, req adminApi.WafLog) erro
 	if err != nil {
 		return err
 	}
+
+	req.RequestIp = userInfo.LastLoginIp
+
 	return s.wafLogRepository.AddWafLog(ctx, &model.WafLog{
 		Uid:        req.Uid,
 		Name:       req.Name,
@@ -181,6 +184,7 @@ func (s *wafLogService) BatchAddWafLog(ctx context.Context, reqs []*adminApi.Waf
 		}
 		
 		req.Name = userInfo.Username
+		req.RequestIp = userInfo.LastLoginIp
 		extraData, err := json.Marshal(req.ExtraData)
 		if err != nil {
 			s.Logger.Error("序列化额外数据失败", zap.Error(err))