gatewayip.go 3.3 KB

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