webforwarding.go 6.5 KB

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