gatewayip.go 3.5 KB

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