udpforwarding.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package repository
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  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 UdpForWardingRepository interface {
  14. GetUdpForWarding(ctx context.Context, id int64) (*model.UdpForWarding, error)
  15. AddUdpForwarding(ctx context.Context, req *model.UdpForWarding) (int, error)
  16. EditUdpForwarding(ctx context.Context, req *model.UdpForWarding) error
  17. DeleteUdpForwarding(ctx context.Context, id int64) error
  18. GetUdpForwardingWafUdpIdById(ctx context.Context, id int) (int, error)
  19. GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
  20. GetUdpForwardingWafUdpAllIds(ctx context.Context, udpId int) ([]int, error)
  21. AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error)
  22. EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error
  23. GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error)
  24. DeleteUdpForwardingIpsById(ctx context.Context, udpId int) error
  25. // 获取ip数量等于1的ip
  26. GetIpCountByIp(ctx context.Context, ips []string) ([]v1.IpCountResult, error)
  27. }
  28. func NewUdpForWardingRepository(
  29. repository *Repository,
  30. ) UdpForWardingRepository {
  31. return &udpForWardingRepository{
  32. Repository: repository,
  33. }
  34. }
  35. type udpForWardingRepository struct {
  36. *Repository
  37. }
  38. func (r *udpForWardingRepository) GetUdpForWarding(ctx context.Context, id int64) (*model.UdpForWarding, error) {
  39. var udpForWarding model.UdpForWarding
  40. if err := r.db.Where("id = ?", id).First(&udpForWarding).Error; err != nil {
  41. return nil, err
  42. }
  43. return &udpForWarding, nil
  44. }
  45. func (r *udpForWardingRepository) AddUdpForwarding(ctx context.Context, req *model.UdpForWarding) (int, error) {
  46. if err := r.db.WithContext(ctx).Create(req).Error; err != nil {
  47. return 0, err
  48. }
  49. return req.Id, nil
  50. }
  51. func (r *udpForWardingRepository) EditUdpForwarding(ctx context.Context, req *model.UdpForWarding) error {
  52. if err := r.db.Updates(req).Error; err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. func (r *udpForWardingRepository) DeleteUdpForwarding(ctx context.Context, id int64) error {
  58. if err := r.db.Where("id = ?", id).Delete(&model.UdpForWarding{}).Error; err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (r *udpForWardingRepository) GetUdpForwardingWafUdpIdById(ctx context.Context, id int) (int, error) {
  64. var WafUdpId int
  65. if err := r.db.Model(&model.UdpForWarding{}).Where("id = ?", id).Select("waf_udp_id").Find(&WafUdpId).Error; err != nil {
  66. return 0, err
  67. }
  68. return WafUdpId, nil
  69. }
  70. func (r *udpForWardingRepository) GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error) {
  71. var count int64
  72. if err := r.db.Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Count(&count).Error; err != nil {
  73. return 0, err
  74. }
  75. return count, nil
  76. }
  77. func (r *udpForWardingRepository) GetUdpForwardingWafUdpAllIds(ctx context.Context, hostId int) ([]int, error) {
  78. var res []int
  79. if err:= r.db.WithContext(ctx).Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Select("id").Find(&res).Error; err != nil {
  80. return nil, err
  81. }
  82. return res, nil
  83. }
  84. // mongodb 插入
  85. func (r *udpForWardingRepository) AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error) {
  86. collection := r.mongoDB.Collection("udp_forwarding_rules")
  87. req.CreatedAt = time.Now()
  88. result, err := collection.InsertOne(ctx, req)
  89. if err != nil {
  90. return primitive.NilObjectID, fmt.Errorf("插入MongoDB失败: %w", err)
  91. }
  92. // 返回插入文档的ID
  93. return result.InsertedID.(primitive.ObjectID), nil
  94. }
  95. func (r *udpForWardingRepository) EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error {
  96. collection := r.mongoDB.Collection("udp_forwarding_rules")
  97. updateData := bson.M{}
  98. if req.Uid != 0 {
  99. updateData["uid"] = req.Uid
  100. }
  101. if req.HostId != 0 {
  102. updateData["host_id"] = req.HostId
  103. }
  104. if req.UdpId != 0 {
  105. updateData["udp_id"] = req.UdpId
  106. }
  107. if len(req.BackendList) > 0 {
  108. updateData["backend_list"] = req.BackendList
  109. }
  110. updateData["cdn_origin_ids"] = req.CdnOriginIds
  111. // 始终更新更新时间
  112. updateData["updated_at"] = time.Now()
  113. // 如果没有任何字段需要更新,则直接返回
  114. if len(updateData) == 0 {
  115. return nil
  116. }
  117. // 执行更新
  118. update := bson.M{"$set": updateData}
  119. err := collection.UpdateOne(ctx, bson.M{"udp_id": req.UdpId}, update)
  120. if err != nil {
  121. return fmt.Errorf("更新MongoDB文档失败: %w", err)
  122. }
  123. return nil
  124. }
  125. func (r *udpForWardingRepository) GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error) {
  126. collection := r.mongoDB.Collection("udp_forwarding_rules")
  127. var result model.UdpForwardingRule
  128. err := collection.Find(ctx, bson.M{"udp_id": udpId}).One(&result)
  129. if err != nil {
  130. if errors.Is(err, mongo.ErrNoDocuments) {
  131. return nil, nil
  132. }
  133. return nil, fmt.Errorf("从MongoDB中获取文档失败: %w", err)
  134. }
  135. return &result, nil
  136. }
  137. func (r *udpForWardingRepository) DeleteUdpForwardingIpsById(ctx context.Context, udpId int) error {
  138. collection := r.mongoDB.Collection("udp_forwarding_rules")
  139. err := collection.Remove(ctx, bson.M{"udp_id": udpId})
  140. if err != nil {
  141. if errors.Is(err, mongo.ErrNoDocuments) {
  142. return fmt.Errorf("记录不存在")
  143. }
  144. return fmt.Errorf("删除MongoDB文档失败: %w", err)
  145. }
  146. return nil
  147. }
  148. // 获取IP数量等于1的IP
  149. func (r *udpForWardingRepository) GetIpCountByIp(ctx context.Context, ips []string) ([]v1.IpCountResult, error) {
  150. if len(ips) == 0 {
  151. return []v1.IpCountResult{}, nil
  152. }
  153. pipeline := []bson.M{
  154. {
  155. "$match": bson.M{
  156. "ip": bson.M{"$in": ips},
  157. },
  158. },
  159. {
  160. "$group": bson.M{
  161. "_id": "$ip",
  162. "count": bson.M{"$sum": 1},
  163. },
  164. },
  165. {
  166. "$project": bson.M{
  167. "_id": 0, // 不输出默认的_id
  168. "ip": "$_id", // 将分组的_id字段重命名为ip
  169. "count": 1, // 保留count字段
  170. },
  171. },
  172. }
  173. var results []v1.IpCountResult
  174. // 使用 qmgo 执行聚合查询
  175. err := r.mongoDB.Collection("udp_forwarding_rules").Aggregate(ctx, pipeline).All(&results)
  176. if err != nil {
  177. return nil, err
  178. }
  179. return results, nil
  180. }