mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	API RPC配置增加disableUpdate,可以停用自动更新API节点
This commit is contained in:
		@@ -10,6 +10,7 @@ import (
 | 
				
			|||||||
type APIConfig struct {
 | 
					type APIConfig struct {
 | 
				
			||||||
	RPC struct {
 | 
						RPC struct {
 | 
				
			||||||
		Endpoints     []string `yaml:"endpoints"`
 | 
							Endpoints     []string `yaml:"endpoints"`
 | 
				
			||||||
 | 
							DisableUpdate bool     `yaml:"disableUpdate"`
 | 
				
			||||||
	} `yaml:"rpc"`
 | 
						} `yaml:"rpc"`
 | 
				
			||||||
	NodeId string `yaml:"nodeId"`
 | 
						NodeId string `yaml:"nodeId"`
 | 
				
			||||||
	Secret string `yaml:"secret"`
 | 
						Secret string `yaml:"secret"`
 | 
				
			||||||
@@ -30,7 +31,7 @@ func LoadAPIConfig() (*APIConfig, error) {
 | 
				
			|||||||
	return config, nil
 | 
						return config, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 保存到文件
 | 
					// WriteFile 保存到文件
 | 
				
			||||||
func (this *APIConfig) WriteFile(path string) error {
 | 
					func (this *APIConfig) WriteFile(path string) error {
 | 
				
			||||||
	data, err := yaml.Marshal(this)
 | 
						data, err := yaml.Marshal(this)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,15 @@
 | 
				
			|||||||
package configs
 | 
					package configs_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "testing"
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/TeaOSLab/EdgeNode/internal/configs"
 | 
				
			||||||
 | 
						_ "github.com/iwind/TeaGo/bootstrap"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestLoadAPIConfig(t *testing.T) {
 | 
					func TestLoadAPIConfig(t *testing.T) {
 | 
				
			||||||
	config, err := LoadAPIConfig()
 | 
						config, err := configs.LoadAPIConfig()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	t.Log(config)
 | 
						t.Logf("%+v", config)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,9 +1,10 @@
 | 
				
			|||||||
package configs
 | 
					package configs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 集群配置
 | 
					// ClusterConfig 集群配置
 | 
				
			||||||
type ClusterConfig struct {
 | 
					type ClusterConfig struct {
 | 
				
			||||||
	RPC struct {
 | 
						RPC struct {
 | 
				
			||||||
		Endpoints     []string `yaml:"endpoints"`
 | 
							Endpoints     []string `yaml:"endpoints"`
 | 
				
			||||||
 | 
							DisableUpdate bool     `yaml:"disableUpdate"`
 | 
				
			||||||
	} `yaml:"rpc"`
 | 
						} `yaml:"rpc"`
 | 
				
			||||||
	ClusterId string `yaml:"clusterId"`
 | 
						ClusterId string `yaml:"clusterId"`
 | 
				
			||||||
	Secret    string `yaml:"secret"`
 | 
						Secret    string `yaml:"secret"`
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -635,11 +635,13 @@ func (this *Node) checkClusterConfig() error {
 | 
				
			|||||||
	if len(resp.Endpoints) == 0 {
 | 
						if len(resp.Endpoints) == 0 {
 | 
				
			||||||
		resp.Endpoints = []string{}
 | 
							resp.Endpoints = []string{}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	apiConfig := &configs.APIConfig{
 | 
						var apiConfig = &configs.APIConfig{
 | 
				
			||||||
		RPC: struct {
 | 
							RPC: struct {
 | 
				
			||||||
			Endpoints     []string `yaml:"endpoints"`
 | 
								Endpoints     []string `yaml:"endpoints"`
 | 
				
			||||||
 | 
								DisableUpdate bool     `yaml:"disableUpdate"`
 | 
				
			||||||
		}{
 | 
							}{
 | 
				
			||||||
			Endpoints:     resp.Endpoints,
 | 
								Endpoints:     resp.Endpoints,
 | 
				
			||||||
 | 
								DisableUpdate: false,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		NodeId: resp.UniqueId,
 | 
							NodeId: resp.UniqueId,
 | 
				
			||||||
		Secret: resp.Secret,
 | 
							Secret: resp.Secret,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -63,6 +63,16 @@ func (this *SyncAPINodesTask) Stop() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *SyncAPINodesTask) Loop() error {
 | 
					func (this *SyncAPINodesTask) Loop() error {
 | 
				
			||||||
 | 
						config, err := configs.LoadAPIConfig()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 是否禁止自动升级
 | 
				
			||||||
 | 
						if config.RPC.DisableUpdate {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var tr = trackers.Begin("SYNC_API_NODES")
 | 
						var tr = trackers.Begin("SYNC_API_NODES")
 | 
				
			||||||
	defer tr.End()
 | 
						defer tr.End()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -76,7 +86,7 @@ func (this *SyncAPINodesTask) Loop() error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	newEndpoints := []string{}
 | 
						var newEndpoints = []string{}
 | 
				
			||||||
	for _, node := range resp.ApiNodes {
 | 
						for _, node := range resp.ApiNodes {
 | 
				
			||||||
		if !node.IsOn {
 | 
							if !node.IsOn {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
@@ -85,16 +95,12 @@ func (this *SyncAPINodesTask) Loop() error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 和现有的对比
 | 
						// 和现有的对比
 | 
				
			||||||
	config, err := configs.LoadAPIConfig()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if this.isSame(newEndpoints, config.RPC.Endpoints) {
 | 
						if this.isSame(newEndpoints, config.RPC.Endpoints) {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 测试是否有API节点可用
 | 
						// 测试是否有API节点可用
 | 
				
			||||||
	hasOk := this.testEndpoints(newEndpoints)
 | 
						var hasOk = this.testEndpoints(newEndpoints)
 | 
				
			||||||
	if !hasOk {
 | 
						if !hasOk {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user