mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2025-12-06 14:59:18 +08:00
@@ -8,6 +8,7 @@ import (
|
|||||||
"SamWaf/model/request"
|
"SamWaf/model/request"
|
||||||
"SamWaf/model/spec"
|
"SamWaf/model/spec"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
@@ -115,3 +116,62 @@ func (w *WafAllowIpApi) NotifyWaf(host_code string) {
|
|||||||
}
|
}
|
||||||
global.GWAF_CHAN_MSG <- chanInfo
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package request
|
|
||||||
|
|
||||||
type WafAllowIpAddReq struct {
|
|
||||||
HostCode string `json:"host_code"` //网站唯一码(主要键)
|
|
||||||
Ip string `json:"ip"` //白名单ip
|
|
||||||
Remarks string `json:"remarks"` //备注
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package request
|
|
||||||
|
|
||||||
type WafAllowIpDelReq struct {
|
|
||||||
Id string `json:"id" form:"id"` //白名单IP唯一键
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package request
|
|
||||||
|
|
||||||
type WafAllowIpDetailReq struct {
|
|
||||||
Id string `json:"id" form:"id"` //白名单IP唯一键
|
|
||||||
}
|
|
||||||
@@ -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"` //备注
|
|
||||||
}
|
|
||||||
34
model/request/waf_allow_ip_req.go
Normal file
34
model/request/waf_allow_ip_req.go
Normal file
@@ -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"` //网站唯一码,为空则删除所有
|
||||||
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -16,4 +16,7 @@ func (receiver *AllowIpRouter) InitAllowIpRouter(group *gin.RouterGroup) {
|
|||||||
allowIpRouter.POST("/samwaf/wafhost/ipwhite/add", AllowIpRouterApi.AddApi)
|
allowIpRouter.POST("/samwaf/wafhost/ipwhite/add", AllowIpRouterApi.AddApi)
|
||||||
allowIpRouter.GET("/samwaf/wafhost/ipwhite/del", AllowIpRouterApi.DelAllowIpApi)
|
allowIpRouter.GET("/samwaf/wafhost/ipwhite/del", AllowIpRouterApi.DelAllowIpApi)
|
||||||
allowIpRouter.POST("/samwaf/wafhost/ipwhite/edit", AllowIpRouterApi.ModifyAllowIpApi)
|
allowIpRouter.POST("/samwaf/wafhost/ipwhite/edit", AllowIpRouterApi.ModifyAllowIpApi)
|
||||||
|
|
||||||
|
allowIpRouter.POST("/samwaf/wafhost/ipwhite/batchdel", AllowIpRouterApi.BatchDelAllowIpApi)
|
||||||
|
allowIpRouter.POST("/samwaf/wafhost/ipwhite/delall", AllowIpRouterApi.DelAllAllowIpApi)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
err = global.GWAF_LOCAL_DB.Where("id = ?", req.Id).Delete(model.IPAllowList{}).Error
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user