udpforwarding.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. GetPortCount(ctx context.Context, hostId int64, port string) (int64, error)
  29. }
  30. func NewUdpForWardingRepository(
  31. repository *Repository,
  32. ) UdpForWardingRepository {
  33. return &udpForWardingRepository{
  34. Repository: repository,
  35. }
  36. }
  37. type udpForWardingRepository struct {
  38. *Repository
  39. }
  40. func (r *udpForWardingRepository) GetUdpForWarding(ctx context.Context, id int64) (*model.UdpForWarding, error) {
  41. var udpForWarding model.UdpForWarding
  42. if err := r.db.Where("id = ?", id).First(&udpForWarding).Error; err != nil {
  43. return nil, err
  44. }
  45. return &udpForWarding, nil
  46. }
  47. func (r *udpForWardingRepository) AddUdpForwarding(ctx context.Context, req *model.UdpForWarding) (int, error) {
  48. if err := r.db.WithContext(ctx).Create(req).Error; err != nil {
  49. return 0, err
  50. }
  51. return req.Id, nil
  52. }
  53. func (r *udpForWardingRepository) EditUdpForwarding(ctx context.Context, req *model.UdpForWarding) error {
  54. data := map[string]interface{}{
  55. "proxy" : req.Proxy,
  56. }
  57. if err := r.db.Updates(req).Updates(data).Error; err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. func (r *udpForWardingRepository) DeleteUdpForwarding(ctx context.Context, id int64) error {
  63. if err := r.db.Where("id = ?", id).Delete(&model.UdpForWarding{}).Error; err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. func (r *udpForWardingRepository) GetUdpForwardingWafUdpIdById(ctx context.Context, id int) (int, error) {
  69. var WafUdpId int
  70. if err := r.db.Model(&model.UdpForWarding{}).Where("id = ?", id).Select("waf_udp_id").Find(&WafUdpId).Error; err != nil {
  71. return 0, err
  72. }
  73. return WafUdpId, nil
  74. }
  75. func (r *udpForWardingRepository) GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error) {
  76. var count int64
  77. if err := r.db.Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Count(&count).Error; err != nil {
  78. return 0, err
  79. }
  80. return count, nil
  81. }
  82. func (r *udpForWardingRepository) GetUdpForwardingWafUdpAllIds(ctx context.Context, hostId int) ([]int, error) {
  83. var res []int
  84. if err:= r.db.WithContext(ctx).Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Select("id").Find(&res).Error; err != nil {
  85. return nil, err
  86. }
  87. return res, nil
  88. }
  89. // mongodb 插入
  90. func (r *udpForWardingRepository) AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error) {
  91. collection := r.mongoDB.Collection("udp_forwarding_rules")
  92. req.CreatedAt = time.Now()
  93. result, err := collection.InsertOne(ctx, req)
  94. if err != nil {
  95. return primitive.NilObjectID, fmt.Errorf("插入MongoDB失败: %w", err)
  96. }
  97. // 返回插入文档的ID
  98. return result.InsertedID.(primitive.ObjectID), nil
  99. }
  100. func (r *udpForWardingRepository) EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error {
  101. collection := r.mongoDB.Collection("udp_forwarding_rules")
  102. updateData := bson.M{}
  103. if req.Uid != 0 {
  104. updateData["uid"] = req.Uid
  105. }
  106. if req.HostId != 0 {
  107. updateData["host_id"] = req.HostId
  108. }
  109. if req.UdpId != 0 {
  110. updateData["udp_id"] = req.UdpId
  111. }
  112. if len(req.BackendList) > 0 {
  113. updateData["backend_list"] = req.BackendList
  114. }
  115. updateData["cdn_origin_ids"] = req.CdnOriginIds
  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. }
  153. // 获取IP数量等于1的IP
  154. func (r *udpForWardingRepository) GetIpCountByIp(ctx context.Context, ips []string) ([]v1.IpCountResult, error) {
  155. if len(ips) == 0 {
  156. return []v1.IpCountResult{}, nil
  157. }
  158. pipeline := []bson.M{
  159. {
  160. "$match": bson.M{
  161. "ip": bson.M{"$in": ips},
  162. },
  163. },
  164. {
  165. "$group": bson.M{
  166. "_id": "$ip",
  167. "count": bson.M{"$sum": 1},
  168. },
  169. },
  170. {
  171. "$project": bson.M{
  172. "_id": 0, // 不输出默认的_id
  173. "ip": "$_id", // 将分组的_id字段重命名为ip
  174. "count": 1, // 保留count字段
  175. },
  176. },
  177. }
  178. var results []v1.IpCountResult
  179. // 使用 qmgo 执行聚合查询
  180. err := r.mongoDB.Collection("udp_forwarding_rules").Aggregate(ctx, pipeline).All(&results)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return results, nil
  185. }
  186. func (r *udpForWardingRepository) GetPortCount(ctx context.Context, hostId int64, port string) (int64, error) {
  187. var count int64
  188. if err := r.db.WithContext(ctx).Model(&model.UdpForWarding{}).Where("host_id = ? AND port = ?", hostId, port).Count(&count).Error; err != nil {
  189. return 0, err
  190. }
  191. return count, nil
  192. }