tcpforwarding.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 TcpforwardingRepository interface {
  14. GetTcpforwarding(ctx context.Context, id int64) (*model.Tcpforwarding, error)
  15. AddTcpforwarding(ctx context.Context, req *model.Tcpforwarding) (int, error)
  16. EditTcpforwarding(ctx context.Context, req *model.Tcpforwarding) error
  17. DeleteTcpforwarding(ctx context.Context, id int64) error
  18. GetTcpforwardingWafTcpIdById(ctx context.Context, id int) (int, error)
  19. GetTcpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
  20. GetTcpForwardingAllIdsByID(ctx context.Context, hostId int) ([]int, error)
  21. AddTcpforwardingIps(ctx context.Context,req model.TcpForwardingRule) (primitive.ObjectID, error)
  22. EditTcpforwardingIps(ctx context.Context, req model.TcpForwardingRule) error
  23. GetTcpForwardingIpsByID(ctx context.Context, tcpId int) (*model.TcpForwardingRule, error)
  24. DeleteTcpForwardingIpsById(ctx context.Context, tcpId int) error
  25. // 获取IP数量等于1的IP
  26. GetIpCountByIp(ctx context.Context,ips []string) ([]v1.IpCountResult, error)
  27. }
  28. func NewTcpforwardingRepository(
  29. repository *Repository,
  30. ) TcpforwardingRepository {
  31. return &tcpforwardingRepository{
  32. Repository: repository,
  33. }
  34. }
  35. type tcpforwardingRepository struct {
  36. *Repository
  37. }
  38. func (r *tcpforwardingRepository) GetTcpforwarding(ctx context.Context, id int64) (*model.Tcpforwarding, error) {
  39. var tcpforwarding model.Tcpforwarding
  40. if err := r.db.Where("id = ?", id).First(&tcpforwarding).Error; err != nil {
  41. return nil, err
  42. }
  43. return &tcpforwarding, nil
  44. }
  45. func (r *tcpforwardingRepository) AddTcpforwarding(ctx context.Context, req *model.Tcpforwarding) (int, error) {
  46. if err := r.db.Create(req).Error; err != nil {
  47. return 0, err
  48. }
  49. return req.Id, nil
  50. }
  51. func (r *tcpforwardingRepository) EditTcpforwarding(ctx context.Context, req *model.Tcpforwarding) error {
  52. if err := r.db.Updates(req).Error; err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. func (r *tcpforwardingRepository) DeleteTcpforwarding(ctx context.Context, id int64) error {
  58. if err := r.db.Where("id = ?", id).Delete(&model.Tcpforwarding{}).Error; err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (r *tcpforwardingRepository) GetTcpforwardingWafTcpIdById(ctx context.Context, id int) (int, error) {
  64. var WafTcpId int
  65. if err := r.db.Model(&model.Tcpforwarding{}).Where("id = ?", id).Select("waf_tcp_id").Find(&WafTcpId).Error; err != nil {
  66. return 0, err
  67. }
  68. return WafTcpId, nil
  69. }
  70. func (r *tcpforwardingRepository) GetTcpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error) {
  71. var count int64
  72. if err := r.db.Model(&model.Tcpforwarding{}).Where("host_id = ?", hostId).Count(&count).Error; err != nil {
  73. return 0, err
  74. }
  75. return count, nil
  76. }
  77. func (r *tcpforwardingRepository) GetTcpForwardingAllIdsByID(ctx context.Context, hostId int) ([]int, error) {
  78. var res []int
  79. if err := r.db.WithContext(ctx).Model(&model.Tcpforwarding{}).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 *tcpforwardingRepository) AddTcpforwardingIps(ctx context.Context,req model.TcpForwardingRule) (primitive.ObjectID, error) {
  86. collection := r.mongoDB.Collection("tcp_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 *tcpforwardingRepository) EditTcpforwardingIps(ctx context.Context, req model.TcpForwardingRule) error {
  96. collection := r.mongoDB.Collection("tcp_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.TcpId != 0 {
  105. updateData["tcp_id"] = req.TcpId
  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{"tcp_id": req.TcpId}, update)
  120. if err != nil {
  121. if errors.Is(err, mongo.ErrNoDocuments) {
  122. return fmt.Errorf("记录不存在")
  123. }
  124. return fmt.Errorf("更新MongoDB文档失败: %w", err)
  125. }
  126. return nil
  127. }
  128. func (r *tcpforwardingRepository) GetTcpForwardingIpsByID(ctx context.Context, tcpId int) (*model.TcpForwardingRule, error) {
  129. collection := r.mongoDB.Collection("tcp_forwarding_rules")
  130. var res model.TcpForwardingRule
  131. err := collection.Find(ctx, bson.M{"tcp_id": tcpId}).One(&res)
  132. if err != nil {
  133. if errors.Is(err, mongo.ErrNoDocuments) {
  134. return nil, fmt.Errorf("记录不存在")
  135. }
  136. return nil, fmt.Errorf("查询MongoDB失败: %w", err)
  137. }
  138. return &res, nil
  139. }
  140. func (r *tcpforwardingRepository) DeleteTcpForwardingIpsById(ctx context.Context, tcpId int) error {
  141. collection := r.mongoDB.Collection("tcp_forwarding_rules")
  142. err := collection.Remove(ctx, bson.M{"tcp_id": tcpId})
  143. if err != nil {
  144. if errors.Is(err, mongo.ErrNoDocuments) {
  145. return fmt.Errorf("记录不存在")
  146. }
  147. return fmt.Errorf("删除MongoDB文档失败: %w", err)
  148. }
  149. return nil
  150. }
  151. // 获取IP数量等于1的IP
  152. func (r *tcpforwardingRepository) GetIpCountByIp(ctx context.Context,ips []string) ([]v1.IpCountResult, error) {
  153. if len(ips) == 0 {
  154. return []v1.IpCountResult{}, nil
  155. }
  156. pipeline := []bson.M{
  157. {
  158. "$match": bson.M{
  159. "ip": bson.M{"$in": ips},
  160. },
  161. },
  162. {
  163. "$group": bson.M{
  164. "_id": "$ip",
  165. "count": bson.M{"$sum": 1},
  166. },
  167. },
  168. {
  169. "$project": bson.M{
  170. "_id": 0, // 不输出默认的_id
  171. "ip": "$_id", // 将分组的_id字段重命名为ip
  172. "count": 1, // 保留count字段
  173. },
  174. },
  175. }
  176. var results []v1.IpCountResult
  177. // 使用 qmgo 执行聚合查询
  178. err := r.mongoDB.Collection("tcp_forwarding_rules").Aggregate(ctx, pipeline).All(&results)
  179. if err != nil {
  180. return nil, err
  181. }
  182. return results, nil
  183. }