allowanddenyip.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  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"
  8. )
  9. type AllowAndDenyIpService interface {
  10. GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error)
  11. GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error)
  12. AddAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error
  13. EditAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error
  14. DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error
  15. }
  16. func NewAllowAndDenyIpService(
  17. service *Service,
  18. allowAndDenyIpRepository repository.AllowAndDenyIpRepository,
  19. gatewayGroupIp GateWayGroupIpService,
  20. wafformatter WafFormatterService,
  21. ) AllowAndDenyIpService {
  22. return &allowAndDenyIpService{
  23. Service: service,
  24. allowAndDenyIpRepository: allowAndDenyIpRepository,
  25. gatewayGroupIp: gatewayGroupIp,
  26. wafformatter : wafformatter,
  27. }
  28. }
  29. type allowAndDenyIpService struct {
  30. *Service
  31. allowAndDenyIpRepository repository.AllowAndDenyIpRepository
  32. gatewayGroupIp GateWayGroupIpService
  33. wafformatter WafFormatterService
  34. }
  35. func (s *allowAndDenyIpService) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) {
  36. res, err := s.allowAndDenyIpRepository.GetAllowAndDenyIp(ctx, id)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return res, nil
  41. }
  42. func (s *allowAndDenyIpService) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) {
  43. res, err := s.allowAndDenyIpRepository.GetAllowAndDenyIpsAllByHostId(ctx, hostId)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return res, nil
  48. }
  49. func (s *allowAndDenyIpService) AddAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error {
  50. // 判断ip是否存在
  51. err := s.IsExistIp(ctx, int64(req.HostId), req.Ip)
  52. if err != nil {
  53. return err
  54. }
  55. gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
  56. if err != nil {
  57. return err
  58. }
  59. color := "black"
  60. if req.AllowOrDeny == 1 {
  61. color = "white"
  62. }
  63. for _, v := range gatewayGroupIps {
  64. go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color)
  65. }
  66. if err := s.allowAndDenyIpRepository.AddAllowAndDenyIps(ctx, model.AllowAndDenyIp{
  67. Ip: req.Ip,
  68. HostId: req.HostId,
  69. AllowOrDeny: req.AllowOrDeny,
  70. Uid: req.Uid,
  71. }); err != nil {
  72. return err
  73. }
  74. return nil
  75. }
  76. func (s *allowAndDenyIpService) EditAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error {
  77. // 判断ip是否存在
  78. err := s.IsExistIp(ctx, int64(req.HostId), req.Ip)
  79. if err != nil {
  80. return err
  81. }
  82. gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
  83. if err != nil {
  84. return err
  85. }
  86. color := "black"
  87. if req.AllowOrDeny == 1 {
  88. color = "white"
  89. }
  90. oldIp, err := s.GetAllowAndDenyIp(ctx, int64(req.Id))
  91. if err != nil {
  92. return err
  93. }
  94. if oldIp.Ip != req.Ip {
  95. for _, v := range gatewayGroupIps {
  96. go s.wafformatter.PublishIpWhitelistTask([]string{oldIp.Ip}, "del",v,color)
  97. }
  98. }
  99. for _, v := range gatewayGroupIps {
  100. go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color)
  101. }
  102. if err := s.allowAndDenyIpRepository.EditAllowAndDenyIps(ctx, model.AllowAndDenyIp{
  103. Id: req.Id,
  104. Ip: req.Ip,
  105. HostId: req.HostId,
  106. AllowOrDeny: req.AllowOrDeny,
  107. Uid: req.Uid,
  108. }); err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. func (s *allowAndDenyIpService) DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error {
  114. for _, id := range req.Ids {
  115. gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
  116. if err != nil {
  117. return err
  118. }
  119. ip, err := s.GetAllowAndDenyIp(ctx, int64(id))
  120. if err != nil {
  121. return err
  122. }
  123. color := "black"
  124. if ip.AllowOrDeny == 1 {
  125. color = "white"
  126. }
  127. for _, v := range gatewayGroupIps {
  128. go s.wafformatter.PublishIpWhitelistTask([]string{ip.Ip}, "del",v,color)
  129. }
  130. if err := s.allowAndDenyIpRepository.DeleteAllowAndDenyIps(ctx, int64(id)); err != nil {
  131. return err
  132. }
  133. }
  134. return nil
  135. }
  136. func (s *allowAndDenyIpService) IsExistIp(ctx context.Context, hostId int64, Ip string) error {
  137. count, err := s.allowAndDenyIpRepository.GetIpCount(ctx, hostId, Ip)
  138. if err != nil {
  139. return err
  140. }
  141. if count > 0 {
  142. return fmt.Errorf("ip已存在,请勿重复添加")
  143. }
  144. return nil
  145. }