feat: 在面板重启后及时更新host信息

This commit is contained in:
naiba 2024-08-02 22:02:11 +08:00
parent f837a6fdc8
commit 1f7706201b
2 changed files with 40 additions and 31 deletions

View File

@ -60,7 +60,7 @@ var (
version string
arch string
client pb.NezhaServiceClient
inited bool
initialized bool
resolver = &net.Resolver{PreferGo: true}
)
@ -222,7 +222,7 @@ func run() {
go pty.DownloadDependency()
}
// 上报服务器信息
go reportState()
go reportStateDaemon()
// 更新IP信息
go monitor.UpdateIP(agentCliParam.UseIPv6CountryCode, agentCliParam.IPReportPeriod)
@ -240,7 +240,7 @@ func run() {
var conn *grpc.ClientConn
retry := func() {
inited = false
initialized = false
println("Error to close connection ...")
if conn != nil {
conn.Close()
@ -280,7 +280,7 @@ func run() {
continue
}
cancel()
inited = true
initialized = true
// 执行 Task
tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB())
if err != nil {
@ -382,7 +382,7 @@ func doTask(task *pb.Task) {
result.Id = task.GetId()
result.Type = task.GetType()
switch task.GetType() {
case model.TaskTypeHTTPGET:
case model.TaskTypeHTTPGet:
handleHttpGetTask(task, &result)
case model.TaskTypeICMPPing:
handleIcmpPingTask(task, &result)
@ -398,6 +398,9 @@ func doTask(task *pb.Task) {
case model.TaskTypeNAT:
handleNATTask(task)
return
case model.TaskTypeReportHostInfo:
reportState(time.Time{})
return
case model.TaskTypeKeepalive:
return
default:
@ -407,17 +410,23 @@ func doTask(task *pb.Task) {
client.ReportTask(context.Background(), &result)
}
// reportState 向server上报状态信息
func reportState() {
// reportStateDaemon 向server上报状态信息
func reportStateDaemon() {
var lastReportHostInfo time.Time
var err error
defer println("reportState exit", time.Now(), "=>", err)
for {
// 为了更准确的记录时段流量inited 后再上传状态信息
if client != nil && inited {
lastReportHostInfo = reportState(lastReportHostInfo)
time.Sleep(time.Second * time.Duration(agentCliParam.ReportDelay))
}
}
func reportState(lastReportHostInfo time.Time) time.Time {
if client != nil && initialized {
monitor.TrackNetworkSpeed()
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState(agentCliParam.SkipConnectionCount, agentCliParam.SkipProcsCount).PB())
_, err := client.ReportSystemState(timeOutCtx, monitor.GetState(agentCliParam.SkipConnectionCount, agentCliParam.SkipProcsCount).PB())
cancel()
if err != nil {
println("reportState error", err)
@ -435,8 +444,7 @@ func reportState() {
}
}
}
time.Sleep(time.Second * time.Duration(agentCliParam.ReportDelay))
}
return lastReportHostInfo
}
// doSelfUpdate 执行更新检查 如果更新成功则会结束进程

View File

@ -2,7 +2,7 @@ package model
const (
_ = iota
TaskTypeHTTPGET
TaskTypeHTTPGet
TaskTypeICMPPing
TaskTypeTCPPing
TaskTypeCommand
@ -11,6 +11,7 @@ const (
TaskTypeKeepalive
TaskTypeTerminalGRPC
TaskTypeNAT
TaskTypeReportHostInfo
)
type TerminalTask struct {