2020-09-26 08:07:24 +08:00
|
|
|
package nodeconfigs
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2020-10-10 19:22:22 +08:00
|
|
|
"fmt"
|
2020-09-26 08:07:24 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
2020-10-08 15:06:56 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
2020-09-26 08:07:24 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
|
|
|
"github.com/iwind/TeaGo/Tea"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var sharedNodeConfig *NodeConfig = nil
|
|
|
|
|
|
|
|
|
|
type NodeConfig struct {
|
2020-10-09 11:06:47 +08:00
|
|
|
Id int64 `yaml:"id" json:"id"`
|
|
|
|
|
NodeId string `yaml:"nodeId" json:"nodeId"`
|
2020-09-26 08:07:24 +08:00
|
|
|
IsOn bool `yaml:"isOn" json:"isOn"`
|
|
|
|
|
Servers []*serverconfigs.ServerConfig `yaml:"servers" json:"servers"`
|
|
|
|
|
Version int64 `yaml:"version" json:"version"`
|
|
|
|
|
Name string `yaml:"name" json:"name"`
|
2020-10-10 12:31:40 +08:00
|
|
|
MaxCPU int32 `yaml:"maxCPU" json:"maxCPU"`
|
2020-09-26 08:07:24 +08:00
|
|
|
|
|
|
|
|
// 全局配置
|
|
|
|
|
GlobalConfig *serverconfigs.GlobalConfig `yaml:"globalConfig" json:"globalConfig"` // 全局配置
|
2020-10-05 16:54:21 +08:00
|
|
|
|
2020-12-02 14:26:17 +08:00
|
|
|
// TOA配置
|
|
|
|
|
TOA *TOAConfig `yaml:"toa" json:"toa"`
|
|
|
|
|
|
2020-10-10 19:22:22 +08:00
|
|
|
paddedId string
|
2020-10-08 15:06:56 +08:00
|
|
|
cachePolicies []*serverconfigs.HTTPCachePolicy
|
|
|
|
|
firewallPolicies []*firewallconfigs.HTTPFirewallPolicy
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取得当前节点配置单例
|
|
|
|
|
func SharedNodeConfig() (*NodeConfig, error) {
|
|
|
|
|
shared.Locker.Lock()
|
|
|
|
|
defer shared.Locker.Unlock()
|
|
|
|
|
|
|
|
|
|
if sharedNodeConfig != nil {
|
|
|
|
|
return sharedNodeConfig, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(Tea.ConfigFile("node.json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &NodeConfig{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config := &NodeConfig{}
|
|
|
|
|
err = json.Unmarshal(data, &config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &NodeConfig{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sharedNodeConfig = config
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重置节点配置
|
|
|
|
|
func ResetNodeConfig(nodeConfig *NodeConfig) {
|
|
|
|
|
shared.Locker.Lock()
|
|
|
|
|
sharedNodeConfig = nodeConfig
|
|
|
|
|
shared.Locker.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 16:54:21 +08:00
|
|
|
// 初始化
|
|
|
|
|
func (this *NodeConfig) Init() error {
|
2020-10-10 19:22:22 +08:00
|
|
|
this.paddedId = fmt.Sprintf("%08d", this.Id)
|
|
|
|
|
|
2020-10-05 16:54:21 +08:00
|
|
|
// servers
|
|
|
|
|
for _, server := range this.Servers {
|
|
|
|
|
err := server.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// global config
|
|
|
|
|
if this.GlobalConfig != nil {
|
|
|
|
|
err := this.GlobalConfig.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cache policies
|
|
|
|
|
this.cachePolicies = []*serverconfigs.HTTPCachePolicy{}
|
|
|
|
|
for _, server := range this.Servers {
|
|
|
|
|
if server.Web != nil {
|
2020-10-08 15:06:56 +08:00
|
|
|
this.lookupWeb(server.Web)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// firewall policies
|
|
|
|
|
this.firewallPolicies = []*firewallconfigs.HTTPFirewallPolicy{}
|
|
|
|
|
for _, server := range this.Servers {
|
|
|
|
|
if server.Web != nil {
|
|
|
|
|
this.lookupWeb(server.Web)
|
2020-10-05 16:54:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 14:26:17 +08:00
|
|
|
// TOA
|
|
|
|
|
if this.TOA != nil {
|
|
|
|
|
err := this.TOA.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 16:54:21 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 08:07:24 +08:00
|
|
|
// 根据网络地址和协议分组
|
|
|
|
|
func (this *NodeConfig) AvailableGroups() []*serverconfigs.ServerGroup {
|
|
|
|
|
groupMapping := map[string]*serverconfigs.ServerGroup{} // protocol://addr => Server Group
|
|
|
|
|
for _, server := range this.Servers {
|
|
|
|
|
if !server.IsOn {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, addr := range server.FullAddresses() {
|
|
|
|
|
group, ok := groupMapping[addr]
|
|
|
|
|
if ok {
|
|
|
|
|
group.Add(server)
|
|
|
|
|
} else {
|
|
|
|
|
group = serverconfigs.NewServerGroup(addr)
|
|
|
|
|
group.Add(server)
|
|
|
|
|
}
|
|
|
|
|
groupMapping[addr] = group
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result := []*serverconfigs.ServerGroup{}
|
|
|
|
|
for _, group := range groupMapping {
|
|
|
|
|
result = append(result, group)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 16:54:21 +08:00
|
|
|
// 获取使用的所有的缓存策略
|
|
|
|
|
func (this *NodeConfig) AllCachePolicies() []*serverconfigs.HTTPCachePolicy {
|
|
|
|
|
return this.cachePolicies
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
2020-10-08 15:06:56 +08:00
|
|
|
// 获取使用的所有的WAF策略
|
|
|
|
|
func (this *NodeConfig) AllHTTPFirewallPolicies() []*firewallconfigs.HTTPFirewallPolicy {
|
|
|
|
|
return this.firewallPolicies
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 08:07:24 +08:00
|
|
|
// 写入到文件
|
|
|
|
|
func (this *NodeConfig) Save() error {
|
|
|
|
|
shared.Locker.Lock()
|
|
|
|
|
defer shared.Locker.Unlock()
|
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(this)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ioutil.WriteFile(Tea.ConfigFile("node.json"), data, 0777)
|
|
|
|
|
}
|
2020-10-05 16:54:21 +08:00
|
|
|
|
2020-10-10 19:22:22 +08:00
|
|
|
// 获取填充后的ID
|
|
|
|
|
func (this *NodeConfig) PaddedId() string {
|
|
|
|
|
return this.paddedId
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 15:06:56 +08:00
|
|
|
// 查找Web中的缓存策略、防火墙策略等
|
|
|
|
|
func (this *NodeConfig) lookupWeb(web *serverconfigs.HTTPWebConfig) {
|
2020-10-05 16:54:21 +08:00
|
|
|
if web == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-10-08 15:06:56 +08:00
|
|
|
|
|
|
|
|
// cache
|
2020-10-05 16:54:21 +08:00
|
|
|
if web.Cache != nil && len(web.Cache.CacheRefs) > 0 {
|
|
|
|
|
for _, cacheRef := range web.Cache.CacheRefs {
|
|
|
|
|
if cacheRef.CachePolicy != nil && !this.hasCachePolicy(cacheRef.CachePolicyId) {
|
|
|
|
|
this.cachePolicies = append(this.cachePolicies, cacheRef.CachePolicy)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 15:06:56 +08:00
|
|
|
// firewall
|
|
|
|
|
if web.FirewallPolicy != nil && !this.hasHTTPFirewallPolicy(web.FirewallPolicy.Id) {
|
|
|
|
|
this.firewallPolicies = append(this.firewallPolicies, web.FirewallPolicy)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 16:54:21 +08:00
|
|
|
for _, location := range web.Locations {
|
2020-10-08 15:06:56 +08:00
|
|
|
this.lookupWeb(location.Web)
|
2020-10-05 16:54:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查缓存策略是否已收集
|
|
|
|
|
func (this *NodeConfig) hasCachePolicy(cachePolicyId int64) bool {
|
|
|
|
|
for _, cachePolicy := range this.cachePolicies {
|
|
|
|
|
if cachePolicy.Id == cachePolicyId {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-10-08 15:06:56 +08:00
|
|
|
|
|
|
|
|
// 检查防火墙策略是否已收集
|
|
|
|
|
func (this *NodeConfig) hasHTTPFirewallPolicy(firewallPolicyId int64) bool {
|
|
|
|
|
for _, p := range this.firewallPolicies {
|
|
|
|
|
if p.Id == firewallPolicyId {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|