Commit 7f257399 by Li Feifei

结构修改

parent 008a4b79
package helper package middleware
import ( import (
"context" "context"
...@@ -9,7 +9,8 @@ import ( ...@@ -9,7 +9,8 @@ import (
"strconv" "strconv"
"time" "time"
"golang.org/x/time/rate" "im-microservice/helper"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
...@@ -25,7 +26,7 @@ func permissionError(msg string) error { ...@@ -25,7 +26,7 @@ func permissionError(msg string) error {
func checksum(req map[string]string) error { func checksum(req map[string]string) error {
err, AppSecret := GetSecretByKey(req["Appkey"]) err, AppSecret := helper.GetSecretByKey(req["Appkey"])
if err != nil { if err != nil {
return status.Error(codes.NotFound, "appkey不存在") return status.Error(codes.NotFound, "appkey不存在")
} }
...@@ -53,42 +54,38 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...@@ -53,42 +54,38 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) { handler grpc.UnaryHandler) (interface{}, error) {
if info.FullMethod != health_path { if info.FullMethod != health_path {
limiter := rate.NewLimiter(rate.Every(100*time.Millisecond), 1)
if limiter.Allow() { request_map := make(map[string]string)
request_map := make(map[string]string) s := fmt.Sprintf("%v", req)
s := fmt.Sprintf("%v", req)
key_r, _ := regexp.Compile(`Appkey:"(.*?)"`)
key_r, _ := regexp.Compile(`Appkey:"(.*?)"`) nonce_r, _ := regexp.Compile(`Nonce:"(.*?)"`)
nonce_r, _ := regexp.Compile(`Nonce:"(.*?)"`) c_r, _ := regexp.Compile(`Curtime:"(.*?)"`)
c_r, _ := regexp.Compile(`Curtime:"(.*?)"`) cs_r, _ := regexp.Compile(`Checksum:"(.*?)"`)
cs_r, _ := regexp.Compile(`Checksum:"(.*?)"`)
appkey := key_r.FindAllStringSubmatch(s, -1)
appkey := key_r.FindAllStringSubmatch(s, -1) nonce := nonce_r.FindAllStringSubmatch(s, -1)
nonce := nonce_r.FindAllStringSubmatch(s, -1) curtime := c_r.FindAllStringSubmatch(s, -1)
curtime := c_r.FindAllStringSubmatch(s, -1) check_sum := cs_r.FindAllStringSubmatch(s, -1)
check_sum := cs_r.FindAllStringSubmatch(s, -1)
if len(appkey) < 1 {
if len(appkey) < 1 { return nil, permissionError("缺少appkey")
return nil, permissionError("缺少appkey") }
} if len(nonce) < 1 {
if len(nonce) < 1 { return nil, permissionError("缺少nonce")
return nil, permissionError("缺少nonce") }
} if len(curtime) < 1 {
if len(curtime) < 1 { return nil, permissionError("缺少curtime")
return nil, permissionError("缺少curtime") }
} if len(check_sum) < 1 {
if len(check_sum) < 1 { return nil, permissionError("缺少checksum")
return nil, permissionError("缺少checksum") }
} request_map["Appkey"] = appkey[0][1]
request_map["Appkey"] = appkey[0][1] request_map["Nonce"] = nonce[0][1]
request_map["Nonce"] = nonce[0][1] request_map["Curtime"] = curtime[0][1]
request_map["Curtime"] = curtime[0][1] request_map["Checksum"] = check_sum[0][1]
request_map["Checksum"] = check_sum[0][1] if err := checksum(request_map); err != nil {
if err := checksum(request_map); err != nil { return nil, err
return nil, err
}
} else {
return nil, status.Error(codes.Unauthenticated, "请求超过限制,请稍后再试")
} }
} }
......
package middleware
import (
"context"
"time"
"golang.org/x/time/rate"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func Interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
if info.FullMethod != health_path {
limiter := rate.NewLimiter(rate.Every(100*time.Millisecond), 1)
if limiter.Allow() {
return handler(ctx, req)
} else {
return nil, status.Error(codes.Unauthenticated, "请求超过限制,请稍后再试")
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment