178 lines
4.1 KiB
Go
178 lines
4.1 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build plus
|
|
|
|
package tasks
|
|
|
|
import (
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
|
|
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/events"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/goman"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/plus"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/setup"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
plusutils "github.com/TeaOSLab/EdgePlus/pkg/utils"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/logs"
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
events.On(events.EventStart, func() {
|
|
var task = NewAuthorityTask()
|
|
goman.New(func() {
|
|
task.Start()
|
|
})
|
|
})
|
|
}
|
|
|
|
var authorityTaskNotifyChange = make(chan bool, 1)
|
|
|
|
func NotifyAuthorityTask() {
|
|
select {
|
|
case authorityTaskNotifyChange <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
type AuthorityTask struct {
|
|
oldIsPlus bool
|
|
}
|
|
|
|
func NewAuthorityTask() *AuthorityTask {
|
|
return &AuthorityTask{}
|
|
}
|
|
|
|
func (this *AuthorityTask) Start() {
|
|
// 从缓存中读取
|
|
var config = configs.ReadPlusConfig()
|
|
if config != nil {
|
|
teaconst.IsPlus = config.IsPlus
|
|
this.notifyChange()
|
|
plus.UpdateComponents(config.Edition, config.Components)
|
|
}
|
|
|
|
// 开始计时器
|
|
var ticker = time.NewTicker(10 * time.Minute)
|
|
if Tea.IsTesting() {
|
|
// 快速测试
|
|
ticker = time.NewTicker(1 * time.Minute)
|
|
}
|
|
|
|
// 初始化的时候先获取一次
|
|
var timeout = time.NewTimer(3 * time.Second)
|
|
<-timeout.C
|
|
err := this.Loop()
|
|
if err != nil {
|
|
logs.Println("[TASK][AuthorityTask]" + err.Error())
|
|
}
|
|
|
|
// 定时获取
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
case <-authorityTaskNotifyChange:
|
|
}
|
|
err := this.Loop()
|
|
if err != nil {
|
|
logs.Println("[TASK][AuthorityTask]" + err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *AuthorityTask) Loop() error {
|
|
// 如果还没有安装直接返回
|
|
if !setup.IsConfigured() {
|
|
return nil
|
|
}
|
|
|
|
rpcClient, err := rpc.SharedRPC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := rpcClient.AuthorityKeyRPC().ReadAuthorityKey(rpcClient.Context(0), &pb.ReadAuthorityKeyRequest{})
|
|
if err != nil {
|
|
teaconst.IsPlus = false
|
|
|
|
if !rpc.IsConnError(err) {
|
|
this.notifyChange()
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
defer this.notifyChange()
|
|
|
|
if resp.AuthorityKey == nil {
|
|
teaconst.IsPlus = false
|
|
return nil
|
|
}
|
|
|
|
plus.ErrString = ""
|
|
var plusEdition = ""
|
|
if resp.AuthorityKey != nil && len(resp.AuthorityKey.Value) > 0 && resp.AuthorityKey.DayTo >= timeutil.Format("Y-m-d") {
|
|
nativeKey, err := plusutils.DecodeKey([]byte(resp.AuthorityKey.Value))
|
|
if err != nil {
|
|
teaconst.IsPlus = false
|
|
return nil
|
|
}
|
|
var isOk = true
|
|
|
|
if len(nativeKey.RequestCode) > 0 {
|
|
isOk, _ = plusutils.ValidateRequestCode(nativeKey.RequestCode)
|
|
} else if len(nativeKey.MacAddresses) > 0 {
|
|
var macAddresses = resp.AuthorityKey.MacAddresses
|
|
for _, addr := range macAddresses {
|
|
if !plus.ValidateMac(addr) {
|
|
isOk = false
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if isOk {
|
|
plusEdition = resp.AuthorityKey.Edition
|
|
teaconst.IsPlus = true
|
|
plus.UpdateComponents(resp.AuthorityKey.Edition, resp.AuthorityKey.Components)
|
|
} else {
|
|
plus.ErrString = "当前管理平台所在服务器环境与商业版认证分发时发生了变化,因此无法使用商业版功能。如果你正在迁移管理平台,请联系开发者重新申请新的注册码。"
|
|
teaconst.IsPlus = false
|
|
}
|
|
} else {
|
|
teaconst.IsPlus = false
|
|
}
|
|
|
|
_ = configs.WritePlusConfig(&configs.PlusConfig{
|
|
Edition: plusEdition,
|
|
IsPlus: teaconst.IsPlus,
|
|
Components: resp.AuthorityKey.Components,
|
|
DayTo: resp.AuthorityKey.DayTo,
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (this *AuthorityTask) notifyChange() {
|
|
if this.oldIsPlus == teaconst.IsPlus {
|
|
return
|
|
}
|
|
this.oldIsPlus = teaconst.IsPlus
|
|
|
|
go func() {
|
|
time.Sleep(1 * time.Second) // 等待默认的加载程序完成
|
|
|
|
var err error
|
|
if teaconst.IsPlus {
|
|
err = iplibrary.InitPlus()
|
|
} else {
|
|
err = iplibrary.InitDefault()
|
|
}
|
|
if err != nil {
|
|
logs.Println("[NODE]initialize ip library failed: " + err.Error())
|
|
}
|
|
}()
|
|
}
|