webforwarding.go 6.2 KB

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