143 lines
3.1 KiB
Go
143 lines
3.1 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build plus && script
|
|
|
|
package nodes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
iplib "github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
|
"github.com/TeaOSLab/EdgeNode/internal/events"
|
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
|
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/lists"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
if !teaconst.IsMain {
|
|
return
|
|
}
|
|
|
|
events.On(events.EventLoaded, func() {
|
|
var plusTicker = time.NewTicker(1 * time.Hour)
|
|
if Tea.IsTesting() {
|
|
plusTicker = time.NewTicker(1 * time.Minute)
|
|
}
|
|
goman.New(func() {
|
|
for range plusTicker.C {
|
|
_ = nodeInstance.notifyPlusChange()
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
var lastEdition string
|
|
|
|
func (this *Node) reloadCommonScripts() error {
|
|
// 下载配置
|
|
rpcClient, err := rpc.SharedRPC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
configsResp, err := rpcClient.ScriptRPC.ComposeScriptConfigs(rpcClient.Context(), &pb.ComposeScriptConfigsRequest{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(configsResp.ScriptConfigsJSON) == 0 {
|
|
sharedNodeConfig.CommonScripts = nil
|
|
} else {
|
|
var configs = []*serverconfigs.CommonScript{}
|
|
err = json.Unmarshal(configsResp.ScriptConfigsJSON, &configs)
|
|
if err != nil {
|
|
return fmt.Errorf("decode script configs failed: %w", err)
|
|
}
|
|
sharedNodeConfig.CommonScripts = configs
|
|
}
|
|
|
|
// 通知更新
|
|
select {
|
|
case commonScriptsChangesChan <- true:
|
|
default:
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *Node) reloadIPLibrary() {
|
|
if sharedNodeConfig.Edition == lastEdition {
|
|
return
|
|
}
|
|
|
|
go func() {
|
|
var err error
|
|
lastEdition = sharedNodeConfig.Edition
|
|
if len(lastEdition) > 0 && (lists.ContainsString([]string{"pro", "ent", "max", "ultra"}, lastEdition)) {
|
|
err = iplib.InitPlus()
|
|
} else {
|
|
err = iplib.InitDefault()
|
|
}
|
|
if err != nil {
|
|
remotelogs.Error("IP_LIBRARY", "load ip library failed: "+err.Error())
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (this *Node) notifyPlusChange() error {
|
|
rpcClient, err := rpc.SharedRPC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := rpcClient.AuthorityKeyRPC.CheckAuthority(rpcClient.Context(), &pb.CheckAuthorityRequest{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var isChanged = resp.Edition != sharedNodeConfig.Edition
|
|
if resp.IsPlus {
|
|
sharedNodeConfig.Edition = resp.Edition
|
|
} else {
|
|
sharedNodeConfig.Edition = ""
|
|
}
|
|
|
|
if isChanged {
|
|
this.reloadIPLibrary()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (this *Node) execTOAChangedTask() error {
|
|
if sharedTOAManager == nil {
|
|
return nil
|
|
}
|
|
|
|
rpcClient, err := rpc.SharedRPC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := rpcClient.NodeRPC.FindNodeTOAConfig(rpcClient.Context(), &pb.FindNodeTOAConfigRequest{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(resp.ToaJSON) == 0 {
|
|
return sharedTOAManager.Apply(&nodeconfigs.TOAConfig{IsOn: false})
|
|
}
|
|
|
|
var config = nodeconfigs.NewTOAConfig()
|
|
err = json.Unmarshal(resp.ToaJSON, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return sharedTOAManager.Apply(config)
|
|
}
|