whitelist.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "time"
  8. "github.com/mhaya/game/game_cluster/internal/mdb"
  9. "github.com/mhaya/game/game_cluster/internal/mdb/models"
  10. "github.com/mhaya/game/game_cluster/nodes/webadmin/entity"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/mongo"
  13. "go.mongodb.org/mongo-driver/mongo/options"
  14. )
  15. type Whitelist struct {
  16. }
  17. func NewWhitelist() *Whitelist {
  18. return &Whitelist{}
  19. }
  20. // Add 添加IP白名单
  21. func (w *Whitelist) Add(ip, de string) error {
  22. whitelistModel := models.Whitelist{}
  23. collection := mdb.MDB.Collection(whitelistModel.TableName())
  24. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  25. defer cancel()
  26. // 判断是否已经存在
  27. err := collection.FindOne(ctx, bson.M{"ip": ip}).Decode(&whitelistModel)
  28. if err == nil {
  29. return fmt.Errorf("IP %s already exists in the whitelist", ip)
  30. }
  31. _, err = collection.InsertOne(ctx, bson.M{
  32. "ip": ip,
  33. "created_at": time.Now().Unix(),
  34. "updated_at": time.Now().Unix(),
  35. "desc": de,
  36. })
  37. if err != nil {
  38. log.Printf("Error inserting IP %s into whitelist: %v", ip, err)
  39. if errors.Is(err, mongo.ErrNoDocuments) {
  40. return fmt.Errorf("IP %s already exists in the whitelist", ip)
  41. }
  42. return fmt.Errorf("failed to insert IP %s into whitelist: %v", ip, err)
  43. }
  44. return nil
  45. }
  46. // Remove 删除IP白名单
  47. func (w *Whitelist) Remove(ip string) error {
  48. whitelistModel := models.Whitelist{}
  49. collection := mdb.MDB.Collection(whitelistModel.TableName())
  50. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  51. defer cancel()
  52. _, err := collection.DeleteOne(ctx, bson.M{"ip": ip})
  53. if err != nil {
  54. log.Printf("Error deleting IP %s from whitelist: %v", ip, err)
  55. return fmt.Errorf("failed to delete IP %s from whitelist: %v", ip, err)
  56. }
  57. return nil
  58. }
  59. // Check 检查IP是否在白名单中
  60. func (w *Whitelist) Check(ip string) error {
  61. whitelistModel := models.Whitelist{}
  62. collection := mdb.MDB.Collection(whitelistModel.TableName())
  63. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  64. defer cancel()
  65. // 判断是否已经存在
  66. err := collection.FindOne(ctx, bson.M{"ip": ip}).Decode(&whitelistModel)
  67. if err == nil {
  68. return fmt.Errorf("IP %s already exists in the whitelist", ip)
  69. }
  70. return nil
  71. }
  72. // GetAll 获取所有白名单数据
  73. func (w *Whitelist) GetAll(req *entity.WhitelistListReq) ([]models.Whitelist, error) {
  74. // 定义集合
  75. whitelistModel := models.Whitelist{}
  76. collection := mdb.MDB.Collection(whitelistModel.TableName())
  77. // 设置上下文
  78. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  79. defer cancel()
  80. // 解析分页参数
  81. page := req.Page
  82. pageSize := req.Size
  83. if page == 0 {
  84. page = 1
  85. }
  86. if pageSize == 0 {
  87. pageSize = 10
  88. }
  89. // 设置分页选项
  90. skip := (page - 1) * pageSize
  91. findOptions := options.Find().SetSkip(int64(skip)).SetLimit(int64(pageSize))
  92. count, err := collection.CountDocuments(ctx, bson.D{})
  93. if err != nil {
  94. return nil, err
  95. }
  96. req.Count = count
  97. // 查询数据库
  98. var results []models.Whitelist
  99. cursor, err := collection.Find(ctx, bson.D{}, findOptions)
  100. if err != nil {
  101. log.Printf("Error finding documents: %v", err)
  102. return nil, err
  103. }
  104. defer cursor.Close(ctx)
  105. // 解析查询结果
  106. for cursor.Next(ctx) {
  107. var whitelist models.Whitelist
  108. if err := cursor.Decode(&whitelist); err != nil {
  109. log.Printf("Error decoding document: %v", err)
  110. return nil, err
  111. }
  112. results = append(results, whitelist)
  113. }
  114. if err := cursor.Err(); err != nil {
  115. log.Printf("Cursor error: %v", err)
  116. return nil, err
  117. }
  118. return results, nil
  119. }