From eb3bd0ea6ab3629a786fb8173fcc6f4382546bb7 Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 22 Aug 2025 08:53:22 +0800 Subject: [PATCH 1/5] 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 +} From f23d2490d143de84763d1af4494274398007567a Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 22 Aug 2025 09:26:26 +0800 Subject: [PATCH 2/5] feat:allow url batch delete #436 --- api/waf_allow_url.go | 60 +++++++++++++++++++++++ model/request/waf_allow_url_add_req.go | 8 --- model/request/waf_allow_url_del_req.go | 5 -- model/request/waf_allow_url_detail_req.go | 5 -- model/request/waf_allow_url_edit_req.go | 9 ---- model/request/waf_allow_url_req.go | 38 ++++++++++++++ model/request/waf_allow_url_search.go | 9 ---- router/waf_allow_url.go | 4 ++ service/waf_service/waf_allow_url.go | 50 +++++++++++++++++++ 9 files changed, 152 insertions(+), 36 deletions(-) delete mode 100644 model/request/waf_allow_url_add_req.go delete mode 100644 model/request/waf_allow_url_del_req.go delete mode 100644 model/request/waf_allow_url_detail_req.go delete mode 100644 model/request/waf_allow_url_edit_req.go create mode 100644 model/request/waf_allow_url_req.go delete mode 100644 model/request/waf_allow_url_search.go diff --git a/api/waf_allow_url.go b/api/waf_allow_url.go index bd45292..69a4b7d 100644 --- a/api/waf_allow_url.go +++ b/api/waf_allow_url.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 *WafAllowUrlApi) NotifyWaf(host_code string) { } global.GWAF_CHAN_MSG <- chanInfo } + +// 新增批量删除API +func (w *WafAllowUrlApi) BatchDelAllowUrlApi(c *gin.Context) { + var req request.WafAllowUrlBatchDelReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafUrlAllowService.GetHostCodesByIds(req.Ids) + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + // 执行批量删除 + err = wafUrlAllowService.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) + } +} + +// 新增全部删除API +func (w *WafAllowUrlApi) DelAllAllowUrlApi(c *gin.Context) { + var req request.WafAllowUrlDelAllReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafUrlAllowService.GetHostCodes() + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + err = wafUrlAllowService.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("成功删除该网站的所有URL白名单", c) + } else { + response.OkWithMessage("成功删除所有URL白名单", c) + } + } + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/model/request/waf_allow_url_add_req.go b/model/request/waf_allow_url_add_req.go deleted file mode 100644 index b8055cd..0000000 --- a/model/request/waf_allow_url_add_req.go +++ /dev/null @@ -1,8 +0,0 @@ -package request - -type WafAllowUrlAddReq struct { - HostCode string `json:"host_code"` //网站唯一码(主要键) - CompareType string `json:"compare_type" form:"compare_type"` //对比方式 - Url string `json:"url"` //白名单url - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_allow_url_del_req.go b/model/request/waf_allow_url_del_req.go deleted file mode 100644 index 2fd0a32..0000000 --- a/model/request/waf_allow_url_del_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafAllowUrlDelReq struct { - Id string `json:"id" form:"id"` //白名单url唯一键 -} diff --git a/model/request/waf_allow_url_detail_req.go b/model/request/waf_allow_url_detail_req.go deleted file mode 100644 index 90c3e71..0000000 --- a/model/request/waf_allow_url_detail_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafAllowUrlDetailReq struct { - Id string `json:"id" form:"id"` //白名单Url唯一键 -} diff --git a/model/request/waf_allow_url_edit_req.go b/model/request/waf_allow_url_edit_req.go deleted file mode 100644 index 27c31b6..0000000 --- a/model/request/waf_allow_url_edit_req.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -type WafAllowUrlEditReq struct { - Id string `json:"id"` //白名单url唯一键 - HostCode string `json:"host_code"` //网站唯一码(主要键) - CompareType string `json:"compare_type" form:"compare_type"` //对比方式 - Url string `json:"url"` //白名单url - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_allow_url_req.go b/model/request/waf_allow_url_req.go new file mode 100644 index 0000000..3f986c0 --- /dev/null +++ b/model/request/waf_allow_url_req.go @@ -0,0 +1,38 @@ +package request + +import "SamWaf/model/common/request" + +type WafAllowUrlAddReq struct { + HostCode string `json:"host_code"` //网站唯一码(主要键) + CompareType string `json:"compare_type" form:"compare_type"` //对比方式 + Url string `json:"url"` //白名单url + Remarks string `json:"remarks"` //备注 +} +type WafAllowUrlDelReq struct { + Id string `json:"id" form:"id"` //白名单url唯一键 +} + +type WafAllowUrlDetailReq struct { + Id string `json:"id" form:"id"` //白名单Url唯一键 +} +type WafAllowUrlEditReq struct { + Id string `json:"id"` //白名单url唯一键 + HostCode string `json:"host_code"` //网站唯一码(主要键) + CompareType string `json:"compare_type" form:"compare_type"` //对比方式 + Url string `json:"url"` //白名单url + Remarks string `json:"remarks"` //备注 +} + +type WafAllowUrlSearchReq struct { + HostCode string `json:"host_code" ` //主机码 + Url string `json:"url"` //白名单url + request.PageInfo +} + +type WafAllowUrlBatchDelReq struct { + Ids []string `json:"ids" form:"ids"` //白名单url唯一键数组 +} + +type WafAllowUrlDelAllReq struct { + HostCode string `json:"host_code" form:"host_code"` //网站唯一码 +} diff --git a/model/request/waf_allow_url_search.go b/model/request/waf_allow_url_search.go deleted file mode 100644 index 3c863b6..0000000 --- a/model/request/waf_allow_url_search.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -import "SamWaf/model/common/request" - -type WafAllowUrlSearchReq struct { - HostCode string `json:"host_code" ` //主机码 - Url string `json:"url"` //白名单url - request.PageInfo -} diff --git a/router/waf_allow_url.go b/router/waf_allow_url.go index 73ebe40..06b53fc 100644 --- a/router/waf_allow_url.go +++ b/router/waf_allow_url.go @@ -16,4 +16,8 @@ func (receiver *AllowUrlRouter) InitAllowUrlRouter(group *gin.RouterGroup) { allowUrlRouter.POST("/samwaf/wafhost/urlwhite/add", AllowUrlRouterApi.AddApi) allowUrlRouter.GET("/samwaf/wafhost/urlwhite/del", AllowUrlRouterApi.DelAllowUrlApi) allowUrlRouter.POST("/samwaf/wafhost/urlwhite/edit", AllowUrlRouterApi.ModifyAllowUrlApi) + // 新增批量删除路由 + allowUrlRouter.POST("/samwaf/wafhost/urlwhite/batchdel", AllowUrlRouterApi.BatchDelAllowUrlApi) + // 新增全部删除路由 + allowUrlRouter.POST("/samwaf/wafhost/urlwhite/delall", AllowUrlRouterApi.DelAllAllowUrlApi) } diff --git a/service/waf_service/waf_allow_url.go b/service/waf_service/waf_allow_url.go index 5d80495..c48baea 100644 --- a/service/waf_service/waf_allow_url.go +++ b/service/waf_service/waf_allow_url.go @@ -107,3 +107,53 @@ func (receiver *WafWhiteUrlService) DelApi(req request.WafAllowUrlDelReq) error err = global.GWAF_LOCAL_DB.Where("id = ?", req.Id).Delete(model.URLAllowList{}).Error return err } + +// 批量删除方法 +func (receiver *WafWhiteUrlService) BatchDelApi(req request.WafAllowUrlBatchDelReq) error { + // 添加用户和租户验证 + err := global.GWAF_LOCAL_DB.Where("id IN ? AND user_code = ? AND tenant_id = ?", req.Ids, global.GWAF_USER_CODE, global.GWAF_TENANT_ID).Delete(&model.URLAllowList{}).Error + return err +} + +// 全部删除方法 +func (receiver *WafWhiteUrlService) DelAllApi(req request.WafAllowUrlDelAllReq) 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.URLAllowList{}).Where(whereCondition, whereValues...).Count(&count).Error + if err != nil { + return err + } + + if count == 0 { + return errors.New("没有URL白名单记录") + } + + // 执行删除 + err = global.GWAF_LOCAL_DB.Where(whereCondition, whereValues...).Delete(&model.URLAllowList{}).Error + return err +} + +// GetHostCodesByIds 根据ID数组获取对应的HostCode列表 +func (receiver *WafWhiteUrlService) GetHostCodesByIds(ids []string) ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.URLAllowList{}).Where("id IN ?", ids).Distinct("host_code").Pluck("host_code", &hostCodes).Error + return hostCodes, err +} + +// GetHostCodes 获取所有HostCode列表 +func (receiver *WafWhiteUrlService) GetHostCodes() ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.URLAllowList{}).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 +} From f9bd01adee4c172204a476509aaa9059af31481e Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 22 Aug 2025 09:44:18 +0800 Subject: [PATCH 3/5] feat:block url batch delete #436 --- api/waf_block_url.go | 60 +++++++++++++++++++++ model/request/waf_block_url_add_req.go | 8 --- model/request/waf_block_url_del_req.go | 5 -- model/request/waf_block_url_detail_req.go | 5 -- model/request/waf_block_url_edit_req.go | 9 ---- model/request/waf_block_url_req.go | 39 ++++++++++++++ model/request/waf_block_url_search.go | 9 ---- router/waf_block_url.go | 2 + service/waf_service/waf_block_url.go | 65 +++++++++++++++++++++++ 9 files changed, 166 insertions(+), 36 deletions(-) delete mode 100644 model/request/waf_block_url_add_req.go delete mode 100644 model/request/waf_block_url_del_req.go delete mode 100644 model/request/waf_block_url_detail_req.go delete mode 100644 model/request/waf_block_url_edit_req.go create mode 100644 model/request/waf_block_url_req.go delete mode 100644 model/request/waf_block_url_search.go diff --git a/api/waf_block_url.go b/api/waf_block_url.go index e5fddd0..4b3b7a0 100644 --- a/api/waf_block_url.go +++ b/api/waf_block_url.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 *WafBlockUrlApi) NotifyWaf(host_code string) { } global.GWAF_CHAN_MSG <- chanInfo } + +// BatchDelBlockUrlApi 批量删除URL黑名单 +func (w *WafBlockUrlApi) BatchDelBlockUrlApi(c *gin.Context) { + var req request.WafBlockUrlBatchDelReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafUrlBlockService.GetHostCodesByIds(req.Ids) + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + // 执行批量删除 + err = wafUrlBlockService.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) + } +} + +// DelAllBlockUrlApi 删除指定网站的所有URL黑名单 +func (w *WafBlockUrlApi) DelAllBlockUrlApi(c *gin.Context) { + var req request.WafBlockUrlDelAllReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafUrlBlockService.GetHostCodes() + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + err = wafUrlBlockService.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("成功删除该网站的所有URL黑名单", c) + } else { + response.OkWithMessage("成功删除所有URL黑名单", c) + } + } + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/model/request/waf_block_url_add_req.go b/model/request/waf_block_url_add_req.go deleted file mode 100644 index 35f4498..0000000 --- a/model/request/waf_block_url_add_req.go +++ /dev/null @@ -1,8 +0,0 @@ -package request - -type WafBlockUrlAddReq struct { - HostCode string `json:"host_code"` //网站唯一码(主要键) - CompareType string `json:"compare_type" form:"compare_type"` //对比方式 - Url string `json:"url"` //Block url - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_block_url_del_req.go b/model/request/waf_block_url_del_req.go deleted file mode 100644 index 6746f90..0000000 --- a/model/request/waf_block_url_del_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafBlockUrlDelReq struct { - Id string `json:"id" form:"id"` //Block url唯一键 -} diff --git a/model/request/waf_block_url_detail_req.go b/model/request/waf_block_url_detail_req.go deleted file mode 100644 index f23ff5d..0000000 --- a/model/request/waf_block_url_detail_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafBlockUrlDetailReq struct { - Id string `json:"id" form:"id"` //Block Url唯一键 -} diff --git a/model/request/waf_block_url_edit_req.go b/model/request/waf_block_url_edit_req.go deleted file mode 100644 index 494afa6..0000000 --- a/model/request/waf_block_url_edit_req.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -type WafBlockUrlEditReq struct { - Id string `json:"id"` //Block url唯一键 - HostCode string `json:"host_code"` //网站唯一码(主要键) - CompareType string `json:"compare_type" form:"compare_type"` //对比方式 - Url string `json:"url"` //Block url - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_block_url_req.go b/model/request/waf_block_url_req.go new file mode 100644 index 0000000..ae3dc5c --- /dev/null +++ b/model/request/waf_block_url_req.go @@ -0,0 +1,39 @@ +package request + +import "SamWaf/model/common/request" + +type WafBlockUrlAddReq struct { + HostCode string `json:"host_code"` //网站唯一码(主要键) + CompareType string `json:"compare_type" form:"compare_type"` //对比方式 + Url string `json:"url"` //Block url + Remarks string `json:"remarks"` //备注 +} +type WafBlockUrlSearchReq struct { + HostCode string `json:"host_code" ` //主机码 + Url string `json:"url"` //Block url + request.PageInfo +} + +type WafBlockUrlDelReq struct { + Id string `json:"id" form:"id"` //Block url唯一键 +} +type WafBlockUrlDetailReq struct { + Id string `json:"id" form:"id"` //Block Url唯一键 +} +type WafBlockUrlEditReq struct { + Id string `json:"id"` //Block url唯一键 + HostCode string `json:"host_code"` //网站唯一码(主要键) + CompareType string `json:"compare_type" form:"compare_type"` //对比方式 + Url string `json:"url"` //Block url + Remarks string `json:"remarks"` //备注 +} + +// 批量删除请求结构体 +type WafBlockUrlBatchDelReq struct { + Ids []string `json:"ids" binding:"required"` // 要删除的ID列表 +} + +// 全部删除请求结构体 +type WafBlockUrlDelAllReq struct { + HostCode string `json:"host_code"` // 网站唯一码,为空则删除所有 +} diff --git a/model/request/waf_block_url_search.go b/model/request/waf_block_url_search.go deleted file mode 100644 index 3c0839f..0000000 --- a/model/request/waf_block_url_search.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -import "SamWaf/model/common/request" - -type WafBlockUrlSearchReq struct { - HostCode string `json:"host_code" ` //主机码 - Url string `json:"url"` //Block url - request.PageInfo -} diff --git a/router/waf_block_url.go b/router/waf_block_url.go index 7424993..0af5c5e 100644 --- a/router/waf_block_url.go +++ b/router/waf_block_url.go @@ -16,4 +16,6 @@ func (receiver *BlockUrlRouter) InitBlockUrlRouter(group *gin.RouterGroup) { router.POST("/samwaf/wafhost/urlblock/add", api.AddApi) router.GET("/samwaf/wafhost/urlblock/del", api.DelBlockUrlApi) router.POST("/samwaf/wafhost/urlblock/edit", api.ModifyBlockUrlApi) + router.POST("/samwaf/wafhost/urlblock/batchdel", api.BatchDelBlockUrlApi) + router.POST("/samwaf/wafhost/urlblock/delall", api.DelAllBlockUrlApi) } diff --git a/service/waf_service/waf_block_url.go b/service/waf_service/waf_block_url.go index 0f2f91f..3731489 100644 --- a/service/waf_service/waf_block_url.go +++ b/service/waf_service/waf_block_url.go @@ -107,3 +107,68 @@ func (receiver *WafBlockUrlService) DelApi(req request.WafBlockUrlDelReq) error err = global.GWAF_LOCAL_DB.Where("id = ?", req.Id).Delete(model.URLBlockList{}).Error return err } + +// BatchDelApi 批量删除指定ID的URL黑名单 +func (receiver *WafBlockUrlService) BatchDelApi(req request.WafBlockUrlBatchDelReq) error { + if len(req.Ids) == 0 { + return errors.New("删除ID列表不能为空") + } + + // 先检查所有ID是否存在 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.URLBlockList{}).Where("id IN ? AND user_code = ? AND tenant_id = ?", req.Ids, global.GWAF_USER_CODE, global.GWAF_TENANT_ID).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 ? AND user_code = ? AND tenant_id = ?", req.Ids, global.GWAF_USER_CODE, global.GWAF_TENANT_ID).Delete(&model.URLBlockList{}).Error + return err +} + +// DelAllApi 删除指定网站的所有URL黑名单 +func (receiver *WafBlockUrlService) DelAllApi(req request.WafBlockUrlDelAllReq) 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.URLBlockList{}).Where(whereCondition, whereValues...).Count(&count).Error + if err != nil { + return err + } + + if count == 0 { + return errors.New("没有URL黑名单记录") + } + + // 执行删除 + err = global.GWAF_LOCAL_DB.Where(whereCondition, whereValues...).Delete(&model.URLBlockList{}).Error + return err +} + +// GetHostCodesByIds 根据ID数组获取对应的HostCode列表 +func (receiver *WafBlockUrlService) GetHostCodesByIds(ids []string) ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.URLBlockList{}).Where("id IN ?", ids).Distinct("host_code").Pluck("host_code", &hostCodes).Error + return hostCodes, err +} + +// GetHostCodes 获取所有HostCode列表 +func (receiver *WafBlockUrlService) GetHostCodes() ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.URLBlockList{}).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 +} From a2b3e12308b69c0cdec3a689c70522923828cce2 Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 22 Aug 2025 10:04:33 +0800 Subject: [PATCH 4/5] feat:ldp url batch delete #436 --- api/waf_ldp_url.go | 60 +++++++++++++++++++++++ model/request/waf_ldp_url_add_req.go | 8 --- model/request/waf_ldp_url_del_req.go | 5 -- model/request/waf_ldp_url_detail_req.go | 5 -- model/request/waf_ldp_url_edit_req.go | 9 ---- model/request/waf_ldp_url_req.go | 35 +++++++++++++ model/request/waf_ldp_url_search.go | 9 ---- router/waf_ldp_url.go | 2 + service/waf_service/waf_ldp.go | 65 +++++++++++++++++++++++++ 9 files changed, 162 insertions(+), 36 deletions(-) delete mode 100644 model/request/waf_ldp_url_add_req.go delete mode 100644 model/request/waf_ldp_url_del_req.go delete mode 100644 model/request/waf_ldp_url_detail_req.go delete mode 100644 model/request/waf_ldp_url_edit_req.go create mode 100644 model/request/waf_ldp_url_req.go delete mode 100644 model/request/waf_ldp_url_search.go diff --git a/api/waf_ldp_url.go b/api/waf_ldp_url.go index 4e591eb..f5ea06a 100644 --- a/api/waf_ldp_url.go +++ b/api/waf_ldp_url.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 *WafLdpUrlApi) NotifyWaf(host_code string) { } global.GWAF_CHAN_MSG <- chanInfo } + +// BatchDelLdpUrlApi 批量删除隐私保护URL +func (w *WafLdpUrlApi) BatchDelLdpUrlApi(c *gin.Context) { + var req request.WafLdpUrlBatchDelReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafLdpUrlService.GetHostCodesByIds(req.Ids) + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + // 执行批量删除 + err = wafLdpUrlService.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) + } +} + +// DelAllLdpUrlApi 删除指定网站的所有隐私保护URL +func (w *WafLdpUrlApi) DelAllLdpUrlApi(c *gin.Context) { + var req request.WafLdpUrlDelAllReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafLdpUrlService.GetHostCodes() + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + err = wafLdpUrlService.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("成功删除该网站的所有隐私保护URL", c) + } else { + response.OkWithMessage("成功删除所有隐私保护URL", c) + } + } + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/model/request/waf_ldp_url_add_req.go b/model/request/waf_ldp_url_add_req.go deleted file mode 100644 index 810f5cf..0000000 --- a/model/request/waf_ldp_url_add_req.go +++ /dev/null @@ -1,8 +0,0 @@ -package request - -type WafLdpUrlAddReq struct { - HostCode string `json:"host_code"` //网站唯一码(主要键) - CompareType string `json:"compare_type"` //对比方式 - Url string `json:"url"` //加隐私保护的url - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_ldp_url_del_req.go b/model/request/waf_ldp_url_del_req.go deleted file mode 100644 index ca35c29..0000000 --- a/model/request/waf_ldp_url_del_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafLdpUrlDelReq struct { - Id string `json:"id" form:"id"` //隐私保护url唯一键 -} diff --git a/model/request/waf_ldp_url_detail_req.go b/model/request/waf_ldp_url_detail_req.go deleted file mode 100644 index 475700e..0000000 --- a/model/request/waf_ldp_url_detail_req.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafLdpUrlDetailReq struct { - Id string `json:"id" form:"id"` //隐私保护Url唯一键 -} diff --git a/model/request/waf_ldp_url_edit_req.go b/model/request/waf_ldp_url_edit_req.go deleted file mode 100644 index 6b2b3c7..0000000 --- a/model/request/waf_ldp_url_edit_req.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -type WafLdpUrlEditReq struct { - Id string `json:"id"` //隐私保护url唯一键 - HostCode string `json:"host_code"` //网站唯一码(主要键) - CompareType string `json:"compare_type"` //对比方式 - Url string `json:"url"` //隐私保护url - Remarks string `json:"remarks"` //备注 -} diff --git a/model/request/waf_ldp_url_req.go b/model/request/waf_ldp_url_req.go new file mode 100644 index 0000000..6cf3260 --- /dev/null +++ b/model/request/waf_ldp_url_req.go @@ -0,0 +1,35 @@ +package request + +import "SamWaf/model/common/request" + +type WafLdpUrlAddReq struct { + HostCode string `json:"host_code"` //网站唯一码(主要键) + CompareType string `json:"compare_type"` //对比方式 + Url string `json:"url"` //加隐私保护的url + Remarks string `json:"remarks"` //备注 +} +type WafLdpUrlDelReq struct { + Id string `json:"id" form:"id"` //隐私保护url唯一键 +} +type WafLdpUrlDetailReq struct { + Id string `json:"id" form:"id"` //隐私保护Url唯一键 +} +type WafLdpUrlEditReq struct { + Id string `json:"id"` //隐私保护url唯一键 + HostCode string `json:"host_code"` //网站唯一码(主要键) + CompareType string `json:"compare_type"` //对比方式 + Url string `json:"url"` //隐私保护url + Remarks string `json:"remarks"` //备注 +} +type WafLdpUrlSearchReq struct { + HostCode string `json:"host_code" ` //主机码 + Url string `json:"url"` //隐私保护url + request.PageInfo +} +type WafLdpUrlBatchDelReq struct { + Ids []string `json:"ids" binding:"required"` //隐私保护URL唯一键数组 +} + +type WafLdpUrlDelAllReq struct { + HostCode string `json:"host_code" form:"host_code"` //网站唯一码,为空则删除所有 +} diff --git a/model/request/waf_ldp_url_search.go b/model/request/waf_ldp_url_search.go deleted file mode 100644 index a082a0d..0000000 --- a/model/request/waf_ldp_url_search.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -import "SamWaf/model/common/request" - -type WafLdpUrlSearchReq struct { - HostCode string `json:"host_code" ` //主机码 - Url string `json:"url"` //隐私保护url - request.PageInfo -} diff --git a/router/waf_ldp_url.go b/router/waf_ldp_url.go index b9ec861..47c33fa 100644 --- a/router/waf_ldp_url.go +++ b/router/waf_ldp_url.go @@ -16,4 +16,6 @@ func (receiver *LdpUrlRouter) InitLdpUrlRouter(group *gin.RouterGroup) { ldpUrlRouter.POST("/samwaf/wafhost/ldpurl/add", LdpUrlRouterApi.AddApi) ldpUrlRouter.GET("/samwaf/wafhost/ldpurl/del", LdpUrlRouterApi.DelLdpUrlApi) ldpUrlRouter.POST("/samwaf/wafhost/ldpurl/edit", LdpUrlRouterApi.ModifyLdpUrlApi) + ldpUrlRouter.POST("/samwaf/wafhost/ldpurl/batchdel", LdpUrlRouterApi.BatchDelLdpUrlApi) + ldpUrlRouter.POST("/samwaf/wafhost/ldpurl/delall", LdpUrlRouterApi.DelAllLdpUrlApi) } diff --git a/service/waf_service/waf_ldp.go b/service/waf_service/waf_ldp.go index bb9a511..d0e5c62 100644 --- a/service/waf_service/waf_ldp.go +++ b/service/waf_service/waf_ldp.go @@ -108,3 +108,68 @@ func (receiver *WafLdpUrlService) DelApi(req request.WafLdpUrlDelReq) error { err = global.GWAF_LOCAL_DB.Where("id = ?", req.Id).Delete(model.LDPUrl{}).Error return err } + +// BatchDelApi 批量删除指定ID的隐私保护URL +func (receiver *WafLdpUrlService) BatchDelApi(req request.WafLdpUrlBatchDelReq) error { + if len(req.Ids) == 0 { + return errors.New("删除ID列表不能为空") + } + + // 先检查所有ID是否存在 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.LDPUrl{}).Where("id IN ? AND user_code = ? AND tenant_id = ?", req.Ids, global.GWAF_USER_CODE, global.GWAF_TENANT_ID).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 ? AND user_code = ? AND tenant_id = ?", req.Ids, global.GWAF_USER_CODE, global.GWAF_TENANT_ID).Delete(&model.LDPUrl{}).Error + return err +} + +// DelAllApi 删除指定网站的所有隐私保护URL +func (receiver *WafLdpUrlService) DelAllApi(req request.WafLdpUrlDelAllReq) 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.LDPUrl{}).Where(whereCondition, whereValues...).Count(&count).Error + if err != nil { + return err + } + + if count == 0 { + return errors.New("没有隐私保护URL记录") + } + + // 执行删除 + err = global.GWAF_LOCAL_DB.Where(whereCondition, whereValues...).Delete(&model.LDPUrl{}).Error + return err +} + +// GetHostCodesByIds 根据ID列表获取HostCode列表 +func (receiver *WafLdpUrlService) GetHostCodesByIds(ids []string) ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.LDPUrl{}).Where("id IN ?", ids).Pluck("host_code", &hostCodes).Error + return hostCodes, err +} + +// GetHostCodes 获取所有HostCode列表 +func (receiver *WafLdpUrlService) GetHostCodes() ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.LDPUrl{}).Pluck("host_code", &hostCodes).Error + return hostCodes, err +} From 0e73773fb034fa3b32ecb55ac0e0946ac7dc3351 Mon Sep 17 00:00:00 2001 From: samwaf Date: Fri, 22 Aug 2025 11:14:32 +0800 Subject: [PATCH 5/5] feat:rule batch delete #436 --- api/waf_rule.go | 59 +++++++++++++++++++++++ model/request/waf_rule_add_req.go | 8 ---- model/request/waf_rule_del.go | 5 -- model/request/waf_rule_detail.go | 5 -- model/request/waf_rule_edit_req.go | 8 ---- model/request/waf_rule_req.go | 34 ++++++++++++++ model/request/waf_rule_search.go | 9 ---- router/waf_rule.go | 2 + service/waf_service/waf_rule.go | 75 ++++++++++++++++++++++++++++++ 9 files changed, 170 insertions(+), 35 deletions(-) delete mode 100644 model/request/waf_rule_add_req.go delete mode 100644 model/request/waf_rule_del.go delete mode 100644 model/request/waf_rule_detail.go delete mode 100644 model/request/waf_rule_edit_req.go create mode 100644 model/request/waf_rule_req.go delete mode 100644 model/request/waf_rule_search.go diff --git a/api/waf_rule.go b/api/waf_rule.go index ef14576..6fff62a 100644 --- a/api/waf_rule.go +++ b/api/waf_rule.go @@ -217,3 +217,62 @@ func (w *WafRuleAPi) NotifyWaf(host_code string) { } global.GWAF_CHAN_MSG <- chanInfo } + +// BatchDelRuleApi 批量删除规则 +func (w *WafRuleAPi) BatchDelRuleApi(c *gin.Context) { + var req request.WafRuleBatchDelReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafRuleService.GetHostCodesByCodes(req.Codes) + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + // 执行批量删除 + err = wafRuleService.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.Codes)), c) + } + } else { + response.FailWithMessage("解析失败", c) + } +} + +// DelAllRuleApi 删除指定网站的所有规则 +func (w *WafRuleAPi) DelAllRuleApi(c *gin.Context) { + var req request.WafRuleDelAllReq + err := c.ShouldBindJSON(&req) + if err == nil { + // 先获取要删除的记录对应的HostCode,用于后续通知WAF引擎 + hostCodes, err := wafRuleService.GetHostCodes() + if err != nil { + response.FailWithMessage("获取网站信息失败", c) + return + } + + err = wafRuleService.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("成功删除该网站的所有规则", c) + } else { + response.OkWithMessage("成功删除所有规则", c) + } + } + } else { + response.FailWithMessage("解析失败", c) + } +} diff --git a/model/request/waf_rule_add_req.go b/model/request/waf_rule_add_req.go deleted file mode 100644 index 7e92b57..0000000 --- a/model/request/waf_rule_add_req.go +++ /dev/null @@ -1,8 +0,0 @@ -package request - -type WafRuleAddReq struct { - RuleCode string `json:"rule_code"` //规则编号v4 - RuleJson string - IsManualRule int `json:"is_manual_rule"` - RuleContent string `json:"rule_content"` //规则内容 -} diff --git a/model/request/waf_rule_del.go b/model/request/waf_rule_del.go deleted file mode 100644 index 1a273cf..0000000 --- a/model/request/waf_rule_del.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafRuleDelReq struct { - CODE string `json:"code"` -} diff --git a/model/request/waf_rule_detail.go b/model/request/waf_rule_detail.go deleted file mode 100644 index 622e4f9..0000000 --- a/model/request/waf_rule_detail.go +++ /dev/null @@ -1,5 +0,0 @@ -package request - -type WafRuleDetailReq struct { - CODE string `json:"code"` -} diff --git a/model/request/waf_rule_edit_req.go b/model/request/waf_rule_edit_req.go deleted file mode 100644 index 8ab1e30..0000000 --- a/model/request/waf_rule_edit_req.go +++ /dev/null @@ -1,8 +0,0 @@ -package request - -type WafRuleEditReq struct { - CODE string `json:"code"` - RuleJson string `json:"rulejson"` - IsManualRule int `json:"is_manual_rule"` - RuleContent string `json:"rule_content"` //规则内容 -} diff --git a/model/request/waf_rule_req.go b/model/request/waf_rule_req.go new file mode 100644 index 0000000..bf0f7c8 --- /dev/null +++ b/model/request/waf_rule_req.go @@ -0,0 +1,34 @@ +package request + +import "SamWaf/model/common/request" + +type WafRuleAddReq struct { + RuleCode string `json:"rule_code"` //规则编号v4 + RuleJson string + IsManualRule int `json:"is_manual_rule"` + RuleContent string `json:"rule_content"` //规则内容 +} +type WafRuleDelReq struct { + CODE string `json:"code"` +} +type WafRuleDetailReq struct { + CODE string `json:"code"` +} +type WafRuleEditReq struct { + CODE string `json:"code"` + RuleJson string `json:"rulejson"` + IsManualRule int `json:"is_manual_rule"` + RuleContent string `json:"rule_content"` //规则内容 +} +type WafRuleSearchReq struct { + HostCode string `json:"host_code" form:"host_code"` //主机码 + RuleName string `json:"rule_name" form:"rule_name"` //规则名 + request.PageInfo +} +type WafRuleBatchDelReq struct { + Codes []string `json:"codes" binding:"required"` //规则编码数组 +} + +type WafRuleDelAllReq struct { + HostCode string `json:"host_code" form:"host_code"` //网站唯一码,为空则删除所有 +} diff --git a/model/request/waf_rule_search.go b/model/request/waf_rule_search.go deleted file mode 100644 index d4ae84c..0000000 --- a/model/request/waf_rule_search.go +++ /dev/null @@ -1,9 +0,0 @@ -package request - -import "SamWaf/model/common/request" - -type WafRuleSearchReq struct { - HostCode string `json:"host_code" form:"host_code"` //主机码 - RuleName string `json:"rule_name" form:"rule_name"` //规则名 - request.PageInfo -} diff --git a/router/waf_rule.go b/router/waf_rule.go index 723cd7e..bce4b1f 100644 --- a/router/waf_rule.go +++ b/router/waf_rule.go @@ -16,4 +16,6 @@ func (receiver *RuleRouter) InitRuleRouter(group *gin.RouterGroup) { wafRuleRouter.POST("/samwaf/wafhost/rule/add", ruleApi.AddApi) wafRuleRouter.GET("/samwaf/wafhost/rule/del", ruleApi.DelRuleApi) wafRuleRouter.POST("/samwaf/wafhost/rule/edit", ruleApi.ModifyRuleApi) + wafRuleRouter.POST("/samwaf/wafhost/rule/batchdel", ruleApi.BatchDelRuleApi) + wafRuleRouter.POST("/samwaf/wafhost/rule/delall", ruleApi.DelAllRuleApi) } diff --git a/service/waf_service/waf_rule.go b/service/waf_service/waf_rule.go index 27b86fe..2f029bb 100644 --- a/service/waf_service/waf_rule.go +++ b/service/waf_service/waf_rule.go @@ -159,3 +159,78 @@ func (receiver *WafRuleService) DelRuleApi(req request.WafRuleDelReq) error { } return nil } + +// BatchDelApi 批量删除指定编码的规则 +func (receiver *WafRuleService) BatchDelApi(req request.WafRuleBatchDelReq) error { + if len(req.Codes) == 0 { + return errors.New("删除编码列表不能为空") + } + + // 先检查所有编码是否存在 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.Rules{}).Where("rule_code IN ? AND user_code = ? AND tenant_id = ? AND rule_status <> 999", req.Codes, global.GWAF_USER_CODE, global.GWAF_TENANT_ID).Count(&count).Error + if err != nil { + return err + } + + if count != int64(len(req.Codes)) { + return errors.New("部分规则编码不存在") + } + + // 执行批量删除(软删除) + ruleMap := map[string]interface{}{ + "RuleStatus": "999", + "RuleVersion": 999999, + "UPDATE_TIME": customtype.JsonTime(time.Now()), + } + err = global.GWAF_LOCAL_DB.Model(&model.Rules{}).Where("rule_code IN ? AND user_code = ? AND tenant_id = ?", req.Codes, global.GWAF_USER_CODE, global.GWAF_TENANT_ID).Updates(ruleMap).Error + return err +} + +// DelAllApi 删除指定网站的所有规则 +func (receiver *WafRuleService) DelAllApi(req request.WafRuleDelAllReq) error { + var whereCondition string + var whereValues []interface{} + + if len(req.HostCode) > 0 { + whereCondition = "host_code = ? AND user_code = ? AND tenant_id = ? AND rule_status <> 999" + whereValues = append(whereValues, req.HostCode, global.GWAF_USER_CODE, global.GWAF_TENANT_ID) + } else { + whereCondition = "user_code = ? AND tenant_id = ? AND rule_status <> 999" + whereValues = append(whereValues, global.GWAF_USER_CODE, global.GWAF_TENANT_ID) + } + + // 先检查是否存在记录 + var count int64 + err := global.GWAF_LOCAL_DB.Model(&model.Rules{}).Where(whereCondition, whereValues...).Count(&count).Error + if err != nil { + return err + } + + if count == 0 { + return errors.New("没有规则记录") + } + + // 执行删除(软删除) + ruleMap := map[string]interface{}{ + "RuleStatus": "999", + "RuleVersion": 999999, + "UPDATE_TIME": customtype.JsonTime(time.Now()), + } + err = global.GWAF_LOCAL_DB.Model(&model.Rules{}).Where(whereCondition, whereValues...).Updates(ruleMap).Error + return err +} + +// GetHostCodesByCodes 根据规则编码列表获取HostCode列表 +func (receiver *WafRuleService) GetHostCodesByCodes(codes []string) ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.Rules{}).Where("rule_code IN ? AND rule_status <> 999", codes).Pluck("host_code", &hostCodes).Error + return hostCodes, err +} + +// GetHostCodes 获取所有HostCode列表 +func (receiver *WafRuleService) GetHostCodes() ([]string, error) { + var hostCodes []string + err := global.GWAF_LOCAL_DB.Model(&model.Rules{}).Where("rule_status <> 999").Pluck("host_code", &hostCodes).Error + return hostCodes, err +}