mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	优化TOA
This commit is contained in:
		@@ -54,10 +54,11 @@ function build() {
 | 
			
		||||
	cp -R "$ROOT"/www "$DIST"/
 | 
			
		||||
	cp -R "$ROOT"/pages "$DIST"/
 | 
			
		||||
 | 
			
		||||
	# we support TOA on linux/amd64 only
 | 
			
		||||
	if [ "$OS" == "linux" ] && [ "$ARCH" == "amd64" ]
 | 
			
		||||
	# we support TOA on linux only
 | 
			
		||||
	if [ "$OS" == "linux" ] && [ -f "${ROOT}/edge-toa/edge-toa-${ARCH}" ]
 | 
			
		||||
	then
 | 
			
		||||
		cp -R "$ROOT"/edge-toa "$DIST"
 | 
			
		||||
		mkdir "$DIST/edge-toa"
 | 
			
		||||
		cp "${ROOT}/edge-toa/edge-toa-${ARCH}" "$DIST/edge-toa/edge-toa"
 | 
			
		||||
	fi
 | 
			
		||||
 | 
			
		||||
	echo "building ..."
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@@ -15,3 +15,7 @@ func (this *Node) reloadIPLibrary() {
 | 
			
		||||
func (this *Node) notifyPlusChange() error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Node) execTOAChangedTask() error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -90,6 +90,8 @@ func (this *Node) execTask(rpcClient *rpc.RPCClient, task *pb.NodeTask) error {
 | 
			
		||||
		err = this.execUpdatingServersTask(rpcClient)
 | 
			
		||||
	case "plusChanged":
 | 
			
		||||
		err = this.notifyPlusChange()
 | 
			
		||||
	case "toaChanged":
 | 
			
		||||
		err = this.execTOAChangedTask()
 | 
			
		||||
	default:
 | 
			
		||||
		remotelogs.Error("NODE", "task '"+types.String(task.Id)+"', type '"+task.Type+"' has not been handled")
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,117 +1,31 @@
 | 
			
		||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
 | 
			
		||||
//go:build !plus
 | 
			
		||||
 | 
			
		||||
package nodes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/events"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
 | 
			
		||||
	executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"net"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
import "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
 | 
			
		||||
var sharedTOAManager = NewTOAManager()
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	if !teaconst.IsMain {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	events.On(events.EventReload, func() {
 | 
			
		||||
		err := sharedTOAManager.Run(sharedNodeConfig.TOA)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			remotelogs.Error("TOA", err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type TOAManager struct {
 | 
			
		||||
	config *nodeconfigs.TOAConfig
 | 
			
		||||
	pid    int
 | 
			
		||||
	conn   net.Conn
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewTOAManager() *TOAManager {
 | 
			
		||||
	return &TOAManager{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *TOAManager) Run(config *nodeconfigs.TOAConfig) error {
 | 
			
		||||
	this.config = config
 | 
			
		||||
 | 
			
		||||
	if this.pid > 0 {
 | 
			
		||||
		remotelogs.Println("TOA", "stopping ...")
 | 
			
		||||
		err := this.Quit()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			remotelogs.Error("TOA", "quit error: "+err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if this.conn != nil {
 | 
			
		||||
			_ = this.conn.Close()
 | 
			
		||||
		}
 | 
			
		||||
		this.conn = nil
 | 
			
		||||
		this.pid = 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !config.IsOn {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var binPath = Tea.Root + "/edge-toa/edge-toa" // TODO 可以做成配置
 | 
			
		||||
	_, err := os.Stat(binPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	remotelogs.Println("TOA", "starting ...")
 | 
			
		||||
	remotelogs.Println("TOA", "args: "+strings.Join(config.AsArgs(), " "))
 | 
			
		||||
	var cmd = executils.NewCmd(binPath, config.AsArgs()...)
 | 
			
		||||
	err = cmd.Start()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	var process = cmd.Process()
 | 
			
		||||
	if process == nil {
 | 
			
		||||
		return errors.New("start failed")
 | 
			
		||||
	}
 | 
			
		||||
	this.pid = process.Pid
 | 
			
		||||
 | 
			
		||||
	goman.New(func() {
 | 
			
		||||
		_ = cmd.Wait()
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
func (this *TOAManager) Apply(config *nodeconfigs.TOAConfig) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *TOAManager) Config() *nodeconfigs.TOAConfig {
 | 
			
		||||
	return this.config
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *TOAManager) Quit() error {
 | 
			
		||||
	return this.SendMsg("quit:0")
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *TOAManager) SendMsg(msg string) error {
 | 
			
		||||
	if this.config == nil {
 | 
			
		||||
	return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if this.conn != nil {
 | 
			
		||||
		_, err := this.conn.Write([]byte(msg + "\n"))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			_ = this.conn.Close()
 | 
			
		||||
			this.conn = nil
 | 
			
		||||
		}
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conn, err := net.DialTimeout("unix", this.config.SockFile(), 1*time.Second)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	this.conn = conn
 | 
			
		||||
	_, err = this.conn.Write([]byte(msg + "\n"))
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,17 +0,0 @@
 | 
			
		||||
package nodes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestTOAManager_Run(t *testing.T) {
 | 
			
		||||
	manager := NewTOAManager()
 | 
			
		||||
	err := manager.Run(&nodeconfigs.TOAConfig{
 | 
			
		||||
		IsOn: true,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	t.Log("ok")
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user