buildaudun.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package waf
  2. import (
  3. "context"
  4. "fmt"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. wafRep "github.com/go-nunu/nunu-layout-advanced/internal/repository/api/waf"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  8. "github.com/hashicorp/go-multierror"
  9. "strconv"
  10. "sync"
  11. )
  12. type BuildAudunService interface {
  13. AddBandwidth(ctx context.Context, req v1.Bandwidth) error
  14. DelBandwidth(ctx context.Context, req v1.Bandwidth) error
  15. Bandwidth(ctx context.Context,hostId int64, action string) error
  16. }
  17. func NewBuildAudunService(
  18. service *service.Service,
  19. audun service.AoDunService,
  20. gatewayIpRep wafRep.GatewayipRepository,
  21. host service.HostService,
  22. ) BuildAudunService {
  23. return &buildAudunService{
  24. Service: service,
  25. audun: audun,
  26. gatewayIpRep: gatewayIpRep,
  27. host: host,
  28. }
  29. }
  30. type buildAudunService struct {
  31. *service.Service
  32. audun service.AoDunService
  33. gatewayIpRep wafRep.GatewayipRepository
  34. host service.HostService
  35. }
  36. func (s *buildAudunService) BuildName(ip string, bandwidth string, apiName string) string {
  37. return apiName + ip + "限速" + bandwidth + "M"
  38. }
  39. func (s *buildAudunService) AddBandwidth(ctx context.Context, req v1.Bandwidth) error {
  40. err := s.audun.AddBandwidthLimit(ctx, v1.Bandwidth{
  41. Action: "limit",
  42. ClientIPType: "all",
  43. Direction: "out",
  44. Name: s.BuildName(req.ServerIPStart, strconv.FormatInt(req.SpeedlimitOut, 10), ""),
  45. Protocol: 0,
  46. ServerIPStart: req.ServerIPStart,
  47. ServerIPType: "single",
  48. SpeedlimitOut: req.SpeedlimitOut,
  49. })
  50. if err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. func (s *buildAudunService) DelBandwidth(ctx context.Context, req v1.Bandwidth) error {
  56. err := s.audun.DelBandwidthLimit(ctx, v1.Bandwidth{
  57. Name: s.BuildName(req.ServerIPStart, strconv.FormatInt(req.SpeedlimitOut, 10), "KFW-API-RESTAPI-"),
  58. })
  59. if err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func (s *buildAudunService) Bandwidth(ctx context.Context,hostId int64, action string) error {
  65. ips, err := s.gatewayIpRep.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  66. if err != nil {
  67. return err
  68. }
  69. if len(ips) == 0 {
  70. return nil
  71. }
  72. config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
  73. if err != nil {
  74. return err
  75. }
  76. bpsInt, err := strconv.Atoi(config.Bps)
  77. if err != nil {
  78. return err
  79. }
  80. var errChan = make(chan error, len(ips))
  81. var wg sync.WaitGroup
  82. var allErrors error
  83. wg.Add(len(ips))
  84. for _, ip := range ips {
  85. go func(ip string) {
  86. var e error
  87. defer wg.Done()
  88. switch action {
  89. case "add":
  90. e = s.AddBandwidth(ctx, v1.Bandwidth{
  91. ServerIPStart: ip,
  92. SpeedlimitOut: int64(bpsInt),
  93. })
  94. case "del":
  95. e = s.DelBandwidth(ctx,v1.Bandwidth{
  96. ServerIPStart: ip,
  97. SpeedlimitOut: int64(bpsInt),
  98. })
  99. default:
  100. e = fmt.Errorf("未知操作")
  101. }
  102. if e != nil {
  103. errChan <- fmt.Errorf("设置限速 %s失败: %w", ip, e)
  104. }
  105. }(ip)
  106. }
  107. wg.Wait()
  108. close(errChan)
  109. for err := range errChan {
  110. allErrors = multierror.Append(allErrors, err)
  111. }
  112. if allErrors != nil {
  113. return allErrors
  114. }
  115. return nil
  116. }