Switch to ConPty on Windows 10 & support windows arm64 platform (#20)

This commit is contained in:
UUBulb
2024-05-21 18:40:08 +08:00
committed by GitHub
parent 060d13a759
commit f882ca3498
8 changed files with 296 additions and 146 deletions
+4 -5
View File
@@ -1,16 +1,15 @@
package main
import (
"fmt"
"strings"
"net"
"errors"
"fmt"
"net"
"strings"
"github.com/spf13/cobra"
"github.com/AlecAivazis/survey/v2"
"github.com/shirou/gopsutil/v3/disk"
psnet "github.com/shirou/gopsutil/v3/net"
"github.com/spf13/cobra"
)
var editCmd = &cobra.Command{
+114 -7
View File
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
@@ -22,6 +23,7 @@ import (
"github.com/go-ping/ping"
"github.com/gorilla/websocket"
"github.com/nezhahq/go-github-selfupdate/selfupdate"
"github.com/nezhahq/service"
"github.com/quic-go/quic-go/http3"
"github.com/shirou/gopsutil/v3/host"
"github.com/spf13/cobra"
@@ -52,18 +54,28 @@ type AgentCliParam struct {
IPReportPeriod uint32 // 上报IP间隔
}
type program struct {
exit chan struct{}
service service.Service
}
var (
version string
arch string
client pb.NezhaServiceClient
inited bool
logger service.Logger
)
var agentCmd = &cobra.Command{
Use: "agent",
Run: func(cmd *cobra.Command, args []string) {
run()
},
Use: "agent",
Run: func(cmd *cobra.Command, args []string) {
if runtime.GOOS == "darwin" {
run() // macOS launchctl 如使用 runService 则无法启动,原因未知
} else {
runService("")
}
},
PreRun: preRun,
PersistentPreRun: persistPreRun,
}
@@ -149,9 +161,9 @@ func init() {
func main() {
if err := agentCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(err)
os.Exit(1)
}
}
func persistPreRun(cmd *cobra.Command, args []string) {
@@ -196,6 +208,33 @@ func preRun(cmd *cobra.Command, args []string) {
}
}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) Stop(s service.Service) error {
close(p.exit)
if service.Interactive() {
os.Exit(0)
}
return nil
}
func (p *program) run() {
defer func() {
if service.Interactive() {
p.Stop(p.service)
} else {
p.service.Stop()
}
}()
run()
return
}
func run() {
auth := model.AuthHandler{
ClientSecret: agentCliParam.ClientSecret,
@@ -274,6 +313,74 @@ func run() {
}
}
func runService(action string) {
var tlsoption string
dir, err := os.Getwd()
if err != nil {
println("获取当前工作目录时出错: ", err)
return
}
if agentCliParam.TLS {
tlsoption = "--tls"
}
svcConfig := &service.Config{
Name: "nezha-agent",
DisplayName: "Nezha Agent",
Description: "哪吒探针监控端",
Arguments: []string{
"-s", agentCliParam.Server,
"-p", agentCliParam.ClientSecret,
tlsoption,
},
WorkingDirectory: dir,
}
prg := &program{
exit: make(chan struct{}),
}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal("创建服务时出错: ", err)
}
prg.service = s
errs := make(chan error, 5)
logger, err = s.Logger(errs)
if err != nil {
log.Fatal(err)
}
go func() {
for {
err := <-errs
if err != nil {
log.Print(err)
}
}
}()
if action == "install" {
initName := s.Platform()
log.Println("Init system is:", initName)
}
if len(action) != 0 {
err := service.Control(s, action)
if err != nil {
log.Fatal(err)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}
func receiveTasks(tasks pb.NezhaService_RequestTaskClient) error {
var err error
defer println("receiveTasks exit", time.Now(), "=>", err)
+4 -78
View File
@@ -2,22 +2,15 @@ package main
import (
"os"
"log"
"github.com/spf13/cobra"
"github.com/nezhahq/service"
)
type program struct {
exit chan struct{}
service service.Service
}
var serviceCmd = &cobra.Command{
Use: "service <install/uninstall/start/stop/restart>",
Short: "服务与自启动设置",
Args: cobra.ExactArgs(1),
Run: runService,
Run: serviceActions,
PreRun: servicePreRun,
}
@@ -39,74 +32,7 @@ func servicePreRun(cmd *cobra.Command, args []string) {
}
}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {
defer func() {
if service.Interactive() {
p.Stop(p.service)
} else {
p.service.Stop()
}
}()
run()
return
}
func (p *program) Stop(s service.Service) error {
close(p.exit)
if service.Interactive() {
os.Exit(0)
}
return nil
}
func runService(cmd *cobra.Command, args []string) {
var tlsoption string
mode := args[0]
dir, err := os.Getwd()
if err != nil {
println("获取当前工作目录时出错: ", err)
return
}
if agentCliParam.TLS {
tlsoption = "--tls"
}
svcConfig := &service.Config{
Name: "nezha-agent",
DisplayName: "Nezha Agent",
Description: "哪吒探针监控端",
Arguments: []string{
"-s", agentCliParam.Server,
"-p", agentCliParam.ClientSecret,
tlsoption,
},
WorkingDirectory: dir,
}
prg := &program{
exit: make(chan struct{}),
}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal("创建服务时出错: ", err)
}
if mode == "install" {
initName := s.Platform()
log.Println("Init system is:", initName)
}
err = service.Control(s, mode)
if err != nil {
log.Fatal(err)
}
func serviceActions(cmd *cobra.Command, args []string) {
action := args[0]
runService(action)
}