webforwarding.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package repository
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/qiniu/qmgo"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo"
  11. "time"
  12. )
  13. type WebForwardingRepository interface {
  14. GetWebForwarding(ctx context.Context, id int64) (*model.WebForwarding, error)
  15. AddWebForwarding(ctx context.Context, req *model.WebForwarding) (int, error)
  16. EditWebForwarding(ctx context.Context, req *model.WebForwarding) error
  17. DeleteWebForwarding(ctx context.Context, id int64) error
  18. GetWebForwardingWafWebIdById(ctx context.Context, id int) (int, error)
  19. GetWebForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
  20. GetWebForwardingDomainCountByHostId(ctx context.Context, hostId int) (int64, []string, error)
  21. AddWebForwardingIps(ctx context.Context, req model.WebForwardingRule) (primitive.ObjectID, error)
  22. EditWebForwardingIps(ctx context.Context, req model.WebForwardingRule) error
  23. GetWebForwardingByID(ctx context.Context, webId int) (*model.WebForwardingRule, error)
  24. }
  25. func NewWebForwardingRepository(
  26. repository *Repository,
  27. ) WebForwardingRepository {
  28. return &webForwardingRepository{
  29. Repository: repository,
  30. }
  31. }
  32. type webForwardingRepository struct {
  33. *Repository
  34. }
  35. func (r *webForwardingRepository) GetWebForwarding(ctx context.Context, id int64) (*model.WebForwarding, error) {
  36. var webForwarding model.WebForwarding
  37. if err := r.db.WithContext(ctx).Where("id = ?", id).First(&webForwarding).Error; err != nil {
  38. return nil, err
  39. }
  40. return &webForwarding, nil
  41. }
  42. func (r *webForwardingRepository) AddWebForwarding(ctx context.Context, req *model.WebForwarding) (int, error) {
  43. if err := r.db.WithContext(ctx).Create(req).Error; err != nil {
  44. return 0, err
  45. }
  46. return req.Id, nil
  47. }
  48. func (r *webForwardingRepository) EditWebForwarding(ctx context.Context, req *model.WebForwarding) error {
  49. if err := r.db.WithContext(ctx).Updates(req).Error; err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. func (r *webForwardingRepository) DeleteWebForwarding(ctx context.Context, id int64) error {
  55. if err := r.db.WithContext(ctx).Where("id = ?", id).Delete(&model.WebForwarding{}).Error; err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func (r *webForwardingRepository) GetWebForwardingWafWebIdById(ctx context.Context, id int) (int, error) {
  61. var WafWebId int
  62. if err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).Where("id = ?", id).Select("waf_web_id").Find(&WafWebId).Error; err != nil {
  63. return 0, err
  64. }
  65. return WafWebId, nil
  66. }
  67. func (r *webForwardingRepository) GetWebForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error) {
  68. var count int64
  69. if err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).Where("host_id = ?", hostId).Count(&count).Error; err != nil {
  70. return 0, err
  71. }
  72. return count, nil
  73. }
  74. func (r *webForwardingRepository) GetWebForwardingDomainCountByHostId(ctx context.Context, hostId int) (int64, []string, error) {
  75. var distinctDomains []string
  76. err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).
  77. Distinct(). // 确保我们只获取唯一的 domain 值
  78. Where("host_id = ? AND domain IS NOT NULL AND domain != ''", hostId). // 额外添加 domain != '' 以排除空字符串
  79. Pluck("domain", &distinctDomains).Error
  80. if err != nil {
  81. return 0, nil, err
  82. }
  83. count := int64(len(distinctDomains))
  84. return count, distinctDomains, nil
  85. }
  86. // mongodb 插入
  87. func (r *webForwardingRepository) AddWebForwardingIps(ctx context.Context, req model.WebForwardingRule) (primitive.ObjectID, error) {
  88. collection := r.mongoDB.Collection("web_forwarding_rules")
  89. req.CreatedAt = time.Now()
  90. result, err := collection.InsertOne(ctx, req)
  91. if err != nil {
  92. return primitive.NilObjectID, fmt.Errorf("插入MongoDB失败: %w", err)
  93. }
  94. // 返回插入文档的ID
  95. return result.InsertedID.(primitive.ObjectID), nil
  96. }
  97. func (r *webForwardingRepository) EditWebForwardingIps(ctx context.Context, req model.WebForwardingRule) error {
  98. collection := r.mongoDB.Collection("web_forwarding_rules")
  99. updateData := bson.M{}
  100. if req.Uid != 0 {
  101. updateData["uid"] = req.Uid
  102. }
  103. if req.HostId != 0 {
  104. updateData["host_id"] = req.HostId
  105. }
  106. if req.WebId != 0 {
  107. updateData["web_id"] = req.WebId
  108. }
  109. if req.AccessRule != "" {
  110. updateData["access_rule"] = req.AccessRule
  111. }
  112. if len(req.BackendList) > 0 {
  113. updateData["backend_list"] = req.BackendList
  114. }
  115. if len(req.AllowIpList) > 0 {
  116. updateData["allow_ip_list"] = req.AllowIpList
  117. }
  118. if len(req.DenyIpList) > 0 {
  119. updateData["deny_ip_list"] = req.DenyIpList
  120. }
  121. // 始终更新更新时间
  122. updateData["updated_at"] = time.Now()
  123. // 如果没有任何字段需要更新,则直接返回
  124. if len(updateData) == 0 {
  125. return nil
  126. }
  127. // 执行更新
  128. update := bson.M{"$set": updateData}
  129. err := collection.UpdateOne(ctx, bson.M{"web_id": req.WebId}, update)
  130. if err != nil {
  131. return fmt.Errorf("更新MongoDB文档失败: %w", err)
  132. }
  133. return nil
  134. }
  135. func (r *webForwardingRepository) GetWebForwardingByID(ctx context.Context, webId int) (*model.WebForwardingRule, error) {
  136. // 获取集合
  137. collection := r.mongoDB.Collection("web_forwarding_rules")
  138. // 创建一个结构体来存储查询到的文档
  139. var rule model.WebForwardingRule
  140. // 使用 FindByID 方法来查找文档
  141. // FindByID 是 QMgo 封装的一个方便的方法,它内部会构建查询 _id = id
  142. err := collection.Find(ctx, qmgo.M{"web_id": webId}).One(&rule) // QMgo 的 FindOne 返回一个 QueryBuilder,接着调用 .One() 来执行查询并解码到 rule
  143. if err != nil {
  144. if errors.Is(err, mongo.ErrNoDocuments) {
  145. return nil, fmt.Errorf("记录不存在")
  146. }
  147. // 其他错误
  148. return nil, fmt.Errorf("查询MongoDB失败: %w", err)
  149. }
  150. // 返回找到的文档
  151. return &rule, nil
  152. }