mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-11 19:53:55 +08:00
发布v2.8.4版本,更新内容请查看:https://github.com/bufanyun/hotgo/tree/v2.0/docs/guide-zh-CN/addon-version-upgrade.md
This commit is contained in:
75
server/internal/logic/tcpserver/server_interceptor.go
Normal file
75
server/internal/logic/tcpserver/server_interceptor.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Package tcpserver
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package tcpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"hotgo/internal/library/network/tcp"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
// 免登录路由
|
||||
var noLoginRouter = map[string]struct{}{
|
||||
"ServerLoginReq": {}, // 服务登录
|
||||
}
|
||||
|
||||
// 免验证路由
|
||||
var noVerifyRouter = map[string]struct{}{
|
||||
"ServerHeartbeatReq": {}, // 心跳
|
||||
}
|
||||
|
||||
func (s *sTCPServer) isNoLoginRouter(router string) bool {
|
||||
_, ok := noLoginRouter[router]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s *sTCPServer) isNoVerifyRouter(router string) bool {
|
||||
_, ok := noVerifyRouter[router]
|
||||
return ok
|
||||
}
|
||||
|
||||
// DefaultInterceptor 默认拦截器
|
||||
func (s *sTCPServer) DefaultInterceptor(ctx context.Context, msg *tcp.Message) (err error) {
|
||||
conn := tcp.ConnFromCtx(ctx)
|
||||
//g.Log().Debugf(ctx, "DefaultInterceptor msg:%+v, conn:%+v", msg, gjson.New(conn).String())
|
||||
|
||||
// 免登录
|
||||
if s.isNoLoginRouter(msg.Router) {
|
||||
return
|
||||
}
|
||||
|
||||
if conn.Auth == nil {
|
||||
err = gerror.NewCode(gcode.CodeNotAuthorized, "未进行登录认证,请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
// 检查授权有效期
|
||||
if conn.Auth.EndAt.Before(gtime.Now()) {
|
||||
err = gerror.NewCode(gcode.CodeNotAuthorized, "授权已过期")
|
||||
return
|
||||
}
|
||||
|
||||
// 免验证
|
||||
if s.isNoVerifyRouter(msg.Router) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证路由权限
|
||||
if len(conn.Auth.Routes) > 0 && !validate.InSlice(conn.Auth.Routes, msg.Router) {
|
||||
err = gerror.NewCodef(gcode.CodeNotAuthorized, "没有授权路由访问权限:%v", msg.Router)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// PreFilterInterceptor 预处理
|
||||
func (s *sTCPServer) PreFilterInterceptor(ctx context.Context, msg *tcp.Message) (err error) {
|
||||
//g.Log().Debug(ctx, "PreFilterInterceptor...")
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user