allowanddenyip.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package service
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  7. )
  8. type AllowAndDenyIpService interface {
  9. GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error)
  10. GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error)
  11. AddAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error
  12. EditAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error
  13. DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error
  14. }
  15. func NewAllowAndDenyIpService(
  16. service *Service,
  17. allowAndDenyIpRepository repository.AllowAndDenyIpRepository,
  18. gatewayGroupIp GateWayGroupIpService,
  19. wafformatter WafFormatterService,
  20. ) AllowAndDenyIpService {
  21. return &allowAndDenyIpService{
  22. Service: service,
  23. allowAndDenyIpRepository: allowAndDenyIpRepository,
  24. gatewayGroupIp: gatewayGroupIp,
  25. wafformatter : wafformatter,
  26. }
  27. }
  28. type allowAndDenyIpService struct {
  29. *Service
  30. allowAndDenyIpRepository repository.AllowAndDenyIpRepository
  31. gatewayGroupIp GateWayGroupIpService
  32. wafformatter WafFormatterService
  33. }
  34. func (s *allowAndDenyIpService) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) {
  35. res, err := s.allowAndDenyIpRepository.GetAllowAndDenyIp(ctx, id)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return res, nil
  40. }
  41. func (s *allowAndDenyIpService) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) {
  42. res, err := s.allowAndDenyIpRepository.GetAllowAndDenyIpsAllByHostId(ctx, hostId)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return res, nil
  47. }
  48. func (s *allowAndDenyIpService) AddAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error {
  49. gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
  50. if err != nil {
  51. return err
  52. }
  53. color := "black"
  54. if req.AllowOrDeny == 1 {
  55. color = "white"
  56. }
  57. for _, v := range gatewayGroupIps {
  58. go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color)
  59. }
  60. if err := s.allowAndDenyIpRepository.AddAllowAndDenyIps(ctx, model.AllowAndDenyIp{
  61. Ip: req.Ip,
  62. HostId: req.HostId,
  63. AllowOrDeny: req.AllowOrDeny,
  64. Uid: req.Uid,
  65. }); err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. func (s *allowAndDenyIpService) EditAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error {
  71. gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
  72. if err != nil {
  73. return err
  74. }
  75. color := "black"
  76. if req.AllowOrDeny == 1 {
  77. color = "white"
  78. }
  79. oldIp, err := s.GetAllowAndDenyIp(ctx, int64(req.Id))
  80. if err != nil {
  81. return err
  82. }
  83. if oldIp.Ip != req.Ip {
  84. for _, v := range gatewayGroupIps {
  85. go s.wafformatter.PublishIpWhitelistTask([]string{oldIp.Ip}, "del",v,color)
  86. }
  87. }
  88. for _, v := range gatewayGroupIps {
  89. go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color)
  90. }
  91. if err := s.allowAndDenyIpRepository.EditAllowAndDenyIps(ctx, model.AllowAndDenyIp{
  92. Id: req.Id,
  93. Ip: req.Ip,
  94. HostId: req.HostId,
  95. AllowOrDeny: req.AllowOrDeny,
  96. Uid: req.Uid,
  97. }); err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func (s *allowAndDenyIpService) DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error {
  103. for _, id := range req.Ids {
  104. gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
  105. if err != nil {
  106. return err
  107. }
  108. ip, err := s.GetAllowAndDenyIp(ctx, int64(id))
  109. if err != nil {
  110. return err
  111. }
  112. color := "black"
  113. if ip.AllowOrDeny == 1 {
  114. color = "white"
  115. }
  116. for _, v := range gatewayGroupIps {
  117. go s.wafformatter.PublishIpWhitelistTask([]string{ip.Ip}, "del",v,color)
  118. }
  119. if err := s.allowAndDenyIpRepository.DeleteAllowAndDenyIps(ctx, int64(id)); err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }