middleware.go 3.0 KB

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