Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make service working on Windows & Switch winpty to conpty on Windows 10 #20

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ builds:
ignore:
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
main: ./cmd/agent
binary: nezha-agent
- id: darwin-amd64
Expand Down
7 changes: 3 additions & 4 deletions cmd/agent/edit.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
"errors"
"fmt"
"strings"
"net"
"errors"
"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{
Expand Down
121 changes: 114 additions & 7 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
82 changes: 4 additions & 78 deletions cmd/agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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)
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/DaRealFreak/cloudflare-bp-go v1.0.4
github.com/UserExistsError/conpty v0.1.3
github.com/artdarek/go-unzip v1.0.0
github.com/blang/semver v3.5.1+incompatible
github.com/creack/pty v1.1.21
Expand All @@ -15,7 +16,7 @@ require (
github.com/iamacarpet/go-winpty v1.0.4
github.com/json-iterator/go v1.1.12
github.com/nezhahq/go-github-selfupdate v0.0.0-20240418134522-9d84a13bbf2d
github.com/nezhahq/service v0.0.0-20240518110122-594be3ba1962
github.com/nezhahq/service v0.0.0-20240519060034-90701692f8ef
github.com/quic-go/quic-go v0.40.1
github.com/shirou/gopsutil/v3 v3.24.4
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -77,7 +78,7 @@ require (
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/UserExistsError/conpty v0.1.3 h1:YzGQkHAiBBkAihOCO5J2cAnahzb8ePvje2YxG7et1E0=
github.com/UserExistsError/conpty v0.1.3/go.mod h1:PDglKIkX3O/2xVk0MV9a6bCWxRmPVfxqZoTG/5sSd9I=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/artdarek/go-unzip v1.0.0 h1:Ja9wfhiXyl67z5JT37rWjTSb62KXDP+9jHRkdSREUvg=
Expand Down Expand Up @@ -101,12 +103,10 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nezhahq/go-github-selfupdate v0.0.0-20240418134522-9d84a13bbf2d h1:gUt6JLTE/HH7qXS5F7SSYrmsj8NXx3i5CaF6mzYw6pA=
github.com/nezhahq/go-github-selfupdate v0.0.0-20240418134522-9d84a13bbf2d/go.mod h1:fOsabb2tjwCe7/kGSsout6oL2cp0sKhOwYQp6fP/Xfg=
github.com/nezhahq/service v0.0.0-20240518043736-9ae0db11e8df h1:V9mIEc9CKm+hFuPmrEjByCScbWZGoWLOUcPjrAis9ew=
github.com/nezhahq/service v0.0.0-20240518043736-9ae0db11e8df/go.mod h1:1CemEvuMOM4B88ckxMZvLT0dehlP5+bqQOYRLMslSdE=
github.com/nezhahq/service v0.0.0-20240518100746-f22fc3c61e5d h1:OmxtpsZfT0wLBgwo0uw4vusU+nEVWljye0vWfRpbFig=
github.com/nezhahq/service v0.0.0-20240518100746-f22fc3c61e5d/go.mod h1:1CemEvuMOM4B88ckxMZvLT0dehlP5+bqQOYRLMslSdE=
github.com/nezhahq/service v0.0.0-20240518110122-594be3ba1962 h1:2zrICSRNedjDMlmDb58B03QHcLvpKIBe931FV7kHvas=
github.com/nezhahq/service v0.0.0-20240518110122-594be3ba1962/go.mod h1:1CemEvuMOM4B88ckxMZvLT0dehlP5+bqQOYRLMslSdE=
github.com/nezhahq/service v0.0.0-20240519060034-90701692f8ef h1:nC+Cxc9MyBFT+snkl5lBUpxGqSGqXv9B2AMHtZOtXzo=
github.com/nezhahq/service v0.0.0-20240519060034-90701692f8ef/go.mod h1:i6zO7Vzuv5+mdaCzHrvAC4U63W59uXmX9n6o7p4PJGk=
github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q=
github.com/onsi/ginkgo/v2 v2.9.5/go.mod h1:tvAoo1QUJwNEU2ITftXTpR7R1RbCzoZUOs3RonqW57k=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
Expand Down Expand Up @@ -221,6 +221,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
Loading
Loading