gatewayip.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package waf
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  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. "strconv"
  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. ) GatewayipService {
  24. return &gatewayipService{
  25. Service: service,
  26. gatewayipRepository: gatewayipRepository,
  27. host : host,
  28. log : log,
  29. }
  30. }
  31. type gatewayipService struct {
  32. *service.Service
  33. gatewayipRepository waf.GatewayipRepository
  34. host service.HostService
  35. log service.LogService
  36. }
  37. func (s *gatewayipService) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
  38. return s.gatewayipRepository.GetGatewayip(ctx, id)
  39. }
  40. func (s *gatewayipService) AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error {
  41. config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
  42. if err != nil {
  43. return err
  44. }
  45. ips, err := s.gatewayipRepository.GetIpWhereHostIdNull(ctx, v1.GlobalLimitRequireResponse{
  46. HostId: int(hostId),
  47. Bps: config.Bps,
  48. MaxBytesMonth: config.MaxBytesMonth,
  49. IpCount: config.IpCount,
  50. Operator: config.Operator,
  51. NodeArea: config.NodeArea,
  52. ConfigMaxProtection: config.ConfigMaxProtection,
  53. IsBanUdp: config.IsBanUdp,
  54. IsBanOverseas: config.IsBanOverseas,
  55. });
  56. if err != nil {
  57. return err
  58. }
  59. ipsJson, err := json.Marshal(ips)
  60. if err != nil {
  61. return err
  62. }
  63. configJson, err := json.Marshal(config)
  64. if err != nil {
  65. return err
  66. }
  67. if err = s.log.AddLog(ctx, &model.Log{
  68. Uid: uid,
  69. Api: "AddIpWhereHostIdNull,分配网关组IP",
  70. Message: string(configJson) + "," + "hostId:" + strconv.FormatInt(hostId, 10),
  71. ExtraData: ipsJson,
  72. }); err != nil {
  73. return err
  74. }
  75. return nil
  76. }
  77. func (s *gatewayipService) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error) {
  78. gatewayIps, err := s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if len(gatewayIps) == 0 {
  83. expire, err := s.host.CheckExpired(ctx, uid, hostId)
  84. if err != nil {
  85. return nil, err
  86. }
  87. if !expire {
  88. return nil, fmt.Errorf("产品已过期,请及时续费")
  89. }
  90. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  91. if err != nil {
  92. return nil, err
  93. }
  94. gatewayIps, err = s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  95. if err != nil {
  96. return nil, err
  97. }
  98. }
  99. return gatewayIps, nil
  100. }
  101. func (s *gatewayipService) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error) {
  102. gatewayIps, err := s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  103. if err != nil {
  104. return "", err
  105. }
  106. if len(gatewayIps) == 0 {
  107. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  108. if err != nil {
  109. return "", err
  110. }
  111. gatewayIps, err = s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  112. if err != nil {
  113. return "", err
  114. }
  115. }
  116. return gatewayIps, nil
  117. }