From 5b5e006b97a8e86ed74ac273f85fd42acf2002e1 Mon Sep 17 00:00:00 2001 From: naiba Date: Sat, 7 Dec 2024 11:06:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20GeoIP=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/dashboard/main.go | 18 ---- cmd/dashboard/rpc/rpc.go | 2 +- model/service.go | 2 +- proto/nezha.pb.go | 182 ++++++++++++++++++++++----------- proto/nezha.proto | 3 + proto/nezha_grpc.pb.go | 37 +++++++ service/rpc/nezha.go | 14 ++- service/singleton/singleton.go | 1 + 8 files changed, 180 insertions(+), 79 deletions(-) diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 8c561d3..f0c216a 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -3,7 +3,6 @@ package main import ( "context" "embed" - _ "embed" "flag" "fmt" "log" @@ -130,8 +129,6 @@ func main() { http2Server := &http2.Server{} muxServer := &http.Server{Handler: h2c.NewHandler(muxHandler, http2Server), ReadHeaderTimeout: time.Second * 5} - go dispatchReportInfoTask() - if err := graceful.Graceful(func() error { log.Printf("NEZHA>> Dashboard::START ON %s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort) return muxServer.Serve(l) @@ -145,21 +142,6 @@ func main() { } } -func dispatchReportInfoTask() { - time.Sleep(time.Second * 15) - singleton.ServerLock.RLock() - defer singleton.ServerLock.RUnlock() - for _, server := range singleton.ServerList { - if server == nil || server.TaskStream == nil { - continue - } - server.TaskStream.Send(&proto.Task{ - Type: model.TaskTypeReportHostInfo, - Data: "", - }) - } -} - func newHTTPandGRPCMux(httpHandler http.Handler, grpcHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { natConfig := singleton.GetNATConfigByDomain(r.Host) diff --git a/cmd/dashboard/rpc/rpc.go b/cmd/dashboard/rpc/rpc.go index 35ec1b5..dd95637 100644 --- a/cmd/dashboard/rpc/rpc.go +++ b/cmd/dashboard/rpc/rpc.go @@ -113,7 +113,7 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Service) { } func DispatchKeepalive() { - singleton.Cron.AddFunc("@every 30s", func() { + singleton.Cron.AddFunc("@every 20s", func() { singleton.SortedServerLock.RLock() defer singleton.SortedServerLock.RUnlock() for i := 0; i < len(singleton.SortedServerList); i++ { diff --git a/model/service.go b/model/service.go index 2a423cd..3c828c7 100644 --- a/model/service.go +++ b/model/service.go @@ -22,7 +22,7 @@ const ( TaskTypeKeepalive TaskTypeTerminalGRPC TaskTypeNAT - TaskTypeReportHostInfo + TaskTypeReportHostInfoDeprecated TaskTypeFM ) diff --git a/proto/nezha.pb.go b/proto/nezha.pb.go index 72eb4ef..63c3987 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 326e496..8401bc1 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 1669475..bda7c77 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{ { diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 990011a..17c5749 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -116,11 +116,11 @@ func (s *NezhaHandler) ReportSystemState(stream pb.NezhaService_ReportSystemStat } } -func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Receipt, error) { +func (s *NezhaHandler) onReportSystemInfo(c context.Context, r *pb.Host) error { var clientID uint64 var err error if clientID, err = s.Auth.Check(c); err != nil { - return nil, err + return err } host := model.PB2Host(r) singleton.ServerLock.RLock() @@ -137,9 +137,19 @@ func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Rece } singleton.ServerList[clientID].Host = &host + return nil +} + +func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Receipt, error) { + s.onReportSystemInfo(c, r) return &pb.Receipt{Proced: true}, nil } +func (s *NezhaHandler) ReportSystemInfo2(c context.Context, r *pb.Host) (*pb.Unit64Receipt, error) { + s.onReportSystemInfo(c, r) + return &pb.Unit64Receipt{Data: singleton.DashboardBootTime}, nil +} + func (s *NezhaHandler) IOStream(stream pb.NezhaService_IOStreamServer) error { if _, err := s.Auth.Check(stream.Context()); err != nil { return err diff --git a/service/singleton/singleton.go b/service/singleton/singleton.go index f67fef2..2f155fe 100644 --- a/service/singleton/singleton.go +++ b/service/singleton/singleton.go @@ -33,6 +33,7 @@ var ( Community: true, }, } + DashboardBootTime = uint64(time.Now().Unix()) ) func InitTimezoneAndCache() {