From 8181dcf2f39d5658fc76873fbbe061b53bc4acb6 Mon Sep 17 00:00:00 2001 From: YuanJay <2218773049@qq.com> Date: Mon, 31 Jul 2023 10:47:58 +0800 Subject: [PATCH] key --- cmd/openim-rpc/openim-rpc-key/Makefile | 34 ++ cmd/openim-rpc/openim-rpc-key/main.go | 18 + internal/api/key.go | 18 + internal/api/route.go | 6 + internal/rpc/key/key.go | 39 ++ pkg/common/db/controller/key.go | 27 ++ pkg/common/db/relation/key_model.go | 26 ++ pkg/common/db/table/relation/key.go | 22 ++ pkg/proto/gen.cmd | 1 + pkg/proto/key/key.pb.go | 496 +++++++++++++++++++++++++ pkg/proto/key/key.proto | 25 ++ pkg/rpcclient/key.go | 23 ++ pkg/utils/hash.go | 57 +++ test/main/main.go | 63 ++++ 14 files changed, 855 insertions(+) create mode 100644 cmd/openim-rpc/openim-rpc-key/Makefile create mode 100644 cmd/openim-rpc/openim-rpc-key/main.go create mode 100644 internal/api/key.go create mode 100644 internal/rpc/key/key.go create mode 100644 pkg/common/db/controller/key.go create mode 100644 pkg/common/db/relation/key_model.go create mode 100644 pkg/common/db/table/relation/key.go create mode 100644 pkg/proto/key/key.pb.go create mode 100644 pkg/proto/key/key.proto create mode 100644 pkg/rpcclient/key.go create mode 100644 pkg/utils/hash.go create mode 100644 test/main/main.go diff --git a/cmd/openim-rpc/openim-rpc-key/Makefile b/cmd/openim-rpc/openim-rpc-key/Makefile new file mode 100644 index 000000000..3a16cbf7a --- /dev/null +++ b/cmd/openim-rpc/openim-rpc-key/Makefile @@ -0,0 +1,34 @@ +.PHONY: all build run gotool install clean help + +NAME=openim-rpc-key +BIN_DIR=../../../bin/ + +OS:= $(or $(os),linux) +ARCH:=$(or $(arch),amd64) +all: gotool build + +ifeq ($(OS),windows) + +BINARY_NAME=${NAME}.exe + +else + +BINARY_NAME=${NAME} + +endif + +build: + CGO_ENABLED=0 GOOS=${OS} GOARCH=${ARCH}; go build -ldflags="-w -s" -o ${BINARY_NAME} + +run: + @go run ./ + +gotool: + go fmt ./ + go vet ./ + +install:build + mv ${BINARY_NAME} ${BIN_DIR} + +clean: + @if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi diff --git a/cmd/openim-rpc/openim-rpc-key/main.go b/cmd/openim-rpc/openim-rpc-key/main.go new file mode 100644 index 000000000..0e4e864ec --- /dev/null +++ b/cmd/openim-rpc/openim-rpc-key/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/key" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" +) + +func main() { + rpcCmd := cmd.NewRpcCmd("key") + rpcCmd.AddPortFlag() + rpcCmd.AddPrometheusPortFlag() + if err := rpcCmd.Exec(); err != nil { + panic(err.Error()) + } + if err := rpcCmd.StartSvr("key", key.Start); err != nil { + panic(err.Error()) + } +} diff --git a/internal/api/key.go b/internal/api/key.go new file mode 100644 index 000000000..dcdfffebc --- /dev/null +++ b/internal/api/key.go @@ -0,0 +1,18 @@ +package api + +import ( + "github.com/OpenIMSDK/Open-IM-Server/pkg/a2r" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/key" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/gin-gonic/gin" +) + +type KeyApi rpcclient.Key + +func NewKeyApi(client rpcclient.Key) KeyApi { + return KeyApi(client) +} + +func (o *KeyApi) GetKey(c *gin.Context) { + a2r.Call(key.KeyClient.GetKey, o.Client, c) +} diff --git a/internal/api/route.go b/internal/api/route.go index 2dfb9f6c4..2caed50d3 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -49,6 +49,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive conversationRpc := rpcclient.NewConversation(discov) authRpc := rpcclient.NewAuth(discov) thirdRpc := rpcclient.NewThird(discov) + keyRpc := rpcclient.NewKey(discov) u := NewUserApi(*userRpc) m := NewMessageApi(messageRpc, userRpc) @@ -184,5 +185,10 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive statisticsGroup.POST("/group/create", g.GroupCreateCount) statisticsGroup.POST("/group/active", m.GetActiveGroup) } + keyGroup := r.Group("/key", ParseToken) + { + key := NewKeyApi(*keyRpc) + keyGroup.POST("/get_Key", key.GetKey) + } return r } diff --git a/internal/rpc/key/key.go b/internal/rpc/key/key.go new file mode 100644 index 000000000..d1eb06522 --- /dev/null +++ b/internal/rpc/key/key.go @@ -0,0 +1,39 @@ +package key + +import ( + "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/key" + "google.golang.org/grpc" +) + +type keyServer struct { + authDatabase controller.KeyDatabase + keyRpcClient key.KeyClient + RegisterCenter discoveryregistry.SvcDiscoveryRegistry +} + +func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error { + db, err := relation.NewGormDB() + if err != nil { + return err + } + keyDB := relation.NewKeyDB(db) + database := controller.NewKeyDatabase(keyDB) + key.RegisterKeyServer(server, &keyServer{ + authDatabase: database, + keyRpcClient: nil, + RegisterCenter: client, + }) + return nil +} + +func (k keyServer) GetKey(ctx context.Context, req *key.GetKeyReq) (*key.KeyResp, error) { + return &key.KeyResp{}, nil +} +func (k keyServer) GetAllKey(ctx context.Context, req *key.GetAllKeyReq) (*key.GetAllKeyResp, error) { + m := new(map[string]string) + return &key.GetAllKeyResp{Keys: *m}, nil +} diff --git a/pkg/common/db/controller/key.go b/pkg/common/db/controller/key.go new file mode 100644 index 000000000..9893f9522 --- /dev/null +++ b/pkg/common/db/controller/key.go @@ -0,0 +1,27 @@ +package controller + +import ( + "context" + relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" +) + +type KeyDatabase interface { + DBGetKey(ctx context.Context, cID string) (key relationTb.KeyModel, err error) + DBInstallKey(ctx context.Context, key relationTb.KeyModel) (err error) +} +type keyDatabase struct { + keyDB relationTb.KeyModelInterface + //cache cache.KeyCache +} + +func NewKeyDatabase(key relationTb.KeyModelInterface) KeyDatabase { + return &keyDatabase{ + keyDB: key, + } +} +func (k *keyDatabase) DBGetKey(ctx context.Context, cID string) (key relationTb.KeyModel, err error) { + return k.keyDB.GetKey(ctx, cID) +} +func (k *keyDatabase) DBInstallKey(ctx context.Context, key relationTb.KeyModel) (err error) { + return k.keyDB.InstallKey(ctx, key) +} diff --git a/pkg/common/db/relation/key_model.go b/pkg/common/db/relation/key_model.go new file mode 100644 index 000000000..37883625d --- /dev/null +++ b/pkg/common/db/relation/key_model.go @@ -0,0 +1,26 @@ +package relation + +import ( + "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" + "gorm.io/gorm" +) + +var _ relation.KeyModelInterface = (*KeyGorm)(nil) + +type KeyGorm struct { + *MetaDB +} + +func NewKeyDB(db *gorm.DB) relation.KeyModelInterface { + return &KeyGorm{NewMetaDB(db, &relation.KeyModel{})} +} +func (k KeyGorm) InstallKey(ctx context.Context, key relation.KeyModel) (err error) { + return utils.Wrap(k.DB.Create(&key).Error, "") +} + +func (k KeyGorm) GetKey(ctx context.Context, cid string) (key relation.KeyModel, err error) { + err = k.db(ctx).Where("cid=?", cid).Find(&key).Error + return key, err +} diff --git a/pkg/common/db/table/relation/key.go b/pkg/common/db/table/relation/key.go new file mode 100644 index 000000000..ee4e37257 --- /dev/null +++ b/pkg/common/db/table/relation/key.go @@ -0,0 +1,22 @@ +package relation + +import "context" + +const ( + KeyModelTableName = "keys" +) + +type KeyModel struct { + CId string `gorm:"column:cid;primary_key;size:64" json:"cid" binding:"required"` + Key string `gorm:"column:key;size:64" json:"key" binding:"required"` + CType int32 `gorm:"column:cType" json:"cType" binding:"required"` +} + +func (KeyModel) TableName() string { + return KeyModelTableName +} + +type KeyModelInterface interface { + InstallKey(ctx context.Context, key KeyModel) (err error) + GetKey(ctx context.Context, cid string) (key KeyModel, err error) +} diff --git a/pkg/proto/gen.cmd b/pkg/proto/gen.cmd index 930a636ec..a92742d26 100644 --- a/pkg/proto/gen.cmd +++ b/pkg/proto/gen.cmd @@ -1,4 +1,5 @@ protoc --go_out=plugins=grpc:./auth --go_opt=module=github.com/OpenIMSDK/Open-IM-Server/pkg/proto/auth auth/auth.proto +protoc --go_out=plugins=grpc:./key --go_opt=module=github.com/OpenIMSDK/Open-IM-Server/pkg/proto/key key/key.proto protoc --go_out=plugins=grpc:./conversation --go_opt=module=github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation conversation/conversation.proto protoc --go_out=plugins=grpc:./errinfo --go_opt=module=github.com/OpenIMSDK/Open-IM-Server/pkg/proto/errinfo errinfo/errinfo.proto protoc --go_out=plugins=grpc:./friend --go_opt=module=github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend friend/friend.proto diff --git a/pkg/proto/key/key.pb.go b/pkg/proto/key/key.pb.go new file mode 100644 index 000000000..ccd38a15b --- /dev/null +++ b/pkg/proto/key/key.pb.go @@ -0,0 +1,496 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.29.1 +// protoc v4.22.0 +// source: key/key.proto + +package key + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + 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 GetKeyReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ConversationId string `protobuf:"bytes,1,opt,name=conversationId,proto3" json:"conversationId"` + ConversationType uint32 `protobuf:"varint,2,opt,name=conversationType,proto3" json:"conversationType"` + UserId string `protobuf:"bytes,3,opt,name=userId,proto3" json:"userId"` +} + +func (x *GetKeyReq) Reset() { + *x = GetKeyReq{} + if protoimpl.UnsafeEnabled { + mi := &file_key_key_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetKeyReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetKeyReq) ProtoMessage() {} + +func (x *GetKeyReq) ProtoReflect() protoreflect.Message { + mi := &file_key_key_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 GetKeyReq.ProtoReflect.Descriptor instead. +func (*GetKeyReq) Descriptor() ([]byte, []int) { + return file_key_key_proto_rawDescGZIP(), []int{0} +} + +func (x *GetKeyReq) GetConversationId() string { + if x != nil { + return x.ConversationId + } + return "" +} + +func (x *GetKeyReq) GetConversationType() uint32 { + if x != nil { + return x.ConversationType + } + return 0 +} + +func (x *GetKeyReq) GetUserId() string { + if x != nil { + return x.UserId + } + return "" +} + +type KeyResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key"` +} + +func (x *KeyResp) Reset() { + *x = KeyResp{} + if protoimpl.UnsafeEnabled { + mi := &file_key_key_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KeyResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KeyResp) ProtoMessage() {} + +func (x *KeyResp) ProtoReflect() protoreflect.Message { + mi := &file_key_key_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 KeyResp.ProtoReflect.Descriptor instead. +func (*KeyResp) Descriptor() ([]byte, []int) { + return file_key_key_proto_rawDescGZIP(), []int{1} +} + +func (x *KeyResp) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +type GetAllKeyReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UserId string `protobuf:"bytes,1,opt,name=userId,proto3" json:"userId"` +} + +func (x *GetAllKeyReq) Reset() { + *x = GetAllKeyReq{} + if protoimpl.UnsafeEnabled { + mi := &file_key_key_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetAllKeyReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetAllKeyReq) ProtoMessage() {} + +func (x *GetAllKeyReq) ProtoReflect() protoreflect.Message { + mi := &file_key_key_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 GetAllKeyReq.ProtoReflect.Descriptor instead. +func (*GetAllKeyReq) Descriptor() ([]byte, []int) { + return file_key_key_proto_rawDescGZIP(), []int{2} +} + +func (x *GetAllKeyReq) GetUserId() string { + if x != nil { + return x.UserId + } + return "" +} + +type GetAllKeyResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Keys map[string]string `protobuf:"bytes,1,rep,name=keys,proto3" json:"keys" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` +} + +func (x *GetAllKeyResp) Reset() { + *x = GetAllKeyResp{} + if protoimpl.UnsafeEnabled { + mi := &file_key_key_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetAllKeyResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetAllKeyResp) ProtoMessage() {} + +func (x *GetAllKeyResp) ProtoReflect() protoreflect.Message { + mi := &file_key_key_proto_msgTypes[3] + 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 GetAllKeyResp.ProtoReflect.Descriptor instead. +func (*GetAllKeyResp) Descriptor() ([]byte, []int) { + return file_key_key_proto_rawDescGZIP(), []int{3} +} + +func (x *GetAllKeyResp) GetKeys() map[string]string { + if x != nil { + return x.Keys + } + return nil +} + +var File_key_key_proto protoreflect.FileDescriptor + +var file_key_key_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x6b, 0x65, 0x79, 0x2f, 0x6b, 0x65, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x15, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x22, 0x77, 0x0a, 0x09, 0x47, 0x65, 0x74, 0x4b, 0x65, 0x79, + 0x52, 0x65, 0x71, 0x12, 0x26, 0x0a, 0x0e, 0x63, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x73, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0e, 0x63, 0x6f, 0x6e, + 0x76, 0x65, 0x72, 0x73, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x63, + 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x73, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x10, 0x63, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x73, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, + 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22, + 0x1b, 0x0a, 0x07, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, + 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x26, 0x0a, 0x0c, + 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, + 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, + 0x65, 0x72, 0x49, 0x64, 0x22, 0x8c, 0x01, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x4b, + 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x12, 0x42, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x2e, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x65, 0x72, + 0x76, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x47, 0x65, 0x74, + 0x41, 0x6c, 0x6c, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x2e, 0x4b, 0x65, 0x79, 0x73, 0x45, + 0x6e, 0x74, 0x72, 0x79, 0x52, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x1a, 0x37, 0x0a, 0x09, 0x4b, 0x65, + 0x79, 0x73, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, + 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, + 0x02, 0x38, 0x01, 0x32, 0xa9, 0x01, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x4a, 0x0a, 0x06, 0x47, + 0x65, 0x74, 0x4b, 0x65, 0x79, 0x12, 0x20, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x65, + 0x72, 0x76, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x47, 0x65, + 0x74, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, 0x1a, 0x1e, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, + 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, + 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x12, 0x56, 0x0a, 0x09, 0x47, 0x65, 0x74, 0x41, 0x6c, + 0x6c, 0x4b, 0x65, 0x79, 0x12, 0x23, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x65, 0x72, + 0x76, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x47, 0x65, 0x74, + 0x41, 0x6c, 0x6c, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, 0x1a, 0x24, 0x2e, 0x4f, 0x70, 0x65, 0x6e, + 0x49, 0x4d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, + 0x66, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x42, + 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x4f, 0x70, + 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x44, 0x4b, 0x2f, 0x4f, 0x70, 0x65, 0x6e, 0x2d, 0x49, 0x4d, 0x2d, + 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x2f, 0x6b, 0x65, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_key_key_proto_rawDescOnce sync.Once + file_key_key_proto_rawDescData = file_key_key_proto_rawDesc +) + +func file_key_key_proto_rawDescGZIP() []byte { + file_key_key_proto_rawDescOnce.Do(func() { + file_key_key_proto_rawDescData = protoimpl.X.CompressGZIP(file_key_key_proto_rawDescData) + }) + return file_key_key_proto_rawDescData +} + +var file_key_key_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_key_key_proto_goTypes = []interface{}{ + (*GetKeyReq)(nil), // 0: OpenIMServer.protobuf.GetKeyReq + (*KeyResp)(nil), // 1: OpenIMServer.protobuf.KeyResp + (*GetAllKeyReq)(nil), // 2: OpenIMServer.protobuf.GetAllKeyReq + (*GetAllKeyResp)(nil), // 3: OpenIMServer.protobuf.GetAllKeyResp + nil, // 4: OpenIMServer.protobuf.GetAllKeyResp.KeysEntry +} +var file_key_key_proto_depIdxs = []int32{ + 4, // 0: OpenIMServer.protobuf.GetAllKeyResp.keys:type_name -> OpenIMServer.protobuf.GetAllKeyResp.KeysEntry + 0, // 1: OpenIMServer.protobuf.key.GetKey:input_type -> OpenIMServer.protobuf.GetKeyReq + 2, // 2: OpenIMServer.protobuf.key.GetAllKey:input_type -> OpenIMServer.protobuf.GetAllKeyReq + 1, // 3: OpenIMServer.protobuf.key.GetKey:output_type -> OpenIMServer.protobuf.KeyResp + 3, // 4: OpenIMServer.protobuf.key.GetAllKey:output_type -> OpenIMServer.protobuf.GetAllKeyResp + 3, // [3:5] is the sub-list for method output_type + 1, // [1:3] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_key_key_proto_init() } +func file_key_key_proto_init() { + if File_key_key_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_key_key_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetKeyReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_key_key_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KeyResp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_key_key_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetAllKeyReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_key_key_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetAllKeyResp); 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_key_key_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_key_key_proto_goTypes, + DependencyIndexes: file_key_key_proto_depIdxs, + MessageInfos: file_key_key_proto_msgTypes, + }.Build() + File_key_key_proto = out.File + file_key_key_proto_rawDesc = nil + file_key_key_proto_goTypes = nil + file_key_key_proto_depIdxs = nil +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConnInterface + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// KeyClient is the client API for Key service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type KeyClient interface { + GetKey(ctx context.Context, in *GetKeyReq, opts ...grpc.CallOption) (*KeyResp, error) + GetAllKey(ctx context.Context, in *GetAllKeyReq, opts ...grpc.CallOption) (*GetAllKeyResp, error) +} + +type keyClient struct { + cc grpc.ClientConnInterface +} + +func NewKeyClient(cc grpc.ClientConnInterface) KeyClient { + return &keyClient{cc} +} + +func (c *keyClient) GetKey(ctx context.Context, in *GetKeyReq, opts ...grpc.CallOption) (*KeyResp, error) { + out := new(KeyResp) + err := c.cc.Invoke(ctx, "/OpenIMServer.protobuf.key/GetKey", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *keyClient) GetAllKey(ctx context.Context, in *GetAllKeyReq, opts ...grpc.CallOption) (*GetAllKeyResp, error) { + out := new(GetAllKeyResp) + err := c.cc.Invoke(ctx, "/OpenIMServer.protobuf.key/GetAllKey", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// KeyServer is the server API for Key service. +type KeyServer interface { + GetKey(context.Context, *GetKeyReq) (*KeyResp, error) + GetAllKey(context.Context, *GetAllKeyReq) (*GetAllKeyResp, error) +} + +// UnimplementedKeyServer can be embedded to have forward compatible implementations. +type UnimplementedKeyServer struct { +} + +func (*UnimplementedKeyServer) GetKey(context.Context, *GetKeyReq) (*KeyResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetKey not implemented") +} +func (*UnimplementedKeyServer) GetAllKey(context.Context, *GetAllKeyReq) (*GetAllKeyResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetAllKey not implemented") +} + +func RegisterKeyServer(s *grpc.Server, srv KeyServer) { + s.RegisterService(&_Key_serviceDesc, srv) +} + +func _Key_GetKey_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetKeyReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(KeyServer).GetKey(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/OpenIMServer.protobuf.key/GetKey", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(KeyServer).GetKey(ctx, req.(*GetKeyReq)) + } + return interceptor(ctx, in, info, handler) +} + +func _Key_GetAllKey_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetAllKeyReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(KeyServer).GetAllKey(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/OpenIMServer.protobuf.key/GetAllKey", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(KeyServer).GetAllKey(ctx, req.(*GetAllKeyReq)) + } + return interceptor(ctx, in, info, handler) +} + +var _Key_serviceDesc = grpc.ServiceDesc{ + ServiceName: "OpenIMServer.protobuf.key", + HandlerType: (*KeyServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetKey", + Handler: _Key_GetKey_Handler, + }, + { + MethodName: "GetAllKey", + Handler: _Key_GetAllKey_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "key/key.proto", +} diff --git a/pkg/proto/key/key.proto b/pkg/proto/key/key.proto new file mode 100644 index 000000000..28effc60f --- /dev/null +++ b/pkg/proto/key/key.proto @@ -0,0 +1,25 @@ +syntax = "proto3"; + +package OpenIMServer.protobuf; + +option go_package = "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/key"; + +message GetKeyReq { + string conversationId = 1; + uint32 conversationType = 2; + string userId = 3; +} +message KeyResp { + string key = 1; +} +message GetAllKeyReq { + string userId = 1; +} +message GetAllKeyResp { + map keys = 1; +} + +service key { + rpc GetKey(GetKeyReq) returns(KeyResp); + rpc GetAllKey(GetAllKeyReq) returns(GetAllKeyResp); +} \ No newline at end of file diff --git a/pkg/rpcclient/key.go b/pkg/rpcclient/key.go new file mode 100644 index 000000000..b868a02ce --- /dev/null +++ b/pkg/rpcclient/key.go @@ -0,0 +1,23 @@ +package rpcclient + +import ( + "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/key" + "google.golang.org/grpc" +) + +func NewKey(discov discoveryregistry.SvcDiscoveryRegistry) *Key { + conn, err := discov.GetConn(context.Background(), "key") + if err != nil { + panic(err) + } + client := key.NewKeyClient(conn) + return &Key{discov: discov, conn: conn, Client: client} +} + +type Key struct { + conn grpc.ClientConnInterface + Client key.KeyClient + discov discoveryregistry.SvcDiscoveryRegistry +} diff --git a/pkg/utils/hash.go b/pkg/utils/hash.go new file mode 100644 index 000000000..1c070c6a4 --- /dev/null +++ b/pkg/utils/hash.go @@ -0,0 +1,57 @@ +package utils + +import ( + "crypto/md5" + "crypto/sha1" + "encoding/hex" + "fmt" + "hash/fnv" + "sort" + "strings" +) + +func Get2StringHash(str1, str2 string) uint32 { + // 将两个字符串进行拼接 + concatenated := strings.Join([]string{str1, str2}, "") + // 将拼接后的字符串转换为字节数组 + bytes := []byte(concatenated) + // 对字节数组进行排序 + sort.Slice(bytes, func(i, j int) bool { + return bytes[i] < bytes[j] + }) + // 创建一个新的FNV哈希对象 + hash := fnv.New32() + // 计算排序后的字节数组的哈希值 + hash.Write(bytes) + hashValue := hash.Sum32() + + fmt.Println("Hash Value:", hashValue) + return hashValue +} +func GetStringHash(str string) string { + // 使用MD5哈希算法获取哈希值 + md5Hash := md5.Sum([]byte(str)) + md5HashString := hex.EncodeToString(md5Hash[:]) + fmt.Println("MD5:", md5HashString) + + // 使用SHA1哈希算法获取哈希值 + sha1Hash := sha1.Sum([]byte(str)) + sha1HashString := hex.EncodeToString(sha1Hash[:]) + fmt.Println("SHA1:", sha1HashString) + return sha1HashString + //// 使用SHA256哈希算法获取哈希值 + //sha256Hash := sha256.Sum256([]byte(str)) + //sha256HashString := hex.EncodeToString(sha256Hash[:]) + //fmt.Println("SHA256:", sha256HashString) +} +func GetStringHashInt(str string) uint32 { + // 创建一个新的FNV哈希对象 + hash := fnv.New32() + + // 将字符串转换为字节数组并计算哈希值 + hash.Write([]byte(str)) + hashValue := hash.Sum32() + + fmt.Println("Hash Value:", hashValue) + return hashValue +} diff --git a/test/main/main.go b/test/main/main.go new file mode 100644 index 000000000..5d0c5da70 --- /dev/null +++ b/test/main/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "crypto/md5" + "crypto/sha1" + "encoding/hex" + "fmt" + "hash/fnv" + "sort" + "strings" +) + +func main() { + //fmt.Println(GetStringHash("1231asdasd")) + //fmt.Println(GetStringHashInt("1231asdasd")) + Get2StringHash("", "") +} + +func Get2StringHash(str1, str2 string) uint32 { + // 将两个字符串进行拼接 + concatenated := strings.Join([]string{str1, str2}, "") + // 将拼接后的字符串转换为字节数组 + bytes := []byte(concatenated) + // 对字节数组进行排序 + sort.Slice(bytes, func(i, j int) bool { + return bytes[i] < bytes[j] + }) + // 创建一个新的FNV哈希对象 + hash := fnv.New32() + // 计算排序后的字节数组的哈希值 + hash.Write(bytes) + hashValue := hash.Sum32() + + fmt.Println("Hash Value:", hashValue) + return hashValue +} +func GetStringHash(str string) string { + // 使用MD5哈希算法获取哈希值 + md5Hash := md5.Sum([]byte(str)) + md5HashString := hex.EncodeToString(md5Hash[:]) + fmt.Println("MD5:", md5HashString) + + // 使用SHA1哈希算法获取哈希值 + sha1Hash := sha1.Sum([]byte(str)) + sha1HashString := hex.EncodeToString(sha1Hash[:]) + fmt.Println("SHA1:", sha1HashString) + return sha1HashString + //// 使用SHA256哈希算法获取哈希值 + //sha256Hash := sha256.Sum256([]byte(str)) + //sha256HashString := hex.EncodeToString(sha256Hash[:]) + //fmt.Println("SHA256:", sha256HashString) +} +func GetStringHashInt(str string) uint32 { + // 创建一个新的FNV哈希对象 + hash := fnv.New32() + + // 将字符串转换为字节数组并计算哈希值 + hash.Write([]byte(str)) + hashValue := hash.Sum32() + + fmt.Println("Hash Value:", hashValue) + return hashValue +}