middleware.go 2.9 KB

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