udpforwarding.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package repository
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. "time"
  11. )
  12. type UdpForWardingRepository interface {
  13. GetUdpForWarding(ctx context.Context, id int64) (*model.UdpForWarding, error)
  14. AddUdpForwarding(ctx context.Context, req *model.UdpForWarding) (int, error)
  15. EditUdpForwarding(ctx context.Context, req *model.UdpForWarding) error
  16. DeleteUdpForwarding(ctx context.Context, id int64) error
  17. GetUdpForwardingWafUdpIdById(ctx context.Context, id int) (int, error)
  18. GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
  19. GetUdpForwardingWafUdpAllIds(ctx context.Context, udpId int) ([]int, error)
  20. AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error)
  21. EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error
  22. GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error)
  23. DeleteUdpForwardingIpsById(ctx context.Context, udpId int) error
  24. }
  25. func NewUdpForWardingRepository(
  26. repository *Repository,
  27. ) UdpForWardingRepository {
  28. return &udpForWardingRepository{
  29. Repository: repository,
  30. }
  31. }
  32. type udpForWardingRepository struct {
  33. *Repository
  34. }
  35. func (r *udpForWardingRepository) GetUdpForWarding(ctx context.Context, id int64) (*model.UdpForWarding, error) {
  36. var udpForWarding model.UdpForWarding
  37. if err := r.db.Where("id = ?", id).First(&udpForWarding).Error; err != nil {
  38. return nil, err
  39. }
  40. return &udpForWarding, nil
  41. }
  42. func (r *udpForWardingRepository) AddUdpForwarding(ctx context.Context, req *model.UdpForWarding) (int, error) {
  43. if err := r.db.Create(req).Error; err != nil {
  44. return 0, err
  45. }
  46. return req.Id, nil
  47. }
  48. func (r *udpForWardingRepository) EditUdpForwarding(ctx context.Context, req *model.UdpForWarding) error {
  49. if err := r.db.Updates(req).Error; err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. func (r *udpForWardingRepository) DeleteUdpForwarding(ctx context.Context, id int64) error {
  55. if err := r.db.Where("id = ?", id).Delete(&model.UdpForWarding{}).Error; err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func (r *udpForWardingRepository) GetUdpForwardingWafUdpIdById(ctx context.Context, id int) (int, error) {
  61. var WafUdpId int
  62. if err := r.db.Model(&model.UdpForWarding{}).Where("id = ?", id).Select("waf_udp_id").Find(&WafUdpId).Error; err != nil {
  63. return 0, err
  64. }
  65. return WafUdpId, nil
  66. }
  67. func (r *udpForWardingRepository) GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error) {
  68. var count int64
  69. if err := r.db.Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Count(&count).Error; err != nil {
  70. return 0, err
  71. }
  72. return count, nil
  73. }
  74. func (r *udpForWardingRepository) GetUdpForwardingWafUdpAllIds(ctx context.Context, hostId int) ([]int, error) {
  75. var res []int
  76. if err:= r.db.WithContext(ctx).Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Select("id").Find(&res).Error; err != nil {
  77. return nil, err
  78. }
  79. return res, nil
  80. }
  81. // mongodb 插入
  82. func (r *udpForWardingRepository) AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error) {
  83. collection := r.mongoDB.Collection("udp_forwarding_rules")
  84. req.CreatedAt = time.Now()
  85. result, err := collection.InsertOne(ctx, req)
  86. if err != nil {
  87. return primitive.NilObjectID, fmt.Errorf("插入MongoDB失败: %w", err)
  88. }
  89. // 返回插入文档的ID
  90. return result.InsertedID.(primitive.ObjectID), nil
  91. }
  92. func (r *udpForWardingRepository) EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error {
  93. collection := r.mongoDB.Collection("udp_forwarding_rules")
  94. updateData := bson.M{}
  95. if req.Uid != 0 {
  96. updateData["uid"] = req.Uid
  97. }
  98. if req.HostId != 0 {
  99. updateData["host_id"] = req.HostId
  100. }
  101. if req.UdpId != 0 {
  102. updateData["udp_id"] = req.UdpId
  103. }
  104. if req.AccessRule != "" {
  105. updateData["access_rule"] = req.AccessRule
  106. }
  107. if len(req.BackendList) > 0 {
  108. updateData["backend_list"] = req.BackendList
  109. }
  110. if len(req.AllowIpList) > 0 {
  111. updateData["allow_ip_list"] = req.AllowIpList
  112. }
  113. if len(req.DenyIpList) > 0 {
  114. updateData["deny_ip_list"] = req.DenyIpList
  115. }
  116. // 始终更新更新时间
  117. updateData["updated_at"] = time.Now()
  118. // 如果没有任何字段需要更新,则直接返回
  119. if len(updateData) == 0 {
  120. return nil
  121. }
  122. // 执行更新
  123. update := bson.M{"$set": updateData}
  124. err := collection.UpdateOne(ctx, bson.M{"udp_id": req.UdpId}, update)
  125. if err != nil {
  126. return fmt.Errorf("更新MongoDB文档失败: %w", err)
  127. }
  128. return nil
  129. }
  130. func (r *udpForWardingRepository) GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error) {
  131. collection := r.mongoDB.Collection("udp_forwarding_rules")
  132. var result model.UdpForwardingRule
  133. err := collection.Find(ctx, bson.M{"udp_id": udpId}).One(&result)
  134. if err != nil {
  135. if errors.Is(err, mongo.ErrNoDocuments) {
  136. return nil, nil
  137. }
  138. return nil, fmt.Errorf("从MongoDB中获取文档失败: %w", err)
  139. }
  140. return &result, nil
  141. }
  142. func (r *udpForWardingRepository) DeleteUdpForwardingIpsById(ctx context.Context, udpId int) error {
  143. collection := r.mongoDB.Collection("udp_forwarding_rules")
  144. err := collection.Remove(ctx, bson.M{"udp_id": udpId})
  145. if err != nil {
  146. if errors.Is(err, mongo.ErrNoDocuments) {
  147. return fmt.Errorf("记录不存在")
  148. }
  149. return fmt.Errorf("删除MongoDB文档失败: %w", err)
  150. }
  151. return nil
  152. }