mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-07 10:40:26 +08:00
将api.yaml修改为api_node.yaml
This commit is contained in:
@@ -49,7 +49,7 @@ function build() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cp "$ROOT"/configs/api.template.yaml "$DIST"/configs
|
cp "$ROOT"/configs/api_node.template.yaml "$DIST"/configs
|
||||||
cp "$ROOT"/configs/cluster.template.yaml "$DIST"/configs
|
cp "$ROOT"/configs/cluster.template.yaml "$DIST"/configs
|
||||||
cp -R "$ROOT"/www "$DIST"/
|
cp -R "$ROOT"/www "$DIST"/
|
||||||
cp -R "$ROOT"/pages "$DIST"/
|
cp -R "$ROOT"/pages "$DIST"/
|
||||||
|
|||||||
1
build/configs/.gitignore
vendored
1
build/configs/.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
node.json
|
node.json
|
||||||
api.yaml
|
api.yaml
|
||||||
|
api_node.yaml
|
||||||
cluster.yaml
|
cluster.yaml
|
||||||
*.cache
|
*.cache
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
* `api.template.yaml` - API相关配置模板
|
* `api_node.template.yaml` - API相关配置模板
|
||||||
* `cluster.template.yaml` - 通过集群自动接入节点模板
|
* `cluster.template.yaml` - 通过集群自动接入节点模板
|
||||||
@@ -37,7 +37,7 @@ func main() {
|
|||||||
// validate config
|
// validate config
|
||||||
_, err := configs.LoadAPIConfig()
|
_, err := configs.LoadAPIConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile("api.yaml") + "' failed: " + err.Error())
|
fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile(configs.ConfigFileName) + "' failed: " + err.Error())
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -64,7 +64,7 @@ func main() {
|
|||||||
// verify dir
|
// verify dir
|
||||||
{
|
{
|
||||||
fmt.Println("Checking '" + dir + "' ...")
|
fmt.Println("Checking '" + dir + "' ...")
|
||||||
for _, subDir := range []string{"bin", "configs/api.yaml", "logs"} {
|
for _, subDir := range []string{"bin/" + filepath.Base(exe), "configs", "logs"} {
|
||||||
_, err := os.Stat(dir + "/" + subDir)
|
_, err := os.Stat(dir + "/" + subDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("[ERROR]program directory structure has been broken, please remove it manually.")
|
fmt.Println("[ERROR]program directory structure has been broken, please remove it manually.")
|
||||||
|
|||||||
@@ -7,12 +7,17 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// APIConfig 节点API配置
|
const ConfigFileName = "api_node.yaml"
|
||||||
|
const oldConfigFileName = "api.yaml"
|
||||||
|
|
||||||
type APIConfig struct {
|
type APIConfig struct {
|
||||||
RPC struct {
|
OldRPC struct {
|
||||||
Endpoints []string `yaml:"endpoints" json:"endpoints"`
|
Endpoints []string `yaml:"endpoints" json:"endpoints"`
|
||||||
DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"`
|
DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"`
|
||||||
} `yaml:"rpc" json:"rpc"`
|
} `yaml:"rpc" json:"rpc"`
|
||||||
|
|
||||||
|
RPCEndpoints []string `yaml:"rpc.endpoints" json:"rpc.endpoints"`
|
||||||
|
RPCDisableUpdate bool `yaml:"rpc.disableUpdate" json:"rpc.disableUpdate"`
|
||||||
NodeId string `yaml:"nodeId" json:"nodeId"`
|
NodeId string `yaml:"nodeId" json:"nodeId"`
|
||||||
Secret string `yaml:"secret" json:"secret"`
|
Secret string `yaml:"secret" json:"secret"`
|
||||||
}
|
}
|
||||||
@@ -22,9 +27,16 @@ func NewAPIConfig() *APIConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *APIConfig) Init() error {
|
func (this *APIConfig) Init() error {
|
||||||
if len(this.RPC.Endpoints) == 0 {
|
// compatible with old
|
||||||
|
if len(this.RPCEndpoints) == 0 && len(this.OldRPC.Endpoints) > 0 {
|
||||||
|
this.RPCEndpoints = this.OldRPC.Endpoints
|
||||||
|
this.RPCDisableUpdate = this.OldRPC.DisableUpdate
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.RPCEndpoints) == 0 {
|
||||||
return errors.New("no valid 'rpc.endpoints'")
|
return errors.New("no valid 'rpc.endpoints'")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(this.NodeId) == 0 {
|
if len(this.NodeId) == 0 {
|
||||||
return errors.New("'nodeId' required")
|
return errors.New("'nodeId' required")
|
||||||
}
|
}
|
||||||
@@ -35,8 +47,12 @@ func (this *APIConfig) Init() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadAPIConfig() (*APIConfig, error) {
|
func LoadAPIConfig() (*APIConfig, error) {
|
||||||
data, err := os.ReadFile(Tea.ConfigFile("api.yaml"))
|
for _, filename := range []string{ConfigFileName, oldConfigFileName} {
|
||||||
|
data, err := os.ReadFile(Tea.ConfigFile(filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +69,8 @@ func LoadAPIConfig() (*APIConfig, error) {
|
|||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
return nil, errors.New("no config file '" + ConfigFileName + "' found")
|
||||||
|
}
|
||||||
|
|
||||||
// WriteFile 保存到文件
|
// WriteFile 保存到文件
|
||||||
func (this *APIConfig) WriteFile(path string) error {
|
func (this *APIConfig) WriteFile(path string) error {
|
||||||
|
|||||||
@@ -438,10 +438,10 @@ func (this *APIStream) handleChangeAPINode(message *pb.NodeStreamMessage) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
config.RPC.Endpoints = []string{messageData.Addr}
|
config.RPCEndpoints = []string{messageData.Addr}
|
||||||
|
|
||||||
// 保存到文件
|
// 保存到文件
|
||||||
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
|
err = config.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.replyFail(message.RequestId, "save config file failed: "+err.Error())
|
this.replyFail(message.RequestId, "save config file failed: "+err.Error())
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -314,15 +314,15 @@ func (this *Node) syncConfig(taskVersion int64) error {
|
|||||||
this.locker.Lock()
|
this.locker.Lock()
|
||||||
defer this.locker.Unlock()
|
defer this.locker.Unlock()
|
||||||
|
|
||||||
// 检查api.yaml是否存在
|
// 检查api_node.yaml是否存在
|
||||||
apiConfigFile := Tea.ConfigFile("api.yaml")
|
var apiConfigFile = Tea.ConfigFile(configs.ConfigFileName)
|
||||||
_, err := os.Stat(apiConfigFile)
|
_, err := os.Stat(apiConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
clusterErr := this.checkClusterConfig()
|
clusterErr := this.checkClusterConfig()
|
||||||
if clusterErr != nil {
|
if clusterErr != nil {
|
||||||
if os.IsNotExist(clusterErr) {
|
if os.IsNotExist(clusterErr) {
|
||||||
return errors.New("can not find config file 'configs/api.yaml'")
|
return fmt.Errorf("can not find config file 'configs/%s'", configs.ConfigFileName)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("check cluster config failed: %w", clusterErr)
|
return fmt.Errorf("check cluster config failed: %w", clusterErr)
|
||||||
}
|
}
|
||||||
@@ -524,7 +524,7 @@ func (this *Node) startSyncTimer() {
|
|||||||
|
|
||||||
// 检查集群设置
|
// 检查集群设置
|
||||||
func (this *Node) checkClusterConfig() error {
|
func (this *Node) checkClusterConfig() error {
|
||||||
configFile := Tea.ConfigFile("cluster.yaml")
|
var configFile = Tea.ConfigFile("cluster.yaml")
|
||||||
data, err := os.ReadFile(configFile)
|
data, err := os.ReadFile(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -536,7 +536,8 @@ func (this *Node) checkClusterConfig() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rpcClient, err := rpc.NewRPCClient(&configs.APIConfig{
|
rpcClient, err := rpc.NewRPCClient(&configs.APIConfig{
|
||||||
RPC: config.RPC,
|
RPCEndpoints: config.RPC.Endpoints,
|
||||||
|
RPCDisableUpdate: config.RPC.DisableUpdate,
|
||||||
NodeId: config.ClusterId,
|
NodeId: config.ClusterId,
|
||||||
Secret: config.Secret,
|
Secret: config.Secret,
|
||||||
})
|
})
|
||||||
@@ -556,22 +557,17 @@ func (this *Node) checkClusterConfig() error {
|
|||||||
resp.Endpoints = []string{}
|
resp.Endpoints = []string{}
|
||||||
}
|
}
|
||||||
var apiConfig = &configs.APIConfig{
|
var apiConfig = &configs.APIConfig{
|
||||||
RPC: struct {
|
RPCEndpoints: resp.Endpoints,
|
||||||
Endpoints []string `yaml:"endpoints" json:"endpoints"`
|
RPCDisableUpdate: false,
|
||||||
DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"`
|
|
||||||
}{
|
|
||||||
Endpoints: resp.Endpoints,
|
|
||||||
DisableUpdate: false,
|
|
||||||
},
|
|
||||||
NodeId: resp.UniqueId,
|
NodeId: resp.UniqueId,
|
||||||
Secret: resp.Secret,
|
Secret: resp.Secret,
|
||||||
}
|
}
|
||||||
remotelogs.Debug("NODE", "writing 'configs/api.yaml' ...")
|
remotelogs.Debug("NODE", "writing 'configs/"+configs.ConfigFileName+"' ...")
|
||||||
err = apiConfig.WriteFile(Tea.ConfigFile("api.yaml"))
|
err = apiConfig.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
remotelogs.Debug("NODE", "wrote 'configs/api.yaml' successfully")
|
remotelogs.Debug("NODE", "wrote 'configs/"+configs.ConfigFileName+"' successfully")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1136,7 +1132,7 @@ func (this *Node) changeAPINodeAddrs(apiNodeAddrs []*serverconfigs.NetworkAddres
|
|||||||
if config == nil {
|
if config == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var oldEndpoints = config.RPC.Endpoints
|
var oldEndpoints = config.RPCEndpoints
|
||||||
|
|
||||||
rpcClient, err := rpc.SharedRPC()
|
rpcClient, err := rpc.SharedRPC()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1150,9 +1146,9 @@ func (this *Node) changeAPINodeAddrs(apiNodeAddrs []*serverconfigs.NetworkAddres
|
|||||||
go func(v int64) {
|
go func(v int64) {
|
||||||
// 测试新的API节点地址
|
// 测试新的API节点地址
|
||||||
if rpcClient.TestEndpoints(addrs) {
|
if rpcClient.TestEndpoints(addrs) {
|
||||||
config.RPC.Endpoints = addrs
|
config.RPCEndpoints = addrs
|
||||||
} else {
|
} else {
|
||||||
config.RPC.Endpoints = oldEndpoints
|
config.RPCEndpoints = oldEndpoints
|
||||||
this.lastAPINodeAddrs = nil // 恢复为空,以便于下次更新重试
|
this.lastAPINodeAddrs = nil // 恢复为空,以便于下次更新重试
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/configs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls"
|
"github.com/TeaOSLab/EdgeNode/internal/firewalls"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
||||||
@@ -26,8 +27,8 @@ func (this *Node) loopTasks() error {
|
|||||||
var tr = trackers.Begin("CHECK_NODE_CONFIG_CHANGES")
|
var tr = trackers.Begin("CHECK_NODE_CONFIG_CHANGES")
|
||||||
defer tr.End()
|
defer tr.End()
|
||||||
|
|
||||||
// 检查api.yaml是否存在
|
// 检查api_node.yaml是否存在
|
||||||
var apiConfigFile = Tea.ConfigFile("api.yaml")
|
var apiConfigFile = Tea.ConfigFile(configs.ConfigFileName)
|
||||||
_, err := os.Stat(apiConfigFile)
|
_, err := os.Stat(apiConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func (this *SyncAPINodesTask) Loop() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 是否禁止自动升级
|
// 是否禁止自动升级
|
||||||
if config.RPC.DisableUpdate {
|
if config.RPCDisableUpdate {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ func (this *SyncAPINodesTask) Loop() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 和现有的对比
|
// 和现有的对比
|
||||||
if utils.EqualStrings(newEndpoints, config.RPC.Endpoints) {
|
if utils.EqualStrings(newEndpoints, config.RPCEndpoints) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +107,7 @@ func (this *SyncAPINodesTask) Loop() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 修改RPC对象配置
|
// 修改RPC对象配置
|
||||||
config.RPC.Endpoints = newEndpoints
|
config.RPCEndpoints = newEndpoints
|
||||||
|
|
||||||
// 更新当前RPC
|
// 更新当前RPC
|
||||||
if !hasCustomizedAPINodeAddrs {
|
if !hasCustomizedAPINodeAddrs {
|
||||||
@@ -118,7 +118,7 @@ func (this *SyncAPINodesTask) Loop() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 保存到文件
|
// 保存到文件
|
||||||
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
|
err = config.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ func (this *RPCClient) TestEndpoints(endpoints []string) bool {
|
|||||||
func (this *RPCClient) init() error {
|
func (this *RPCClient) init() error {
|
||||||
// 重新连接
|
// 重新连接
|
||||||
var conns = []*grpc.ClientConn{}
|
var conns = []*grpc.ClientConn{}
|
||||||
for _, endpoint := range this.apiConfig.RPC.Endpoints {
|
for _, endpoint := range this.apiConfig.RPCEndpoints {
|
||||||
u, err := url.Parse(endpoint)
|
u, err := url.Parse(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse endpoint failed: %w", err)
|
return fmt.Errorf("parse endpoint failed: %w", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user