166 lines
3.8 KiB
Go
166 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"f5"
|
|
"mt"
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
MT_SERVER_INFO = 0
|
|
MT_ALI_KEY = iota
|
|
MT_LAUNCH_WHITE_LIST
|
|
MT_LAUNCH_BLACK_LIST
|
|
MT_BLOCK_PROVINCE_CITY
|
|
MT_MAX
|
|
)
|
|
|
|
type MetaMgr struct {
|
|
f5.MetaMgr
|
|
|
|
accessKeyId string
|
|
accessSecret string
|
|
sdkInfoMutex sync.RWMutex
|
|
|
|
fixedBlockRegionHash map[string]int32
|
|
fixedBlockRegionHashMutex sync.RWMutex
|
|
}
|
|
|
|
func (this *MetaMgr) Init() *MetaMgr {
|
|
this.MetaMgr.Init()
|
|
configDir := "../config/"
|
|
if !f5.IsOnlineEnv() {
|
|
configDir = "/var/data/conf_test/analyseapi/"
|
|
}
|
|
metaClasses := &[]f5.MetaClass{
|
|
f5.MetaClass{
|
|
PrimKey: "InstanceId",
|
|
FileName: configDir + "analyseapi.cluster.json",
|
|
Idx: MT_SERVER_INFO,
|
|
RawMeta: (*mt.ServerInfoMetas)(nil),
|
|
WrapMeta: (*MtwServerInfo)(nil)},
|
|
f5.MetaClass{
|
|
FileName: configDir + "ali_key.json",
|
|
Idx: MT_ALI_KEY,
|
|
RawMeta: (*mt.AliKeyInfoMetas)(nil),
|
|
WrapMeta: (*MtwAliKeyConf)(nil)},
|
|
f5.MetaClass{
|
|
FileName: configDir + "launch_whitelist.json",
|
|
Idx: MT_LAUNCH_WHITE_LIST,
|
|
RawMeta: (*mt.LaunchWhiteListMetas)(nil),
|
|
WrapMeta: (*MtwLaunchWhiteList)(nil)},
|
|
f5.MetaClass{
|
|
FileName: configDir + "launch_blacklist.json",
|
|
Idx: MT_LAUNCH_BLACK_LIST,
|
|
RawMeta: (*mt.LaunchBlackListMetas)(nil),
|
|
WrapMeta: (*MtwLaunchBlackList)(nil)},
|
|
f5.MetaClass{
|
|
FileName: configDir + "block_province_city.json",
|
|
Idx: MT_BLOCK_PROVINCE_CITY,
|
|
RawMeta: (*mt.BlockProvinceCityMetas)(nil),
|
|
WrapMeta: (*MtwBlockProvinceCity)(nil)},
|
|
}
|
|
this.MetaMgr.RegisterMetaClasses(metaClasses)
|
|
this.Load()
|
|
this.secondInit()
|
|
return this
|
|
}
|
|
|
|
func (this *MetaMgr) UnInit() {
|
|
this.MetaMgr.UnInit()
|
|
}
|
|
|
|
func (this *MetaMgr) secondInit() {
|
|
this.sdkInfoMutex.Lock()
|
|
this.fixedBlockRegionHashMutex.Lock()
|
|
defer this.sdkInfoMutex.Unlock()
|
|
defer this.fixedBlockRegionHashMutex.Unlock()
|
|
|
|
this.accessKeyId = this.GetAliKey().GetAccessKeyid()
|
|
this.accessSecret = this.GetAliKey().GetAccessSecret()
|
|
|
|
this.fixedBlockRegionHash = make(map[string]int32)
|
|
|
|
for _, val := range this.GetFixedBlockRegion().GetList() {
|
|
this.fixedBlockRegionHash[val] = 1
|
|
}
|
|
}
|
|
|
|
func (this *MetaMgr) GetServer(instance_id int32) *MtwServerInfo {
|
|
v, ok := this.MetaMgr.GetMetaById(MT_SERVER_INFO, instance_id).(*MtwServerInfo)
|
|
if ok {
|
|
return v
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *MetaMgr) GetCurrServer() *MtwServerInfo {
|
|
return this.GetServer(int32(f5.App.GetInstanceId()))
|
|
}
|
|
|
|
func (this *MetaMgr) GetAliKey() *MtwAliKeyConf {
|
|
v, ok := this.MetaMgr.GetMetaById(MT_ALI_KEY, 1).(*MtwAliKeyConf)
|
|
if ok {
|
|
return v
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *MetaMgr) GetLaunchWhiteList() *MtwLaunchWhiteList {
|
|
v, ok := this.MetaMgr.GetMetaById(MT_LAUNCH_WHITE_LIST, 1).(*MtwLaunchWhiteList)
|
|
if ok {
|
|
return v
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *MetaMgr) GetLaunchBlackList() *MtwLaunchBlackList {
|
|
v, ok := this.MetaMgr.GetMetaById(MT_LAUNCH_BLACK_LIST, 1).(*MtwLaunchBlackList)
|
|
if ok {
|
|
return v
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *MetaMgr) GetFixedBlockRegion() *MtwBlockProvinceCity {
|
|
v, ok := this.MetaMgr.GetMetaById(MT_BLOCK_PROVINCE_CITY, 1).(*MtwBlockProvinceCity)
|
|
if ok {
|
|
return v
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this* MetaMgr) GetSdkInfo(accessKeyId* string, accessSecret* string) {
|
|
this.sdkInfoMutex.Lock()
|
|
defer this.sdkInfoMutex.Unlock()
|
|
*accessKeyId = "" + this.accessKeyId
|
|
*accessSecret = "" + this.accessSecret
|
|
}
|
|
|
|
func (this* MetaMgr) IsBlockZone(country string, province string, city string) bool {
|
|
this.fixedBlockRegionHashMutex.Lock()
|
|
defer this.fixedBlockRegionHashMutex.Unlock()
|
|
|
|
if country == "" || province == "" || city == "" {
|
|
return true
|
|
}
|
|
if country != "中国" {
|
|
return true
|
|
}
|
|
if _, ok := this.fixedBlockRegionHash[province]; ok {
|
|
return true
|
|
}
|
|
if _, ok := this.fixedBlockRegionHash[city]; ok {
|
|
return true
|
|
}
|
|
if _, ok := this.fixedBlockRegionHash[province + "/" + city]; ok {
|
|
return true
|
|
}
|
|
return false
|
|
}
|