mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-23 18:00:32 +08:00
rpc custom header
This commit is contained in:
parent
c0d72d91fb
commit
900858e4ea
@ -275,7 +275,7 @@ const OpUserID = "opUserID"
|
||||
const ConnID = "connID"
|
||||
const OpUserPlatform = "platform"
|
||||
const Token = "token"
|
||||
const RpcMwCustom = "hCustom"
|
||||
const RpcCustomHeader = "customHeader" // rpc中间件自定义ctx参数
|
||||
const CheckKey = "CheckKey"
|
||||
|
||||
const (
|
||||
|
@ -15,8 +15,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
block cipher.Block
|
||||
once sync.Once
|
||||
block cipher.Block
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -37,7 +37,7 @@ func initAesKey() {
|
||||
func genReqKey(args []string) string {
|
||||
initAesKey()
|
||||
plaintext := md5.Sum([]byte(strings.Join(args, ":")))
|
||||
var iv = make([]byte, aes.BlockSize, aes.BlockSize+md5.Size)
|
||||
iv := make([]byte, aes.BlockSize, aes.BlockSize+md5.Size)
|
||||
if _, err := rand.Read(iv); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -24,14 +24,18 @@ func rpcClientInterceptor(ctx context.Context, method string, req, resp interfac
|
||||
}
|
||||
log.ZInfo(ctx, "rpc client req", "funcName", method, "req", rpcString(req))
|
||||
md := metadata.Pairs()
|
||||
if keys, _ := ctx.Value(constant.RpcMwCustom).([]string); len(keys) > 0 {
|
||||
if keys, _ := ctx.Value(constant.RpcCustomHeader).([]string); len(keys) > 0 {
|
||||
for _, key := range keys {
|
||||
val, ok := ctx.Value(key).([]string)
|
||||
if !ok {
|
||||
return errs.ErrInternalServer.Wrap(fmt.Sprintf("ctx missing key %s", key))
|
||||
}
|
||||
if len(val) == 0 {
|
||||
return errs.ErrInternalServer.Wrap(fmt.Sprintf("ctx key %s value is empty", key))
|
||||
}
|
||||
md.Set(key, val...)
|
||||
}
|
||||
md.Set(constant.RpcCustomHeader, keys...)
|
||||
}
|
||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||
if !ok {
|
||||
|
@ -56,6 +56,15 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
if !ok {
|
||||
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
|
||||
}
|
||||
if keys := md.Get(constant.RpcCustomHeader); len(keys) > 0 {
|
||||
for _, key := range keys {
|
||||
values := md.Get(key)
|
||||
if len(values) == 0 {
|
||||
return nil, status.New(codes.InvalidArgument, fmt.Sprintf("missing metadata key %s", key)).Err()
|
||||
}
|
||||
ctx = context.WithValue(ctx, key, values)
|
||||
}
|
||||
}
|
||||
args := make([]string, 0, 4)
|
||||
if opts := md.Get(constant.OperationID); len(opts) != 1 || opts[0] == "" {
|
||||
return nil, status.New(codes.InvalidArgument, "operationID error").Err()
|
||||
|
Loading…
x
Reference in New Issue
Block a user