feat: use robot to migrate code

Signed-off-by: kubbot & kubecub <3293172751ysy@gmail.com>
This commit is contained in:
kubbot & kubecub
2023-06-30 09:45:02 +08:00
parent 2d41819008
commit 539e0fdfb6
529 changed files with 64588 additions and 54413 deletions
+66
View File
@@ -0,0 +1,66 @@
package mw
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/base64"
"errors"
"fmt"
"math/rand"
"strings"
"sync"
"time"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
)
var (
once sync.Once
block cipher.Block
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func initAesKey() {
once.Do(func() {
key := md5.Sum([]byte("openim:" + config.Config.TokenPolicy.AccessSecret))
var err error
block, err = aes.NewCipher(key[:])
if err != nil {
panic(err)
}
})
}
func genReqKey(args []string) string {
initAesKey()
plaintext := md5.Sum([]byte(strings.Join(args, ":")))
iv := make([]byte, aes.BlockSize, aes.BlockSize+md5.Size)
if _, err := rand.Read(iv); err != nil {
panic(err)
}
ciphertext := make([]byte, md5.Size)
cipher.NewCBCEncrypter(block, iv).CryptBlocks(ciphertext, plaintext[:])
return base64.StdEncoding.EncodeToString(append(iv, ciphertext...))
}
func verifyReqKey(args []string, key string) error {
initAesKey()
k, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return fmt.Errorf("invalid key %v", err)
}
if len(k) != aes.BlockSize+md5.Size {
return errors.New("invalid key")
}
plaintext := make([]byte, md5.Size)
cipher.NewCBCDecrypter(block, k[:aes.BlockSize]).CryptBlocks(plaintext, k[aes.BlockSize:])
sum := md5.Sum([]byte(strings.Join(args, ":")))
if string(plaintext) != string(sum[:]) {
return errors.New("mismatch key")
}
return nil
}
+28
View File
@@ -0,0 +1,28 @@
package mw
import (
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"testing"
)
func TestCheck(t *testing.T) {
config.Config.TokenPolicy.Secret = "123456"
args := []string{"1", "2", "3"}
key := genReqKey(args)
fmt.Println("key:", key)
err := verifyReqKey(args, key)
fmt.Println(err)
args = []string{"4", "5", "6"}
key = genReqKey(args)
fmt.Println("key:", key)
err = verifyReqKey(args, key)
fmt.Println(err)
}
+167
View File
@@ -0,0 +1,167 @@
package mw
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)
type GinMwOptions func(*gin.RouterGroup)
func WithRecovery() GinMwOptions {
return func(group *gin.RouterGroup) {
group.Use(gin.Recovery())
}
}
func WithCorsHandler() GinMwOptions {
return func(group *gin.RouterGroup) {
group.Use(CorsHandler())
}
}
func WithGinParseOperationID() GinMwOptions {
return func(group *gin.RouterGroup) {
group.Use(GinParseOperationID())
}
}
func WithGinParseToken(rdb redis.UniversalClient) GinMwOptions {
return func(group *gin.RouterGroup) {
group.Use(GinParseToken(rdb))
}
}
func NewRouterGroup(routerGroup *gin.RouterGroup, route string, options ...GinMwOptions) *gin.RouterGroup {
routerGroup = routerGroup.Group(route)
for _, option := range options {
option(routerGroup)
}
return routerGroup
}
func CorsHandler() gin.HandlerFunc {
return func(context *gin.Context) {
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
context.Header("Access-Control-Allow-Methods", "*")
context.Header("Access-Control-Allow-Headers", "*")
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
context.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
context.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
context.Header("content-type", "application/json") // 设置返回格式是json
//Release all option pre-requests
if context.Request.Method == http.MethodOptions {
context.JSON(http.StatusOK, "Options Request!")
}
context.Next()
}
}
func GinParseOperationID() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.Method == http.MethodPost {
operationID := c.Request.Header.Get(constant.OperationID)
if operationID == "" {
body, err := io.ReadAll(c.Request.Body)
if err != nil {
log.ZWarn(c, "read request body error", errs.ErrArgs.Wrap("read request body error: "+err.Error()))
apiresp.GinError(c, errs.ErrArgs.Wrap("read request body error: "+err.Error()))
c.Abort()
return
}
req := struct {
OperationID string `json:"operationID"`
}{}
if err := json.Unmarshal(body, &req); err != nil {
log.ZWarn(c, "json unmarshal error", errs.ErrArgs.Wrap(err.Error()))
apiresp.GinError(c, errs.ErrArgs.Wrap("json unmarshal error"+err.Error()))
c.Abort()
return
}
if req.OperationID == "" {
err := errors.New("header must have operationID")
log.ZWarn(c, "header must have operationID", err)
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error()))
c.Abort()
return
}
c.Request.Body = io.NopCloser(bytes.NewReader(body))
operationID = req.OperationID
c.Request.Header.Set(constant.OperationID, operationID)
}
c.Set(constant.OperationID, operationID)
c.Next()
return
}
c.Next()
}
}
func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc {
dataBase := controller.NewAuthDatabase(cache.NewMsgCacheModel(rdb), config.Config.TokenPolicy.AccessSecret, config.Config.TokenPolicy.AccessExpire)
return func(c *gin.Context) {
switch c.Request.Method {
case http.MethodPost:
token := c.Request.Header.Get(constant.Token)
if token == "" {
log.ZWarn(c, "header get token error", errs.ErrArgs.Wrap("header must have token"))
apiresp.GinError(c, errs.ErrArgs.Wrap("header must have token"))
c.Abort()
return
}
claims, err := tokenverify.GetClaimFromToken(token)
if err != nil {
log.ZWarn(c, "jwt get token error", errs.ErrTokenUnknown.Wrap())
apiresp.GinError(c, errs.ErrTokenUnknown.Wrap())
c.Abort()
return
}
m, err := dataBase.GetTokensWithoutError(c, claims.UserID, claims.PlatformID)
if err != nil {
log.ZWarn(c, "cache get token error", errs.ErrTokenNotExist.Wrap())
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
c.Abort()
return
}
if len(m) == 0 {
log.ZWarn(c, "cache do not exist token error", errs.ErrTokenNotExist.Wrap())
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
c.Abort()
return
}
if v, ok := m[token]; ok {
switch v {
case constant.NormalToken:
case constant.KickedToken:
log.ZWarn(c, "cache kicked token error", errs.ErrTokenKicked.Wrap())
apiresp.GinError(c, errs.ErrTokenKicked.Wrap())
c.Abort()
return
default:
log.ZWarn(c, "cache unknown token error", errs.ErrTokenUnknown.Wrap())
apiresp.GinError(c, errs.ErrTokenUnknown.Wrap())
c.Abort()
return
}
} else {
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
return
}
c.Set(constant.OpUserPlatform, constant.PlatformIDToName(claims.PlatformID))
c.Set(constant.OpUserID, claims.UserID)
c.Next()
}
}
}
+27
View File
@@ -0,0 +1,27 @@
package mw
import (
"context"
"google.golang.org/grpc"
)
func InterceptChain(intercepts ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
l := len(intercepts)
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
chain := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler {
return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) {
return currentInter(
currentCtx,
currentReq,
info,
currentHandler)
}
}
chainHandler := handler
for i := l - 1; i >= 0; i-- {
chainHandler = chain(intercepts[i], chainHandler)
}
return chainHandler(ctx, req)
}
}
+94
View File
@@ -0,0 +1,94 @@
package mw
import (
"context"
"errors"
"fmt"
"strings"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/errinfo"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func GrpcClient() grpc.DialOption {
return grpc.WithUnaryInterceptor(RpcClientInterceptor)
}
func RpcClientInterceptor(ctx context.Context, method string, req, resp interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
if ctx == nil {
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
}
log.ZInfo(ctx, "rpc client req", "funcName", method, "req", rpcString(req), "invoker", invoker, "invoker_type", fmt.Sprintf("%T", invoker))
ctx, err = getRpcContext(ctx, method)
if err != nil {
return err
}
log.ZDebug(ctx, "get rpc ctx success", "conn target", cc.Target())
err = invoker(ctx, method, req, resp, cc, opts...)
if err == nil {
log.ZInfo(ctx, "rpc client resp", "funcName", method, "resp", rpcString(resp))
return nil
}
log.ZError(ctx, "rpc resp error", err)
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
if !ok {
return errs.ErrInternalServer.Wrap(err.Error())
}
sta := rpcErr.GRPCStatus()
if sta.Code() == 0 {
return errs.NewCodeError(errs.ServerInternalError, err.Error()).Wrap()
}
if details := sta.Details(); len(details) > 0 {
errInfo, ok := details[0].(*errinfo.ErrorInfo)
if ok {
s := strings.Join(errInfo.Warp, "->") + errInfo.Cause
return errs.NewCodeError(int(sta.Code()), sta.Message()).WithDetail(s).Wrap()
}
}
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
}
func getRpcContext(ctx context.Context, method string) (context.Context, error) {
md := metadata.Pairs()
if keys, _ := ctx.Value(constant.RpcCustomHeader).([]string); len(keys) > 0 {
for _, key := range keys {
val, ok := ctx.Value(key).([]string)
if !ok {
return nil, errs.ErrInternalServer.Wrap(fmt.Sprintf("ctx missing key %s", key))
}
if len(val) == 0 {
return nil, 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 {
log.ZWarn(ctx, "ctx missing operationID", errors.New("ctx missing operationID"), "funcName", method)
return nil, errs.ErrArgs.Wrap("ctx missing operationID")
}
md.Set(constant.OperationID, operationID)
var checkArgs []string
checkArgs = append(checkArgs, constant.OperationID, operationID)
opUserID, ok := ctx.Value(constant.OpUserID).(string)
if ok {
md.Set(constant.OpUserID, opUserID)
checkArgs = append(checkArgs, constant.OpUserID, opUserID)
}
opUserIDPlatformID, ok := ctx.Value(constant.OpUserPlatform).(string)
if ok {
md.Set(constant.OpUserPlatform, opUserIDPlatformID)
}
connID, ok := ctx.Value(constant.ConnID).(string)
if ok {
md.Set(constant.ConnID, connID)
}
md.Set(constant.CheckKey, genReqKey(checkArgs))
return metadata.NewOutgoingContext(ctx, md), nil
}
+150
View File
@@ -0,0 +1,150 @@
package mw
import (
"context"
"fmt"
"math"
"runtime"
"strings"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/errinfo"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func rpcString(v interface{}) string {
if s, ok := v.(interface{ String() string }); ok {
return s.String()
}
return fmt.Sprintf("%+v", v)
}
func RpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
log.ZDebug(ctx, "rpc server req", "req", rpcString(req))
//defer func() {
// if r := recover(); r != nil {
// log.ZError(ctx, "rpc panic", nil, "FullMethod", info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r)
// fmt.Printf("panic: %+v\nstack info: %s\n", r, string(debug.Stack()))
// pc, file, line, ok := runtime.Caller(4)
// if !ok {
// panic("get runtime.Caller failed")
// }
// errInfo := &errinfo.ErrorInfo{
// Path: file,
// Line: uint32(line),
// Name: runtime.FuncForPC(pc).Name(),
// Cause: fmt.Sprintf("%s", r),
// Warp: nil,
// }
// sta, err_ := status.New(codes.Code(errs.ErrInternalServer.Code()), errs.ErrInternalServer.Msg()).WithDetails(errInfo)
// if err_ != nil {
// panic(err_)
// }
// err = sta.Err()
// }
//}()
funcName := info.FullMethod
md, ok := metadata.FromIncomingContext(ctx)
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()
} else {
args = append(args, constant.OperationID, opts[0])
ctx = context.WithValue(ctx, constant.OperationID, opts[0])
}
if opts := md.Get(constant.OpUserID); len(opts) == 1 {
args = append(args, constant.OpUserID, opts[0])
ctx = context.WithValue(ctx, constant.OpUserID, opts[0])
}
if opts := md.Get(constant.OpUserPlatform); len(opts) == 1 {
ctx = context.WithValue(ctx, constant.OpUserPlatform, opts[0])
}
if opts := md.Get(constant.ConnID); len(opts) == 1 {
ctx = context.WithValue(ctx, constant.ConnID, opts[0])
}
if opts := md.Get(constant.CheckKey); len(opts) != 1 || opts[0] == "" {
return nil, status.New(codes.InvalidArgument, "check key empty").Err()
} else {
if err := verifyReqKey(args, opts[0]); err != nil {
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
}
}
log.ZInfo(ctx, "rpc server req", "funcName", funcName, "req", rpcString(req))
resp, err = handler(ctx, req)
if err == nil {
log.ZInfo(ctx, "rpc server resp", "funcName", funcName, "resp", rpcString(resp))
return resp, nil
}
log.ZError(ctx, "rpc server resp", err, "funcName", funcName)
unwrap := errs.Unwrap(err)
codeErr := specialerror.ErrCode(unwrap)
if codeErr == nil {
log.ZError(ctx, "rpc InternalServer error", err, "req", req)
codeErr = errs.ErrInternalServer
}
code := codeErr.Code()
if code <= 0 || code > math.MaxUint32 {
log.ZError(ctx, "rpc UnknownError", err, "rpc UnknownCode:", code)
code = errs.ServerInternalError
}
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
var errInfo *errinfo.ErrorInfo
if config.Config.Log.WithStack {
if unwrap != err {
sti, ok := err.(interface{ StackTrace() errors.StackTrace })
if ok {
log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName, "unwrap", unwrap.Error(), "stack", fmt.Sprintf("%+v", err))
if fs := sti.StackTrace(); len(fs) > 0 {
pc := uintptr(fs[0])
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
errInfo = &errinfo.ErrorInfo{
Path: file,
Line: uint32(line),
Name: fn.Name(),
Cause: unwrap.Error(),
Warp: nil,
}
if arr := strings.Split(err.Error(), ": "); len(arr) > 1 {
errInfo.Warp = arr[:len(arr)-1]
}
}
}
}
}
if errInfo == nil {
errInfo = &errinfo.ErrorInfo{Cause: err.Error()}
}
details, err := grpcStatus.WithDetails(errInfo)
if err != nil {
panic(err)
}
log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName)
return nil, details.Err()
}
func GrpcServer() grpc.ServerOption {
return grpc.UnaryInterceptor(RpcServerInterceptor)
}
+33
View File
@@ -0,0 +1,33 @@
package specialerror
import "github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
var handlers []func(err error) errs.CodeError
func AddErrHandler(h func(err error) errs.CodeError) {
if h == nil {
panic("nil handler")
}
handlers = append(handlers, h)
}
func AddReplace(target error, codeErr errs.CodeError) {
AddErrHandler(func(err error) errs.CodeError {
if err == target {
return codeErr
}
return nil
})
}
func ErrCode(err error) errs.CodeError {
if codeErr, ok := err.(errs.CodeError); ok {
return codeErr
}
for i := 0; i < len(handlers); i++ {
if codeErr := handlers[i](err); codeErr != nil {
return codeErr
}
}
return nil
}