middleware.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package router
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-redis/redis/v8"
  8. cfacade "github.com/mhaya/facade"
  9. "github.com/mhaya/game/game_cluster/internal/code"
  10. "github.com/mhaya/game/game_cluster/internal/constant"
  11. "github.com/mhaya/game/game_cluster/internal/mdb"
  12. "github.com/mhaya/game/game_cluster/internal/mdb/models"
  13. "github.com/mhaya/game/game_cluster/nodes/webadmin/common"
  14. mhayaLogger "github.com/mhaya/logger"
  15. "go.mongodb.org/mongo-driver/bson"
  16. "go.mongodb.org/mongo-driver/mongo"
  17. )
  18. func Auth(settingObj cfacade.ProfileJSON) gin.HandlerFunc {
  19. return func(c *gin.Context) {
  20. tokenString := c.GetHeader("Token")
  21. if tokenString == "" {
  22. common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is empty")
  23. return
  24. }
  25. roleId, err := mdb.RDB.Get(context.Background(), tokenString).Result()
  26. if err != nil && err != redis.Nil {
  27. mhayaLogger.Warnf("Auth Get error: %s", err.Error())
  28. common.PackUnauthorizedResult(c, code.InternalError, "token is empty")
  29. return
  30. }
  31. if roleId == "" {
  32. common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is invalid")
  33. return
  34. }
  35. if roleId != constant.AdminAccess {
  36. urlAccess, err := mdb.RDB.HGet(context.Background(), common.GetTokenKey(tokenString), c.Request.URL.Path).Result()
  37. if err != nil {
  38. mhayaLogger.Warnf("Auth HGet s error: %s", err.Error())
  39. common.PackUnauthorizedResult(c, code.InternalError, "")
  40. return
  41. }
  42. // 检查url权限
  43. if urlAccess == "" {
  44. common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is no auth")
  45. return
  46. }
  47. // 非管理员需要进行ip校验
  48. openIpWhitelist := settingObj.Get("open_ip_whitelist").ToBool()
  49. if openIpWhitelist {
  50. err = checkIPWhitelist(c)
  51. if err != nil {
  52. mhayaLogger.Warnf("Auth checkIPWhitelist error: %s", err.Error())
  53. common.PackForbiddenResult(c, code.ForbiddenError, "ip is no auth")
  54. return
  55. }
  56. }
  57. }
  58. adminAccess, err := mdb.RDB.HGet(context.Background(), common.GetTokenKey(tokenString), constant.AdminAccess).Result()
  59. if err != nil {
  60. mhayaLogger.Warnf("Auth HGet ss error: %s", err.Error())
  61. common.PackUnauthorizedResult(c, code.InternalError, "")
  62. return
  63. }
  64. // 检查管理员权限
  65. if adminAccess == "" {
  66. common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is no auth")
  67. return
  68. }
  69. c.Next()
  70. }
  71. }
  72. // checkIP
  73. func checkIPWhitelist(c *gin.Context) error {
  74. // 获取请求的ip
  75. ip := c.ClientIP()
  76. var whitelistModel *models.Whitelist
  77. collection := mdb.MDB.Collection(whitelistModel.TableName())
  78. // 设置超时时间
  79. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  80. defer cancel() // 确保在函数退出时取消上下文
  81. // 示例:查询 IP 是否在白名单中
  82. err := collection.FindOne(ctx, bson.M{"ip": ip}).Decode(&whitelistModel)
  83. if err != nil && err != mongo.ErrNoDocuments {
  84. return err
  85. }
  86. // 根据查询结果决定是否允许访问
  87. if whitelistModel == nil {
  88. return errors.New("IP not in whitelist") // 拒绝访问
  89. }
  90. return nil // 允许访问
  91. }