From 3a812398c0257e9d5fd140d373dc198db09db37b Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 18 Jul 2025 10:00:07 +0800 Subject: [PATCH] feat:batch delete block ip #ICGQJF --- api/waf_block_ip.go | 56 +++++++++++++++++++++ model/request/waf_block_ip_add_req.go | 7 --- model/request/waf_block_ip_del_req.go | 5 -- model/request/waf_block_ip_detail_req.go | 5 -- model/request/waf_block_ip_edit_req.go | 8 --- model/request/waf_block_ip_req.go | 36 ++++++++++++++ model/request/waf_block_ip_search.go | 9 ---- router/waf_block_ip.go | 2 + service/waf_service/waf_block_ip.go | 62 ++++++++++++++++++++++++ 9 files changed, 156 insertions(+), 34 deletions(-) delete mode 100644 model/request/waf_block_ip_add_req.go delete mode 100644 model/request/waf_block_ip_del_req.go delete mode 100644 model/request/waf_block_ip_detail_req.go delete mode 100644 model/request/waf_block_ip_edit_req.go create mode 100644 model/request/waf_block_ip_req.go delete mode 100644 model/request/waf_block_ip_search.go diff --git a/api/waf_block_ip.go b/api/waf_block_ip.go index e25607f..a9ebdc3 100644 --- a/api/waf_block_ip.go +++ b/api/waf_block_ip.go @@ -8,6 +8,7 @@ import ( "SamWaf/model/request" "SamWaf/model/spec" "errors" + "fmt" "github.com/gin-gonic/gin" "gorm.io/gorm" ) @@ -101,6 +102,61 @@ func (w *WafBlockIpApi) ModifyBlockIpApi(c *gin.Context) { } } +// BatchDelBlockIpApi 批量删除IP黑名单 +func (w *WafBlockIpApi) BatchDelBlockIpApi(c *gin.Context) { + var req request.WafBlockIpBatchDelReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafIpBlockService.GetHostCodesByIds(req.Ids) + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + // 执行批量删除 + err = wafIpBlockService.BatchDelApi(req) + if err != nil { + response.FailWithMessage("批量删除失败: "+err.Error(), c) + } else { + // 通知所有相关的网站更新配置 + for _, hostCode := range hostCodes { + w.NotifyWaf(hostCode) + } + response.OkWithMessage(fmt.Sprintf("成功删除 %d 条记录", len(req.Ids)), c) + } + } else { + response.FailWithMessage("解析失败", c) + } +} + +// DelAllBlockIpApi 删除指定网站的所有IP黑名单 +func (w *WafBlockIpApi) DelAllBlockIpApi(c *gin.Context) { + var req request.WafBlockIpDelAllReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafIpBlockService.GetHostCodes() + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + err = wafIpBlockService.DelAllApi(req) + if err != nil { + response.FailWithMessage("全量删除失败: "+err.Error(), c) + } else { + // 通知所有相关的网站更新配置 + for _, hostCode := range hostCodes { + w.NotifyWaf(hostCode) + } + response.OkWithMessage("成功删除该网站的所有IP黑名单", c) + } + } else { + response.FailWithMessage("解析失败", c) + } +} + /* * 通知到waf引擎实时生效 diff --git a/model/request/waf_block_ip_add_req.go b/model/request/waf_block_ip_add_req.go deleted file mode 100644 index 933bd2c..0000000 --- a/model/request/waf_block_ip_add_req.go +++ /dev/null @@ -1,7 +0,0 @@ -package request - -type WafBlockIpAddReq struct { - HostCode string `json:"host_code"` //网站唯一码(主要键) - Ip string `json:"ip"` //Block ip - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_block_ip_del_req.go b/model/request/waf_block_ip_del_req.go deleted file mode 100644 index 81da0f1..0000000 --- a/model/request/waf_block_ip_del_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafBlockIpDelReq struct { - Id string `json:"id" form:"id"` //Block IP唯一键 -} diff --git a/model/request/waf_block_ip_detail_req.go b/model/request/waf_block_ip_detail_req.go deleted file mode 100644 index bafa8ff..0000000 --- a/model/request/waf_block_ip_detail_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafBlockIpDetailReq struct { - Id string `json:"id" form:"id"` //Block IP唯一键 -} diff --git a/model/request/waf_block_ip_edit_req.go b/model/request/waf_block_ip_edit_req.go deleted file mode 100644 index e1fa672..0000000 --- a/model/request/waf_block_ip_edit_req.go +++ /dev/null @@ -1,8 +0,0 @@ -package request - -type WafBlockIpEditReq struct { - Id string `json:"id"` //Block IP唯一键 - HostCode string `json:"host_code"` //网站唯一码(主要键) - Ip string `json:"ip"` //Block ip - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_block_ip_req.go b/model/request/waf_block_ip_req.go new file mode 100644 index 0000000..703a716 --- /dev/null +++ b/model/request/waf_block_ip_req.go @@ -0,0 +1,36 @@ +package request + +import "SamWaf/model/common/request" + +type WafBlockIpAddReq struct { + HostCode string `json:"host_code"` //网站唯一码(主要键) + Ip string `json:"ip"` //Block ip + Remarks string `json:"remarks"` //备注 +} + +type WafBlockIpEditReq struct { + Id string `json:"id"` //Block IP唯一键 + HostCode string `json:"host_code"` //网站唯一码(主要键) + Ip string `json:"ip"` //Block ip + Remarks string `json:"remarks"` //备注 +} +type WafBlockIpDelReq struct { + Id string `json:"id" form:"id"` //Block IP唯一键 +} + +type WafBlockIpSearchReq struct { + HostCode string `json:"host_code" ` //主机码 + Ip string `json:"ip"` //Block ip + request.PageInfo +} + +type WafBlockIpDetailReq struct { + Id string `json:"id" form:"id"` //Block IP唯一键 +} + +type WafBlockIpBatchDelReq struct { + Ids []string `json:"ids" binding:"required"` //Block IP唯一键数组 +} + +type WafBlockIpDelAllReq struct { +} diff --git a/model/request/waf_block_ip_search.go b/model/request/waf_block_ip_search.go deleted file mode 100644 index a0c9a0f..0000000 --- a/model/request/waf_block_ip_search.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -import "SamWaf/model/common/request" - -type WafBlockIpSearchReq struct { - HostCode string `json:"host_code" ` //主机码 - Ip string `json:"ip"` //Block ip - request.PageInfo -} diff --git a/router/waf_block_ip.go b/router/waf_block_ip.go index efd486d..175de73 100644 --- a/router/waf_block_ip.go +++ b/router/waf_block_ip.go @@ -16,4 +16,6 @@ func (receiver *BlockIpRouter) InitBlockIpRouter(group *gin.RouterGroup) { router.POST("/samwaf/wafhost/ipblock/add", api.AddApi) router.GET("/samwaf/wafhost/ipblock/del", api.DelBlockIpApi) router.POST("/samwaf/wafhost/ipblock/edit", api.ModifyBlockIpApi) + router.POST("/samwaf/wafhost/ipblock/batch/del", api.BatchDelBlockIpApi) + router.POST("/samwaf/wafhost/ipblock/delall", api.DelAllBlockIpApi) } diff --git a/service/waf_service/waf_block_ip.go b/service/waf_service/waf_block_ip.go index 142a754..c5c8d54 100644 --- a/service/waf_service/waf_block_ip.go +++ b/service/waf_service/waf_block_ip.go @@ -110,3 +110,65 @@ func (receiver *WafBlockIpService) DelApi(req request.WafBlockIpDelReq) error { err = global.GWAF_LOCAL_DB.Where("id = ?", req.Id).Delete(model.IPBlockList{}).Error return err } + +// BatchDelApi 批量删除指定ID的IP黑名单 +func (receiver *WafBlockIpService) BatchDelApi(req request.WafBlockIpBatchDelReq) error { + if len(req.Ids) == 0 { + return errors.New("删除ID列表不能为空") + } + + // 先检查所有ID是否存在 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.IPBlockList{}).Where("id IN ?", req.Ids).Count(&count).Error + if err != nil { + return err + } + + if count != int64(len(req.Ids)) { + return errors.New("部分ID不存在") + } + + // 执行批量删除 + err = global.GWAF_LOCAL_DB.Where("id IN ?", req.Ids).Delete(&model.IPBlockList{}).Error + return err +} + +// DelAllApi 删除指定网站的所有IP黑名单 +func (receiver *WafBlockIpService) DelAllApi(req request.WafBlockIpDelAllReq) error { + // 先检查是否存在记录 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.IPBlockList{}).Count(&count).Error + if err != nil { + return err + } + + if count == 0 { + return errors.New("没有IP黑名单记录") + } + + // 执行全量删除 - 限制在当前租户和用户范围内 + err = global.GWAF_LOCAL_DB. + Where("user_code = ? AND tenant_id = ?", global.GWAF_USER_CODE, global.GWAF_TENANT_ID). + Delete(&model.IPBlockList{}).Error + return err +} + +// GetHostCodesByIds 根据ID列表获取对应的HostCode列表(用于通知WAF引擎) +func (receiver *WafBlockIpService) GetHostCodesByIds(ids []string) ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.IPBlockList{}). + Where("id IN ?", ids). + Distinct("host_code"). + Pluck("host_code", &hostCodes).Error + return hostCodes, err +} + +// GetHostCodes 获取所有HostCode列表(用于通知WAF引擎) +func (receiver *WafBlockIpService) GetHostCodes() ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.IPBlockList{}). + Distinct("host_code"). + Where("user_code = ? AND tenant_id = ?", global.GWAF_USER_CODE, global.GWAF_TENANT_ID). + Pluck("host_code", &hostCodes).Error + return hostCodes, err +}