diff --git a/api/proto/pb/victron.pb.go b/api/proto/pb/victron.pb.go new file mode 100644 index 000000000..fa4d17af6 --- /dev/null +++ b/api/proto/pb/victron.pb.go @@ -0,0 +1,249 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.1 +// protoc v5.26.1 +// source: proto/victron.proto + +package pb + +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 VictronRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ProductId string `protobuf:"bytes,1,opt,name=productId,proto3" json:"productId,omitempty"` + VrmId string `protobuf:"bytes,2,opt,name=vrmId,proto3" json:"vrmId,omitempty"` + Serial string `protobuf:"bytes,3,opt,name=serial,proto3" json:"serial,omitempty"` + Board string `protobuf:"bytes,4,opt,name=board,proto3" json:"board,omitempty"` +} + +func (x *VictronRequest) Reset() { + *x = VictronRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_victron_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *VictronRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*VictronRequest) ProtoMessage() {} + +func (x *VictronRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_victron_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 VictronRequest.ProtoReflect.Descriptor instead. +func (*VictronRequest) Descriptor() ([]byte, []int) { + return file_proto_victron_proto_rawDescGZIP(), []int{0} +} + +func (x *VictronRequest) GetProductId() string { + if x != nil { + return x.ProductId + } + return "" +} + +func (x *VictronRequest) GetVrmId() string { + if x != nil { + return x.VrmId + } + return "" +} + +func (x *VictronRequest) GetSerial() string { + if x != nil { + return x.Serial + } + return "" +} + +func (x *VictronRequest) GetBoard() string { + if x != nil { + return x.Board + } + return "" +} + +type VictronReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Authorized bool `protobuf:"varint,1,opt,name=authorized,proto3" json:"authorized,omitempty"` + Subject string `protobuf:"bytes,2,opt,name=subject,proto3" json:"subject,omitempty"` +} + +func (x *VictronReply) Reset() { + *x = VictronReply{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_victron_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *VictronReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*VictronReply) ProtoMessage() {} + +func (x *VictronReply) ProtoReflect() protoreflect.Message { + mi := &file_proto_victron_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 VictronReply.ProtoReflect.Descriptor instead. +func (*VictronReply) Descriptor() ([]byte, []int) { + return file_proto_victron_proto_rawDescGZIP(), []int{1} +} + +func (x *VictronReply) GetAuthorized() bool { + if x != nil { + return x.Authorized + } + return false +} + +func (x *VictronReply) GetSubject() string { + if x != nil { + return x.Subject + } + return "" +} + +var File_proto_victron_proto protoreflect.FileDescriptor + +var file_proto_victron_proto_rawDesc = []byte{ + 0x0a, 0x13, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x76, 0x69, 0x63, 0x74, 0x72, 0x6f, 0x6e, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x72, 0x0a, 0x0e, 0x56, 0x69, 0x63, 0x74, 0x72, 0x6f, 0x6e, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x75, + 0x63, 0x74, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x70, 0x72, 0x6f, 0x64, + 0x75, 0x63, 0x74, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x72, 0x6d, 0x49, 0x64, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x72, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, + 0x65, 0x72, 0x69, 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x65, 0x72, + 0x69, 0x61, 0x6c, 0x12, 0x14, 0x0a, 0x05, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x05, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x22, 0x48, 0x0a, 0x0c, 0x56, 0x69, 0x63, + 0x74, 0x72, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1e, 0x0a, 0x0a, 0x61, 0x75, 0x74, + 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x65, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x61, + 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x62, + 0x6a, 0x65, 0x63, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x75, 0x62, 0x6a, + 0x65, 0x63, 0x74, 0x32, 0x3c, 0x0a, 0x07, 0x56, 0x69, 0x63, 0x74, 0x72, 0x6f, 0x6e, 0x12, 0x31, + 0x0a, 0x0d, 0x49, 0x73, 0x56, 0x61, 0x6c, 0x69, 0x64, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x12, + 0x0f, 0x2e, 0x56, 0x69, 0x63, 0x74, 0x72, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x0d, 0x2e, 0x56, 0x69, 0x63, 0x74, 0x72, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, + 0x00, 0x42, 0x0a, 0x5a, 0x08, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_victron_proto_rawDescOnce sync.Once + file_proto_victron_proto_rawDescData = file_proto_victron_proto_rawDesc +) + +func file_proto_victron_proto_rawDescGZIP() []byte { + file_proto_victron_proto_rawDescOnce.Do(func() { + file_proto_victron_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_victron_proto_rawDescData) + }) + return file_proto_victron_proto_rawDescData +} + +var file_proto_victron_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proto_victron_proto_goTypes = []interface{}{ + (*VictronRequest)(nil), // 0: VictronRequest + (*VictronReply)(nil), // 1: VictronReply +} +var file_proto_victron_proto_depIdxs = []int32{ + 0, // 0: Victron.IsValidDevice:input_type -> VictronRequest + 1, // 1: Victron.IsValidDevice:output_type -> VictronReply + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] 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_proto_victron_proto_init() } +func file_proto_victron_proto_init() { + if File_proto_victron_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_victron_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*VictronRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_victron_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*VictronReply); 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_proto_victron_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_victron_proto_goTypes, + DependencyIndexes: file_proto_victron_proto_depIdxs, + MessageInfos: file_proto_victron_proto_msgTypes, + }.Build() + File_proto_victron_proto = out.File + file_proto_victron_proto_rawDesc = nil + file_proto_victron_proto_goTypes = nil + file_proto_victron_proto_depIdxs = nil +} diff --git a/api/proto/pb/victron_grpc.pb.go b/api/proto/pb/victron_grpc.pb.go new file mode 100644 index 000000000..c93af9444 --- /dev/null +++ b/api/proto/pb/victron_grpc.pb.go @@ -0,0 +1,109 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.3.0 +// - protoc v5.26.1 +// source: proto/victron.proto + +package pb + +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 + +const ( + Victron_IsValidDevice_FullMethodName = "/Victron/IsValidDevice" +) + +// VictronClient is the client API for Victron 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 VictronClient interface { + IsValidDevice(ctx context.Context, in *VictronRequest, opts ...grpc.CallOption) (*VictronReply, error) +} + +type victronClient struct { + cc grpc.ClientConnInterface +} + +func NewVictronClient(cc grpc.ClientConnInterface) VictronClient { + return &victronClient{cc} +} + +func (c *victronClient) IsValidDevice(ctx context.Context, in *VictronRequest, opts ...grpc.CallOption) (*VictronReply, error) { + out := new(VictronReply) + err := c.cc.Invoke(ctx, Victron_IsValidDevice_FullMethodName, in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// VictronServer is the server API for Victron service. +// All implementations must embed UnimplementedVictronServer +// for forward compatibility +type VictronServer interface { + IsValidDevice(context.Context, *VictronRequest) (*VictronReply, error) + mustEmbedUnimplementedVictronServer() +} + +// UnimplementedVictronServer must be embedded to have forward compatible implementations. +type UnimplementedVictronServer struct { +} + +func (UnimplementedVictronServer) IsValidDevice(context.Context, *VictronRequest) (*VictronReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method IsValidDevice not implemented") +} +func (UnimplementedVictronServer) mustEmbedUnimplementedVictronServer() {} + +// UnsafeVictronServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to VictronServer will +// result in compilation errors. +type UnsafeVictronServer interface { + mustEmbedUnimplementedVictronServer() +} + +func RegisterVictronServer(s grpc.ServiceRegistrar, srv VictronServer) { + s.RegisterService(&Victron_ServiceDesc, srv) +} + +func _Victron_IsValidDevice_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(VictronRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(VictronServer).IsValidDevice(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Victron_IsValidDevice_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(VictronServer).IsValidDevice(ctx, req.(*VictronRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Victron_ServiceDesc is the grpc.ServiceDesc for Victron service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Victron_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "Victron", + HandlerType: (*VictronServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "IsValidDevice", + Handler: _Victron_IsValidDevice_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "proto/victron.proto", +} diff --git a/api/proto/victron.proto b/api/proto/victron.proto new file mode 100644 index 000000000..9c6614c67 --- /dev/null +++ b/api/proto/victron.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +// protoc proto/victron.proto --go_out=. --go-grpc_out=. + +option go_package = "proto/pb"; + +service Victron { + rpc IsValidDevice (VictronRequest) returns (VictronReply) {} +} + +message VictronRequest { + string productId = 1; + string vrmId = 2; + string serial = 3; + string board = 4; +} + +message VictronReply { + bool authorized = 1; + string subject = 2; +} diff --git a/util/sponsor/auth.go b/util/sponsor/auth.go index a2f4b14f5..56243153e 100644 --- a/util/sponsor/auth.go +++ b/util/sponsor/auth.go @@ -16,19 +16,27 @@ var ( ExpiresAt time.Time ) -const unavailable = "sponsorship unavailable" +const ( + unavailable = "sponsorship unavailable" + victron = "victron-device" +) func IsAuthorized() bool { return len(Subject) > 0 } func IsAuthorizedForApi() bool { - return IsAuthorized() && Subject != unavailable + return IsAuthorized() && Token != "" } // check and set sponsorship token func ConfigureSponsorship(token string) error { if token == "" { + if sub := checkVictron(); sub != "" { + Subject = sub + return nil + } + var err error if token, err = readSerial(); token == "" || err != nil { return err diff --git a/util/sponsor/victron.go b/util/sponsor/victron.go new file mode 100644 index 000000000..c6a774780 --- /dev/null +++ b/util/sponsor/victron.go @@ -0,0 +1,114 @@ +package sponsor + +import ( + "context" + "errors" + "os/exec" + "runtime" + "strings" + "time" + + "github.com/evcc-io/evcc/api/proto/pb" + "github.com/evcc-io/evcc/util/cloud" + "github.com/evcc-io/evcc/util/request" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// checkVictron checks if the hardware is a supported victron device and returns sponsor subject +func checkVictron() string { + vd, err := victronDeviceInfo() + if err != nil { + // unable to retrieve all device info + return "" + } + + conn, err := cloud.Connection() + if err != nil { + // unable to connect to cloud + return unavailable + } + + ctx, cancel := context.WithTimeout(context.Background(), request.Timeout) + defer cancel() + + client := pb.NewVictronClient(conn) + res, err := client.IsValidDevice(ctx, &pb.VictronRequest{ + ProductId: vd.ProductId, + VrmId: vd.VrmId, + Serial: vd.Serial, + Board: vd.Board, + }) + + if err == nil && res.Authorized { + // cloud check successful + return victron + } + + if s, ok := status.FromError(err); ok && s.Code() != codes.Unknown { + // technical error during validation + return unavailable + } + + return "" +} + +type victronDevice struct { + ProductId string + VrmId string + Serial string + Board string +} + +func commandExists(cmd string) error { + _, err := exec.LookPath(cmd) + return err +} + +func executeCommand(ctx context.Context, cmd string, args ...string) (string, error) { + command := exec.CommandContext(ctx, cmd, args...) + output, err := command.Output() + if err != nil { + return "", err + } + result := strings.TrimSpace(string(output)) + if result == "" { + return "", errors.New("empty output") + } + return result, nil +} + +func victronDeviceInfo() (victronDevice, error) { + if runtime.GOOS != "linux" { + return victronDevice{}, errors.New("non-linux os") + } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + var vd victronDevice + + commands := []struct { + field *string + cmd string + args []string + }{ + {field: &vd.Board, cmd: "/usr/bin/board-compat"}, + {field: &vd.ProductId, cmd: "/usr/bin/product-id"}, + {field: &vd.VrmId, cmd: "/sbin/get-unique-id"}, + {field: &vd.Serial, cmd: "/opt/victronenergy/venus-eeprom/eeprom", args: []string{"--show", "serial-number"}}, + } + + for _, detail := range commands { + if err := commandExists(detail.cmd); err != nil { + return vd, errors.New("cmd not found: " + detail.cmd) + } + output, err := executeCommand(ctx, detail.cmd, detail.args...) + if err != nil { + return vd, err + } + *detail.field = output + } + + return vd, nil +}