mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2026-11-03 19:52:14 +08:00
!16 feat:batch delete block ip
Merge pull request !16 from samwaf/feat_batchdel_blockip
This commit is contained in:
@@ -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引擎实时生效
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package request
|
||||
|
||||
type WafBlockIpAddReq struct {
|
||||
HostCode string `json:"host_code"` //网站唯一码(主要键)
|
||||
Ip string `json:"ip"` //Block ip
|
||||
Remarks string `json:"remarks"` //备注
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package request
|
||||
|
||||
type WafBlockIpDelReq struct {
|
||||
Id string `json:"id" form:"id"` //Block IP唯一键
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package request
|
||||
|
||||
type WafBlockIpDetailReq struct {
|
||||
Id string `json:"id" form:"id"` //Block IP唯一键
|
||||
}
|
||||
@@ -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"` //备注
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user