Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
im-microservice
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Li Feifei
im-microservice
Commits
7f257399
Commit
7f257399
authored
Jul 03, 2020
by
Li Feifei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
结构修改
parent
008a4b79
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
39 deletions
+61
-39
auth.go
middleware/auth.go
+36
-39
interceptor.go
middleware/interceptor.go
+25
-0
No files found.
helper
/auth.go
→
middleware
/auth.go
View file @
7f257399
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
,
"请求超过限制,请稍后再试"
)
}
}
}
}
...
...
middleware/interceptor.go
0 → 100644
View file @
7f257399
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
,
"请求超过限制,请稍后再试"
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment