mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-07 02:20:24 +08:00
WAF策略:可以修改分组代号/导入时可以根据名称合并
This commit is contained in:
@@ -95,6 +95,7 @@ func (this *HTTPFirewallRuleGroupDAO) ComposeFirewallRuleGroup(tx *dbs.Tx, group
|
|||||||
config.Name = group.Name
|
config.Name = group.Name
|
||||||
config.Description = group.Description
|
config.Description = group.Description
|
||||||
config.Code = group.Code
|
config.Code = group.Code
|
||||||
|
config.IsTemplate = group.IsTemplate == 1
|
||||||
|
|
||||||
if IsNotNull(group.Sets) {
|
if IsNotNull(group.Sets) {
|
||||||
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
||||||
@@ -125,6 +126,7 @@ func (this *HTTPFirewallRuleGroupDAO) CreateGroupFromConfig(tx *dbs.Tx, groupCon
|
|||||||
op.Description = groupConfig.Description
|
op.Description = groupConfig.Description
|
||||||
op.State = HTTPFirewallRuleGroupStateEnabled
|
op.State = HTTPFirewallRuleGroupStateEnabled
|
||||||
op.Code = groupConfig.Code
|
op.Code = groupConfig.Code
|
||||||
|
op.IsTemplate = groupConfig.IsTemplate
|
||||||
|
|
||||||
// sets
|
// sets
|
||||||
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
setRefs := []*firewallconfigs.HTTPFirewallRuleSetRef{}
|
||||||
@@ -178,7 +180,7 @@ func (this *HTTPFirewallRuleGroupDAO) CreateGroup(tx *dbs.Tx, isOn bool, name st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateGroup 修改分组
|
// UpdateGroup 修改分组
|
||||||
func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isOn bool, name string, description string) error {
|
func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isOn bool, name string, code string, description string) error {
|
||||||
if groupId <= 0 {
|
if groupId <= 0 {
|
||||||
return errors.New("invalid groupId")
|
return errors.New("invalid groupId")
|
||||||
}
|
}
|
||||||
@@ -186,6 +188,7 @@ func (this *HTTPFirewallRuleGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, isO
|
|||||||
op.Id = groupId
|
op.Id = groupId
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
op.Name = name
|
op.Name = name
|
||||||
|
op.Code = code
|
||||||
op.Description = description
|
op.Description = description
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
// 防火墙规则分组
|
// HTTPFirewallRuleGroup 防火墙规则分组
|
||||||
type HTTPFirewallRuleGroup struct {
|
type HTTPFirewallRuleGroup struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
Code string `field:"code"` // 代号
|
Code string `field:"code"` // 代号
|
||||||
|
IsTemplate uint8 `field:"isTemplate"` // 是否为预置模板
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
@@ -20,6 +21,7 @@ type HTTPFirewallRuleGroupOperator struct {
|
|||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
Code interface{} // 代号
|
Code interface{} // 代号
|
||||||
|
IsTemplate interface{} // 是否为预置模板
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
|
|||||||
@@ -506,9 +506,18 @@ func (this *HTTPFirewallPolicyService) ImportHTTPFirewallPolicy(ctx context.Cont
|
|||||||
// 入站分组
|
// 入站分组
|
||||||
if newConfig.Inbound != nil {
|
if newConfig.Inbound != nil {
|
||||||
for _, g := range newConfig.Inbound.Groups {
|
for _, g := range newConfig.Inbound.Groups {
|
||||||
|
var oldGroup *firewallconfigs.HTTPFirewallRuleGroup
|
||||||
|
|
||||||
|
// 使用代号查找
|
||||||
if len(g.Code) > 0 {
|
if len(g.Code) > 0 {
|
||||||
// 对于有代号的,覆盖或者添加
|
oldGroup = oldConfig.FindRuleGroupWithCode(g.Code)
|
||||||
oldGroup := oldConfig.FindRuleGroupWithCode(g.Code)
|
}
|
||||||
|
|
||||||
|
// 再次根据Name查找
|
||||||
|
if oldGroup == nil && len(g.Name) > 0 {
|
||||||
|
oldGroup = oldConfig.FindRuleGroupWithName(g.Name)
|
||||||
|
}
|
||||||
|
|
||||||
if oldGroup == nil {
|
if oldGroup == nil {
|
||||||
// 新创建分组
|
// 新创建分组
|
||||||
groupId, err := models.SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, g)
|
groupId, err := models.SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, g)
|
||||||
@@ -535,35 +544,35 @@ func (this *HTTPFirewallPolicyService) ImportHTTPFirewallPolicy(ctx context.Cont
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroupIsOn(tx, oldGroup.Id, true)
|
|
||||||
|
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroup(tx, oldGroup.Id, g.IsOn, g.Name, g.Code, g.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroupSets(tx, oldGroup.Id, setsJSON)
|
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroupSets(tx, oldGroup.Id, setsJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 没有代号的直接创建
|
|
||||||
groupId, err := models.SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, g)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
oldConfig.Inbound.GroupRefs = append(oldConfig.Inbound.GroupRefs, &firewallconfigs.HTTPFirewallRuleGroupRef{
|
|
||||||
IsOn: true,
|
|
||||||
GroupId: groupId,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 出站分组
|
// 出站分组
|
||||||
if newConfig.Outbound != nil {
|
if newConfig.Outbound != nil {
|
||||||
for _, g := range newConfig.Outbound.Groups {
|
for _, g := range newConfig.Outbound.Groups {
|
||||||
|
var oldGroup *firewallconfigs.HTTPFirewallRuleGroup
|
||||||
|
|
||||||
|
// 使用代号查找
|
||||||
if len(g.Code) > 0 {
|
if len(g.Code) > 0 {
|
||||||
// 对于有代号的,覆盖或者添加
|
oldGroup = oldConfig.FindRuleGroupWithCode(g.Code)
|
||||||
oldGroup := oldConfig.FindRuleGroupWithCode(g.Code)
|
}
|
||||||
|
|
||||||
|
// 再次根据Name查找
|
||||||
|
if oldGroup == nil && len(g.Name) > 0 {
|
||||||
|
oldGroup = oldConfig.FindRuleGroupWithName(g.Name)
|
||||||
|
}
|
||||||
|
|
||||||
if oldGroup == nil {
|
if oldGroup == nil {
|
||||||
// 新创建分组
|
// 新创建分组
|
||||||
groupId, err := models.SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, g)
|
groupId, err := models.SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, g)
|
||||||
@@ -590,7 +599,7 @@ func (this *HTTPFirewallPolicyService) ImportHTTPFirewallPolicy(ctx context.Cont
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroupIsOn(tx, oldGroup.Id, true)
|
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroup(tx, oldGroup.Id, g.IsOn, g.Name, g.Code, g.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -599,17 +608,6 @@ func (this *HTTPFirewallPolicyService) ImportHTTPFirewallPolicy(ctx context.Cont
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 没有代号的直接创建
|
|
||||||
groupId, err := models.SharedHTTPFirewallRuleGroupDAO.CreateGroupFromConfig(tx, g)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
oldConfig.Outbound.GroupRefs = append(oldConfig.Outbound.GroupRefs, &firewallconfigs.HTTPFirewallRuleGroupRef{
|
|
||||||
IsOn: true,
|
|
||||||
GroupId: groupId,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func (this *HTTPFirewallRuleGroupService) UpdateHTTPFirewallRuleGroup(ctx contex
|
|||||||
|
|
||||||
tx := this.NullTx()
|
tx := this.NullTx()
|
||||||
|
|
||||||
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroup(tx, req.FirewallRuleGroupId, req.IsOn, req.Name, req.Description)
|
err = models.SharedHTTPFirewallRuleGroupDAO.UpdateGroup(tx, req.FirewallRuleGroupId, req.IsOn, req.Name, req.Code, req.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -543,5 +543,12 @@ func upgradeV0_3_7(db *dbs.DB) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WAF预置分组
|
||||||
|
_, err = db.Exec("UPDATE edgeHTTPFirewallRuleGroups SET isTemplate=1 WHERE LENGTH(code)>0")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user