gatewayip.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. IsBanOverseas: config.IsBanOverseas,
  54. });
  55. if err != nil {
  56. return err
  57. }
  58. ipsJson, err := json.Marshal(ips)
  59. if err != nil {
  60. return err
  61. }
  62. configJson, err := json.Marshal(config)
  63. if err != nil {
  64. return err
  65. }
  66. if err = s.log.AddLog(ctx, &model.Log{
  67. Uid: uid,
  68. Api: "AddIpWhereHostIdNull,分配网关组IP",
  69. Message: string(configJson) + "," + "hostId:" + strconv.FormatInt(hostId, 10),
  70. ExtraData: ipsJson,
  71. }); err != nil {
  72. return err
  73. }
  74. return nil
  75. }
  76. func (s *gatewayipService) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error) {
  77. gatewayIps, err := s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if len(gatewayIps) == 0 {
  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. }