nezha/service/rpc/nezha.go

103 lines
3.2 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package rpc
2019-12-07 05:14:40 -05:00
import (
"context"
"fmt"
2020-10-24 09:29:05 -04:00
"time"
2019-12-07 05:14:40 -05:00
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/model"
"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"
2019-12-07 05:14:40 -05:00
)
type NezhaHandler struct {
Auth *AuthHandler
}
func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Receipt, error) {
2019-12-09 05:14:31 -05:00
var err error
var clientID uint64
if clientID, err = s.Auth.Check(c); err != nil {
return nil, err
}
if r.GetType() != model.TaskTypeCommand {
dao.ServiceSentinelShared.Dispatch(dao.ReportData{
Data: r,
Reporter: clientID,
})
} else {
// 处理上报的计划任务
dao.CronLock.RLock()
defer dao.CronLock.RUnlock()
cr := dao.Crons[r.GetId()]
if cr != nil {
2021-05-27 08:48:12 -04:00
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
if cr.PushSuccessful && r.GetSuccessful() {
2021-05-27 08:48:12 -04:00
dao.SendNotification(fmt.Sprintf("成功计划任务:%s ,服务器:%s日志\n%s", cr.Name, dao.ServerList[clientID].Name, r.GetData()), false)
}
if !r.GetSuccessful() {
2021-05-27 08:48:12 -04:00
dao.SendNotification(fmt.Sprintf("失败计划任务:%s ,服务器:%s日志\n%s", cr.Name, dao.ServerList[clientID].Name, r.GetData()), false)
}
dao.DB.Model(cr).Updates(model.Cron{
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
LastResult: r.GetSuccessful(),
})
}
}
2019-12-07 05:14:40 -05:00
return &pb.Receipt{Proced: true}, nil
}
func (s *NezhaHandler) RequestTask(h *pb.Host, stream pb.NezhaService_RequestTaskServer) error {
2021-01-08 08:04:50 -05:00
var clientID uint64
2019-12-09 05:14:31 -05:00
var err error
if clientID, err = s.Auth.Check(stream.Context()); err != nil {
2019-12-07 05:14:40 -05:00
return err
}
2019-12-10 04:57:57 -05:00
closeCh := make(chan error)
2021-01-17 09:05:59 -05:00
dao.ServerLock.RLock()
dao.ServerList[clientID].TaskStream = stream
dao.ServerList[clientID].TaskClose = closeCh
2021-01-17 09:05:59 -05:00
dao.ServerLock.RUnlock()
return <-closeCh
2019-12-07 05:14:40 -05:00
}
func (s *NezhaHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.Receipt, error) {
var clientID uint64
var err error
if clientID, err = s.Auth.Check(c); err != nil {
return nil, err
}
state := model.PB2State(r)
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
2021-01-18 00:45:06 -05:00
dao.ServerList[clientID].LastActive = time.Now()
dao.ServerList[clientID].State = &state
return &pb.Receipt{Proced: true}, nil
}
func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Receipt, error) {
2021-01-08 08:04:50 -05:00
var clientID uint64
2019-12-09 05:14:31 -05:00
var err error
if clientID, err = s.Auth.Check(c); err != nil {
2019-12-07 05:14:40 -05:00
return nil, err
}
2019-12-13 04:56:14 -05:00
host := model.PB2Host(r)
2019-12-10 04:57:57 -05:00
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
if dao.Conf.EnableIPChangeNotification &&
2021-06-22 02:05:36 -04:00
((dao.Conf.Cover == model.ConfigCoverAll && !dao.Conf.IgnoredIPNotificationServerIDs[clientID]) ||
(dao.Conf.Cover == model.ConfigCoverIgnoreAll && dao.Conf.IgnoredIPNotificationServerIDs[clientID])) &&
dao.ServerList[clientID].Host != nil &&
dao.ServerList[clientID].Host.IP != "" &&
host.IP != "" &&
dao.ServerList[clientID].Host.IP != host.IP {
dao.SendNotification(fmt.Sprintf(
2021-01-16 01:11:51 -05:00
"IP变更提醒 服务器:%s 旧IP%s新IP%s。",
dao.ServerList[clientID].Name, utils.IPDesensitize(dao.ServerList[clientID].Host.IP), utils.IPDesensitize(host.IP)), true)
}
2019-12-13 04:56:14 -05:00
dao.ServerList[clientID].Host = &host
2019-12-07 05:14:40 -05:00
return &pb.Receipt{Proced: true}, nil
}