gatewayip.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package waf
  2. import (
  3. "context"
  4. "fmt"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/repository/api/waf"
  9. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  10. )
  11. type GatewayipService interface {
  12. GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error)
  13. GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error)
  14. GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error)
  15. AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error
  16. }
  17. func NewGatewayipService(
  18. service *service.Service,
  19. gatewayipRepository waf.GatewayipRepository,
  20. host service.HostService,
  21. log service.LogService,
  22. wafLogService WaflogService,
  23. ) GatewayipService {
  24. return &gatewayipService{
  25. Service: service,
  26. gatewayipRepository: gatewayipRepository,
  27. host : host,
  28. log : log,
  29. wafLogService : wafLogService,
  30. }
  31. }
  32. type gatewayipService struct {
  33. *service.Service
  34. gatewayipRepository waf.GatewayipRepository
  35. host service.HostService
  36. log service.LogService
  37. wafLogService WaflogService
  38. }
  39. func (s *gatewayipService) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
  40. return s.gatewayipRepository.GetGatewayip(ctx, id)
  41. }
  42. func (s *gatewayipService) AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error {
  43. config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
  44. if err != nil {
  45. return err
  46. }
  47. ips, err := s.gatewayipRepository.GetIpWhereHostIdNull(ctx, v1.GlobalLimitRequireResponse{
  48. HostId: int(hostId),
  49. Bps: config.Bps,
  50. MaxBytesMonth: config.MaxBytesMonth,
  51. IpCount: config.IpCount,
  52. Operator: config.Operator,
  53. NodeArea: config.NodeArea,
  54. ConfigMaxProtection: config.ConfigMaxProtection,
  55. IsBanUdp: config.IsBanUdp,
  56. IsBanOverseas: config.IsBanOverseas,
  57. });
  58. if err != nil {
  59. return err
  60. }
  61. go s.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
  62. Uid: int(uid),
  63. Api: "分配网关组", // 复制 Path
  64. HostId: int(hostId),
  65. ExtraData: ips,
  66. })
  67. return nil
  68. }
  69. func (s *gatewayipService) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error) {
  70. gatewayIps, err := s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if len(gatewayIps) == 0 {
  75. expire, err := s.host.CheckExpired(ctx, uid, hostId)
  76. if err != nil {
  77. return nil, err
  78. }
  79. if !expire {
  80. return nil, fmt.Errorf("产品已过期,请及时续费")
  81. }
  82. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  83. if err != nil {
  84. return nil, err
  85. }
  86. gatewayIps, err = s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. return gatewayIps, nil
  92. }
  93. func (s *gatewayipService) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error) {
  94. gatewayIps, err := s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  95. if err != nil {
  96. return "", err
  97. }
  98. if len(gatewayIps) == 0 {
  99. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  100. if err != nil {
  101. return "", err
  102. }
  103. gatewayIps, err = s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  104. if err != nil {
  105. return "", err
  106. }
  107. }
  108. return gatewayIps, nil
  109. }