|
@@ -3,83 +3,69 @@ package router
|
|
|
import (
|
|
|
"context"
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
"time"
|
|
|
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
- cfacade "github.com/mhaya/facade"
|
|
|
- "github.com/mhaya/game/game_cluster/internal/code"
|
|
|
"github.com/mhaya/game/game_cluster/internal/constant"
|
|
|
- "github.com/mhaya/game/game_cluster/internal/mdb"
|
|
|
"github.com/mhaya/game/game_cluster/internal/mdb/models"
|
|
|
- "github.com/mhaya/game/game_cluster/nodes/webadmin/common"
|
|
|
- mhayaLogger "github.com/mhaya/logger"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
- "go.mongodb.org/mongo-driver/mongo"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/mhaya/game/game_cluster/internal/mdb"
|
|
|
)
|
|
|
|
|
|
-func Auth(settingObj cfacade.ProfileJSON) gin.HandlerFunc {
|
|
|
+func Auth() gin.HandlerFunc {
|
|
|
return func(c *gin.Context) {
|
|
|
tokenString := c.GetHeader("Token")
|
|
|
if tokenString == "" {
|
|
|
- common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is empty")
|
|
|
+ c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "msg": "token is empty",
|
|
|
+ })
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
result, err := mdb.RDB.Get(context.Background(), tokenString).Result()
|
|
|
if err != nil {
|
|
|
- mhayaLogger.Warnf("Auth Get error: %s", err.Error())
|
|
|
- common.PackUnauthorizedResult(c, code.InternalError, "token is empty")
|
|
|
+ c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "msg": "token is empty",
|
|
|
+ })
|
|
|
return
|
|
|
}
|
|
|
if result == "" {
|
|
|
- common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is invalid")
|
|
|
+ c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "msg": "token is invalid",
|
|
|
+ })
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
// 获取请求URL
|
|
|
- // url := c.Request.URL.Path
|
|
|
- // s, err := mdb.RDB.HGet(context.Background(), "admin::token::"+tokenString, url).Result()
|
|
|
- // if err != nil {
|
|
|
- // mhayaLogger.Warnf("Auth HGet s error: %s", err.Error())
|
|
|
- // common.PackUnauthorizedResult(c, code.InternalError, "")
|
|
|
- // return
|
|
|
- // }
|
|
|
- // mhayaLogger.Warnf("Auth s: %s", s)
|
|
|
-
|
|
|
- ss, err := mdb.RDB.HGet(context.Background(), "admin::token::"+tokenString, constant.AdminAccess).Result()
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("Auth HGet ss error: %s", err.Error())
|
|
|
- common.PackUnauthorizedResult(c, code.InternalError, "")
|
|
|
- return
|
|
|
- }
|
|
|
- mhayaLogger.Warnf("Auth ss: %s", ss)
|
|
|
-
|
|
|
+ url := c.Request.URL.Path
|
|
|
+ s, _ := mdb.RDB.HGet(context.Background(), "admin::token::"+tokenString, url).Result()
|
|
|
+ ss, _ := mdb.RDB.HGet(context.Background(), "admin::token::"+tokenString, constant.AdminAccess).Result()
|
|
|
+ fmt.Println(ss)
|
|
|
// 检查是否有权限
|
|
|
- if ss == "" && result == "" {
|
|
|
- common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is no auth")
|
|
|
+ if s == "" && result == "" {
|
|
|
+ c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "msg": "token is no auth",
|
|
|
+ })
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
- openIpWhitelist := settingObj.Get("open_ip_whitelist").ToBool()
|
|
|
- mhayaLogger.Warnf("Auth open_ip_whitelist: %v", openIpWhitelist)
|
|
|
- if openIpWhitelist {
|
|
|
- err = checkIPWhitelist(c)
|
|
|
- if err != nil {
|
|
|
- mhayaLogger.Warnf("Auth checkIPWhitelist error: %s", err.Error())
|
|
|
- common.PackForbiddenResult(c, code.ForbiddenError, "ip is no auth")
|
|
|
+ if result == "" {
|
|
|
+ if err := checkIP(c); err != nil {
|
|
|
+ c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "msg": "ip is no auth",
|
|
|
+ })
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
c.Next()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// checkIP
|
|
|
-func checkIPWhitelist(c *gin.Context) error {
|
|
|
+func checkIP(c *gin.Context) error {
|
|
|
// 获取请求的ip
|
|
|
ip := c.ClientIP()
|
|
|
- var whitelistModel *models.Whitelist
|
|
|
+ whitelistModel := &models.Whitelist{}
|
|
|
collection := mdb.MDB.Collection(whitelistModel.TableName())
|
|
|
|
|
|
// 设置超时时间
|
|
@@ -88,14 +74,14 @@ func checkIPWhitelist(c *gin.Context) error {
|
|
|
|
|
|
// 示例:查询 IP 是否在白名单中
|
|
|
err := collection.FindOne(ctx, bson.M{"ip": ip}).Decode(&whitelistModel)
|
|
|
- if err != nil && err != mongo.ErrNoDocuments {
|
|
|
- return err
|
|
|
+ if err != nil {
|
|
|
+ // 处理查询错误
|
|
|
+ return fmt.Errorf("failed to check IP in whitelist: %w", err)
|
|
|
}
|
|
|
-
|
|
|
// 根据查询结果决定是否允许访问
|
|
|
- if whitelistModel == nil {
|
|
|
+ if whitelistModel != nil {
|
|
|
+ return nil // 允许访问
|
|
|
+ } else {
|
|
|
return errors.New("IP not in whitelist") // 拒绝访问
|
|
|
}
|
|
|
-
|
|
|
- return nil // 允许访问
|
|
|
}
|