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

View File

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