diff --git a/cmd/openim-rpc/openim-rpc-aeskey/Makefile b/cmd/openim-rpc/openim-rpc-aeskey/Makefile new file mode 100644 index 000000000..38d180302 --- /dev/null +++ b/cmd/openim-rpc/openim-rpc-aeskey/Makefile @@ -0,0 +1,34 @@ +.PHONY: all build run gotool install clean help + +NAME=openim-rpc-aesKey +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-aeskey/deploy.Dockerfile b/cmd/openim-rpc/openim-rpc-aeskey/deploy.Dockerfile new file mode 100644 index 000000000..3f3ac082b --- /dev/null +++ b/cmd/openim-rpc/openim-rpc-aeskey/deploy.Dockerfile @@ -0,0 +1,32 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM ubuntu + +WORKDIR /Open-IM-Server/bin + +RUN apt-get update && apt-get install apt-transport-https && apt-get install procps\ +&&apt-get install net-tools +#Non-interactive operation +ENV DEBIAN_FRONTEND=noninteractive +RUN apt-get install -y vim curl tzdata gawk +#Time zone adjusted to East eighth District +RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && dpkg-reconfigure -f noninteractive tzdata +RUN apt-get -qq update \ + && apt-get -qq install -y --no-install-recommends ca-certificates curl +COPY ./openim-rpc-aesKey ./ + +VOLUME ["/Open-IM-Server/logs","/Open-IM-Server/config"] + +CMD ["./openim-rpc-aesKey", "--port", "10160"] diff --git a/cmd/openim-rpc/openim-rpc-aeskey/main.go b/cmd/openim-rpc/openim-rpc-aeskey/main.go new file mode 100644 index 000000000..1708c39e8 --- /dev/null +++ b/cmd/openim-rpc/openim-rpc-aeskey/main.go @@ -0,0 +1,33 @@ +// Copyright © 2023 OpenIM. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/auth" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" +) + +func main() { + aesKeyCmd := cmd.NewRpcCmd("aesKey") + aesKeyCmd.AddPortFlag() + aesKeyCmd.AddPrometheusPortFlag() + if err := aesKeyCmd.Exec(); err != nil { + panic(err.Error()) + } + if err := aesKeyCmd.StartSvr(config.Config.RpcRegisterName.OpenImAesKeyName, auth.Start); err != nil { + panic(err.Error()) + } +} diff --git a/config/config.yaml b/config/config.yaml index 348aa2e93..c094dc73a 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -172,6 +172,7 @@ rpcRegisterName: openImAuthName: Auth openImConversationName: Conversation openImThirdName: Third + openImAesKeyName: aesKey # Log configuration # @@ -372,3 +373,7 @@ prometheus: rtcPrometheusPort: [ 21300 ] thirdPrometheusPort: [ 21301 ] messageTransferPrometheusPort: [ 21400, 21401, 21402, 21403 ] + +encipher: + enable: false + algorithm: "aes" diff --git a/go.mod b/go.mod index 686cadce4..e722b64e4 100644 --- a/go.mod +++ b/go.mod @@ -141,3 +141,7 @@ require ( google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) + +//replace ( +// github.com/OpenIMSDK/protocol => ./pkg/proto +//) \ No newline at end of file diff --git a/internal/api/aes_key.go b/internal/api/aes_key.go new file mode 100644 index 000000000..2b4e93e7a --- /dev/null +++ b/internal/api/aes_key.go @@ -0,0 +1,21 @@ +package api + +import ( + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/aes_key" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/tools/a2r" + "github.com/gin-gonic/gin" +) + +type AesKeyApi rpcclient.AesKey + +func NewAesKeyApi(client rpcclient.AesKey) AesKeyApi { + return AesKeyApi(client) +} + +func (a *AesKeyApi) GetKey(c *gin.Context) { + a2r.Call(aes_key.AesKeyClient.GetAesKey, a.Client, c) +} +func (a *AesKeyApi) GetAllKey(c *gin.Context) { + a2r.Call(aes_key.AesKeyClient.GetAllAesKey, a.Client, c) +} diff --git a/internal/api/encipher.go b/internal/api/encipher.go new file mode 100644 index 000000000..a4dd6527d --- /dev/null +++ b/internal/api/encipher.go @@ -0,0 +1,11 @@ +package api + +import ( + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/gin-gonic/gin" + "net/http" +) + +func IsEncipher(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": config.Config.Encipher}) +} diff --git a/internal/api/route.go b/internal/api/route.go index 5fd3f115a..9771e29b1 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -57,6 +57,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive conversationRpc := rpcclient.NewConversation(discov) authRpc := rpcclient.NewAuth(discov) thirdRpc := rpcclient.NewThird(discov) + aesKeyRpc := rpcclient.NewAesKey(discov) u := NewUserApi(*userRpc) m := NewMessageApi(messageRpc, userRpc) @@ -202,6 +203,17 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive statisticsGroup.POST("/group/create", g.GroupCreateCount) statisticsGroup.POST("/group/active", m.GetActiveGroup) } + encipherGroup := r.Group("encipher") + { + encipherGroup.POST("is_encipher", IsEncipher) + aesGroup := encipherGroup.Group("/aes", ParseToken) + { + k := NewAesKeyApi(*aesKeyRpc) + aesGroup.POST("/get_key", k.GetKey) + aesGroup.POST("/get_all_key", k.GetAllKey) + } + } + return r } diff --git a/internal/rpc/aes_key/key.go b/internal/rpc/aes_key/key.go new file mode 100644 index 000000000..3b3c3d7d9 --- /dev/null +++ b/internal/rpc/aes_key/key.go @@ -0,0 +1,92 @@ +package aes_key + +import ( + "context" + utils "github.com/OpenIMSDK/Open-IM-Server/pkg/aes_utils" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" + relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/aes_key" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/tools/discoveryregistry" + "google.golang.org/grpc" +) + +func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error { + db, err := relation.NewGormDB() + if err != nil { + return err + } + if err := db.AutoMigrate(&relationTb.AesKeyModel{}); err != nil { + return err + } + if err != nil { + return err + } + //redis, err := cache.NewRedis() + //if err != nil { + // return err + //} + //friendDB := relation.NewFriendGorm(db) + userRpcClient := rpcclient.NewAesKeyRpcClient(client) + conversationRpcClient := rpcclient.NewConversationRpcClient(client) + aesKeydatabase := controller.NewAesKeyDatabase(relation.NewAesKeyGorm(db)) + aes_key.RegisterAesKeyServer(server, &keyServer{ + AesKeyDatabase: aesKeydatabase, + //FriendDatabase: controller.NewFriendDatabase( + // friendDB, + // relation.NewFriendRequestGorm(db), + // cache.NewFriendCacheRedis(redis, friendDB, cache.GetDefaultOpt()), + // tx.NewGorm(db), + //), + //GroupDatabase: nil, + User: userRpcClient, + conversationRpcClient: conversationRpcClient, + }) + return nil +} + +type keyServer struct { + AesKeyDatabase controller.AesKeyDatabase + FriendDatabase controller.FriendDatabase + GroupDatabase controller.GroupDatabase + User rpcclient.AesKeyRpcClient + conversationRpcClient rpcclient.ConversationRpcClient +} + +func (k keyServer) GetAesKey(ctx context.Context, req *aes_key.GetAesKeyReq) (*aes_key.GetAesKeyResp, error) { + key, err := k.AesKeyDatabase.GetAesKey(ctx, req.UId, req.SId, req.SType) + if err != nil { + aesKey := utils.GenerateAesKey(utils.Get2StringHash(req.UId, req.SId)) + model := relationTb.AesKeyModel{ + UserID: req.UId, + ConversationID: req.SId, + AesKey: aesKey, + ConversationType: req.SType, + } + err1 := k.AesKeyDatabase.InstallAesKey(ctx, model) + if err1 != nil { + return nil, err + } + model.UserID = req.SId + model.ConversationID = req.UId + err1 = k.AesKeyDatabase.InstallAesKey(ctx, model) + if err1 != nil { + return nil, err + } + return &aes_key.GetAesKeyResp{Key: aesKey}, nil + } + return &aes_key.GetAesKeyResp{Key: key.AesKey}, nil +} + +func (k keyServer) GetAllAesKey(ctx context.Context, req *aes_key.GetAllAesKeyReq) (*aes_key.GetAllAesKeyResp, error) { + key, err := k.AesKeyDatabase.GetAllAesKey(ctx, req.UId) + if err != nil { + return &aes_key.GetAllAesKeyResp{}, err + } + resp := &aes_key.GetAllAesKeyResp{Keys: make(map[string]string)} + for i := range key { + resp.Keys[key[i].ConversationID] = key[i].AesKey + } + return resp, nil +} diff --git a/pkg/aes_utils/hash.go b/pkg/aes_utils/hash.go new file mode 100644 index 000000000..b77bb2fc7 --- /dev/null +++ b/pkg/aes_utils/hash.go @@ -0,0 +1,65 @@ +package utils + +import ( + "crypto/md5" + "crypto/sha1" + "encoding/hex" + "fmt" + "hash/fnv" + "sort" + "strconv" + "strings" +) + +// MD5 生成key +func GenerateAesKey(text string) string { + data := []byte(text) + s := fmt.Sprintf("%x", md5.Sum(data)) // md5.Sum(data)返回byte类型,fmt.Sprintf()格式化 + return s[0:16] +} + +func Get2StringHash(str1, str2 string) string { + // 将两个字符串进行拼接 + 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 strconv.Itoa(int(hashValue)) +} +func HashGeneratePackageId(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/pkg/common/config/config.go b/pkg/common/config/config.go index 1e0bbba16..692dd3d6b 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -159,6 +159,7 @@ type configStruct struct { OpenImAuthName string `yaml:"openImAuthName"` OpenImConversationName string `yaml:"openImConversationName"` OpenImThirdName string `yaml:"openImThirdName"` + OpenImAesKeyName string `yaml:"openImAesKeyName"` } `yaml:"rpcRegisterName"` Log struct { @@ -257,6 +258,10 @@ type configStruct struct { MessageTransferPrometheusPort []int `yaml:"messageTransferPrometheusPort"` ThirdPrometheusPort []int `yaml:"thirdPrometheusPort"` } `yaml:"prometheus"` + Encipher struct { + Enable bool `yaml:"enable" json:"enable"` + Algorithm string `yaml:"algorithm" json:"algorithm"` + } `yaml:"encipher"` Notification notification `yaml:"notification"` } diff --git a/pkg/common/db/controller/aes_key.go b/pkg/common/db/controller/aes_key.go new file mode 100644 index 000000000..7f628e1fd --- /dev/null +++ b/pkg/common/db/controller/aes_key.go @@ -0,0 +1,38 @@ +package controller + +import ( + "context" + relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" +) + +type aesKeyDatabase struct { + aesKeyDB relationTb.AesKeyModelInterface +} + +type AesKeyDatabase interface { + //生成AesKey + InstallAesKey(ctx context.Context, aesKey relationTb.AesKeyModel) error + GetAesKey(ctx context.Context, userId, cid string, cType int32) (aesKey *relationTb.AesKeyModel, err error) + GetAllAesKey(ctx context.Context, userId string) (aesKey []*relationTb.AesKeyModel, err error) +} + +func NewAesKeyDatabase(aesKey relationTb.AesKeyModelInterface) AesKeyDatabase { + return &aesKeyDatabase{ + aesKeyDB: aesKey, + } +} + +func (a *aesKeyDatabase) InstallAesKey(ctx context.Context, aesKey relationTb.AesKeyModel) error { + err := a.aesKeyDB.Install(ctx, aesKey) + if err != nil { + return err + } + return nil +} +func (a *aesKeyDatabase) GetAesKey(ctx context.Context, userId, cid string, cType int32) (aesKey *relationTb.AesKeyModel, err error) { + return a.aesKeyDB.GetAesKey(ctx, userId, cid, cType) +} + +func (a *aesKeyDatabase) GetAllAesKey(ctx context.Context, userId string) (aesKey []*relationTb.AesKeyModel, err error) { + return a.aesKeyDB.GetAllAesKey(ctx, userId) +} diff --git a/pkg/common/db/relation/aes_key_model.go b/pkg/common/db/relation/aes_key_model.go new file mode 100644 index 000000000..c26e63dc5 --- /dev/null +++ b/pkg/common/db/relation/aes_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/tools/utils" + "gorm.io/gorm" +) + +type AesKeyGorm struct { + *MetaDB +} + +func NewAesKeyGorm(db *gorm.DB) relation.AesKeyModelInterface { + return &AesKeyGorm{NewMetaDB(db, &relation.AesKeyModel{})} +} +func (a AesKeyGorm) Install(ctx context.Context, aesKey relation.AesKeyModel) (err error) { + return utils.Wrap(a.db(ctx).Create(&aesKey).Error, "") +} +func (a AesKeyGorm) GetAesKey(ctx context.Context, userId, cid string, cType int32) (aesKey *relation.AesKeyModel, err error) { + return aesKey, utils.Wrap(a.db(ctx).Where("user_id = ? and conversation_id=? and conversation_type =?", userId, cid, cType).Find(&aesKey).Error, "") +} + +func (a AesKeyGorm) GetAllAesKey(ctx context.Context, userId string) (aesKey []*relation.AesKeyModel, err error) { + return aesKey, utils.Wrap(a.db(ctx).Where("user_id = ?", userId).Find(aesKey).Error, "") +} diff --git a/pkg/common/db/table/relation/aes_key.go b/pkg/common/db/table/relation/aes_key.go new file mode 100644 index 000000000..fdf9fe2a9 --- /dev/null +++ b/pkg/common/db/table/relation/aes_key.go @@ -0,0 +1,26 @@ +package relation + +import ( + "context" +) + +const ( + AesKeyModelTableName = "aes-keys" +) + +type AesKeyModel struct { + UserID string `gorm:"column:user_id;size:64"` + ConversationID string `gorm:"column:conversation_id;uniqueIndex:idx_key" json:"conversationID"` + AesKey string `gorm:"column:conversation_id" json:"aesKey"` + ConversationType int32 `gorm:"column:conversation_type;uniqueIndex:idx_key" json:"conversationType"` +} + +func (AesKeyModel) TableName() string { + return AesKeyModelTableName +} + +type AesKeyModelInterface interface { + Install(ctx context.Context, aesKey AesKeyModel) (err error) + GetAesKey(ctx context.Context, userId, cid string, cType int32) (aesKey *AesKeyModel, err error) + GetAllAesKey(ctx context.Context, userId string) (aesKey []*AesKeyModel, err error) +} diff --git a/pkg/proto/aes_key/aes_key.pb.go b/pkg/proto/aes_key/aes_key.pb.go new file mode 100644 index 000000000..e09813d3e --- /dev/null +++ b/pkg/proto/aes_key/aes_key.pb.go @@ -0,0 +1,494 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.29.1 +// protoc v4.22.0 +// source: aes_key/aes_key.proto + +package aes_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 GetAesKeyReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + SId string `protobuf:"bytes,1,opt,name=sId,proto3" json:"sId"` + UId string `protobuf:"bytes,2,opt,name=uId,proto3" json:"uId"` + SType int32 `protobuf:"varint,3,opt,name=sType,proto3" json:"sType"` +} + +func (x *GetAesKeyReq) Reset() { + *x = GetAesKeyReq{} + if protoimpl.UnsafeEnabled { + mi := &file_aes_key_aes_key_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetAesKeyReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetAesKeyReq) ProtoMessage() {} + +func (x *GetAesKeyReq) ProtoReflect() protoreflect.Message { + mi := &file_aes_key_aes_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 GetAesKeyReq.ProtoReflect.Descriptor instead. +func (*GetAesKeyReq) Descriptor() ([]byte, []int) { + return file_aes_key_aes_key_proto_rawDescGZIP(), []int{0} +} + +func (x *GetAesKeyReq) GetSId() string { + if x != nil { + return x.SId + } + return "" +} + +func (x *GetAesKeyReq) GetUId() string { + if x != nil { + return x.UId + } + return "" +} + +func (x *GetAesKeyReq) GetSType() int32 { + if x != nil { + return x.SType + } + return 0 +} + +type GetAllAesKeyReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UId string `protobuf:"bytes,1,opt,name=uId,proto3" json:"uId"` +} + +func (x *GetAllAesKeyReq) Reset() { + *x = GetAllAesKeyReq{} + if protoimpl.UnsafeEnabled { + mi := &file_aes_key_aes_key_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetAllAesKeyReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetAllAesKeyReq) ProtoMessage() {} + +func (x *GetAllAesKeyReq) ProtoReflect() protoreflect.Message { + mi := &file_aes_key_aes_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 GetAllAesKeyReq.ProtoReflect.Descriptor instead. +func (*GetAllAesKeyReq) Descriptor() ([]byte, []int) { + return file_aes_key_aes_key_proto_rawDescGZIP(), []int{1} +} + +func (x *GetAllAesKeyReq) GetUId() string { + if x != nil { + return x.UId + } + return "" +} + +type GetAesKeyResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key"` +} + +func (x *GetAesKeyResp) Reset() { + *x = GetAesKeyResp{} + if protoimpl.UnsafeEnabled { + mi := &file_aes_key_aes_key_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetAesKeyResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetAesKeyResp) ProtoMessage() {} + +func (x *GetAesKeyResp) ProtoReflect() protoreflect.Message { + mi := &file_aes_key_aes_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 GetAesKeyResp.ProtoReflect.Descriptor instead. +func (*GetAesKeyResp) Descriptor() ([]byte, []int) { + return file_aes_key_aes_key_proto_rawDescGZIP(), []int{2} +} + +func (x *GetAesKeyResp) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +type GetAllAesKeyResp 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 *GetAllAesKeyResp) Reset() { + *x = GetAllAesKeyResp{} + if protoimpl.UnsafeEnabled { + mi := &file_aes_key_aes_key_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetAllAesKeyResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetAllAesKeyResp) ProtoMessage() {} + +func (x *GetAllAesKeyResp) ProtoReflect() protoreflect.Message { + mi := &file_aes_key_aes_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 GetAllAesKeyResp.ProtoReflect.Descriptor instead. +func (*GetAllAesKeyResp) Descriptor() ([]byte, []int) { + return file_aes_key_aes_key_proto_rawDescGZIP(), []int{3} +} + +func (x *GetAllAesKeyResp) GetKeys() map[string]string { + if x != nil { + return x.Keys + } + return nil +} + +var File_aes_key_aes_key_proto protoreflect.FileDescriptor + +var file_aes_key_aes_key_proto_rawDesc = []byte{ + 0x0a, 0x15, 0x61, 0x65, 0x73, 0x5f, 0x6b, 0x65, 0x79, 0x2f, 0x61, 0x65, 0x73, 0x5f, 0x6b, 0x65, + 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x11, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, + 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x22, 0x48, 0x0a, 0x0c, 0x47, 0x65, + 0x74, 0x41, 0x65, 0x73, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x49, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, 0x49, 0x64, 0x12, 0x10, 0x0a, 0x03, + 0x75, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x49, 0x64, 0x12, 0x14, + 0x0a, 0x05, 0x73, 0x54, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, + 0x54, 0x79, 0x70, 0x65, 0x22, 0x23, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x41, 0x65, + 0x73, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x49, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x49, 0x64, 0x22, 0x21, 0x0a, 0x0d, 0x47, 0x65, 0x74, + 0x41, 0x65, 0x73, 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, 0x8e, 0x01, 0x0a, + 0x10, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x41, 0x65, 0x73, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, + 0x70, 0x12, 0x41, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x2d, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x61, + 0x75, 0x74, 0x68, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x41, 0x65, 0x73, 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, 0xb1, 0x01, + 0x0a, 0x06, 0x41, 0x65, 0x73, 0x4b, 0x65, 0x79, 0x12, 0x4e, 0x0a, 0x09, 0x67, 0x65, 0x74, 0x41, + 0x65, 0x73, 0x4b, 0x65, 0x79, 0x12, 0x1f, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x65, + 0x72, 0x76, 0x65, 0x72, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x65, 0x73, + 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, 0x1a, 0x20, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, + 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x47, 0x65, 0x74, 0x41, 0x65, + 0x73, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x12, 0x57, 0x0a, 0x0c, 0x67, 0x65, 0x74, 0x41, + 0x6c, 0x6c, 0x41, 0x65, 0x73, 0x4b, 0x65, 0x79, 0x12, 0x22, 0x2e, 0x4f, 0x70, 0x65, 0x6e, 0x49, + 0x4d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x47, 0x65, 0x74, + 0x41, 0x6c, 0x6c, 0x41, 0x65, 0x73, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, 0x1a, 0x23, 0x2e, 0x4f, + 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x61, 0x75, 0x74, 0x68, + 0x2e, 0x47, 0x65, 0x74, 0x41, 0x6c, 0x6c, 0x41, 0x65, 0x73, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, + 0x70, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x4f, 0x70, 0x65, 0x6e, 0x49, 0x4d, 0x53, 0x44, 0x4b, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, + 0x6f, 0x6c, 0x2f, 0x61, 0x65, 0x73, 0x5f, 0x6b, 0x65, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_aes_key_aes_key_proto_rawDescOnce sync.Once + file_aes_key_aes_key_proto_rawDescData = file_aes_key_aes_key_proto_rawDesc +) + +func file_aes_key_aes_key_proto_rawDescGZIP() []byte { + file_aes_key_aes_key_proto_rawDescOnce.Do(func() { + file_aes_key_aes_key_proto_rawDescData = protoimpl.X.CompressGZIP(file_aes_key_aes_key_proto_rawDescData) + }) + return file_aes_key_aes_key_proto_rawDescData +} + +var file_aes_key_aes_key_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_aes_key_aes_key_proto_goTypes = []interface{}{ + (*GetAesKeyReq)(nil), // 0: OpenIMServer.auth.GetAesKeyReq + (*GetAllAesKeyReq)(nil), // 1: OpenIMServer.auth.GetAllAesKeyReq + (*GetAesKeyResp)(nil), // 2: OpenIMServer.auth.GetAesKeyResp + (*GetAllAesKeyResp)(nil), // 3: OpenIMServer.auth.GetAllAesKeyResp + nil, // 4: OpenIMServer.auth.GetAllAesKeyResp.KeysEntry +} +var file_aes_key_aes_key_proto_depIdxs = []int32{ + 4, // 0: OpenIMServer.auth.GetAllAesKeyResp.keys:type_name -> OpenIMServer.auth.GetAllAesKeyResp.KeysEntry + 0, // 1: OpenIMServer.auth.AesKey.getAesKey:input_type -> OpenIMServer.auth.GetAesKeyReq + 1, // 2: OpenIMServer.auth.AesKey.getAllAesKey:input_type -> OpenIMServer.auth.GetAllAesKeyReq + 2, // 3: OpenIMServer.auth.AesKey.getAesKey:output_type -> OpenIMServer.auth.GetAesKeyResp + 3, // 4: OpenIMServer.auth.AesKey.getAllAesKey:output_type -> OpenIMServer.auth.GetAllAesKeyResp + 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_aes_key_aes_key_proto_init() } +func file_aes_key_aes_key_proto_init() { + if File_aes_key_aes_key_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_aes_key_aes_key_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetAesKeyReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_aes_key_aes_key_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetAllAesKeyReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_aes_key_aes_key_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetAesKeyResp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_aes_key_aes_key_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetAllAesKeyResp); 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_aes_key_aes_key_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_aes_key_aes_key_proto_goTypes, + DependencyIndexes: file_aes_key_aes_key_proto_depIdxs, + MessageInfos: file_aes_key_aes_key_proto_msgTypes, + }.Build() + File_aes_key_aes_key_proto = out.File + file_aes_key_aes_key_proto_rawDesc = nil + file_aes_key_aes_key_proto_goTypes = nil + file_aes_key_aes_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 + +// AesKeyClient is the client API for AesKey service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type AesKeyClient interface { + GetAesKey(ctx context.Context, in *GetAesKeyReq, opts ...grpc.CallOption) (*GetAesKeyResp, error) + GetAllAesKey(ctx context.Context, in *GetAllAesKeyReq, opts ...grpc.CallOption) (*GetAllAesKeyResp, error) +} + +type aesKeyClient struct { + cc grpc.ClientConnInterface +} + +func NewAesKeyClient(cc grpc.ClientConnInterface) AesKeyClient { + return &aesKeyClient{cc} +} + +func (c *aesKeyClient) GetAesKey(ctx context.Context, in *GetAesKeyReq, opts ...grpc.CallOption) (*GetAesKeyResp, error) { + out := new(GetAesKeyResp) + err := c.cc.Invoke(ctx, "/OpenIMServer.auth.AesKey/getAesKey", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *aesKeyClient) GetAllAesKey(ctx context.Context, in *GetAllAesKeyReq, opts ...grpc.CallOption) (*GetAllAesKeyResp, error) { + out := new(GetAllAesKeyResp) + err := c.cc.Invoke(ctx, "/OpenIMServer.auth.AesKey/getAllAesKey", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// AesKeyServer is the server API for AesKey service. +type AesKeyServer interface { + GetAesKey(context.Context, *GetAesKeyReq) (*GetAesKeyResp, error) + GetAllAesKey(context.Context, *GetAllAesKeyReq) (*GetAllAesKeyResp, error) +} + +// UnimplementedAesKeyServer can be embedded to have forward compatible implementations. +type UnimplementedAesKeyServer struct { +} + +func (*UnimplementedAesKeyServer) GetAesKey(context.Context, *GetAesKeyReq) (*GetAesKeyResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetAesKey not implemented") +} +func (*UnimplementedAesKeyServer) GetAllAesKey(context.Context, *GetAllAesKeyReq) (*GetAllAesKeyResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetAllAesKey not implemented") +} + +func RegisterAesKeyServer(s *grpc.Server, srv AesKeyServer) { + s.RegisterService(&_AesKey_serviceDesc, srv) +} + +func _AesKey_GetAesKey_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetAesKeyReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AesKeyServer).GetAesKey(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/OpenIMServer.auth.AesKey/GetAesKey", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AesKeyServer).GetAesKey(ctx, req.(*GetAesKeyReq)) + } + return interceptor(ctx, in, info, handler) +} + +func _AesKey_GetAllAesKey_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetAllAesKeyReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AesKeyServer).GetAllAesKey(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/OpenIMServer.auth.AesKey/GetAllAesKey", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AesKeyServer).GetAllAesKey(ctx, req.(*GetAllAesKeyReq)) + } + return interceptor(ctx, in, info, handler) +} + +var _AesKey_serviceDesc = grpc.ServiceDesc{ + ServiceName: "OpenIMServer.auth.AesKey", + HandlerType: (*AesKeyServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "getAesKey", + Handler: _AesKey_GetAesKey_Handler, + }, + { + MethodName: "getAllAesKey", + Handler: _AesKey_GetAllAesKey_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "aes_key/aes_key.proto", +} diff --git a/pkg/proto/aes_key/aes_key.proto b/pkg/proto/aes_key/aes_key.proto new file mode 100644 index 000000000..f6457d400 --- /dev/null +++ b/pkg/proto/aes_key/aes_key.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; +package OpenIMServer.auth; +option go_package = "github.com/OpenIMSDK/protocol/aes_key"; + +// protoc --go_out=plugins=grpc:./aes_key --go_opt=module=github.com/OpenIMSDK/protocol/aes_key aes_key/aes_key.proto +message GetAesKeyReq { + string sId = 1; + string uId = 2; + int32 sType = 3; +} +message GetAllAesKeyReq { + string uId = 1; + +} +message GetAesKeyResp { + string key = 1; +} + +message GetAllAesKeyResp { + map keys = 1; +} + +service AesKey{ + rpc getAesKey(GetAesKeyReq) returns(GetAesKeyResp); + rpc getAllAesKey(GetAllAesKeyReq) returns(GetAllAesKeyResp); +} \ No newline at end of file diff --git a/pkg/rpcclient/aes_key.go b/pkg/rpcclient/aes_key.go new file mode 100644 index 000000000..8a6d116c4 --- /dev/null +++ b/pkg/rpcclient/aes_key.go @@ -0,0 +1,43 @@ +package rpcclient + +import ( + "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/aes_key" + "github.com/OpenIMSDK/tools/discoveryregistry" + "google.golang.org/grpc" +) + +type AesKey struct { + conn grpc.ClientConnInterface + Client aes_key.AesKeyClient + discov discoveryregistry.SvcDiscoveryRegistry +} + +func NewAesKey(discov discoveryregistry.SvcDiscoveryRegistry) *AesKey { + conn, err := discov.GetConn(context.Background(), "aesKey") + if err != nil { + panic(err) + } + client := aes_key.NewAesKeyClient(conn) + return &AesKey{discov: discov, conn: conn, Client: client} +} + +type AesKeyRpcClient AesKey + +func NewAesKeyRpcClient(discov discoveryregistry.SvcDiscoveryRegistry) AesKeyRpcClient { + return AesKeyRpcClient(*NewAesKey(discov)) +} +func (a *AesKeyRpcClient) GetKey(ctx context.Context, sId string, sType int32) (*aes_key.GetAesKeyResp, error) { + key, err := a.Client.GetAesKey(ctx, &aes_key.GetAesKeyReq{SId: sId, SType: sType}) + if err != nil { + return nil, err + } + return key, nil +} +func (a *AesKeyRpcClient) GetAllKey(ctx context.Context, uId string) (*aes_key.GetAllAesKeyResp, error) { + key, err := a.Client.GetAllAesKey(ctx, &aes_key.GetAllAesKeyReq{UId: uId}) + if err != nil { + return nil, err + } + return key, nil +}