feat:modify rule

This commit is contained in:
samwaf
2025-11-06 16:39:49 +08:00
parent 59907860ae
commit 17fd450320
4 changed files with 65 additions and 5 deletions

View File

@@ -12,9 +12,10 @@ import (
"SamWaf/utils"
"errors"
"fmt"
"strings"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"strings"
)
type WafRuleAPi struct {
@@ -40,6 +41,9 @@ func (w *WafRuleAPi) AddApi(c *gin.Context) {
if ruleCodeFormDRL != "{SamWafUUID}" {
ruleCode = ruleCodeFormDRL
}
} else {
//手工编码情况下 前端准备好的
ruleCode = req.RuleCode
}
if ruleInfo.RuleBase.RuleDomainCode == "请选择网站" {
response.FailWithMessage("请选择网站", c)
@@ -276,3 +280,46 @@ func (w *WafRuleAPi) DelAllRuleApi(c *gin.Context) {
response.FailWithMessage("解析失败", c)
}
}
// WafRulePreViewReq 规则格式预览
func (w *WafRuleAPi) FormatRuleApi(c *gin.Context) {
ruleHelper := &utils.RuleHelper{}
var req request.WafRulePreViewReq
err := c.ShouldBindJSON(&req)
if err != nil {
response.FailWithMessage("解析失败", c)
return
}
var ruleTool = model.RuleTool{}
ruleInfo, err := ruleTool.LoadRule(req.RuleJson)
if err != nil {
response.FailWithMessage("规则解析错误", c)
return
}
// 与新增/编辑保持一致,先校验网站选择
if ruleInfo.RuleBase.RuleDomainCode == "请选择网站" && req.FormSource != "builder" {
response.FailWithMessage("请选择网站", c)
return
}
chsName := ruleInfo.RuleBase.RuleName
ruleInfo.RuleBase.RuleName = strings.Replace(req.RuleCode, "-", "", -1)
var ruleContent = ruleTool.GenRuleInfo(ruleInfo, chsName)
// 手工模式走合法性校验
if req.IsManualRule == 1 {
ruleContent = ruleInfo.RuleContent
err = ruleHelper.CheckRuleAvailable(ruleContent)
if err != nil {
response.FailWithMessage("规则校验失败", c)
return
}
}
// 返回格式化内容,供前端展示
response.OkWithDetailed(gin.H{
"rule_content": ruleContent,
}, "获取成功", c)
}

View File

@@ -3,7 +3,11 @@ export_download: false
local_port: 26666
notice:
isenable: false
security:
ip_whitelist: 0.0.0.0/0,::/0
soft_id: SamWafCom
user_code: 8ad2bca0-4fd0-46aa-afe6-833132a05eee
zlog:
db_debug_enable: false
debug_enable: false
outputformat: console

View File

@@ -4,9 +4,9 @@ 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"` //规则内容
RuleJson string `json:"rule_json"`
IsManualRule int `json:"is_manual_rule"` // 0 是界面 1是纯代码
RuleContent string `json:"rule_content"` //规则内容
}
type WafRuleDelReq struct {
CODE string `json:"code"`
@@ -16,7 +16,7 @@ type WafRuleDetailReq struct {
}
type WafRuleEditReq struct {
CODE string `json:"code"`
RuleJson string `json:"rulejson"`
RuleJson string `json:"rule_json"`
IsManualRule int `json:"is_manual_rule"`
RuleContent string `json:"rule_content"` //规则内容
}
@@ -32,3 +32,10 @@ type WafRuleBatchDelReq struct {
type WafRuleDelAllReq struct {
HostCode string `json:"host_code" form:"host_code"` //网站唯一码,为空则删除所有
}
type WafRulePreViewReq struct {
RuleCode string `json:"rule_code"` //规则编号v4
RuleJson string `json:"rule_json"` //规则json字符串
IsManualRule int `json:"is_manual_rule"` // 0 是界面 1是纯代码
RuleContent string `json:"rule_content"` //规则内容
FormSource string `json:"form_source"` //来源是 builder 不校验 选择的站点
}

View File

@@ -2,6 +2,7 @@ package router
import (
"SamWaf/api"
"github.com/gin-gonic/gin"
)
@@ -18,4 +19,5 @@ func (receiver *RuleRouter) InitRuleRouter(group *gin.RouterGroup) {
wafRuleRouter.POST("/samwaf/wafhost/rule/edit", ruleApi.ModifyRuleApi)
wafRuleRouter.POST("/samwaf/wafhost/rule/batchdel", ruleApi.BatchDelRuleApi)
wafRuleRouter.POST("/samwaf/wafhost/rule/delall", ruleApi.DelAllRuleApi)
wafRuleRouter.POST("/samwaf/wafhost/rule/format", ruleApi.FormatRuleApi)
}