middleware.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. result, 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 result == "" {
  32. common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is invalid")
  33. return
  34. }
  35. if result != constant.AdminAccess {
  36. // 获取请求URL
  37. url := c.Request.URL.Path
  38. s, err := mdb.RDB.HGet(context.Background(), common.GetTokenKey(tokenString), url).Result()
  39. if err != nil {
  40. mhayaLogger.Warnf("Auth HGet s error: %s", err.Error())
  41. common.PackUnauthorizedResult(c, code.InternalError, "")
  42. return
  43. }
  44. // 检查是否有权限
  45. if s == "" {
  46. common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is no auth")
  47. return
  48. }
  49. }
  50. ss, err := mdb.RDB.HGet(context.Background(), common.GetTokenKey(tokenString), constant.AdminAccess).Result()
  51. if err != nil {
  52. mhayaLogger.Warnf("Auth HGet ss error: %s", err.Error())
  53. common.PackUnauthorizedResult(c, code.InternalError, "")
  54. return
  55. }
  56. // 检查是否有权限
  57. if ss == "" {
  58. common.PackUnauthorizedResult(c, code.UnauthorizedError, "token is no auth")
  59. return
  60. }
  61. openIpWhitelist := settingObj.Get("open_ip_whitelist").ToBool()
  62. if openIpWhitelist {
  63. err = checkIPWhitelist(c)
  64. if err != nil {
  65. mhayaLogger.Warnf("Auth checkIPWhitelist error: %s", err.Error())
  66. common.PackForbiddenResult(c, code.ForbiddenError, "ip is no auth")
  67. return
  68. }
  69. }
  70. c.Next()
  71. }
  72. }
  73. // checkIP
  74. func checkIPWhitelist(c *gin.Context) error {
  75. // 获取请求的ip
  76. ip := c.ClientIP()
  77. var whitelistModel *models.Whitelist
  78. collection := mdb.MDB.Collection(whitelistModel.TableName())
  79. // 设置超时时间
  80. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  81. defer cancel() // 确保在函数退出时取消上下文
  82. // 示例:查询 IP 是否在白名单中
  83. err := collection.FindOne(ctx, bson.M{"ip": ip}).Decode(&whitelistModel)
  84. if err != nil && err != mongo.ErrNoDocuments {
  85. return err
  86. }
  87. // 根据查询结果决定是否允许访问
  88. if whitelistModel == nil {
  89. return errors.New("IP not in whitelist") // 拒绝访问
  90. }
  91. return nil // 允许访问
  92. }