gatewayip.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. )
  10. type GatewayipService interface {
  11. GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error)
  12. GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error)
  13. GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error)
  14. AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error
  15. }
  16. func NewGatewayipService(
  17. service *service.Service,
  18. gatewayipRepository waf.GatewayipRepository,
  19. host service.HostService,
  20. log service.LogService,
  21. ) GatewayipService {
  22. return &gatewayipService{
  23. Service: service,
  24. gatewayipRepository: gatewayipRepository,
  25. host : host,
  26. log : log,
  27. }
  28. }
  29. type gatewayipService struct {
  30. *service.Service
  31. gatewayipRepository waf.GatewayipRepository
  32. host service.HostService
  33. log service.LogService
  34. }
  35. func (s *gatewayipService) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
  36. return s.gatewayipRepository.GetGatewayip(ctx, id)
  37. }
  38. func (s *gatewayipService) AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error {
  39. config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
  40. if err != nil {
  41. return err
  42. }
  43. ips, err := s.gatewayipRepository.GetIpWhereHostIdNull(ctx, v1.GlobalLimitRequireResponse{
  44. HostId: int(hostId),
  45. Bps: config.Bps,
  46. MaxBytesMonth: config.MaxBytesMonth,
  47. IpCount: config.IpCount,
  48. Operator: config.Operator,
  49. NodeArea: config.NodeArea,
  50. ConfigMaxProtection: config.ConfigMaxProtection,
  51. IsBanUdp: config.IsBanUdp,
  52. });
  53. if err != nil {
  54. return err
  55. }
  56. ipsJson, err := json.Marshal(ips)
  57. if err != nil {
  58. return err
  59. }
  60. if err = s.log.AddLog(ctx, &model.Log{
  61. Uid: uid,
  62. Api: "AddIpWhereHostIdNull",
  63. Message: "分配网关组IP",
  64. ExtraData: ipsJson,
  65. }); err != nil {
  66. return err
  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. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  77. if err != nil {
  78. return nil, err
  79. }
  80. gatewayIps, err = s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  81. if err != nil {
  82. return nil, err
  83. }
  84. }
  85. return gatewayIps, nil
  86. }
  87. func (s *gatewayipService) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error) {
  88. gatewayIps, err := s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  89. if err != nil {
  90. return "", err
  91. }
  92. if len(gatewayIps) == 0 {
  93. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  94. if err != nil {
  95. return "", err
  96. }
  97. gatewayIps, err = s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  98. if err != nil {
  99. return "", err
  100. }
  101. }
  102. return gatewayIps, nil
  103. }