udpforwarding.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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, hostId 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. GetUdpAll(ctx context.Context, hostIds []int) ([]int, error)
  30. }
  31. func NewUdpForWardingRepository(
  32. repository *Repository,
  33. ) UdpForWardingRepository {
  34. return &udpForWardingRepository{
  35. Repository: repository,
  36. }
  37. }
  38. type udpForWardingRepository struct {
  39. *Repository
  40. }
  41. func (r *udpForWardingRepository) GetUdpForWarding(ctx context.Context, id int64) (*model.UdpForWarding, error) {
  42. var udpForWarding model.UdpForWarding
  43. if err := r.db.Where("id = ?", id).First(&udpForWarding).Error; err != nil {
  44. return nil, err
  45. }
  46. return &udpForWarding, nil
  47. }
  48. func (r *udpForWardingRepository) AddUdpForwarding(ctx context.Context, req *model.UdpForWarding) (int, error) {
  49. if err := r.db.WithContext(ctx).Create(req).Error; err != nil {
  50. return 0, err
  51. }
  52. return req.Id, nil
  53. }
  54. func (r *udpForWardingRepository) EditUdpForwarding(ctx context.Context, req *model.UdpForWarding) error {
  55. data := map[string]interface{}{
  56. "proxy" : req.Proxy,
  57. }
  58. if err := r.db.Updates(req).Updates(data).Error; err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (r *udpForWardingRepository) DeleteUdpForwarding(ctx context.Context, id int64) error {
  64. if err := r.db.Where("id = ?", id).Delete(&model.UdpForWarding{}).Error; err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. func (r *udpForWardingRepository) GetUdpForwardingWafUdpIdById(ctx context.Context, id int) (int, error) {
  70. var WafUdpId int
  71. if err := r.db.Model(&model.UdpForWarding{}).Where("id = ?", id).Select("waf_udp_id").Find(&WafUdpId).Error; err != nil {
  72. return 0, err
  73. }
  74. return WafUdpId, nil
  75. }
  76. func (r *udpForWardingRepository) GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error) {
  77. var count int64
  78. if err := r.db.Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Count(&count).Error; err != nil {
  79. return 0, err
  80. }
  81. return count, nil
  82. }
  83. func (r *udpForWardingRepository) GetUdpForwardingWafUdpAllIds(ctx context.Context, hostId int) ([]int, error) {
  84. var res []int
  85. if err:= r.db.WithContext(ctx).Model(&model.UdpForWarding{}).Where("host_id = ?", hostId).Select("id").Find(&res).Error; err != nil {
  86. return nil, err
  87. }
  88. return res, nil
  89. }
  90. // mongodb 插入
  91. func (r *udpForWardingRepository) AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error) {
  92. collection := r.mongoDB.Collection("udp_forwarding_rules")
  93. req.CreatedAt = time.Now()
  94. result, err := collection.InsertOne(ctx, req)
  95. if err != nil {
  96. return primitive.NilObjectID, fmt.Errorf("插入MongoDB失败: %w", err)
  97. }
  98. // 返回插入文档的ID
  99. return result.InsertedID.(primitive.ObjectID), nil
  100. }
  101. func (r *udpForWardingRepository) EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error {
  102. collection := r.mongoDB.Collection("udp_forwarding_rules")
  103. updateData := bson.M{}
  104. if req.Uid != 0 {
  105. updateData["uid"] = req.Uid
  106. }
  107. if req.HostId != 0 {
  108. updateData["host_id"] = req.HostId
  109. }
  110. if req.UdpId != 0 {
  111. updateData["udp_id"] = req.UdpId
  112. }
  113. if len(req.BackendList) > 0 {
  114. updateData["backend_list"] = req.BackendList
  115. }
  116. updateData["cdn_origin_ids"] = req.CdnOriginIds
  117. // 始终更新更新时间
  118. updateData["updated_at"] = time.Now()
  119. // 如果没有任何字段需要更新,则直接返回
  120. if len(updateData) == 0 {
  121. return nil
  122. }
  123. // 执行更新
  124. update := bson.M{"$set": updateData}
  125. err := collection.UpdateOne(ctx, bson.M{"udp_id": req.UdpId}, update)
  126. if err != nil {
  127. return fmt.Errorf("更新MongoDB文档失败: %w", err)
  128. }
  129. return nil
  130. }
  131. func (r *udpForWardingRepository) GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error) {
  132. collection := r.mongoDB.Collection("udp_forwarding_rules")
  133. var result model.UdpForwardingRule
  134. err := collection.Find(ctx, bson.M{"udp_id": udpId}).One(&result)
  135. if err != nil {
  136. if errors.Is(err, mongo.ErrNoDocuments) {
  137. return nil, nil
  138. }
  139. return nil, fmt.Errorf("从MongoDB中获取文档失败: %w", err)
  140. }
  141. return &result, nil
  142. }
  143. func (r *udpForWardingRepository) DeleteUdpForwardingIpsById(ctx context.Context, udpId int) error {
  144. collection := r.mongoDB.Collection("udp_forwarding_rules")
  145. err := collection.Remove(ctx, bson.M{"udp_id": udpId})
  146. if err != nil {
  147. if errors.Is(err, mongo.ErrNoDocuments) {
  148. return fmt.Errorf("记录不存在")
  149. }
  150. return fmt.Errorf("删除MongoDB文档失败: %w", err)
  151. }
  152. return nil
  153. }
  154. // 获取IP数量等于1的IP
  155. func (r *udpForWardingRepository) GetIpCountByIp(ctx context.Context, ips []string) ([]v1.IpCountResult, error) {
  156. if len(ips) == 0 {
  157. return []v1.IpCountResult{}, nil
  158. }
  159. // 管道逻辑与 TCP 版本完全相同
  160. pipeline := []bson.M{
  161. {
  162. "$unwind": "$backend_list",
  163. },
  164. {
  165. "$addFields": bson.M{
  166. "extracted_ip": bson.M{
  167. "$arrayElemAt": []interface{}{
  168. bson.M{"$split": []string{"$backend_list", ":"}},
  169. 0,
  170. },
  171. },
  172. },
  173. },
  174. {
  175. "$match": bson.M{
  176. "extracted_ip": bson.M{"$in": ips},
  177. },
  178. },
  179. {
  180. "$group": bson.M{
  181. "_id": "$extracted_ip",
  182. "count": bson.M{"$sum": 1},
  183. },
  184. },
  185. {
  186. "$project": bson.M{
  187. "_id": 0,
  188. "ip": "$_id",
  189. "count": 1,
  190. },
  191. },
  192. }
  193. var results []v1.IpCountResult
  194. err := r.mongoDB.Collection("udp_forwarding_rules").Aggregate(ctx, pipeline).All(&results)
  195. if err != nil {
  196. return nil, fmt.Errorf("聚合查询 udp_forwarding_rules 失败: %w", err)
  197. }
  198. return results, nil
  199. }
  200. func (r *udpForWardingRepository) GetPortCount(ctx context.Context, hostId int64, port string) (int64, error) {
  201. var count int64
  202. if err := r.db.WithContext(ctx).Model(&model.UdpForWarding{}).Where("host_id = ? AND port = ?", hostId, port).Count(&count).Error; err != nil {
  203. return 0, err
  204. }
  205. return count, nil
  206. }
  207. func (r *udpForWardingRepository) GetUdpAll(ctx context.Context, hostIds []int) ([]int, error) {
  208. var res []int
  209. if err:= r.db.WithContext(ctx).Model(&model.UdpForWarding{}).Where("host_id IN ?", hostIds).Select("cdn_web_id").Scan(&res).Error; err != nil {
  210. return nil, err
  211. }
  212. return res, nil
  213. }