allowanddenyip.go 4.5 KB

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