Files
EdgeAdmin/internal/web/actions/default/servers/components/waf/updateSetPopup.go

169 lines
4.2 KiB
Go
Raw Normal View History

2020-10-08 11:11:37 +08:00
package waf
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2023-06-28 19:07:42 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
2020-12-23 09:52:31 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
2020-10-08 11:11:37 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type UpdateSetPopupAction struct {
actionutils.ParentAction
}
func (this *UpdateSetPopupAction) Init() {
this.Nav("", "", "")
}
func (this *UpdateSetPopupAction) RunGet(params struct {
FirewallPolicyId int64
GroupId int64
Type string
SetId int64
}) {
// 日志
2023-06-30 18:08:30 +08:00
defer this.CreateLogInfo(codes.WAFRuleSet_LogUpdateRuleSet, params.SetId)
2020-10-08 11:11:37 +08:00
this.Data["groupId"] = params.GroupId
this.Data["type"] = params.Type
2020-12-23 09:52:31 +08:00
firewallPolicy, err := dao.SharedHTTPFirewallPolicyDAO.FindEnabledHTTPFirewallPolicyConfig(this.AdminContext(), params.FirewallPolicyId)
2020-10-08 11:11:37 +08:00
if err != nil {
this.ErrorPage(err)
return
}
if firewallPolicy == nil {
this.NotFound("firewallPolicy", params.FirewallPolicyId)
return
}
this.Data["firewallPolicy"] = firewallPolicy
// 一些配置
this.Data["connectors"] = []maps.Map{
{
2023-06-30 18:08:30 +08:00
"name": this.Lang(codes.WAF_ConnectorAnd),
2020-10-08 11:11:37 +08:00
"value": firewallconfigs.HTTPFirewallRuleConnectorAnd,
2023-06-30 18:08:30 +08:00
"description": this.Lang(codes.WAF_ConnectorAndDescription),
2020-10-08 11:11:37 +08:00
},
{
2023-06-30 18:08:30 +08:00
"name": this.Lang(codes.WAF_ConnectorOr),
2020-10-08 11:11:37 +08:00
"value": firewallconfigs.HTTPFirewallRuleConnectorOr,
2023-06-30 18:08:30 +08:00
"description": this.Lang(codes.WAF_ConnectorOrDescription),
2020-10-08 11:11:37 +08:00
},
}
var actionMaps = []maps.Map{}
2020-10-08 11:11:37 +08:00
for _, action := range firewallconfigs.AllActions {
actionMaps = append(actionMaps, maps.Map{
"name": action.Name,
"description": action.Description,
"code": action.Code,
})
}
this.Data["actions"] = actionMaps
// 规则集信息
2020-12-23 09:52:31 +08:00
setConfig, err := dao.SharedHTTPFirewallRuleSetDAO.FindRuleSetConfig(this.AdminContext(), params.SetId)
2020-10-08 11:11:37 +08:00
if err != nil {
this.ErrorPage(err)
return
}
if setConfig == nil {
this.NotFound("firewallRuleSet", params.SetId)
return
}
this.Data["setConfig"] = setConfig
2021-07-14 22:45:52 +08:00
// action configs
actionConfigs, err := dao.SharedHTTPFirewallPolicyDAO.FindHTTPFirewallActionConfigs(this.AdminContext(), setConfig.Actions)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["actionConfigs"] = actionConfigs
2020-10-08 11:11:37 +08:00
this.Show()
}
func (this *UpdateSetPopupAction) RunPost(params struct {
GroupId int64
SetId int64
Name string
RulesJSON []byte
Connector string
ActionsJSON []byte
IgnoreLocal bool
IgnoreSearchEngine bool
2020-10-08 11:11:37 +08:00
Must *actions.Must
}) {
// 规则集信息
2020-12-23 09:52:31 +08:00
setConfig, err := dao.SharedHTTPFirewallRuleSetDAO.FindRuleSetConfig(this.AdminContext(), params.SetId)
2020-10-08 11:11:37 +08:00
if err != nil {
this.ErrorPage(err)
return
}
if setConfig == nil {
this.NotFound("firewallRuleSet", params.SetId)
return
}
params.Must.
Field("name", params.Name).
Require("请输入规则集名称")
if len(params.RulesJSON) == 0 {
this.Fail("请添加至少一个规则")
2024-05-06 20:30:51 +08:00
return
2020-10-08 11:11:37 +08:00
}
var rules = []*firewallconfigs.HTTPFirewallRule{}
2020-10-08 11:11:37 +08:00
err = json.Unmarshal(params.RulesJSON, &rules)
if err != nil {
this.ErrorPage(err)
2021-07-14 22:45:52 +08:00
return
2020-10-08 11:11:37 +08:00
}
if len(rules) == 0 {
this.Fail("请添加至少一个规则")
2024-05-06 20:30:51 +08:00
return
2020-10-08 11:11:37 +08:00
}
2021-07-14 22:45:52 +08:00
var actionConfigs = []*firewallconfigs.HTTPFirewallActionConfig{}
if len(params.ActionsJSON) > 0 {
err = json.Unmarshal(params.ActionsJSON, &actionConfigs)
if err != nil {
this.ErrorPage(err)
return
}
}
if len(actionConfigs) == 0 {
this.Fail("请添加至少一个动作")
2024-05-06 20:30:51 +08:00
return
2021-07-14 22:45:52 +08:00
}
2020-10-08 11:11:37 +08:00
setConfig.Name = params.Name
setConfig.Connector = params.Connector
setConfig.Rules = rules
2021-07-14 22:45:52 +08:00
setConfig.Actions = actionConfigs
setConfig.IgnoreLocal = params.IgnoreLocal
setConfig.IgnoreSearchEngine = params.IgnoreSearchEngine
2020-10-08 11:11:37 +08:00
setConfigJSON, err := json.Marshal(setConfig)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPFirewallRuleSetRPC().CreateOrUpdateHTTPFirewallRuleSetFromConfig(this.AdminContext(), &pb.CreateOrUpdateHTTPFirewallRuleSetFromConfigRequest{FirewallRuleSetConfigJSON: setConfigJSON})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}