2019-12-08 16:59:58 +08:00
|
|
|
|
package rpc
|
2019-12-07 18:14:40 +08:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-01-13 22:30:28 +08:00
|
|
|
|
"fmt"
|
2020-10-24 21:29:05 +08:00
|
|
|
|
"time"
|
2019-12-07 18:14:40 +08:00
|
|
|
|
|
2020-11-11 10:07:45 +08:00
|
|
|
|
"github.com/naiba/nezha/model"
|
2021-06-30 18:15:53 +08:00
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2020-11-11 10:07:45 +08:00
|
|
|
|
pb "github.com/naiba/nezha/proto"
|
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
2019-12-07 18:14:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NezhaHandler struct {
|
|
|
|
|
Auth *AuthHandler
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 00:45:49 +08:00
|
|
|
|
func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Receipt, error) {
|
2019-12-09 18:14:31 +08:00
|
|
|
|
var err error
|
2021-01-19 09:59:04 +08:00
|
|
|
|
var clientID uint64
|
|
|
|
|
if clientID, err = s.Auth.Check(c); err != nil {
|
2021-01-16 00:45:49 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-09-27 21:18:09 +08:00
|
|
|
|
if r.GetType() == model.TaskTypeCommand {
|
2021-01-19 09:59:04 +08:00
|
|
|
|
// 处理上报的计划任务
|
|
|
|
|
dao.CronLock.RLock()
|
2021-01-23 15:32:04 +08:00
|
|
|
|
defer dao.CronLock.RUnlock()
|
2021-01-19 09:59:04 +08:00
|
|
|
|
cr := dao.Crons[r.GetId()]
|
2021-01-23 15:32:04 +08:00
|
|
|
|
if cr != nil {
|
2021-05-27 20:48:12 +08:00
|
|
|
|
dao.ServerLock.RLock()
|
|
|
|
|
defer dao.ServerLock.RUnlock()
|
2021-01-23 15:32:04 +08:00
|
|
|
|
if cr.PushSuccessful && r.GetSuccessful() {
|
2021-09-27 21:18:09 +08:00
|
|
|
|
dao.SendNotification(fmt.Sprintf("[任务成功] %s ,服务器:%s,日志:\n%s", cr.Name, dao.ServerList[clientID].Name, r.GetData()), false)
|
2021-01-23 15:32:04 +08:00
|
|
|
|
}
|
|
|
|
|
if !r.GetSuccessful() {
|
2021-09-27 21:18:09 +08:00
|
|
|
|
dao.SendNotification(fmt.Sprintf("[任务失败] %s ,服务器:%s,日志:\n%s", cr.Name, dao.ServerList[clientID].Name, r.GetData()), false)
|
2021-01-23 15:32:04 +08:00
|
|
|
|
}
|
|
|
|
|
dao.DB.Model(cr).Updates(model.Cron{
|
|
|
|
|
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
|
|
|
|
|
LastResult: r.GetSuccessful(),
|
|
|
|
|
})
|
2021-01-19 09:59:04 +08:00
|
|
|
|
}
|
2021-09-27 21:18:09 +08:00
|
|
|
|
} else if model.IsServiceSentinelNeeded(r.GetType()) {
|
|
|
|
|
dao.ServiceSentinelShared.Dispatch(dao.ReportData{
|
|
|
|
|
Data: r,
|
|
|
|
|
Reporter: clientID,
|
|
|
|
|
})
|
2021-01-16 00:45:49 +08:00
|
|
|
|
}
|
2019-12-07 18:14:40 +08:00
|
|
|
|
return &pb.Receipt{Proced: true}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 00:45:49 +08:00
|
|
|
|
func (s *NezhaHandler) RequestTask(h *pb.Host, stream pb.NezhaService_RequestTaskServer) error {
|
2021-01-08 21:04:50 +08:00
|
|
|
|
var clientID uint64
|
2019-12-09 18:14:31 +08:00
|
|
|
|
var err error
|
|
|
|
|
if clientID, err = s.Auth.Check(stream.Context()); err != nil {
|
2019-12-07 18:14:40 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2019-12-10 17:57:57 +08:00
|
|
|
|
closeCh := make(chan error)
|
2021-01-17 22:05:59 +08:00
|
|
|
|
dao.ServerLock.RLock()
|
2021-11-11 21:40:13 +08:00
|
|
|
|
// 修复不断的请求 task 但是没有 return 导致内存泄漏
|
|
|
|
|
if dao.ServerList[clientID].TaskClose != nil {
|
|
|
|
|
close(dao.ServerList[clientID].TaskClose)
|
|
|
|
|
}
|
2021-01-16 00:45:49 +08:00
|
|
|
|
dao.ServerList[clientID].TaskStream = stream
|
|
|
|
|
dao.ServerList[clientID].TaskClose = closeCh
|
2021-01-17 22:05:59 +08:00
|
|
|
|
dao.ServerLock.RUnlock()
|
2021-04-17 23:36:37 +08:00
|
|
|
|
return <-closeCh
|
2019-12-07 18:14:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 00:45:49 +08: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 13:45:06 +08:00
|
|
|
|
dao.ServerList[clientID].LastActive = time.Now()
|
2021-01-16 00:45:49 +08:00
|
|
|
|
dao.ServerList[clientID].State = &state
|
2021-07-14 23:53:37 +08:00
|
|
|
|
|
|
|
|
|
// 如果从未记录过,先打点,等到小时时间点时入库
|
|
|
|
|
if dao.ServerList[clientID].PrevHourlyTransferIn == 0 || dao.ServerList[clientID].PrevHourlyTransferOut == 0 {
|
|
|
|
|
dao.ServerList[clientID].PrevHourlyTransferIn = int64(state.NetInTransfer)
|
|
|
|
|
dao.ServerList[clientID].PrevHourlyTransferOut = int64(state.NetOutTransfer)
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 00:45:49 +08:00
|
|
|
|
return &pb.Receipt{Proced: true}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Receipt, error) {
|
2021-01-08 21:04:50 +08:00
|
|
|
|
var clientID uint64
|
2019-12-09 18:14:31 +08:00
|
|
|
|
var err error
|
|
|
|
|
if clientID, err = s.Auth.Check(c); err != nil {
|
2019-12-07 18:14:40 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-12-13 17:56:14 +08:00
|
|
|
|
host := model.PB2Host(r)
|
2019-12-10 17:57:57 +08:00
|
|
|
|
dao.ServerLock.RLock()
|
|
|
|
|
defer dao.ServerLock.RUnlock()
|
2021-01-16 11:23:42 +08:00
|
|
|
|
if dao.Conf.EnableIPChangeNotification &&
|
2021-06-22 14:05:36 +08:00
|
|
|
|
((dao.Conf.Cover == model.ConfigCoverAll && !dao.Conf.IgnoredIPNotificationServerIDs[clientID]) ||
|
|
|
|
|
(dao.Conf.Cover == model.ConfigCoverIgnoreAll && dao.Conf.IgnoredIPNotificationServerIDs[clientID])) &&
|
2021-01-16 11:23:42 +08:00
|
|
|
|
dao.ServerList[clientID].Host != nil &&
|
2021-01-13 22:30:28 +08:00
|
|
|
|
dao.ServerList[clientID].Host.IP != "" &&
|
|
|
|
|
host.IP != "" &&
|
|
|
|
|
dao.ServerList[clientID].Host.IP != host.IP {
|
2021-01-24 09:41:35 +08:00
|
|
|
|
dao.SendNotification(fmt.Sprintf(
|
2021-09-27 21:18:09 +08:00
|
|
|
|
"[IP变更] %s ,旧IP:%s,新IP:%s。",
|
2021-06-30 18:15:53 +08:00
|
|
|
|
dao.ServerList[clientID].Name, utils.IPDesensitize(dao.ServerList[clientID].Host.IP), utils.IPDesensitize(host.IP)), true)
|
2021-01-13 22:30:28 +08:00
|
|
|
|
}
|
2021-07-16 18:09:50 +08:00
|
|
|
|
|
|
|
|
|
// 判断是否是机器重启,如果是机器重启要录入最后记录的流量里面
|
|
|
|
|
if dao.ServerList[clientID].Host.BootTime < host.BootTime {
|
|
|
|
|
dao.ServerList[clientID].PrevHourlyTransferIn = dao.ServerList[clientID].PrevHourlyTransferIn - int64(dao.ServerList[clientID].State.NetInTransfer)
|
|
|
|
|
dao.ServerList[clientID].PrevHourlyTransferOut = dao.ServerList[clientID].PrevHourlyTransferOut - int64(dao.ServerList[clientID].State.NetOutTransfer)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 17:56:14 +08:00
|
|
|
|
dao.ServerList[clientID].Host = &host
|
2019-12-07 18:14:40 +08:00
|
|
|
|
return &pb.Receipt{Proced: true}, nil
|
|
|
|
|
}
|