2020-10-08 11:11:37 +08:00
|
|
|
package waf
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
|
|
|
|
"github.com/iwind/TeaGo/actions"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreateRulePopupAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateRulePopupAction) Init() {
|
|
|
|
|
this.Nav("", "", "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateRulePopupAction) RunGet(params struct {
|
|
|
|
|
Type string
|
|
|
|
|
}) {
|
|
|
|
|
// check points
|
2023-08-13 10:00:41 +08:00
|
|
|
var checkpointList = []maps.Map{}
|
2020-11-18 19:35:32 +08:00
|
|
|
for _, checkpoint := range firewallconfigs.AllCheckpoints {
|
2021-06-12 10:53:59 +08:00
|
|
|
if (params.Type == "inbound" && checkpoint.IsRequest) || params.Type == "outbound" {
|
2020-10-08 11:11:37 +08:00
|
|
|
checkpointList = append(checkpointList, maps.Map{
|
2020-11-18 19:35:32 +08:00
|
|
|
"name": checkpoint.Name,
|
|
|
|
|
"prefix": checkpoint.Prefix,
|
|
|
|
|
"description": checkpoint.Description,
|
2020-11-21 20:43:45 +08:00
|
|
|
"hasParams": checkpoint.HasParams,
|
2020-11-18 19:35:32 +08:00
|
|
|
"params": checkpoint.Params,
|
|
|
|
|
"options": checkpoint.Options,
|
|
|
|
|
"isComposed": checkpoint.IsComposed,
|
2022-11-16 15:43:08 +08:00
|
|
|
"dataType": checkpoint.DataType,
|
2020-10-08 11:11:37 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// operators
|
2023-08-13 10:00:41 +08:00
|
|
|
var operatorMaps = []maps.Map{}
|
|
|
|
|
for _, operator := range firewallconfigs.AllRuleOperators {
|
|
|
|
|
operatorMaps = append(operatorMaps, maps.Map{
|
|
|
|
|
"name": operator.Name,
|
|
|
|
|
"code": operator.Code,
|
|
|
|
|
"description": operator.Description,
|
|
|
|
|
"case": operator.CaseInsensitive,
|
|
|
|
|
"dataType": operator.DataType,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
this.Data["operators"] = operatorMaps
|
2020-10-08 11:11:37 +08:00
|
|
|
|
|
|
|
|
this.Data["checkpoints"] = checkpointList
|
|
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateRulePopupAction) RunPost(params struct {
|
2020-11-21 20:43:45 +08:00
|
|
|
RuleId int64
|
|
|
|
|
Prefix string
|
|
|
|
|
Operator string
|
|
|
|
|
Param string
|
|
|
|
|
ParamFiltersJSON []byte
|
|
|
|
|
OptionsJSON []byte
|
|
|
|
|
Value string
|
|
|
|
|
Case bool
|
2022-01-10 10:28:23 +08:00
|
|
|
Description string
|
2020-10-08 11:11:37 +08:00
|
|
|
|
|
|
|
|
Must *actions.Must
|
|
|
|
|
}) {
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("prefix", params.Prefix).
|
|
|
|
|
Require("请选择参数")
|
|
|
|
|
|
|
|
|
|
rule := &firewallconfigs.HTTPFirewallRule{
|
|
|
|
|
Id: params.RuleId,
|
|
|
|
|
IsOn: true,
|
|
|
|
|
}
|
|
|
|
|
if len(params.Param) > 0 {
|
|
|
|
|
rule.Param = "${" + params.Prefix + "." + params.Param + "}"
|
|
|
|
|
} else {
|
|
|
|
|
rule.Param = "${" + params.Prefix + "}"
|
|
|
|
|
}
|
2020-11-21 20:43:45 +08:00
|
|
|
|
|
|
|
|
paramFilters := []*firewallconfigs.ParamFilter{}
|
|
|
|
|
if len(params.ParamFiltersJSON) > 0 {
|
|
|
|
|
err := json.Unmarshal(params.ParamFiltersJSON, ¶mFilters)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rule.ParamFilters = paramFilters
|
|
|
|
|
|
2020-10-08 11:11:37 +08:00
|
|
|
rule.Operator = params.Operator
|
|
|
|
|
rule.Value = params.Value
|
|
|
|
|
rule.IsCaseInsensitive = params.Case
|
2022-01-10 10:28:23 +08:00
|
|
|
rule.Description = params.Description
|
2020-10-08 11:11:37 +08:00
|
|
|
|
|
|
|
|
if len(params.OptionsJSON) > 0 {
|
|
|
|
|
options := []maps.Map{}
|
|
|
|
|
err := json.Unmarshal(params.OptionsJSON, &options)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule.CheckpointOptions = map[string]interface{}{}
|
|
|
|
|
for _, option := range options {
|
2020-11-18 19:35:32 +08:00
|
|
|
rule.CheckpointOptions[option.GetString("code")] = option.Get("value")
|
2020-10-08 11:11:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验
|
|
|
|
|
err := rule.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.Fail("校验规则 '" + rule.Param + " " + rule.Operator + " " + rule.Value + "' 失败,原因:" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Data["rule"] = rule
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|