|
@@ -7,32 +7,46 @@ import (
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
+ jsoniter "github.com/json-iterator/go"
|
|
mhayaTime "github.com/mhaya/extend/time"
|
|
mhayaTime "github.com/mhaya/extend/time"
|
|
cfacade "github.com/mhaya/facade"
|
|
cfacade "github.com/mhaya/facade"
|
|
"github.com/mhaya/game/game_cluster/internal/code"
|
|
"github.com/mhaya/game/game_cluster/internal/code"
|
|
"github.com/mhaya/game/game_cluster/internal/constant"
|
|
"github.com/mhaya/game/game_cluster/internal/constant"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb"
|
|
|
|
+ "github.com/mhaya/game/game_cluster/internal/mdb/eventmodels"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb/models"
|
|
"github.com/mhaya/game/game_cluster/internal/mdb/models"
|
|
|
|
+ "github.com/mhaya/game/game_cluster/internal/param"
|
|
|
|
+ rpcLogstash "github.com/mhaya/game/game_cluster/internal/rpc/logstash"
|
|
"github.com/mhaya/game/game_cluster/nodes/webadmin/common"
|
|
"github.com/mhaya/game/game_cluster/nodes/webadmin/common"
|
|
- "github.com/mhaya/game/game_cluster/nodes/webadmin/model"
|
|
|
|
- "github.com/mhaya/game/game_cluster/nodes/webadmin/service"
|
|
|
|
mhayaLogger "github.com/mhaya/logger"
|
|
mhayaLogger "github.com/mhaya/logger"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
)
|
|
|
|
|
|
-func Auth(settingObj cfacade.ProfileJSON) gin.HandlerFunc {
|
|
|
|
|
|
+var (
|
|
|
|
+ json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+func Auth(app cfacade.IApplication) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
return func(c *gin.Context) {
|
|
// 请求开始时间
|
|
// 请求开始时间
|
|
startTime := mhayaTime.Now().UnixMilli()
|
|
startTime := mhayaTime.Now().UnixMilli()
|
|
|
|
|
|
- tokenString := c.GetHeader("Token")
|
|
|
|
|
|
+ tokenString := ""
|
|
|
|
+ roleId := ""
|
|
|
|
+ var err error
|
|
|
|
+
|
|
|
|
+ defer func() {
|
|
|
|
+ sendLogRecord(c, app, tokenString, roleId, startTime)
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ tokenString = c.GetHeader("Token")
|
|
if tokenString == "" {
|
|
if tokenString == "" {
|
|
common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is empty")
|
|
common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is empty")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- roleId, err := mdb.RDB.Get(context.Background(), tokenString).Result()
|
|
|
|
|
|
+ roleId, err = mdb.RDB.Get(context.Background(), tokenString).Result()
|
|
if err != nil && err != redis.Nil {
|
|
if err != nil && err != redis.Nil {
|
|
mhayaLogger.Warnf("Auth Get error: %s", err.Error())
|
|
mhayaLogger.Warnf("Auth Get error: %s", err.Error())
|
|
common.PackUnauthorizedResult(c, code.InternalError, "token is empty")
|
|
common.PackUnauthorizedResult(c, code.InternalError, "token is empty")
|
|
@@ -58,7 +72,7 @@ func Auth(settingObj cfacade.ProfileJSON) gin.HandlerFunc {
|
|
}
|
|
}
|
|
|
|
|
|
// 非管理员需要进行ip校验
|
|
// 非管理员需要进行ip校验
|
|
- openIpWhitelist := settingObj.Get("open_ip_whitelist").ToBool()
|
|
|
|
|
|
+ openIpWhitelist := app.Settings().Get("open_ip_whitelist").ToBool()
|
|
if openIpWhitelist {
|
|
if openIpWhitelist {
|
|
err = checkIPWhitelist(c)
|
|
err = checkIPWhitelist(c)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -83,20 +97,6 @@ func Auth(settingObj cfacade.ProfileJSON) gin.HandlerFunc {
|
|
}
|
|
}
|
|
|
|
|
|
c.Next()
|
|
c.Next()
|
|
-
|
|
|
|
- userName, err := mdb.RDB.Get(context.Background(), common.GetUserNameKey(tokenString)).Result()
|
|
|
|
- if err == nil {
|
|
|
|
- service.NewSynthesis().InsertRecord(model.UserOperationLog{
|
|
|
|
- Username: userName,
|
|
|
|
- RoleId: roleId,
|
|
|
|
- Path: c.Request.URL.Path,
|
|
|
|
- Method: c.Request.Method,
|
|
|
|
- StatusCode: c.Writer.Status(),
|
|
|
|
- Dur: mhayaTime.Now().UnixMilli() - startTime,
|
|
|
|
- ClientIP: c.ClientIP(),
|
|
|
|
- ErrorMessage: c.Errors.ByType(gin.ErrorTypePrivate).String(),
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -124,3 +124,52 @@ func checkIPWhitelist(c *gin.Context) error {
|
|
|
|
|
|
return nil // 允许访问
|
|
return nil // 允许访问
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func sendLogRecord(c *gin.Context, app cfacade.IApplication, tokenString, roleId string, startTime int64) {
|
|
|
|
+ userName, err := mdb.RDB.Get(context.Background(), common.GetUserNameKey(tokenString)).Result()
|
|
|
|
+ if err != nil {
|
|
|
|
+ mhayaLogger.Warnf("sendLogRecord Get userName error: %s, token: %s", err.Error(), tokenString)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ nodeId := app.NodeId()
|
|
|
|
+ req, err := packBackendOperationEventReq(nodeId, &eventmodels.BackendOperationEventContent{
|
|
|
|
+ UserBasic: eventmodels.UserBasic{
|
|
|
|
+ UserName: userName,
|
|
|
|
+ IsRobot: false,
|
|
|
|
+ },
|
|
|
|
+ EventBasic: eventmodels.EventBasic{
|
|
|
|
+ ServerId: nodeId,
|
|
|
|
+ IsSuccess: func() bool {
|
|
|
|
+ return c.Writer.Status() == int(code.OK)
|
|
|
|
+ }(),
|
|
|
|
+ CreateAt: mhayaTime.Now().Unix(),
|
|
|
|
+ },
|
|
|
|
+ RoleId: roleId,
|
|
|
|
+ Path: c.Request.URL.Path,
|
|
|
|
+ Method: c.Request.Method,
|
|
|
|
+ StatusCode: c.Writer.Status(),
|
|
|
|
+ Dur: mhayaTime.Now().UnixMilli() - startTime,
|
|
|
|
+ ClientIP: c.ClientIP(),
|
|
|
|
+ ErrorMessage: c.Errors.ByType(gin.ErrorTypePrivate).String(),
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ mhayaLogger.Warnf("sendLogRecord packBackendOperationEventReq error: %s, token: %s", err.Error(), tokenString)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ rpcLogstash.HandleLogRecord(app, req)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func packBackendOperationEventReq(nodeId string, content *eventmodels.BackendOperationEventContent) (*param.HandleLogReq, error) {
|
|
|
|
+ bytes, err := json.Marshal(content)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return ¶m.HandleLogReq{
|
|
|
|
+ ServerId: nodeId,
|
|
|
|
+ EventName: content.EventName(),
|
|
|
|
+ JsonContent: string(bytes),
|
|
|
|
+ }, nil
|
|
|
|
+}
|