From c8c41ebdb3c9522868946676ffa187eccc0f7b35 Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 7 Dec 2024 11:04:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20GeoIP=20=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/agent/main.go | 90 ++++++++++---------- model/task.go | 2 +- pkg/monitor/myip.go | 2 +- proto/nezha.pb.go | 182 ++++++++++++++++++++++++++++------------- proto/nezha.proto | 3 + proto/nezha_grpc.pb.go | 37 +++++++++ 6 files changed, 214 insertions(+), 102 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 6db41ef5..db304c6b 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -46,15 +46,23 @@ import ( ) var ( - version = monitor.Version // 来自于 GoReleaser 的版本号 - arch string - executablePath string - defaultConfigPath = loadDefaultConfigPath() - client pb.NezhaServiceClient - initialized bool - dnsResolver = &net.Resolver{PreferGo: true} - agentConfig model.AgentConfig - httpClient = &http.Client{ + version = monitor.Version // 来自于 GoReleaser 的版本号 + arch string + executablePath string + defaultConfigPath = loadDefaultConfigPath() + client pb.NezhaServiceClient + initialized bool + agentConfig model.AgentConfig + prevDashboardBootTime uint64 // 面板上次启动时间 + geoipReported bool // 在面板重启后是否上报成功过 GeoIP + lastReportHostInfo time.Time + lastReportIPInfo time.Time + + hostStatus = new(atomic.Bool) + ipStatus = new(atomic.Bool) + + dnsResolver = &net.Resolver{PreferGo: true} + httpClient = &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, @@ -67,9 +75,6 @@ var ( Timeout: time.Second * 30, Transport: &http3.RoundTripper{}, } - - hostStatus = new(atomic.Bool) - ipStatus = new(atomic.Bool) ) var ( @@ -252,6 +257,7 @@ func run() { } var err error + var dashboardBootTimeReceipt *pb.Unit64Receipt var conn *grpc.ClientConn retry := func() { @@ -282,9 +288,9 @@ func run() { continue } client = pb.NewNezhaServiceClient(conn) - // 第一步注册 + timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) - _, err = client.ReportSystemInfo(timeOutCtx, monitor.GetHost().PB()) + dashboardBootTimeReceipt, err = client.ReportSystemInfo2(timeOutCtx, monitor.GetHost().PB()) if err != nil { printf("上报系统信息失败: %v", err) cancel() @@ -292,6 +298,8 @@ func run() { continue } cancel() + geoipReported = geoipReported && prevDashboardBootTime > 0 && dashboardBootTimeReceipt.GetData() == prevDashboardBootTime + prevDashboardBootTime = dashboardBootTimeReceipt.GetData() initialized = true errCh := make(chan error) @@ -313,6 +321,12 @@ func run() { } go reportStateDaemon(reportState, errCh) + go func() { + agentConfig.IPReportPeriod = 1000 + time.Sleep(time.Second * 5) + conn.Close() + }() + for i := 0; i < 2; i++ { err = <-errCh if i == 0 { @@ -436,19 +450,6 @@ func doTask(task *pb.Task) *pb.TaskResult { case model.TaskTypeNAT: handleNATTask(task) return nil - case model.TaskTypeReportHostInfo: - reportHost() - go func() { - monitor.GeoQueryIPChanged = true - for { - reported := reportGeoIP(agentConfig.UseIPv6CountryCode) - if reported { - break - } - time.Sleep(1 * time.Second) - } - }() - return nil case model.TaskTypeFM: handleFMTask(task) return nil @@ -462,10 +463,8 @@ func doTask(task *pb.Task) *pb.TaskResult { // reportStateDaemon 向server上报状态信息 func reportStateDaemon(stateClient pb.NezhaService_ReportSystemStateClient, errCh chan<- error) { - var lastReportHostInfo, lastReportIPInfo time.Time var err error for { - // 为了更准确的记录时段流量,inited 后再上传状态信息 lastReportHostInfo, lastReportIPInfo, err = reportState(stateClient, lastReportHostInfo, lastReportIPInfo) if err != nil { errCh <- fmt.Errorf("reportStateDaemon exit: %v", err) @@ -476,13 +475,15 @@ func reportStateDaemon(stateClient pb.NezhaService_ReportSystemStateClient, errC } func reportState(statClient pb.NezhaService_ReportSystemStateClient, host, ip time.Time) (time.Time, time.Time, error) { - monitor.TrackNetworkSpeed() - if err := statClient.Send(monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB()); err != nil { - return host, ip, err - } - _, err := statClient.Recv() - if err != nil { - return host, ip, err + if initialized { + monitor.TrackNetworkSpeed() + if err := statClient.Send(monitor.GetState(agentConfig.SkipConnectionCount, agentConfig.SkipProcsCount).PB()); err != nil { + return host, ip, err + } + _, err := statClient.Recv() + if err != nil { + return host, ip, err + } } // 每10分钟重新获取一次硬件信息 if host.Before(time.Now().Add(-10 * time.Minute)) { @@ -491,9 +492,10 @@ func reportState(statClient pb.NezhaService_ReportSystemStateClient, host, ip ti } } // 更新IP信息 - if time.Since(ip) > time.Second*time.Duration(agentConfig.IPReportPeriod) { - if reportGeoIP(agentConfig.UseIPv6CountryCode) { + if time.Since(ip) > time.Second*time.Duration(agentConfig.IPReportPeriod) || !geoipReported { + if reportGeoIP(agentConfig.UseIPv6CountryCode, !geoipReported) { ip = time.Now() + geoipReported = true } } return host, ip, nil @@ -506,13 +508,17 @@ func reportHost() bool { defer hostStatus.Store(false) if client != nil && initialized { - client.ReportSystemInfo(context.Background(), monitor.GetHost().PB()) + receipt, err := client.ReportSystemInfo2(context.Background(), monitor.GetHost().PB()) + if err == nil { + geoipReported = receipt.GetData() == prevDashboardBootTime + prevDashboardBootTime = receipt.GetData() + } } return true } -func reportGeoIP(use6 bool) bool { +func reportGeoIP(use6, forceUpdate bool) bool { if !ipStatus.CompareAndSwap(false, true) { return false } @@ -523,11 +529,9 @@ func reportGeoIP(use6 bool) bool { if pbg == nil { return false } - - if !monitor.GeoQueryIPChanged { + if !monitor.GeoQueryIPChanged && !forceUpdate { return true } - geoip, err := client.ReportGeoIP(context.Background(), pbg) if err == nil { monitor.CachedCountryCode = geoip.GetCountryCode() diff --git a/model/task.go b/model/task.go index 04a5ba98..33a21a81 100644 --- a/model/task.go +++ b/model/task.go @@ -11,7 +11,7 @@ const ( TaskTypeKeepalive TaskTypeTerminalGRPC TaskTypeNAT - TaskTypeReportHostInfo + TaskTypeReportHostInfoDeprecated TaskTypeFM ) diff --git a/pkg/monitor/myip.go b/pkg/monitor/myip.go index a6e01d51..9396b739 100644 --- a/pkg/monitor/myip.go +++ b/pkg/monitor/myip.go @@ -16,9 +16,9 @@ import ( var ( cfList = []string{ "https://blog.cloudflare.com/cdn-cgi/trace", + "https://developers.cloudflare.com/cdn-cgi/trace", "https://hostinger.com/cdn-cgi/trace", "https://ahrefs.com/cdn-cgi/trace", - "https://developers.cloudflare.com/cdn-cgi/trace", } CustomEndpoints []string GeoQueryIP, CachedCountryCode string diff --git a/proto/nezha.pb.go b/proto/nezha.pb.go index 72eb4efb..63c39871 100644 --- a/proto/nezha.pb.go +++ b/proto/nezha.pb.go @@ -566,6 +566,53 @@ func (x *Receipt) GetProced() bool { return false } +type Unit64Receipt struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Data uint64 `protobuf:"varint,1,opt,name=data,proto3" json:"data,omitempty"` +} + +func (x *Unit64Receipt) Reset() { + *x = Unit64Receipt{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_nezha_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Unit64Receipt) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Unit64Receipt) ProtoMessage() {} + +func (x *Unit64Receipt) ProtoReflect() protoreflect.Message { + mi := &file_proto_nezha_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Unit64Receipt.ProtoReflect.Descriptor instead. +func (*Unit64Receipt) Descriptor() ([]byte, []int) { + return file_proto_nezha_proto_rawDescGZIP(), []int{6} +} + +func (x *Unit64Receipt) GetData() uint64 { + if x != nil { + return x.Data + } + return 0 +} + type IOStreamData struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -577,7 +624,7 @@ type IOStreamData struct { func (x *IOStreamData) Reset() { *x = IOStreamData{} if protoimpl.UnsafeEnabled { - mi := &file_proto_nezha_proto_msgTypes[6] + mi := &file_proto_nezha_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -590,7 +637,7 @@ func (x *IOStreamData) String() string { func (*IOStreamData) ProtoMessage() {} func (x *IOStreamData) ProtoReflect() protoreflect.Message { - mi := &file_proto_nezha_proto_msgTypes[6] + mi := &file_proto_nezha_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -603,7 +650,7 @@ func (x *IOStreamData) ProtoReflect() protoreflect.Message { // Deprecated: Use IOStreamData.ProtoReflect.Descriptor instead. func (*IOStreamData) Descriptor() ([]byte, []int) { - return file_proto_nezha_proto_rawDescGZIP(), []int{6} + return file_proto_nezha_proto_rawDescGZIP(), []int{7} } func (x *IOStreamData) GetData() []byte { @@ -626,7 +673,7 @@ type GeoIP struct { func (x *GeoIP) Reset() { *x = GeoIP{} if protoimpl.UnsafeEnabled { - mi := &file_proto_nezha_proto_msgTypes[7] + mi := &file_proto_nezha_proto_msgTypes[8] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -639,7 +686,7 @@ func (x *GeoIP) String() string { func (*GeoIP) ProtoMessage() {} func (x *GeoIP) ProtoReflect() protoreflect.Message { - mi := &file_proto_nezha_proto_msgTypes[7] + mi := &file_proto_nezha_proto_msgTypes[8] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -652,7 +699,7 @@ func (x *GeoIP) ProtoReflect() protoreflect.Message { // Deprecated: Use GeoIP.ProtoReflect.Descriptor instead. func (*GeoIP) Descriptor() ([]byte, []int) { - return file_proto_nezha_proto_rawDescGZIP(), []int{7} + return file_proto_nezha_proto_rawDescGZIP(), []int{8} } func (x *GeoIP) GetUse6() bool { @@ -688,7 +735,7 @@ type IP struct { func (x *IP) Reset() { *x = IP{} if protoimpl.UnsafeEnabled { - mi := &file_proto_nezha_proto_msgTypes[8] + mi := &file_proto_nezha_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -701,7 +748,7 @@ func (x *IP) String() string { func (*IP) ProtoMessage() {} func (x *IP) ProtoReflect() protoreflect.Message { - mi := &file_proto_nezha_proto_msgTypes[8] + mi := &file_proto_nezha_proto_msgTypes[9] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -714,7 +761,7 @@ func (x *IP) ProtoReflect() protoreflect.Message { // Deprecated: Use IP.ProtoReflect.Descriptor instead. func (*IP) Descriptor() ([]byte, []int) { - return file_proto_nezha_proto_rawDescGZIP(), []int{8} + return file_proto_nezha_proto_rawDescGZIP(), []int{9} } func (x *IP) GetIpv4() string { @@ -809,37 +856,43 @@ var file_proto_nezha_proto_rawDesc = []byte{ 0x66, 0x75, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x66, 0x75, 0x6c, 0x22, 0x21, 0x0a, 0x07, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x06, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x64, 0x22, 0x22, 0x0a, 0x0c, 0x49, 0x4f, 0x53, 0x74, - 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x59, 0x0a, 0x05, - 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x36, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x08, 0x52, 0x04, 0x75, 0x73, 0x65, 0x36, 0x12, 0x19, 0x0a, 0x02, 0x69, 0x70, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x50, - 0x52, 0x02, 0x69, 0x70, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x5f, - 0x63, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x63, 0x6f, 0x75, 0x6e, - 0x74, 0x72, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x02, 0x49, 0x50, 0x12, 0x12, 0x0a, - 0x04, 0x69, 0x70, 0x76, 0x34, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x70, 0x76, - 0x34, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x70, 0x76, 0x36, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x04, 0x69, 0x70, 0x76, 0x36, 0x32, 0x98, 0x02, 0x0a, 0x0c, 0x4e, 0x65, 0x7a, 0x68, 0x61, 0x53, - 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x37, 0x0a, 0x11, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, - 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x0c, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, - 0x31, 0x0a, 0x10, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, - 0x6e, 0x66, 0x6f, 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74, - 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, - 0x22, 0x00, 0x12, 0x33, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x54, 0x61, 0x73, - 0x6b, 0x12, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, - 0x73, 0x75, 0x6c, 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, 0x61, 0x73, - 0x6b, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x3a, 0x0a, 0x08, 0x49, 0x4f, 0x53, 0x74, 0x72, - 0x65, 0x61, 0x6d, 0x12, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, - 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x22, 0x00, 0x28, - 0x01, 0x30, 0x01, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x47, 0x65, 0x6f, - 0x49, 0x50, 0x12, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, - 0x1a, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x22, 0x00, - 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x52, 0x06, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x64, 0x22, 0x23, 0x0a, 0x0d, 0x55, 0x6e, 0x69, 0x74, + 0x36, 0x34, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, + 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x22, 0x0a, + 0x0c, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, + 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, + 0x61, 0x22, 0x59, 0x0a, 0x05, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x73, + 0x65, 0x36, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x75, 0x73, 0x65, 0x36, 0x12, 0x19, + 0x0a, 0x02, 0x69, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x2e, 0x49, 0x50, 0x52, 0x02, 0x69, 0x70, 0x12, 0x21, 0x0a, 0x0c, 0x63, 0x6f, 0x75, + 0x6e, 0x74, 0x72, 0x79, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x0b, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x2c, 0x0a, 0x02, + 0x49, 0x50, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x70, 0x76, 0x34, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x69, 0x70, 0x76, 0x34, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x70, 0x76, 0x36, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x70, 0x76, 0x36, 0x32, 0xd2, 0x02, 0x0a, 0x0c, 0x4e, + 0x65, 0x7a, 0x68, 0x61, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x37, 0x0a, 0x11, 0x52, + 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x53, 0x74, 0x61, 0x74, 0x65, + 0x12, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x1a, 0x0e, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, + 0x28, 0x01, 0x30, 0x01, 0x12, 0x31, 0x0a, 0x10, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x79, + 0x73, 0x74, 0x65, 0x6d, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, + 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x12, 0x33, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x11, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x54, + 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x1a, 0x0b, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x3a, 0x0a, 0x08, + 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x1a, 0x13, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x49, 0x4f, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x44, 0x61, + 0x74, 0x61, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x2b, 0x0a, 0x0b, 0x52, 0x65, 0x70, 0x6f, + 0x72, 0x74, 0x47, 0x65, 0x6f, 0x49, 0x50, 0x12, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, + 0x47, 0x65, 0x6f, 0x49, 0x50, 0x1a, 0x0c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, + 0x6f, 0x49, 0x50, 0x22, 0x00, 0x12, 0x38, 0x0a, 0x11, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, + 0x79, 0x73, 0x74, 0x65, 0x6d, 0x49, 0x6e, 0x66, 0x6f, 0x32, 0x12, 0x0b, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, + 0x55, 0x6e, 0x69, 0x74, 0x36, 0x34, 0x52, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x22, 0x00, 0x42, + 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, } var ( @@ -854,7 +907,7 @@ func file_proto_nezha_proto_rawDescGZIP() []byte { return file_proto_nezha_proto_rawDescData } -var file_proto_nezha_proto_msgTypes = make([]protoimpl.MessageInfo, 9) +var file_proto_nezha_proto_msgTypes = make([]protoimpl.MessageInfo, 10) var file_proto_nezha_proto_goTypes = []any{ (*Host)(nil), // 0: proto.Host (*State)(nil), // 1: proto.State @@ -862,25 +915,28 @@ var file_proto_nezha_proto_goTypes = []any{ (*Task)(nil), // 3: proto.Task (*TaskResult)(nil), // 4: proto.TaskResult (*Receipt)(nil), // 5: proto.Receipt - (*IOStreamData)(nil), // 6: proto.IOStreamData - (*GeoIP)(nil), // 7: proto.GeoIP - (*IP)(nil), // 8: proto.IP + (*Unit64Receipt)(nil), // 6: proto.Unit64Receipt + (*IOStreamData)(nil), // 7: proto.IOStreamData + (*GeoIP)(nil), // 8: proto.GeoIP + (*IP)(nil), // 9: proto.IP } var file_proto_nezha_proto_depIdxs = []int32{ 2, // 0: proto.State.temperatures:type_name -> proto.State_SensorTemperature - 8, // 1: proto.GeoIP.ip:type_name -> proto.IP + 9, // 1: proto.GeoIP.ip:type_name -> proto.IP 1, // 2: proto.NezhaService.ReportSystemState:input_type -> proto.State 0, // 3: proto.NezhaService.ReportSystemInfo:input_type -> proto.Host 4, // 4: proto.NezhaService.RequestTask:input_type -> proto.TaskResult - 6, // 5: proto.NezhaService.IOStream:input_type -> proto.IOStreamData - 7, // 6: proto.NezhaService.ReportGeoIP:input_type -> proto.GeoIP - 5, // 7: proto.NezhaService.ReportSystemState:output_type -> proto.Receipt - 5, // 8: proto.NezhaService.ReportSystemInfo:output_type -> proto.Receipt - 3, // 9: proto.NezhaService.RequestTask:output_type -> proto.Task - 6, // 10: proto.NezhaService.IOStream:output_type -> proto.IOStreamData - 7, // 11: proto.NezhaService.ReportGeoIP:output_type -> proto.GeoIP - 7, // [7:12] is the sub-list for method output_type - 2, // [2:7] is the sub-list for method input_type + 7, // 5: proto.NezhaService.IOStream:input_type -> proto.IOStreamData + 8, // 6: proto.NezhaService.ReportGeoIP:input_type -> proto.GeoIP + 0, // 7: proto.NezhaService.ReportSystemInfo2:input_type -> proto.Host + 5, // 8: proto.NezhaService.ReportSystemState:output_type -> proto.Receipt + 5, // 9: proto.NezhaService.ReportSystemInfo:output_type -> proto.Receipt + 3, // 10: proto.NezhaService.RequestTask:output_type -> proto.Task + 7, // 11: proto.NezhaService.IOStream:output_type -> proto.IOStreamData + 8, // 12: proto.NezhaService.ReportGeoIP:output_type -> proto.GeoIP + 6, // 13: proto.NezhaService.ReportSystemInfo2:output_type -> proto.Unit64Receipt + 8, // [8:14] is the sub-list for method output_type + 2, // [2:8] is the sub-list for method input_type 2, // [2:2] is the sub-list for extension type_name 2, // [2:2] is the sub-list for extension extendee 0, // [0:2] is the sub-list for field type_name @@ -965,7 +1021,7 @@ func file_proto_nezha_proto_init() { } } file_proto_nezha_proto_msgTypes[6].Exporter = func(v any, i int) any { - switch v := v.(*IOStreamData); i { + switch v := v.(*Unit64Receipt); i { case 0: return &v.state case 1: @@ -977,7 +1033,7 @@ func file_proto_nezha_proto_init() { } } file_proto_nezha_proto_msgTypes[7].Exporter = func(v any, i int) any { - switch v := v.(*GeoIP); i { + switch v := v.(*IOStreamData); i { case 0: return &v.state case 1: @@ -989,6 +1045,18 @@ func file_proto_nezha_proto_init() { } } file_proto_nezha_proto_msgTypes[8].Exporter = func(v any, i int) any { + switch v := v.(*GeoIP); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_nezha_proto_msgTypes[9].Exporter = func(v any, i int) any { switch v := v.(*IP); i { case 0: return &v.state @@ -1007,7 +1075,7 @@ func file_proto_nezha_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_proto_nezha_proto_rawDesc, NumEnums: 0, - NumMessages: 9, + NumMessages: 10, NumExtensions: 0, NumServices: 1, }, diff --git a/proto/nezha.proto b/proto/nezha.proto index 326e496f..8401bc19 100644 --- a/proto/nezha.proto +++ b/proto/nezha.proto @@ -9,6 +9,7 @@ service NezhaService { rpc RequestTask(stream TaskResult) returns (stream Task) {} rpc IOStream(stream IOStreamData) returns (stream IOStreamData) {} rpc ReportGeoIP(GeoIP) returns (GeoIP) {} + rpc ReportSystemInfo2(Host) returns (Unit64Receipt) {} } message Host { @@ -66,6 +67,8 @@ message TaskResult { message Receipt { bool proced = 1; } +message Unit64Receipt { uint64 data = 1; } + message IOStreamData { bytes data = 1; } message GeoIP { diff --git a/proto/nezha_grpc.pb.go b/proto/nezha_grpc.pb.go index 16694754..bda7c771 100644 --- a/proto/nezha_grpc.pb.go +++ b/proto/nezha_grpc.pb.go @@ -24,6 +24,7 @@ const ( NezhaService_RequestTask_FullMethodName = "/proto.NezhaService/RequestTask" NezhaService_IOStream_FullMethodName = "/proto.NezhaService/IOStream" NezhaService_ReportGeoIP_FullMethodName = "/proto.NezhaService/ReportGeoIP" + NezhaService_ReportSystemInfo2_FullMethodName = "/proto.NezhaService/ReportSystemInfo2" ) // NezhaServiceClient is the client API for NezhaService service. @@ -35,6 +36,7 @@ type NezhaServiceClient interface { RequestTask(ctx context.Context, opts ...grpc.CallOption) (NezhaService_RequestTaskClient, error) IOStream(ctx context.Context, opts ...grpc.CallOption) (NezhaService_IOStreamClient, error) ReportGeoIP(ctx context.Context, in *GeoIP, opts ...grpc.CallOption) (*GeoIP, error) + ReportSystemInfo2(ctx context.Context, in *Host, opts ...grpc.CallOption) (*Unit64Receipt, error) } type nezhaServiceClient struct { @@ -156,6 +158,15 @@ func (c *nezhaServiceClient) ReportGeoIP(ctx context.Context, in *GeoIP, opts .. return out, nil } +func (c *nezhaServiceClient) ReportSystemInfo2(ctx context.Context, in *Host, opts ...grpc.CallOption) (*Unit64Receipt, error) { + out := new(Unit64Receipt) + err := c.cc.Invoke(ctx, NezhaService_ReportSystemInfo2_FullMethodName, in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // NezhaServiceServer is the server API for NezhaService service. // All implementations should embed UnimplementedNezhaServiceServer // for forward compatibility @@ -165,6 +176,7 @@ type NezhaServiceServer interface { RequestTask(NezhaService_RequestTaskServer) error IOStream(NezhaService_IOStreamServer) error ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error) + ReportSystemInfo2(context.Context, *Host) (*Unit64Receipt, error) } // UnimplementedNezhaServiceServer should be embedded to have forward compatible implementations. @@ -186,6 +198,9 @@ func (UnimplementedNezhaServiceServer) IOStream(NezhaService_IOStreamServer) err func (UnimplementedNezhaServiceServer) ReportGeoIP(context.Context, *GeoIP) (*GeoIP, error) { return nil, status.Errorf(codes.Unimplemented, "method ReportGeoIP not implemented") } +func (UnimplementedNezhaServiceServer) ReportSystemInfo2(context.Context, *Host) (*Unit64Receipt, error) { + return nil, status.Errorf(codes.Unimplemented, "method ReportSystemInfo2 not implemented") +} // UnsafeNezhaServiceServer may be embedded to opt out of forward compatibility for this service. // Use of this interface is not recommended, as added methods to NezhaServiceServer will @@ -312,6 +327,24 @@ func _NezhaService_ReportGeoIP_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } +func _NezhaService_ReportSystemInfo2_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Host) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(NezhaServiceServer).ReportSystemInfo2(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: NezhaService_ReportSystemInfo2_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(NezhaServiceServer).ReportSystemInfo2(ctx, req.(*Host)) + } + return interceptor(ctx, in, info, handler) +} + // NezhaService_ServiceDesc is the grpc.ServiceDesc for NezhaService service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -327,6 +360,10 @@ var NezhaService_ServiceDesc = grpc.ServiceDesc{ MethodName: "ReportGeoIP", Handler: _NezhaService_ReportGeoIP_Handler, }, + { + MethodName: "ReportSystemInfo2", + Handler: _NezhaService_ReportSystemInfo2_Handler, + }, }, Streams: []grpc.StreamDesc{ {