支持更多的分组全局设置功能

This commit is contained in:
刘祥超
2021-10-07 16:47:21 +08:00
parent 7b1efe65d5
commit 5cf1f9bf33
6 changed files with 161 additions and 13 deletions

View File

@@ -96,9 +96,10 @@ func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (r
} }
// CreateFirewallPolicy 创建策略 // CreateFirewallPolicy 创建策略
func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64, serverId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) (int64, error) { func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64, serverGroupId int64, serverId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) (int64, error) {
op := NewHTTPFirewallPolicyOperator() op := NewHTTPFirewallPolicyOperator()
op.UserId = userId op.UserId = userId
op.GroupId = serverGroupId
op.ServerId = serverId op.ServerId = serverId
op.State = HTTPFirewallPolicyStateEnabled op.State = HTTPFirewallPolicyStateEnabled
op.IsOn = isOn op.IsOn = isOn
@@ -116,7 +117,7 @@ func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, userId int64
// CreateDefaultFirewallPolicy 创建默认的WAF策略 // CreateDefaultFirewallPolicy 创建默认的WAF策略
func (this *HTTPFirewallPolicyDAO) CreateDefaultFirewallPolicy(tx *dbs.Tx, name string) (int64, error) { func (this *HTTPFirewallPolicyDAO) CreateDefaultFirewallPolicy(tx *dbs.Tx, name string) (int64, error) {
policyId, err := this.CreateFirewallPolicy(tx, 0, 0, true, "\""+name+"\"WAF策略", "默认创建的WAF策略", nil, nil) policyId, err := this.CreateFirewallPolicy(tx, 0, 0, 0, true, "\""+name+"\"WAF策略", "默认创建的WAF策略", nil, nil)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@@ -7,6 +7,7 @@ type HTTPFirewallPolicy struct {
AdminId uint32 `field:"adminId"` // 管理员ID AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID UserId uint32 `field:"userId"` // 用户ID
ServerId uint32 `field:"serverId"` // 服务ID ServerId uint32 `field:"serverId"` // 服务ID
GroupId uint32 `field:"groupId"` // 服务分组ID
State uint8 `field:"state"` // 状态 State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间 CreatedAt uint64 `field:"createdAt"` // 创建时间
IsOn uint8 `field:"isOn"` // 是否启用 IsOn uint8 `field:"isOn"` // 是否启用
@@ -24,6 +25,7 @@ type HTTPFirewallPolicyOperator struct {
AdminId interface{} // 管理员ID AdminId interface{} // 管理员ID
UserId interface{} // 用户ID UserId interface{} // 用户ID
ServerId interface{} // 服务ID ServerId interface{} // 服务ID
GroupId interface{} // 服务分组ID
State interface{} // 状态 State interface{} // 状态
CreatedAt interface{} // 创建时间 CreatedAt interface{} // 创建时间
IsOn interface{} // 是否启用 IsOn interface{} // 是否启用

View File

@@ -966,6 +966,39 @@ func (this *HTTPWebDAO) FindWebServerId(tx *dbs.Tx, webId int64) (serverId int64
return this.FindWebServerId(tx, webId) return this.FindWebServerId(tx, webId)
} }
// FindWebServerGroupId 查找使用此Web的分组ID
func (this *HTTPWebDAO) FindWebServerGroupId(tx *dbs.Tx, webId int64) (groupId int64, err error) {
if webId <= 0 {
return 0, nil
}
groupId, err = SharedServerGroupDAO.FindEnabledGroupIdWithWebId(tx, webId)
if err != nil {
return
}
if groupId > 0 {
return
}
// web在Location中的情况
locationId, err := SharedHTTPLocationDAO.FindEnabledLocationIdWithWebId(tx, webId)
if err != nil {
return 0, err
}
if locationId == 0 {
return
}
webId, err = this.FindEnabledWebIdWithLocationId(tx, locationId)
if err != nil {
return
}
if webId <= 0 {
return
}
// 第二轮查找
return this.FindWebServerGroupId(tx, webId)
}
// CheckUserWeb 检查用户权限 // CheckUserWeb 检查用户权限
func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) error { func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) error {
serverId, err := this.FindWebServerId(tx, webId) serverId, err := this.FindWebServerId(tx, webId)
@@ -1015,12 +1048,23 @@ func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, e
// NotifyUpdate 通知更新 // NotifyUpdate 通知更新
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error { func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
// server
serverId, err := this.FindWebServerId(tx, webId) serverId, err := this.FindWebServerId(tx, webId)
if err != nil { if err != nil {
return err return err
} }
if serverId == 0 { if serverId > 0 {
return nil return SharedServerDAO.NotifyUpdate(tx, serverId)
} }
return SharedServerDAO.NotifyUpdate(tx, serverId)
// group
groupId, err := this.FindWebServerGroupId(tx, webId)
if err != nil {
return err
}
if groupId > 0 {
return SharedServerGroupDAO.NotifyUpdate(tx, groupId)
}
return nil
} }

View File

@@ -224,6 +224,48 @@ func (this *ServerGroupDAO) UpdateUDPReverseProxy(tx *dbs.Tx, groupId int64, con
return this.NotifyUpdate(tx, groupId) return this.NotifyUpdate(tx, groupId)
} }
// FindGroupWebId 查找分组WebId
func (this *ServerGroupDAO) FindGroupWebId(tx *dbs.Tx, groupId int64) (webId int64, err error) {
return this.Query(tx).
Pk(groupId).
Result("webId").
FindInt64Col(0)
}
// FindEnabledGroupIdWithWebId 根据WebId查找分组
func (this *ServerGroupDAO) FindEnabledGroupIdWithWebId(tx *dbs.Tx, webId int64) (int64, error) {
if webId <= 0 {
return 0, nil
}
return this.Query(tx).
State(ServerGroupStateEnabled).
ResultPk().
Attr("webId", webId).
FindInt64Col(0)
}
// InitGroupWeb 初始化Web配置
func (this *ServerGroupDAO) InitGroupWeb(tx *dbs.Tx, groupId int64) (int64, error) {
if groupId <= 0 {
return 0, errors.New("invalid groupId")
}
webId, err := SharedHTTPWebDAO.CreateWeb(tx, 0, 0, nil)
if err != nil {
return 0, err
}
err = this.Query(tx).
Pk(groupId).
Set("webId", webId).
UpdateQuickly()
if err != nil {
return 0, err
}
return webId, nil
}
// ComposeGroupConfig 组合配置 // ComposeGroupConfig 组合配置
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap maps.Map) (*serverconfigs.ServerGroupConfig, error) { func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap maps.Map) (*serverconfigs.ServerGroupConfig, error) {
if cacheMap == nil { if cacheMap == nil {
@@ -312,6 +354,17 @@ func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheM
} }
} }
// web
if group.WebId > 0 {
webConfig, err := SharedHTTPWebDAO.ComposeWebConfig(tx, int64(group.WebId), cacheMap)
if err != nil {
return nil, err
}
if webConfig != nil {
config.Web = webConfig
}
}
cacheMap[cacheKey] = config cacheMap[cacheKey] = config
return config, nil return config, nil
@@ -327,7 +380,6 @@ func (this *ServerGroupDAO) FindEnabledGroupIdWithReverseProxyId(tx *dbs.Tx, rev
FindInt64Col(0) FindInt64Col(0)
} }
// NotifyUpdate 通知更新 // NotifyUpdate 通知更新
func (this *ServerGroupDAO) NotifyUpdate(tx *dbs.Tx, groupId int64) error { func (this *ServerGroupDAO) NotifyUpdate(tx *dbs.Tx, groupId int64) error {
serverIds, err := SharedServerDAO.FindAllEnabledServerIdsWithGroupId(tx, groupId) serverIds, err := SharedServerDAO.FindAllEnabledServerIdsWithGroupId(tx, groupId)

View File

@@ -60,7 +60,7 @@ func (this *HTTPFirewallPolicyService) CreateHTTPFirewallPolicy(ctx context.Cont
tx := this.NullTx() tx := this.NullTx()
policyId, err := models.SharedHTTPFirewallPolicyDAO.CreateFirewallPolicy(tx, userId, req.ServerId, req.IsOn, req.Name, req.Description, nil, nil) policyId, err := models.SharedHTTPFirewallPolicyDAO.CreateFirewallPolicy(tx, userId, req.ServerGroupId, req.ServerId, req.IsOn, req.Name, req.Description, nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -137,7 +137,7 @@ func (this *HTTPFirewallPolicyService) CreateEmptyHTTPFirewallPolicy(ctx context
tx := this.NullTx() tx := this.NullTx()
policyId, err := models.SharedHTTPFirewallPolicyDAO.CreateFirewallPolicy(tx, userId, req.ServerId, req.IsOn, req.Name, req.Description, nil, nil) policyId, err := models.SharedHTTPFirewallPolicyDAO.CreateFirewallPolicy(tx, userId, req.ServerGroupId, req.ServerId, req.IsOn, req.Name, req.Description, nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -386,11 +386,7 @@ func (this *ServerGroupService) FindEnabledServerGroupConfigInfo(ctx context.Con
} }
if group == nil { if group == nil {
return &pb.FindEnabledServerGroupConfigInfoResponse{ return &pb.FindEnabledServerGroupConfigInfoResponse{}, nil
HasHTTPReverseProxy: false,
HasTCPReverseProxy: false,
HasUDPReverseProxy: false,
}, nil
} }
var result = &pb.FindEnabledServerGroupConfigInfoResponse{ var result = &pb.FindEnabledServerGroupConfigInfoResponse{
@@ -424,5 +420,58 @@ func (this *ServerGroupService) FindEnabledServerGroupConfigInfo(ctx context.Con
result.HasUDPReverseProxy = ref.IsPrior result.HasUDPReverseProxy = ref.IsPrior
} }
config, err := models.SharedServerGroupDAO.ComposeGroupConfig(tx, int64(group.Id), nil)
if err != nil {
return nil, err
}
if config != nil {
var webConfig = config.Web
if webConfig != nil {
result.HasRootConfig = webConfig != nil && webConfig.Root != nil && webConfig.Root.IsPrior
result.HasWAFConfig = webConfig != nil && webConfig.FirewallRef != nil && webConfig.FirewallRef.IsPrior
result.HasCacheConfig = webConfig != nil && webConfig.Cache != nil && webConfig.Cache.IsPrior
result.HasCharsetConfig = webConfig != nil && webConfig.Charset != nil && webConfig.Charset.IsPrior
result.HasAccessLogConfig = webConfig != nil && webConfig.AccessLogRef != nil && webConfig.AccessLogRef.IsPrior
result.HasStatConfig = webConfig != nil && webConfig.StatRef != nil && webConfig.StatRef.IsPrior
result.HasCompressionConfig = webConfig != nil && webConfig.Compression != nil && webConfig.Compression.IsPrior
result.HasWebsocketConfig = webConfig != nil && webConfig.WebsocketRef != nil && webConfig.WebsocketRef.IsPrior
result.HasRequestHeadersConfig = webConfig != nil && webConfig.RequestHeaderPolicyRef != nil && webConfig.RequestHeaderPolicyRef.IsPrior
result.HasResponseHeadersConfig = webConfig != nil && webConfig.ResponseHeaderPolicyRef != nil && webConfig.ResponseHeaderPolicyRef.IsPrior
result.HasWebPConfig = webConfig != nil && webConfig.WebP != nil && webConfig.WebP.IsPrior
result.HasRemoteAddrConfig = webConfig != nil && webConfig.RemoteAddr != nil && webConfig.RemoteAddr.IsPrior
}
}
return result, nil return result, nil
} }
// FindAndInitServerGroupWebConfig 初始化Web设置
func (this *ServerGroupService) FindAndInitServerGroupWebConfig(ctx context.Context, req *pb.FindAndInitServerGroupWebConfigRequest) (*pb.FindAndInitServerGroupWebConfigResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
webId, err := models.SharedServerGroupDAO.FindGroupWebId(tx, req.ServerGroupId)
if err != nil {
return nil, err
}
if webId == 0 {
webId, err = models.SharedServerGroupDAO.InitGroupWeb(tx, req.ServerGroupId)
if err != nil {
return nil, err
}
}
webConfig, err := models.SharedHTTPWebDAO.ComposeWebConfig(tx, webId, nil)
if err != nil {
return nil, err
}
webConfigJSON, err := json.Marshal(webConfig)
if err != nil {
return nil, err
}
return &pb.FindAndInitServerGroupWebConfigResponse{WebJSON: webConfigJSON}, nil
}