From 8ce44343c802df0c35f33ee02340c10effee03b7 Mon Sep 17 00:00:00 2001 From: Patrick Schork Date: Tue, 23 Apr 2024 12:50:14 -0700 Subject: [PATCH] Refactor --- api/grpc/scanner/scanner.pb.go | 321 ------------------ api/grpc/scanner/scanner_grpc.pb.go | 137 -------- scanner/Makefile | 15 - scanner/cmd/Dockerfile | 21 -- scanner/cmd/main.go | 66 ---- tools/npc/.env | 5 + tools/npc/.gitignore | 1 + tools/npc/Makefile | 18 + {scanner => tools/npc}/README.md | 0 tools/npc/cmd/Dockerfile | 19 ++ tools/npc/cmd/main.go | 66 ++++ {scanner => tools/npc}/config.go | 10 +- {scanner => tools/npc}/flags/flags.go | 4 +- {scanner => tools/npc}/grpc/server.go | 19 +- {scanner => tools/npc}/metrics.go | 25 +- .../scanner.proto => tools/npc/npc.proto | 4 +- {scanner => tools/npc}/scanner.go | 10 +- 17 files changed, 138 insertions(+), 603 deletions(-) delete mode 100644 api/grpc/scanner/scanner.pb.go delete mode 100644 api/grpc/scanner/scanner_grpc.pb.go delete mode 100644 scanner/Makefile delete mode 100644 scanner/cmd/Dockerfile delete mode 100644 scanner/cmd/main.go create mode 100644 tools/npc/.env create mode 100644 tools/npc/.gitignore create mode 100644 tools/npc/Makefile rename {scanner => tools/npc}/README.md (100%) create mode 100644 tools/npc/cmd/Dockerfile create mode 100644 tools/npc/cmd/main.go rename {scanner => tools/npc}/config.go (84%) rename {scanner => tools/npc}/flags/flags.go (96%) rename {scanner => tools/npc}/grpc/server.go (90%) rename {scanner => tools/npc}/metrics.go (74%) rename api/proto/scanner/scanner.proto => tools/npc/npc.proto (82%) rename {scanner => tools/npc}/scanner.go (63%) diff --git a/api/grpc/scanner/scanner.pb.go b/api/grpc/scanner/scanner.pb.go deleted file mode 100644 index 60ef27f751..0000000000 --- a/api/grpc/scanner/scanner.pb.go +++ /dev/null @@ -1,321 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.26.0 -// protoc v5.26.1 -// source: scanner/scanner.proto - -package scanner - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type PortCheckRequest struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Port uint32 `protobuf:"varint,1,opt,name=port,proto3" json:"port,omitempty"` -} - -func (x *PortCheckRequest) Reset() { - *x = PortCheckRequest{} - if protoimpl.UnsafeEnabled { - mi := &file_scanner_scanner_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *PortCheckRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*PortCheckRequest) ProtoMessage() {} - -func (x *PortCheckRequest) ProtoReflect() protoreflect.Message { - mi := &file_scanner_scanner_proto_msgTypes[0] - 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 PortCheckRequest.ProtoReflect.Descriptor instead. -func (*PortCheckRequest) Descriptor() ([]byte, []int) { - return file_scanner_scanner_proto_rawDescGZIP(), []int{0} -} - -func (x *PortCheckRequest) GetPort() uint32 { - if x != nil { - return x.Port - } - return 0 -} - -type HostPortCheckRequest struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Port uint32 `protobuf:"varint,1,opt,name=port,proto3" json:"port,omitempty"` - Host string `protobuf:"bytes,2,opt,name=host,proto3" json:"host,omitempty"` -} - -func (x *HostPortCheckRequest) Reset() { - *x = HostPortCheckRequest{} - if protoimpl.UnsafeEnabled { - mi := &file_scanner_scanner_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *HostPortCheckRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*HostPortCheckRequest) ProtoMessage() {} - -func (x *HostPortCheckRequest) ProtoReflect() protoreflect.Message { - mi := &file_scanner_scanner_proto_msgTypes[1] - 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 HostPortCheckRequest.ProtoReflect.Descriptor instead. -func (*HostPortCheckRequest) Descriptor() ([]byte, []int) { - return file_scanner_scanner_proto_rawDescGZIP(), []int{1} -} - -func (x *HostPortCheckRequest) GetPort() uint32 { - if x != nil { - return x.Port - } - return 0 -} - -func (x *HostPortCheckRequest) GetHost() string { - if x != nil { - return x.Host - } - return "" -} - -type PortCheckResponse struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Port uint32 `protobuf:"varint,1,opt,name=port,proto3" json:"port,omitempty"` - Host string `protobuf:"bytes,2,opt,name=host,proto3" json:"host,omitempty"` - Status string `protobuf:"bytes,3,opt,name=status,proto3" json:"status,omitempty"` - Msg string `protobuf:"bytes,4,opt,name=msg,proto3" json:"msg,omitempty"` -} - -func (x *PortCheckResponse) Reset() { - *x = PortCheckResponse{} - if protoimpl.UnsafeEnabled { - mi := &file_scanner_scanner_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *PortCheckResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*PortCheckResponse) ProtoMessage() {} - -func (x *PortCheckResponse) ProtoReflect() protoreflect.Message { - mi := &file_scanner_scanner_proto_msgTypes[2] - 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 PortCheckResponse.ProtoReflect.Descriptor instead. -func (*PortCheckResponse) Descriptor() ([]byte, []int) { - return file_scanner_scanner_proto_rawDescGZIP(), []int{2} -} - -func (x *PortCheckResponse) GetPort() uint32 { - if x != nil { - return x.Port - } - return 0 -} - -func (x *PortCheckResponse) GetHost() string { - if x != nil { - return x.Host - } - return "" -} - -func (x *PortCheckResponse) GetStatus() string { - if x != nil { - return x.Status - } - return "" -} - -func (x *PortCheckResponse) GetMsg() string { - if x != nil { - return x.Msg - } - return "" -} - -var File_scanner_scanner_proto protoreflect.FileDescriptor - -var file_scanner_scanner_proto_rawDesc = []byte{ - 0x0a, 0x15, 0x73, 0x63, 0x61, 0x6e, 0x6e, 0x65, 0x72, 0x2f, 0x73, 0x63, 0x61, 0x6e, 0x6e, 0x65, - 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x73, 0x63, 0x61, 0x6e, 0x6e, 0x65, 0x72, - 0x22, 0x26, 0x0a, 0x10, 0x50, 0x6f, 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x0d, 0x52, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x22, 0x3e, 0x0a, 0x14, 0x48, 0x6f, 0x73, 0x74, - 0x50, 0x6f, 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, - 0x70, 0x6f, 0x72, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x22, 0x65, 0x0a, 0x11, 0x50, 0x6f, 0x72, 0x74, - 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, - 0x04, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x70, 0x6f, 0x72, - 0x74, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x6f, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x04, 0x68, 0x6f, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, - 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x10, 0x0a, - 0x03, 0x6d, 0x73, 0x67, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x32, - 0xa2, 0x01, 0x0a, 0x0c, 0x52, 0x65, 0x61, 0x63, 0x68, 0x61, 0x62, 0x69, 0x6c, 0x69, 0x74, 0x79, - 0x12, 0x44, 0x0a, 0x09, 0x50, 0x6f, 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x12, 0x19, 0x2e, - 0x73, 0x63, 0x61, 0x6e, 0x6e, 0x65, 0x72, 0x2e, 0x50, 0x6f, 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, - 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x73, 0x63, 0x61, 0x6e, 0x6e, - 0x65, 0x72, 0x2e, 0x50, 0x6f, 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x4c, 0x0a, 0x0d, 0x48, 0x6f, 0x73, 0x74, 0x50, 0x6f, - 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x12, 0x1d, 0x2e, 0x73, 0x63, 0x61, 0x6e, 0x6e, 0x65, - 0x72, 0x2e, 0x48, 0x6f, 0x73, 0x74, 0x50, 0x6f, 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x73, 0x63, 0x61, 0x6e, 0x6e, 0x65, 0x72, - 0x2e, 0x50, 0x6f, 0x72, 0x74, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x22, 0x00, 0x42, 0x2f, 0x5a, 0x2d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x4c, 0x61, 0x79, 0x72, 0x2d, 0x4c, 0x61, 0x62, 0x73, 0x2f, 0x65, 0x69, 0x67, - 0x65, 0x6e, 0x64, 0x61, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x73, 0x63, - 0x61, 0x6e, 0x6e, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, -} - -var ( - file_scanner_scanner_proto_rawDescOnce sync.Once - file_scanner_scanner_proto_rawDescData = file_scanner_scanner_proto_rawDesc -) - -func file_scanner_scanner_proto_rawDescGZIP() []byte { - file_scanner_scanner_proto_rawDescOnce.Do(func() { - file_scanner_scanner_proto_rawDescData = protoimpl.X.CompressGZIP(file_scanner_scanner_proto_rawDescData) - }) - return file_scanner_scanner_proto_rawDescData -} - -var file_scanner_scanner_proto_msgTypes = make([]protoimpl.MessageInfo, 3) -var file_scanner_scanner_proto_goTypes = []interface{}{ - (*PortCheckRequest)(nil), // 0: scanner.PortCheckRequest - (*HostPortCheckRequest)(nil), // 1: scanner.HostPortCheckRequest - (*PortCheckResponse)(nil), // 2: scanner.PortCheckResponse -} -var file_scanner_scanner_proto_depIdxs = []int32{ - 0, // 0: scanner.Reachability.PortCheck:input_type -> scanner.PortCheckRequest - 1, // 1: scanner.Reachability.HostPortCheck:input_type -> scanner.HostPortCheckRequest - 2, // 2: scanner.Reachability.PortCheck:output_type -> scanner.PortCheckResponse - 2, // 3: scanner.Reachability.HostPortCheck:output_type -> scanner.PortCheckResponse - 2, // [2:4] is the sub-list for method output_type - 0, // [0:2] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name -} - -func init() { file_scanner_scanner_proto_init() } -func file_scanner_scanner_proto_init() { - if File_scanner_scanner_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_scanner_scanner_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*PortCheckRequest); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_scanner_scanner_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*HostPortCheckRequest); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_scanner_scanner_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*PortCheckResponse); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_scanner_scanner_proto_rawDesc, - NumEnums: 0, - NumMessages: 3, - NumExtensions: 0, - NumServices: 1, - }, - GoTypes: file_scanner_scanner_proto_goTypes, - DependencyIndexes: file_scanner_scanner_proto_depIdxs, - MessageInfos: file_scanner_scanner_proto_msgTypes, - }.Build() - File_scanner_scanner_proto = out.File - file_scanner_scanner_proto_rawDesc = nil - file_scanner_scanner_proto_goTypes = nil - file_scanner_scanner_proto_depIdxs = nil -} diff --git a/api/grpc/scanner/scanner_grpc.pb.go b/api/grpc/scanner/scanner_grpc.pb.go deleted file mode 100644 index 17fbac6c03..0000000000 --- a/api/grpc/scanner/scanner_grpc.pb.go +++ /dev/null @@ -1,137 +0,0 @@ -// Code generated by protoc-gen-go-grpc. DO NOT EDIT. - -package scanner - -import ( - context "context" - grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" -) - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.32.0 or later. -const _ = grpc.SupportPackageIsVersion7 - -// ReachabilityClient is the client API for Reachability service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. -type ReachabilityClient interface { - PortCheck(ctx context.Context, in *PortCheckRequest, opts ...grpc.CallOption) (*PortCheckResponse, error) - HostPortCheck(ctx context.Context, in *HostPortCheckRequest, opts ...grpc.CallOption) (*PortCheckResponse, error) -} - -type reachabilityClient struct { - cc grpc.ClientConnInterface -} - -func NewReachabilityClient(cc grpc.ClientConnInterface) ReachabilityClient { - return &reachabilityClient{cc} -} - -func (c *reachabilityClient) PortCheck(ctx context.Context, in *PortCheckRequest, opts ...grpc.CallOption) (*PortCheckResponse, error) { - out := new(PortCheckResponse) - err := c.cc.Invoke(ctx, "/scanner.Reachability/PortCheck", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -func (c *reachabilityClient) HostPortCheck(ctx context.Context, in *HostPortCheckRequest, opts ...grpc.CallOption) (*PortCheckResponse, error) { - out := new(PortCheckResponse) - err := c.cc.Invoke(ctx, "/scanner.Reachability/HostPortCheck", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -// ReachabilityServer is the server API for Reachability service. -// All implementations must embed UnimplementedReachabilityServer -// for forward compatibility -type ReachabilityServer interface { - PortCheck(context.Context, *PortCheckRequest) (*PortCheckResponse, error) - HostPortCheck(context.Context, *HostPortCheckRequest) (*PortCheckResponse, error) - mustEmbedUnimplementedReachabilityServer() -} - -// UnimplementedReachabilityServer must be embedded to have forward compatible implementations. -type UnimplementedReachabilityServer struct { -} - -func (UnimplementedReachabilityServer) PortCheck(context.Context, *PortCheckRequest) (*PortCheckResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method PortCheck not implemented") -} -func (UnimplementedReachabilityServer) HostPortCheck(context.Context, *HostPortCheckRequest) (*PortCheckResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method HostPortCheck not implemented") -} -func (UnimplementedReachabilityServer) mustEmbedUnimplementedReachabilityServer() {} - -// UnsafeReachabilityServer may be embedded to opt out of forward compatibility for this service. -// Use of this interface is not recommended, as added methods to ReachabilityServer will -// result in compilation errors. -type UnsafeReachabilityServer interface { - mustEmbedUnimplementedReachabilityServer() -} - -func RegisterReachabilityServer(s grpc.ServiceRegistrar, srv ReachabilityServer) { - s.RegisterService(&Reachability_ServiceDesc, srv) -} - -func _Reachability_PortCheck_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(PortCheckRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(ReachabilityServer).PortCheck(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/scanner.Reachability/PortCheck", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(ReachabilityServer).PortCheck(ctx, req.(*PortCheckRequest)) - } - return interceptor(ctx, in, info, handler) -} - -func _Reachability_HostPortCheck_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(HostPortCheckRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(ReachabilityServer).HostPortCheck(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/scanner.Reachability/HostPortCheck", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(ReachabilityServer).HostPortCheck(ctx, req.(*HostPortCheckRequest)) - } - return interceptor(ctx, in, info, handler) -} - -// Reachability_ServiceDesc is the grpc.ServiceDesc for Reachability service. -// It's only intended for direct use with grpc.RegisterService, -// and not to be introspected or modified (even as a copy) -var Reachability_ServiceDesc = grpc.ServiceDesc{ - ServiceName: "scanner.Reachability", - HandlerType: (*ReachabilityServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "PortCheck", - Handler: _Reachability_PortCheck_Handler, - }, - { - MethodName: "HostPortCheck", - Handler: _Reachability_HostPortCheck_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "scanner/scanner.proto", -} diff --git a/scanner/Makefile b/scanner/Makefile deleted file mode 100644 index 0e56f9bc64..0000000000 --- a/scanner/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -build: clean - # cd .. && make protoc - go mod tidy - go build -o ./bin/scanner ./cmd - -docker: build - cd .. && docker build . -t scanner -f scanner/cmd/Dockerfile - -clean: - rm -rf ./bin - -lint: - golint -set_exit_status ./... - go tool fix ./.. - golangci-lint run diff --git a/scanner/cmd/Dockerfile b/scanner/cmd/Dockerfile deleted file mode 100644 index ad4549e080..0000000000 --- a/scanner/cmd/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -FROM golang:1.21.1-alpine3.18 as builder - -RUN apk add --no-cache make musl-dev linux-headers gcc git jq bash - -# build node scanner with local monorepo go modules -COPY ./scanner /app/scanner -COPY api /app/api -COPY common /app/common -COPY core /app/core -COPY go.mod /app -COPY go.sum /app - -WORKDIR /app/scanner - -RUN go build -o ./bin/scanner ./cmd - -FROM alpine:3.18 - -COPY --from=builder /app/scanner/bin/scanner /usr/local/bin - -ENTRYPOINT ["scanner"] diff --git a/scanner/cmd/main.go b/scanner/cmd/main.go deleted file mode 100644 index f46f713fd8..0000000000 --- a/scanner/cmd/main.go +++ /dev/null @@ -1,66 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - "os" - - "github.com/urfave/cli" - - "github.com/Layr-Labs/eigenda/common" - "github.com/Layr-Labs/eigenda/scanner" - "github.com/Layr-Labs/eigenda/scanner/flags" - "github.com/Layr-Labs/eigenda/scanner/grpc" -) - -func main() { - app := cli.NewApp() - app.Flags = flags.Flags - app.Version = fmt.Sprintf("%s-%s-%s", scanner.SemVer, scanner.GitCommit, scanner.GitDate) - app.Name = scanner.AppName - app.Usage = "EigenDA Node Reachability Scanner" - app.Description = "Service for checking the reachabilty of operator nodes" - - app.Action = ScannerMain - err := app.Run(os.Args) - if err != nil { - log.Fatalf("application failed: %v", err) - } - - select {} -} - -// ScannerMain func -func ScannerMain(ctx *cli.Context) error { - log.Println("Initializing node scanner") - config, err := scanner.NewConfig(ctx) - if err != nil { - return err - } - - logger, err := common.NewLogger(config.LoggerConfig) - if err != nil { - return err - } - - // Create the node scanner. - scanner, err := scanner.NewScanner(config, logger) - if err != nil { - return err - } - - err = scanner.Start(context.Background()) - if err != nil { - scanner.Logger.Error("could not start node scanner", "error", err) - return err - } - - // Creates the GRPC server. - server := grpc.NewServer(config, scanner, logger) - if err := server.Start(); err != nil { - log.Fatalf("Failed to start server: %v", err) - } - - return nil -} diff --git a/tools/npc/.env b/tools/npc/.env new file mode 100644 index 0000000000..7820194599 --- /dev/null +++ b/tools/npc/.env @@ -0,0 +1,5 @@ +export SCANNER_LOG_PATH=scanner.log + +export SCANNER_LOG_LEVEL=debug +export SCANNER_LOG_FORMAT=text + diff --git a/tools/npc/.gitignore b/tools/npc/.gitignore new file mode 100644 index 0000000000..d9a5f139e3 --- /dev/null +++ b/tools/npc/.gitignore @@ -0,0 +1 @@ +npc diff --git a/tools/npc/Makefile b/tools/npc/Makefile new file mode 100644 index 0000000000..cc1e2bfa97 --- /dev/null +++ b/tools/npc/Makefile @@ -0,0 +1,18 @@ +build: clean proto + go mod tidy + go build -o ./bin/npc ./cmd + +proto: + protoc --go_out=. npc.proto + protoc --go-grpc_out=. npc.proto + +docker: + cd ../../ && docker build . -t npc -f tools/npc/cmd/Dockerfile + +clean: + rm -rf ./bin ./npc + +lint: + golint -set_exit_status ./... + go tool fix ./.. + golangci-lint run diff --git a/scanner/README.md b/tools/npc/README.md similarity index 100% rename from scanner/README.md rename to tools/npc/README.md diff --git a/tools/npc/cmd/Dockerfile b/tools/npc/cmd/Dockerfile new file mode 100644 index 0000000000..2ce65a8d58 --- /dev/null +++ b/tools/npc/cmd/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.21.1-alpine3.18 as builder + +RUN apk add --no-cache make musl-dev linux-headers gcc git jq bash + +COPY ./tools/npc /app/tools/npc +COPY api /app/api +COPY common /app/common +COPY go.mod /app +COPY go.sum /app + +WORKDIR /app/tools/npc + +RUN go build -o ./bin/npc ./cmd + +FROM alpine:3.18 + +COPY --from=builder /app/tools/npc/bin/npc /usr/local/bin + +ENTRYPOINT ["npc"] diff --git a/tools/npc/cmd/main.go b/tools/npc/cmd/main.go new file mode 100644 index 0000000000..dfb6340efb --- /dev/null +++ b/tools/npc/cmd/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/urfave/cli" + + "github.com/Layr-Labs/eigenda/common" + "github.com/Layr-Labs/eigenda/tools/npc" + "github.com/Layr-Labs/eigenda/tools/npc/flags" + "github.com/Layr-Labs/eigenda/tools/npc/grpc" +) + +func main() { + app := cli.NewApp() + app.Flags = flags.Flags + app.Version = fmt.Sprintf("%s-%s-%s", npc.SemVer, npc.GitCommit, npc.GitDate) + app.Name = npc.AppName + app.Usage = "EigenDA Node Port Check Scanner " + app.Description = "Service for checking the reachabilty of operator nodes" + + app.Action = NpcMain + err := app.Run(os.Args) + if err != nil { + log.Fatalf("application failed: %v", err) + } + + select {} +} + +// NpcMain func +func NpcMain(ctx *cli.Context) error { + log.Println("Initializing node npc") + config, err := npc.NewConfig(ctx) + if err != nil { + return err + } + + logger, err := common.NewLogger(config.LoggerConfig) + if err != nil { + return err + } + + // Create the npc. + npc, err := npc.NewScanner(config, logger) + if err != nil { + return err + } + + err = npc.Start(context.Background()) + if err != nil { + npc.Logger.Error("could not start node npc", "error", err) + return err + } + + // Creates the GRPC server. + server := grpc.NewServer(config, npc, logger) + if err := server.Start(); err != nil { + log.Fatalf("Failed to start server: %v", err) + } + + return nil +} diff --git a/scanner/config.go b/tools/npc/config.go similarity index 84% rename from scanner/config.go rename to tools/npc/config.go index 0abd267832..33541a90ac 100644 --- a/scanner/config.go +++ b/tools/npc/config.go @@ -1,22 +1,22 @@ -package scanner +package npc import ( "time" "github.com/Layr-Labs/eigenda/common" - "github.com/Layr-Labs/eigenda/scanner/flags" + "github.com/Layr-Labs/eigenda/tools/npc/flags" "github.com/urfave/cli" ) -// Scanner +// Npc const ( - AppName = "scanner" + AppName = "npc" SemVer = "1.0.0" GitCommit = "" GitDate = "" ) -// Config contains all of the configuration information for a scanner +// Config contains all of the configuration information for a npc type Config struct { ServerPort string MetricsPort string diff --git a/scanner/flags/flags.go b/tools/npc/flags/flags.go similarity index 96% rename from scanner/flags/flags.go rename to tools/npc/flags/flags.go index 32bdf034a3..7945fc83bc 100644 --- a/scanner/flags/flags.go +++ b/tools/npc/flags/flags.go @@ -7,8 +7,8 @@ import ( // constants const ( - FlagPrefix = "scanner" - EnvVarPrefix = "SCANNER" + FlagPrefix = "npc" + EnvVarPrefix = "NPC" ) var ( diff --git a/scanner/grpc/server.go b/tools/npc/grpc/server.go similarity index 90% rename from scanner/grpc/server.go rename to tools/npc/grpc/server.go index 639e946921..ab72b53397 100644 --- a/scanner/grpc/server.go +++ b/tools/npc/grpc/server.go @@ -7,8 +7,9 @@ import ( "net" "time" - pb "github.com/Layr-Labs/eigenda/api/grpc/scanner" - "github.com/Layr-Labs/eigenda/scanner" + "github.com/Layr-Labs/eigenda/tools/npc" + pb "github.com/Layr-Labs/eigenda/tools/npc/npc/grpc" + "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Ullaakut/nmap" @@ -19,21 +20,21 @@ import ( const localhost = "0.0.0.0" -// Server implements the Scanner proto APIs. +// Server implements the Npc scanner proto APIs. type Server struct { pb.UnimplementedReachabilityServer - scanner *scanner.Scanner - config *scanner.Config + npc *npc.Scanner + config *npc.Config logger logging.Logger } // NewServer func -func NewServer(config *scanner.Config, scanner *scanner.Scanner, logger logging.Logger) *Server { +func NewServer(config *npc.Config, npc *npc.Scanner, logger logging.Logger) *Server { return &Server{ config: config, logger: logger, - scanner: scanner, + npc: npc, } } @@ -81,7 +82,7 @@ func (s *Server) PortCheck(ctx context.Context, in *pb.PortCheckRequest) (*pb.Po nmap.WithPorts(fmt.Sprintf("%d", in.Port)), ) if err != nil { - //log.Fatalf("unable to create nmap scanner: %v", err) + //log.Fatalf("unable to create nmap npc: %v", err) return &pb.PortCheckResponse{Status: "error", Msg: "Port scan failed", Host: ip, Port: in.Port}, nil } @@ -110,7 +111,7 @@ func (s *Server) PortCheck(ctx context.Context, in *pb.PortCheckRequest) (*pb.Po func (s *Server) HostPortCheck(ctx context.Context, in *pb.HostPortCheckRequest) (*pb.PortCheckResponse, error) { if in.Host == "" { - //log.Fatalf("unable to create nmap scanner: %v", err) + //log.Fatalf("unable to create nmap npc: %v", err) return &pb.PortCheckResponse{Status: "error", Msg: "Host must be specified", Host: in.Host, Port: in.Port}, nil } var timeout = 5 * time.Second diff --git a/scanner/metrics.go b/tools/npc/metrics.go similarity index 74% rename from scanner/metrics.go rename to tools/npc/metrics.go index 9986aee6cb..20f1e4ee8a 100644 --- a/scanner/metrics.go +++ b/tools/npc/metrics.go @@ -1,10 +1,7 @@ -package scanner +package npc import ( - "context" - "github.com/Layr-Labs/eigensdk-go/logging" - eigenmetrics "github.com/Layr-Labs/eigensdk-go/metrics" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" @@ -13,7 +10,7 @@ import ( // Namespace const ( - Namespace = "scanner" + Namespace = "npc" ) // Metrics struct @@ -24,8 +21,6 @@ type Metrics struct { AccNumRequests *prometheus.CounterVec // The latency (in ms) to process the request. RequestLatency *prometheus.SummaryVec - // EigenMetrics - EigenMetrics eigenmetrics.Metrics registry *prometheus.Registry // socketAddr is the address at which the metrics server will be listening. @@ -34,7 +29,7 @@ type Metrics struct { } // NewMetrics metrics -func NewMetrics(eigenMetrics eigenmetrics.Metrics, reg *prometheus.Registry, logger logging.Logger, socketAddr string) *Metrics { +func NewMetrics(reg *prometheus.Registry, logger logging.Logger, socketAddr string) *Metrics { // Add Go module collectors reg.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) @@ -45,22 +40,21 @@ func NewMetrics(eigenMetrics eigenmetrics.Metrics, reg *prometheus.Registry, log AccNumRequests: promauto.With(reg).NewCounterVec( prometheus.CounterOpts{ Namespace: Namespace, - Name: "eigenda_scanner_requests_total", - Help: "the total number of scan requests processed by the scanner", + Name: "eigenda_npc_requests_total", + Help: "the total number of scan requests processed by the npc scanner", }, []string{"method", "status"}, ), RequestLatency: promauto.With(reg).NewSummaryVec( prometheus.SummaryOpts{ Namespace: Namespace, - Name: "eigenda_scanner_request_latency_ms", + Name: "eigenda_npc_request_latency_ms", Help: "latency summary in milliseconds", Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.95: 0.01, 0.99: 0.001}, }, []string{"method", "stage"}, ), - EigenMetrics: eigenMetrics, - logger: logger.With("component", "ScannerMetrics"), + logger: logger.With("component", "NpcMetrics"), registry: reg, socketAddr: socketAddr, } @@ -68,11 +62,6 @@ func NewMetrics(eigenMetrics eigenmetrics.Metrics, reg *prometheus.Registry, log return metrics } -// Start func -func (g *Metrics) Start() { - _ = g.EigenMetrics.Start(context.Background(), g.registry) -} - // RecordRPCRequest func func (g *Metrics) RecordRPCRequest(method string, status string) { g.AccNumRequests.WithLabelValues(method, status).Inc() diff --git a/api/proto/scanner/scanner.proto b/tools/npc/npc.proto similarity index 82% rename from api/proto/scanner/scanner.proto rename to tools/npc/npc.proto index 5630eaf3d7..bf6dea2af6 100644 --- a/api/proto/scanner/scanner.proto +++ b/tools/npc/npc.proto @@ -1,8 +1,8 @@ syntax = "proto3"; -package scanner; +package npc; -option go_package = "github.com/Layr-Labs/eigenda/api/grpc/scanner"; +option go_package = "npc/grpc"; message PortCheckRequest { uint32 port = 1; diff --git a/scanner/scanner.go b/tools/npc/scanner.go similarity index 63% rename from scanner/scanner.go rename to tools/npc/scanner.go index d6f94d0f7e..085212a0bf 100644 --- a/scanner/scanner.go +++ b/tools/npc/scanner.go @@ -1,4 +1,4 @@ -package scanner +package npc import ( "context" @@ -6,7 +6,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/Layr-Labs/eigensdk-go/logging" - "github.com/Layr-Labs/eigensdk-go/metrics" ) // Scanner struct @@ -16,12 +15,10 @@ type Scanner struct { Metrics *Metrics } -// NewScanner creates a new scanner with the provided config. +// NewScanner creates a new npc scanner with the provided config. func NewScanner(config *Config, logger logging.Logger) (*Scanner, error) { promReg := prometheus.NewRegistry() - eigenMetrics := metrics.NewEigenMetrics(AppName, ":"+config.MetricsPort, promReg, logger.With("component", "EigenMetrics")) - - metrics := NewMetrics(eigenMetrics, promReg, logger, ":"+config.MetricsPort) + metrics := NewMetrics(promReg, logger, ":"+config.MetricsPort) return &Scanner{ Config: config, @@ -32,7 +29,6 @@ func NewScanner(config *Config, logger logging.Logger) (*Scanner, error) { // Start the Scanner. func (s *Scanner) Start(ctx context.Context) error { - s.Metrics.Start() s.Logger.Info("Enabled metrics", "socket", s.Metrics.socketAddr) return nil