mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-05 01:11:54 +08:00
实现集群自定义页面
This commit is contained in:
@@ -996,7 +996,7 @@ func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, ca
|
|||||||
cluster, err := this.Query(tx).
|
cluster, err := this.Query(tx).
|
||||||
Pk(clusterId).
|
Pk(clusterId).
|
||||||
State(NodeClusterStateEnabled).
|
State(NodeClusterStateEnabled).
|
||||||
Result("id", "name", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "isOn", "ddosProtection", "clock", "globalServerConfig", "autoInstallNftables").
|
Result("id", "name", "timeZone", "nodeMaxThreads", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "uam", "httpPages", "isOn", "ddosProtection", "clock", "globalServerConfig", "autoInstallNftables").
|
||||||
Find()
|
Find()
|
||||||
if err != nil || cluster == nil {
|
if err != nil || cluster == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1095,7 +1095,7 @@ func (this *NodeClusterDAO) UpdateClusterUAMPolicy(tx *dbs.Tx, clusterId int64,
|
|||||||
return this.NotifyUAMUpdate(tx, clusterId)
|
return this.NotifyUAMUpdate(tx, clusterId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindClusterUAMPolicy 查询设置
|
// FindClusterUAMPolicy 查询UAM设置
|
||||||
func (this *NodeClusterDAO) FindClusterUAMPolicy(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*nodeconfigs.UAMPolicy, error) {
|
func (this *NodeClusterDAO) FindClusterUAMPolicy(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*nodeconfigs.UAMPolicy, error) {
|
||||||
var cacheKey = this.Table + ":FindClusterUAMPolicy:" + types.String(clusterId)
|
var cacheKey = this.Table + ":FindClusterUAMPolicy:" + types.String(clusterId)
|
||||||
if cacheMap != nil {
|
if cacheMap != nil {
|
||||||
@@ -1125,6 +1125,87 @@ func (this *NodeClusterDAO) FindClusterUAMPolicy(tx *dbs.Tx, clusterId int64, ca
|
|||||||
return policy, nil
|
return policy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateClusterHTTPPagesPolicy 修改自定义页面设置
|
||||||
|
func (this *NodeClusterDAO) UpdateClusterHTTPPagesPolicy(tx *dbs.Tx, clusterId int64, httpPagesPolicy *nodeconfigs.HTTPPagesPolicy) error {
|
||||||
|
if httpPagesPolicy == nil {
|
||||||
|
err := this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Set("httpPages", dbs.SQL("null")).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyHTTPPagesPolicyUpdate(tx, clusterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除不需要保存的内容
|
||||||
|
var newPages = []*serverconfigs.HTTPPageConfig{}
|
||||||
|
for _, page := range httpPagesPolicy.Pages {
|
||||||
|
newPages = append(newPages, &serverconfigs.HTTPPageConfig{Id: page.Id})
|
||||||
|
}
|
||||||
|
httpPagesPolicy.Pages = newPages
|
||||||
|
|
||||||
|
httpPagesPolicyJSON, err := json.Marshal(httpPagesPolicy)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Set("httpPages", httpPagesPolicyJSON).
|
||||||
|
UpdateQuickly()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.NotifyHTTPPagesPolicyUpdate(tx, clusterId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindClusterHTTPPagesPolicy 查询自定义页面设置
|
||||||
|
func (this *NodeClusterDAO) FindClusterHTTPPagesPolicy(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*nodeconfigs.HTTPPagesPolicy, error) {
|
||||||
|
var cacheKey = this.Table + ":FindClusterHTTPPagesPolicy:" + types.String(clusterId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap.Get(cacheKey)
|
||||||
|
if ok {
|
||||||
|
return cache.(*nodeconfigs.HTTPPagesPolicy), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pagesJSON, err := this.Query(tx).
|
||||||
|
Pk(clusterId).
|
||||||
|
Result("httpPages").
|
||||||
|
FindJSONCol()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsNull(pagesJSON) {
|
||||||
|
return nodeconfigs.NewHTTPPagesPolicy(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var policy = nodeconfigs.NewHTTPPagesPolicy()
|
||||||
|
err = json.Unmarshal(pagesJSON, policy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取Page信息
|
||||||
|
var newPages = []*serverconfigs.HTTPPageConfig{}
|
||||||
|
for _, page := range policy.Pages {
|
||||||
|
pageConfig, err := SharedHTTPPageDAO.ComposePageConfig(tx, page.Id, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if pageConfig == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newPages = append(newPages, pageConfig)
|
||||||
|
}
|
||||||
|
policy.Pages = newPages
|
||||||
|
|
||||||
|
return policy, nil
|
||||||
|
}
|
||||||
|
|
||||||
// FindClusterDDoSProtection 获取集群的DDoS设置
|
// FindClusterDDoSProtection 获取集群的DDoS设置
|
||||||
func (this *NodeClusterDAO) FindClusterDDoSProtection(tx *dbs.Tx, clusterId int64) (*ddosconfigs.ProtectionConfig, error) {
|
func (this *NodeClusterDAO) FindClusterDDoSProtection(tx *dbs.Tx, clusterId int64) (*ddosconfigs.ProtectionConfig, error) {
|
||||||
one, err := this.Query(tx).
|
one, err := this.Query(tx).
|
||||||
@@ -1217,6 +1298,11 @@ func (this *NodeClusterDAO) NotifyUAMUpdate(tx *dbs.Tx, clusterId int64) error {
|
|||||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeUAMPolicyChanged)
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeUAMPolicyChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotifyHTTPPagesPolicyUpdate 通知HTTP Pages更新
|
||||||
|
func (this *NodeClusterDAO) NotifyHTTPPagesPolicyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||||
|
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, 0, NodeTaskTypeHTTPPagesPolicyChanged)
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyDNSUpdate 通知DNS更新
|
// NotifyDNSUpdate 通知DNS更新
|
||||||
// TODO 更新新的DNS解析记录的同时,需要删除老的DNS解析记录
|
// TODO 更新新的DNS解析记录的同时,需要删除老的DNS解析记录
|
||||||
func (this *NodeClusterDAO) NotifyDNSUpdate(tx *dbs.Tx, clusterId int64) error {
|
func (this *NodeClusterDAO) NotifyDNSUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ type NodeCluster struct {
|
|||||||
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
|
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
|
||||||
AutoInstallNftables bool `field:"autoInstallNftables"` // 自动安装nftables
|
AutoInstallNftables bool `field:"autoInstallNftables"` // 自动安装nftables
|
||||||
IsAD bool `field:"isAD"` // 是否为高防集群
|
IsAD bool `field:"isAD"` // 是否为高防集群
|
||||||
|
HttpPages dbs.JSON `field:"httpPages"` // 自定义页面设置
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeClusterOperator struct {
|
type NodeClusterOperator struct {
|
||||||
@@ -81,6 +82,7 @@ type NodeClusterOperator struct {
|
|||||||
AutoRemoteStart any // 自动远程启动
|
AutoRemoteStart any // 自动远程启动
|
||||||
AutoInstallNftables any // 自动安装nftables
|
AutoInstallNftables any // 自动安装nftables
|
||||||
IsAD any // 是否为高防集群
|
IsAD any // 是否为高防集群
|
||||||
|
HttpPages any // 自定义页面设置
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeClusterOperator() *NodeClusterOperator {
|
func NewNodeClusterOperator() *NodeClusterOperator {
|
||||||
|
|||||||
@@ -1085,6 +1085,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, dataMap *shared
|
|||||||
var clusterIndex = 0
|
var clusterIndex = 0
|
||||||
config.WebPImagePolicies = map[int64]*nodeconfigs.WebPImagePolicy{}
|
config.WebPImagePolicies = map[int64]*nodeconfigs.WebPImagePolicy{}
|
||||||
config.UAMPolicies = map[int64]*nodeconfigs.UAMPolicy{}
|
config.UAMPolicies = map[int64]*nodeconfigs.UAMPolicy{}
|
||||||
|
config.HTTPPagesPolicies = map[int64]*nodeconfigs.HTTPPagesPolicy{}
|
||||||
var allowIPMaps = map[string]bool{}
|
var allowIPMaps = map[string]bool{}
|
||||||
for _, clusterId := range clusterIds {
|
for _, clusterId := range clusterIds {
|
||||||
nodeCluster, err := SharedNodeClusterDAO.FindClusterBasicInfo(tx, clusterId, cacheMap)
|
nodeCluster, err := SharedNodeClusterDAO.FindClusterBasicInfo(tx, clusterId, cacheMap)
|
||||||
@@ -1178,6 +1179,31 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, dataMap *shared
|
|||||||
config.UAMPolicies[clusterId] = uamPolicy
|
config.UAMPolicies[clusterId] = uamPolicy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTP Pages
|
||||||
|
if IsNotNull(nodeCluster.HttpPages) {
|
||||||
|
var httpPagesPolicy = nodeconfigs.NewHTTPPagesPolicy()
|
||||||
|
err = json.Unmarshal(nodeCluster.HttpPages, httpPagesPolicy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if httpPagesPolicy.IsOn {
|
||||||
|
var newPages = []*serverconfigs.HTTPPageConfig{}
|
||||||
|
for _, page := range httpPagesPolicy.Pages {
|
||||||
|
pageConfig, err := SharedHTTPPageDAO.ComposePageConfig(tx, page.Id, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if pageConfig != nil && pageConfig.IsOn {
|
||||||
|
newPages = append(newPages, pageConfig)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
httpPagesPolicy.Pages = newPages
|
||||||
|
if len(newPages) > 0 {
|
||||||
|
config.HTTPPagesPolicies[clusterId] = httpPagesPolicy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 自动安装nftables
|
// 自动安装nftables
|
||||||
if clusterIndex == 0 {
|
if clusterIndex == 0 {
|
||||||
config.AutoInstallNftables = nodeCluster.AutoInstallNftables
|
config.AutoInstallNftables = nodeCluster.AutoInstallNftables
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ const (
|
|||||||
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged" // 节点级别变化
|
NodeTaskTypeNodeLevelChanged NodeTaskType = "nodeLevelChanged" // 节点级别变化
|
||||||
NodeTaskTypeUserServersStateChanged NodeTaskType = "userServersStateChanged" // 用户服务状态变化
|
NodeTaskTypeUserServersStateChanged NodeTaskType = "userServersStateChanged" // 用户服务状态变化
|
||||||
NodeTaskTypeUAMPolicyChanged NodeTaskType = "uamPolicyChanged" // UAM策略变化
|
NodeTaskTypeUAMPolicyChanged NodeTaskType = "uamPolicyChanged" // UAM策略变化
|
||||||
|
NodeTaskTypeHTTPPagesPolicyChanged NodeTaskType = "httpPagesPolicyChanged" // 自定义页面变化
|
||||||
NodeTaskTypeUpdatingServers NodeTaskType = "updatingServers" // 更新一组服务
|
NodeTaskTypeUpdatingServers NodeTaskType = "updatingServers" // 更新一组服务
|
||||||
|
|
||||||
// NS相关
|
// NS相关
|
||||||
|
|||||||
@@ -2112,6 +2112,8 @@ func (this *NodeService) FindEnabledNodeConfigInfo(ctx context.Context, req *pb.
|
|||||||
// schedule
|
// schedule
|
||||||
result.HasScheduleSettings = node.HasScheduleSettings()
|
result.HasScheduleSettings = node.HasScheduleSettings()
|
||||||
|
|
||||||
|
// pages
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2291,3 +2293,39 @@ func (this *NodeService) FindNodeUAMPolicies(ctx context.Context, req *pb.FindNo
|
|||||||
UamPolicies: pbPolicies,
|
UamPolicies: pbPolicies,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindNodeHTTPPagesPolicies 查找节点的自定义页面策略
|
||||||
|
func (this *NodeService) FindNodeHTTPPagesPolicies(ctx context.Context, req *pb.FindNodeHTTPPagesPoliciesRequest) (*pb.FindNodeHTTPPagesPoliciesResponse, error) {
|
||||||
|
nodeId, err := this.ValidateNode(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var tx = this.NullTx()
|
||||||
|
clusterIds, err := models.SharedNodeDAO.FindEnabledAndOnNodeClusterIds(tx, nodeId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var pbPolicies = []*pb.FindNodeHTTPPagesPoliciesResponse_HTTPPagesPolicy{}
|
||||||
|
for _, clusterId := range clusterIds {
|
||||||
|
policy, err := models.SharedNodeClusterDAO.FindClusterHTTPPagesPolicy(tx, clusterId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if policy == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
policyJSON, err := json.Marshal(policy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pbPolicies = append(pbPolicies, &pb.FindNodeHTTPPagesPoliciesResponse_HTTPPagesPolicy{
|
||||||
|
NodeClusterId: clusterId,
|
||||||
|
HttpPagesPolicyJSON: policyJSON,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return &pb.FindNodeHTTPPagesPoliciesResponse{
|
||||||
|
HttpPagesPolicies: pbPolicies,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1157,6 +1157,16 @@ func (this *NodeClusterService) FindEnabledNodeClusterConfigInfo(ctx context.Con
|
|||||||
// ddos
|
// ddos
|
||||||
result.HasDDoSProtection = cluster.HasDDoSProtection()
|
result.HasDDoSProtection = cluster.HasDDoSProtection()
|
||||||
|
|
||||||
|
// HTTP Pages
|
||||||
|
if models.IsNotNull(cluster.HttpPages) {
|
||||||
|
var pagesPolicy = nodeconfigs.NewHTTPPagesPolicy()
|
||||||
|
err = json.Unmarshal(cluster.HttpPages, pagesPolicy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result.HasHTTPPagesPolicy = pagesPolicy.IsOn && len(pagesPolicy.Pages) > 0
|
||||||
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1367,3 +1377,51 @@ func (this *NodeClusterService) UpdateNodeClusterGlobalServerConfig(ctx context.
|
|||||||
|
|
||||||
return this.Success()
|
return this.Success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindNodeClusterHTTPPagesPolicy 获取集群的自定义页面设置
|
||||||
|
func (this *NodeClusterService) FindNodeClusterHTTPPagesPolicy(ctx context.Context, req *pb.FindNodeClusterHTTPPagesPolicyRequest) (*pb.FindNodeClusterHTTPPagesPolicyResponse, error) {
|
||||||
|
_, err := this.ValidateAdmin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var tx = this.NullTx()
|
||||||
|
httpPagesPolicy, err := models.SharedNodeClusterDAO.FindClusterHTTPPagesPolicy(tx, req.NodeClusterId, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
httpPagesPolicyJSON, err := json.Marshal(httpPagesPolicy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &pb.FindNodeClusterHTTPPagesPolicyResponse{
|
||||||
|
HttpPagesPolicyJSON: httpPagesPolicyJSON,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateNodeClusterHTTPPagesPolicy 修改集群的自定义页面设置
|
||||||
|
func (this *NodeClusterService) UpdateNodeClusterHTTPPagesPolicy(ctx context.Context, req *pb.UpdateNodeClusterHTTPPagesPolicyRequest) (*pb.RPCSuccess, error) {
|
||||||
|
_, err := this.ValidateAdmin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var tx = this.NullTx()
|
||||||
|
var policy = nodeconfigs.NewHTTPPagesPolicy()
|
||||||
|
err = json.Unmarshal(req.HttpPagesPolicyJSON, policy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = policy.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("validate policy failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = models.SharedNodeClusterDAO.UpdateClusterHTTPPagesPolicy(tx, req.NodeClusterId, policy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Success()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user