From eb3bd0ea6ab3629a786fb8173fcc6f4382546bb7 Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 22 Aug 2025 08:53:22 +0800 Subject: [PATCH] feat:allow ip batch delete #436 --- api/waf_allow_ip.go | 60 ++++++++++++++++++++++ model/request/waf_allow_ip_add_req.go | 7 --- model/request/waf_allow_ip_del_req.go | 5 -- model/request/waf_allow_ip_detail_req.go | 5 -- model/request/waf_allow_ip_edit_req.go | 8 --- model/request/waf_allow_ip_req.go | 34 +++++++++++++ model/request/waf_allow_ip_search.go | 9 ---- router/waf_allow_ip.go | 3 ++ service/waf_service/waf_allow_ip.go | 65 ++++++++++++++++++++++++ 9 files changed, 162 insertions(+), 34 deletions(-) delete mode 100644 model/request/waf_allow_ip_add_req.go delete mode 100644 model/request/waf_allow_ip_del_req.go delete mode 100644 model/request/waf_allow_ip_detail_req.go delete mode 100644 model/request/waf_allow_ip_edit_req.go create mode 100644 model/request/waf_allow_ip_req.go delete mode 100644 model/request/waf_allow_ip_search.go diff --git a/api/waf_allow_ip.go b/api/waf_allow_ip.go index eac1890..312ebb6 100644 --- a/api/waf_allow_ip.go +++ b/api/waf_allow_ip.go @@ -8,6 +8,7 @@ import ( "SamWaf/model/request" "SamWaf/model/spec" "errors" + "fmt" "github.com/gin-gonic/gin" "gorm.io/gorm" ) @@ -115,3 +116,62 @@ func (w *WafAllowIpApi) NotifyWaf(host_code string) { } global.GWAF_CHAN_MSG <- chanInfo } + +// BatchDelAllowIpApi 批量删除IP白名单 +func (w *WafAllowIpApi) BatchDelAllowIpApi(c *gin.Context) { + var req request.WafAllowIpBatchDelReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafIpAllowService.GetHostCodesByIds(req.Ids) + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + // 执行批量删除 + err = wafIpAllowService.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) + } +} + +// DelAllAllowIpApi 删除指定网站的所有IP白名单 +func (w *WafAllowIpApi) DelAllAllowIpApi(c *gin.Context) { + var req request.WafAllowIpDelAllReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafIpAllowService.GetHostCodes() + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + err = wafIpAllowService.DelAllApi(req) + if err != nil { + response.FailWithMessage("全量删除失败: "+err.Error(), c) + } else { + // 通知所有相关的网站更新配置 + for _, hostCode := range hostCodes { + w.NotifyWaf(hostCode) + } + if len(req.HostCode) > 0 { + response.OkWithMessage("成功删除该网站的所有IP白名单", c) + } else { + response.OkWithMessage("成功删除所有IP白名单", c) + } + } + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/model/request/waf_allow_ip_add_req.go b/model/request/waf_allow_ip_add_req.go deleted file mode 100644 index 7b13cae..0000000 --- a/model/request/waf_allow_ip_add_req.go +++ /dev/null @@ -1,7 +0,0 @@ -package request - -type WafAllowIpAddReq struct { - HostCode string `json:"host_code"` //网站唯一码(主要键) - Ip string `json:"ip"` //白名单ip - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_allow_ip_del_req.go b/model/request/waf_allow_ip_del_req.go deleted file mode 100644 index 0c75efe..0000000 --- a/model/request/waf_allow_ip_del_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafAllowIpDelReq struct { - Id string `json:"id" form:"id"` //白名单IP唯一键 -} diff --git a/model/request/waf_allow_ip_detail_req.go b/model/request/waf_allow_ip_detail_req.go deleted file mode 100644 index dab2a8d..0000000 --- a/model/request/waf_allow_ip_detail_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafAllowIpDetailReq struct { - Id string `json:"id" form:"id"` //白名单IP唯一键 -} diff --git a/model/request/waf_allow_ip_edit_req.go b/model/request/waf_allow_ip_edit_req.go deleted file mode 100644 index 75f63c7..0000000 --- a/model/request/waf_allow_ip_edit_req.go +++ /dev/null @@ -1,8 +0,0 @@ -package request - -type WafAllowIpEditReq struct { - Id string `json:"id"` //白名单IP唯一键 - HostCode string `json:"host_code"` //网站唯一码(主要键) - Ip string `json:"ip"` //白名单ip - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_allow_ip_req.go b/model/request/waf_allow_ip_req.go new file mode 100644 index 0000000..73f25dc --- /dev/null +++ b/model/request/waf_allow_ip_req.go @@ -0,0 +1,34 @@ +package request + +import "SamWaf/model/common/request" + +type WafAllowIpAddReq struct { + HostCode string `json:"host_code"` //网站唯一码(主要键) + Ip string `json:"ip"` //白名单ip + Remarks string `json:"remarks"` //备注 +} +type WafAllowIpDelReq struct { + Id string `json:"id" form:"id"` //白名单IP唯一键 +} +type WafAllowIpDetailReq struct { + Id string `json:"id" form:"id"` //白名单IP唯一键 +} + +type WafAllowIpEditReq struct { + Id string `json:"id"` //白名单IP唯一键 + HostCode string `json:"host_code"` //网站唯一码(主要键) + Ip string `json:"ip"` //白名单ip + Remarks string `json:"remarks"` //备注 +} +type WafAllowIpSearchReq struct { + HostCode string `json:"host_code" ` //主机码 + Ip string `json:"ip"` //白名单ip + request.PageInfo +} +type WafAllowIpBatchDelReq struct { + Ids []string `json:"ids" binding:"required"` //白名单IP唯一键数组 +} + +type WafAllowIpDelAllReq struct { + HostCode string `json:"host_code" form:"host_code"` //网站唯一码,为空则删除所有 +} diff --git a/model/request/waf_allow_ip_search.go b/model/request/waf_allow_ip_search.go deleted file mode 100644 index c6bc1b6..0000000 --- a/model/request/waf_allow_ip_search.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -import "SamWaf/model/common/request" - -type WafAllowIpSearchReq struct { - HostCode string `json:"host_code" ` //主机码 - Ip string `json:"ip"` //白名单ip - request.PageInfo -} diff --git a/router/waf_allow_ip.go b/router/waf_allow_ip.go index b0795ad..cffa654 100644 --- a/router/waf_allow_ip.go +++ b/router/waf_allow_ip.go @@ -16,4 +16,7 @@ func (receiver *AllowIpRouter) InitAllowIpRouter(group *gin.RouterGroup) { allowIpRouter.POST("/samwaf/wafhost/ipwhite/add", AllowIpRouterApi.AddApi) allowIpRouter.GET("/samwaf/wafhost/ipwhite/del", AllowIpRouterApi.DelAllowIpApi) allowIpRouter.POST("/samwaf/wafhost/ipwhite/edit", AllowIpRouterApi.ModifyAllowIpApi) + + allowIpRouter.POST("/samwaf/wafhost/ipwhite/batchdel", AllowIpRouterApi.BatchDelAllowIpApi) + allowIpRouter.POST("/samwaf/wafhost/ipwhite/delall", AllowIpRouterApi.DelAllAllowIpApi) } diff --git a/service/waf_service/waf_allow_ip.go b/service/waf_service/waf_allow_ip.go index 578432a..150a679 100644 --- a/service/waf_service/waf_allow_ip.go +++ b/service/waf_service/waf_allow_ip.go @@ -110,3 +110,68 @@ func (receiver *WafWhiteIpService) DelApi(req request.WafAllowIpDelReq) error { err = global.GWAF_LOCAL_DB.Where("id = ?", req.Id).Delete(model.IPAllowList{}).Error return err } + +// BatchDelApi 批量删除指定ID的IP白名单 +func (receiver *WafWhiteIpService) BatchDelApi(req request.WafAllowIpBatchDelReq) error { + if len(req.Ids) == 0 { + return errors.New("删除ID列表不能为空") + } + + // 先检查所有ID是否存在 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.IPAllowList{}).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.IPAllowList{}).Error + return err +} + +// DelAllApi 删除指定网站的所有IP白名单 +func (receiver *WafWhiteIpService) DelAllApi(req request.WafAllowIpDelAllReq) error { + var whereCondition string + var whereValues []interface{} + + if len(req.HostCode) > 0 { + whereCondition = "host_code = ? AND user_code = ? AND tenant_id = ?" + whereValues = append(whereValues, req.HostCode, global.GWAF_USER_CODE, global.GWAF_TENANT_ID) + } else { + whereCondition = "user_code = ? AND tenant_id = ?" + whereValues = append(whereValues, global.GWAF_USER_CODE, global.GWAF_TENANT_ID) + } + + // 先检查是否存在记录 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.IPAllowList{}).Where(whereCondition, whereValues...).Count(&count).Error + if err != nil { + return err + } + + if count == 0 { + return errors.New("没有IP白名单记录") + } + + // 执行删除 + err = global.GWAF_LOCAL_DB.Where(whereCondition, whereValues...).Delete(&model.IPAllowList{}).Error + return err +} + +// GetHostCodesByIds 根据ID数组获取对应的HostCode列表 +func (receiver *WafWhiteIpService) GetHostCodesByIds(ids []string) ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.IPAllowList{}).Where("id IN ?", ids).Distinct("host_code").Pluck("host_code", &hostCodes).Error + return hostCodes, err +} + +// GetHostCodes 获取所有HostCode列表 +func (receiver *WafWhiteIpService) GetHostCodes() ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.IPAllowList{}).Where("user_code = ? AND tenant_id = ?", global.GWAF_USER_CODE, global.GWAF_TENANT_ID).Distinct("host_code").Pluck("host_code", &hostCodes).Error + return hostCodes, err +}