2019-12-05 09:36:58 -05:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-01-16 01:11:51 -05:00
|
|
|
|
"crypto/tls"
|
2021-01-15 11:45:49 -05:00
|
|
|
|
"errors"
|
2019-12-07 05:14:40 -05:00
|
|
|
|
"fmt"
|
2019-12-05 09:36:58 -05:00
|
|
|
|
"log"
|
2021-01-15 11:45:49 -05:00
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2019-12-08 10:49:38 -05:00
|
|
|
|
"os"
|
2021-01-18 20:59:04 -05:00
|
|
|
|
"os/exec"
|
2021-01-15 11:45:49 -05:00
|
|
|
|
"strings"
|
2019-12-07 05:14:40 -05:00
|
|
|
|
"time"
|
2019-12-05 09:36:58 -05:00
|
|
|
|
|
2020-11-29 11:07:27 -05:00
|
|
|
|
"github.com/blang/semver"
|
2021-01-15 11:45:49 -05:00
|
|
|
|
"github.com/genkiroid/cert"
|
|
|
|
|
"github.com/go-ping/ping"
|
2020-12-12 12:31:22 -05:00
|
|
|
|
"github.com/p14yground/go-github-selfupdate/selfupdate"
|
2019-12-08 10:49:38 -05:00
|
|
|
|
"github.com/spf13/cobra"
|
2019-12-05 09:36:58 -05:00
|
|
|
|
"google.golang.org/grpc"
|
2019-12-05 10:42:20 -05:00
|
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
|
"github.com/naiba/nezha/model"
|
2021-01-28 10:19:59 -05:00
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2020-11-10 21:07:45 -05:00
|
|
|
|
pb "github.com/naiba/nezha/proto"
|
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
|
|
|
|
"github.com/naiba/nezha/service/monitor"
|
|
|
|
|
"github.com/naiba/nezha/service/rpc"
|
2019-12-05 09:36:58 -05:00
|
|
|
|
)
|
|
|
|
|
|
2019-12-08 10:49:38 -05:00
|
|
|
|
var (
|
2020-11-29 11:07:27 -05:00
|
|
|
|
clientID string
|
|
|
|
|
server string
|
|
|
|
|
clientSecret string
|
|
|
|
|
debug bool
|
2020-12-18 23:43:03 -05:00
|
|
|
|
version string
|
2020-11-29 11:07:27 -05:00
|
|
|
|
|
2019-12-08 10:49:38 -05:00
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
|
Use: "nezha-agent",
|
|
|
|
|
Short: "「哪吒面板」监控、备份、站点管理一站式服务",
|
|
|
|
|
Long: `哪吒面板
|
|
|
|
|
================================
|
|
|
|
|
监控、备份、站点管理一站式服务
|
|
|
|
|
啦啦啦,啦啦啦,我是 mjj 小行家`,
|
2020-11-29 11:07:27 -05:00
|
|
|
|
Run: run,
|
|
|
|
|
Version: version,
|
2019-12-08 10:49:38 -05:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2020-11-30 01:24:00 -05:00
|
|
|
|
var (
|
|
|
|
|
reporting bool
|
|
|
|
|
client pb.NezhaServiceClient
|
|
|
|
|
ctx = context.Background()
|
|
|
|
|
delayWhenError = time.Second * 10
|
|
|
|
|
updateCh = make(chan struct{}, 0)
|
2021-01-20 20:37:29 -05:00
|
|
|
|
httpClient = &http.Client{
|
|
|
|
|
Transport: &http.Transport{
|
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
|
},
|
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
},
|
|
|
|
|
}
|
2020-11-30 01:24:00 -05:00
|
|
|
|
)
|
|
|
|
|
|
2020-11-29 11:07:27 -05:00
|
|
|
|
func doSelfUpdate() {
|
|
|
|
|
defer func() {
|
2020-11-30 00:49:31 -05:00
|
|
|
|
time.Sleep(time.Minute * 20)
|
2020-11-29 11:07:27 -05:00
|
|
|
|
updateCh <- struct{}{}
|
|
|
|
|
}()
|
|
|
|
|
v := semver.MustParse(version)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
log.Println("Check update", v)
|
2020-11-29 11:07:27 -05:00
|
|
|
|
latest, err := selfupdate.UpdateSelf(v, "naiba/nezha")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("Binary update failed:", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if latest.Version.Equals(v) {
|
|
|
|
|
// latest version is the same as current version. It means current binary is up to date.
|
|
|
|
|
log.Println("Current binary is the latest version", version)
|
|
|
|
|
} else {
|
|
|
|
|
log.Println("Successfully updated to version", latest.Version)
|
2020-12-20 00:03:03 -05:00
|
|
|
|
os.Exit(1)
|
2020-11-29 11:07:27 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 02:43:34 -05:00
|
|
|
|
func init() {
|
|
|
|
|
cert.TimeoutSeconds = 30
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 09:36:58 -05:00
|
|
|
|
func main() {
|
2020-11-29 23:45:51 -05:00
|
|
|
|
// 来自于 GoReleaser 的版本号
|
|
|
|
|
dao.Version = version
|
2019-12-09 10:45:23 -05:00
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&server, "server", "s", "localhost:5555", "客户端ID")
|
2019-12-09 03:02:49 -05:00
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&clientID, "id", "i", "", "客户端ID")
|
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&clientSecret, "secret", "p", "", "客户端Secret")
|
|
|
|
|
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "开启Debug")
|
2019-12-08 10:49:38 -05:00
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run(cmd *cobra.Command, args []string) {
|
2019-12-09 03:02:49 -05:00
|
|
|
|
dao.Conf = &model.Config{
|
|
|
|
|
Debug: debug,
|
2019-12-05 09:36:58 -05:00
|
|
|
|
}
|
2019-12-09 03:02:49 -05:00
|
|
|
|
auth := rpc.AuthHandler{
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
ClientSecret: clientSecret,
|
2019-12-05 09:36:58 -05:00
|
|
|
|
}
|
2019-12-10 04:57:57 -05:00
|
|
|
|
|
|
|
|
|
// 上报服务器信息
|
|
|
|
|
go reportState()
|
|
|
|
|
|
2020-12-19 10:27:59 -05:00
|
|
|
|
if version != "" {
|
|
|
|
|
go func() {
|
|
|
|
|
for range updateCh {
|
|
|
|
|
go doSelfUpdate()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
updateCh <- struct{}{}
|
|
|
|
|
}
|
2020-11-29 11:07:27 -05:00
|
|
|
|
|
2019-12-10 04:57:57 -05:00
|
|
|
|
var err error
|
|
|
|
|
var conn *grpc.ClientConn
|
|
|
|
|
|
2020-02-09 09:04:59 -05:00
|
|
|
|
retry := func() {
|
|
|
|
|
log.Println("Error to close connection ...")
|
|
|
|
|
if conn != nil {
|
|
|
|
|
conn.Close()
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(delayWhenError)
|
|
|
|
|
log.Println("Try to reconnect ...")
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 05:14:31 -05:00
|
|
|
|
for {
|
2019-12-09 10:45:23 -05:00
|
|
|
|
conn, err = grpc.Dial(server, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
|
2019-12-09 03:02:49 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("grpc.Dial err: %v", err)
|
2019-12-09 05:14:31 -05:00
|
|
|
|
retry()
|
2019-12-09 03:02:49 -05:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
client = pb.NewNezhaServiceClient(conn)
|
|
|
|
|
// 第一步注册
|
2021-01-15 11:45:49 -05:00
|
|
|
|
_, err = client.ReportSystemInfo(ctx, monitor.GetHost().PB())
|
2019-12-09 05:14:31 -05:00
|
|
|
|
if err != nil {
|
2021-01-15 11:45:49 -05:00
|
|
|
|
log.Printf("client.ReportSystemInfo err: %v", err)
|
2019-12-09 05:14:31 -05:00
|
|
|
|
retry()
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
// 执行 Task
|
|
|
|
|
tasks, err := client.RequestTask(ctx, monitor.GetHost().PB())
|
2019-12-09 03:02:49 -05:00
|
|
|
|
if err != nil {
|
2021-01-15 11:45:49 -05:00
|
|
|
|
log.Printf("client.RequestTask err: %v", err)
|
2019-12-09 05:14:31 -05:00
|
|
|
|
retry()
|
2019-12-09 03:02:49 -05:00
|
|
|
|
continue
|
|
|
|
|
}
|
2021-01-15 11:45:49 -05:00
|
|
|
|
err = receiveTasks(tasks)
|
2021-01-16 05:04:47 -05:00
|
|
|
|
log.Printf("receiveTasks exit to main: %v", err)
|
2019-12-09 05:14:31 -05:00
|
|
|
|
retry()
|
2019-12-07 05:14:40 -05:00
|
|
|
|
}
|
2019-12-09 03:02:49 -05:00
|
|
|
|
}
|
2019-12-07 05:14:40 -05:00
|
|
|
|
|
2021-01-15 11:45:49 -05:00
|
|
|
|
func receiveTasks(tasks pb.NezhaService_RequestTaskClient) error {
|
2019-12-09 03:02:49 -05:00
|
|
|
|
var err error
|
2021-01-16 05:04:47 -05:00
|
|
|
|
defer log.Printf("receiveTasks exit %v => %v", time.Now(), err)
|
2019-12-09 03:02:49 -05:00
|
|
|
|
for {
|
2021-01-16 05:04:47 -05:00
|
|
|
|
var task *pb.Task
|
2021-01-15 11:45:49 -05:00
|
|
|
|
task, err = tasks.Recv()
|
2019-12-09 03:02:49 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-01-16 05:04:47 -05:00
|
|
|
|
go doTask(task)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func doTask(task *pb.Task) {
|
|
|
|
|
var result pb.TaskResult
|
|
|
|
|
result.Id = task.GetId()
|
|
|
|
|
result.Type = task.GetType()
|
|
|
|
|
switch task.GetType() {
|
2021-01-18 20:59:04 -05:00
|
|
|
|
case model.TaskTypeHTTPGET:
|
2021-01-16 05:04:47 -05:00
|
|
|
|
start := time.Now()
|
|
|
|
|
resp, err := httpClient.Get(task.GetData())
|
|
|
|
|
if err == nil {
|
|
|
|
|
result.Delay = float32(time.Now().Sub(start).Microseconds()) / 1000.0
|
2021-01-20 20:37:29 -05:00
|
|
|
|
if resp.StatusCode > 399 || resp.StatusCode < 200 {
|
2021-01-16 05:04:47 -05:00
|
|
|
|
err = errors.New("\n应用错误:" + resp.Status)
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
2021-01-16 05:04:47 -05:00
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
if strings.HasPrefix(task.GetData(), "https://") {
|
|
|
|
|
c := cert.NewCert(task.GetData()[8:])
|
|
|
|
|
if c.Error != "" {
|
2021-01-17 02:43:34 -05:00
|
|
|
|
result.Data = "SSL证书错误:" + c.Error
|
2021-01-16 05:04:47 -05:00
|
|
|
|
} else {
|
|
|
|
|
result.Data = c.Issuer + "|" + c.NotAfter
|
|
|
|
|
result.Successful = true
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
2021-01-20 20:37:29 -05:00
|
|
|
|
} else {
|
|
|
|
|
result.Successful = true
|
2021-01-15 11:45:49 -05:00
|
|
|
|
}
|
2021-01-16 05:04:47 -05:00
|
|
|
|
} else {
|
|
|
|
|
result.Data = err.Error()
|
|
|
|
|
}
|
2021-01-18 20:59:04 -05:00
|
|
|
|
case model.TaskTypeICMPPing:
|
2021-01-16 05:04:47 -05:00
|
|
|
|
pinger, err := ping.NewPinger(task.GetData())
|
|
|
|
|
if err == nil {
|
2021-01-18 00:45:06 -05:00
|
|
|
|
pinger.SetPrivileged(true)
|
2021-01-16 05:04:47 -05:00
|
|
|
|
pinger.Count = 10
|
|
|
|
|
pinger.Timeout = time.Second * 20
|
|
|
|
|
err = pinger.Run() // Blocks until finished.
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
result.Delay = float32(pinger.Statistics().AvgRtt.Microseconds()) / 1000.0
|
|
|
|
|
result.Successful = true
|
|
|
|
|
} else {
|
|
|
|
|
result.Data = err.Error()
|
|
|
|
|
}
|
2021-01-18 20:59:04 -05:00
|
|
|
|
case model.TaskTypeTCPPing:
|
2021-01-16 05:04:47 -05:00
|
|
|
|
start := time.Now()
|
|
|
|
|
conn, err := net.DialTimeout("tcp", task.GetData(), time.Second*10)
|
|
|
|
|
if err == nil {
|
|
|
|
|
conn.Write([]byte("ping\n"))
|
|
|
|
|
conn.Close()
|
|
|
|
|
result.Delay = float32(time.Now().Sub(start).Microseconds()) / 1000.0
|
|
|
|
|
result.Successful = true
|
|
|
|
|
} else {
|
|
|
|
|
result.Data = err.Error()
|
2019-12-09 03:02:49 -05:00
|
|
|
|
}
|
2021-01-18 20:59:04 -05:00
|
|
|
|
case model.TaskTypeCommand:
|
|
|
|
|
startedAt := time.Now()
|
2021-01-28 10:19:59 -05:00
|
|
|
|
var cmd *exec.Cmd
|
2021-01-28 21:40:57 -05:00
|
|
|
|
var endCh = make(chan struct{})
|
2021-01-28 22:59:35 -05:00
|
|
|
|
pg, err := utils.NewProcessExitGroup()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 进程组创建失败,直接退出
|
|
|
|
|
result.Data = err.Error()
|
|
|
|
|
client.ReportTask(ctx, &result)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-01-28 21:40:57 -05:00
|
|
|
|
timeout := time.NewTimer(time.Hour * 2)
|
2021-01-28 10:19:59 -05:00
|
|
|
|
if utils.IsWindows() {
|
|
|
|
|
cmd = exec.Command("cmd", "/c", task.GetData())
|
|
|
|
|
} else {
|
|
|
|
|
cmd = exec.Command("sh", "-c", task.GetData())
|
|
|
|
|
}
|
2021-01-28 22:59:35 -05:00
|
|
|
|
pg.AddProcess(cmd)
|
2021-01-28 21:40:57 -05:00
|
|
|
|
go func() {
|
|
|
|
|
select {
|
|
|
|
|
case <-timeout.C:
|
|
|
|
|
result.Data = "任务执行超时\n"
|
2021-01-28 22:59:35 -05:00
|
|
|
|
pg.Dispose()
|
2021-01-28 21:40:57 -05:00
|
|
|
|
close(endCh)
|
|
|
|
|
case <-endCh:
|
2021-01-28 20:22:24 -05:00
|
|
|
|
}
|
2021-01-28 21:40:57 -05:00
|
|
|
|
}()
|
|
|
|
|
output, err := cmd.Output()
|
|
|
|
|
if err != nil {
|
|
|
|
|
result.Data += fmt.Sprintf("%s\n%s", string(output), err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
close(endCh)
|
|
|
|
|
result.Data = string(output)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
result.Successful = true
|
|
|
|
|
}
|
2021-01-28 20:22:24 -05:00
|
|
|
|
result.Delay = float32(time.Now().Sub(startedAt).Seconds())
|
2021-01-16 05:04:47 -05:00
|
|
|
|
default:
|
|
|
|
|
log.Printf("Unknown action: %v", task)
|
2019-12-07 05:14:40 -05:00
|
|
|
|
}
|
2021-01-16 05:04:47 -05:00
|
|
|
|
client.ReportTask(ctx, &result)
|
2019-12-09 03:02:49 -05:00
|
|
|
|
}
|
2019-12-05 09:36:58 -05:00
|
|
|
|
|
2019-12-09 03:02:49 -05:00
|
|
|
|
func reportState() {
|
2021-01-13 09:30:28 -05:00
|
|
|
|
var lastReportHostInfo time.Time
|
2019-12-09 03:02:49 -05:00
|
|
|
|
var err error
|
2020-12-19 10:27:59 -05:00
|
|
|
|
defer log.Printf("reportState exit %v => %v", time.Now(), err)
|
2019-12-09 03:02:49 -05:00
|
|
|
|
for {
|
2020-12-19 10:27:59 -05:00
|
|
|
|
if client != nil {
|
2019-12-09 10:45:23 -05:00
|
|
|
|
monitor.TrackNetworkSpeed()
|
2021-01-15 11:45:49 -05:00
|
|
|
|
_, err = client.ReportSystemState(ctx, monitor.GetState(dao.ReportDelay).PB())
|
2019-12-09 05:14:31 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("reportState error %v", err)
|
|
|
|
|
time.Sleep(delayWhenError)
|
|
|
|
|
}
|
2021-01-13 09:30:28 -05:00
|
|
|
|
if lastReportHostInfo.Before(time.Now().Add(-10 * time.Minute)) {
|
|
|
|
|
lastReportHostInfo = time.Now()
|
2021-01-15 11:45:49 -05:00
|
|
|
|
client.ReportSystemInfo(ctx, monitor.GetHost().PB())
|
2021-01-13 09:30:28 -05:00
|
|
|
|
}
|
2019-12-05 10:42:20 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 09:36:58 -05:00
|
|
|
|
}
|