allowanddenyip.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if len(gatewayGroupIps) == 0 {
  61. return fmt.Errorf("请先配置实例")
  62. }
  63. color := "black"
  64. if req.AllowOrDeny == 1 {
  65. color = "white"
  66. }
  67. for _, v := range gatewayGroupIps {
  68. go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color)
  69. }
  70. if err := s.allowAndDenyIpRepository.AddAllowAndDenyIps(ctx, model.AllowAndDenyIp{
  71. Ip: req.Ip,
  72. HostId: req.HostId,
  73. AllowOrDeny: req.AllowOrDeny,
  74. Uid: req.Uid,
  75. }); err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func (s *allowAndDenyIpService) EditAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error {
  81. // 判断ip是否存在
  82. err := s.IsExistIp(ctx, int64(req.HostId), req.Ip)
  83. if err != nil {
  84. return err
  85. }
  86. gatewayGroupIps, err := s.gatewayIp.GetGatewayipOnlyIpByHostIdAll(ctx, int64(req.HostId), int64(req.Uid))
  87. if err != nil {
  88. return err
  89. }
  90. if len(gatewayGroupIps) == 0 {
  91. return fmt.Errorf("请先配置实例")
  92. }
  93. color := "black"
  94. if req.AllowOrDeny == 1 {
  95. color = "white"
  96. }
  97. oldIp, err := s.GetAllowAndDenyIp(ctx, int64(req.Id))
  98. if err != nil {
  99. return err
  100. }
  101. if oldIp.Ip != req.Ip {
  102. for _, v := range gatewayGroupIps {
  103. go s.wafformatter.PublishIpWhitelistTask([]string{oldIp.Ip}, "del",v,color)
  104. }
  105. }
  106. for _, v := range gatewayGroupIps {
  107. go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color)
  108. }
  109. if err := s.allowAndDenyIpRepository.EditAllowAndDenyIps(ctx, model.AllowAndDenyIp{
  110. Id: req.Id,
  111. Ip: req.Ip,
  112. HostId: req.HostId,
  113. AllowOrDeny: req.AllowOrDeny,
  114. Uid: req.Uid,
  115. }); err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120. func (s *allowAndDenyIpService) DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error {
  121. for _, id := range req.Ids {
  122. gatewayGroupIps, err := s.gatewayIp.GetGatewayipOnlyIpByHostIdAll(ctx, int64(req.HostId), int64(req.Uid))
  123. if err != nil {
  124. return err
  125. }
  126. if len(gatewayGroupIps) == 0 {
  127. return fmt.Errorf("请先配置实例")
  128. }
  129. ip, err := s.GetAllowAndDenyIp(ctx, int64(id))
  130. if err != nil {
  131. return err
  132. }
  133. color := "black"
  134. if ip.AllowOrDeny == 1 {
  135. color = "white"
  136. }
  137. for _, v := range gatewayGroupIps {
  138. go s.wafformatter.PublishIpWhitelistTask([]string{ip.Ip}, "del",v,color)
  139. }
  140. if err := s.allowAndDenyIpRepository.DeleteAllowAndDenyIps(ctx, int64(id)); err != nil {
  141. return err
  142. }
  143. }
  144. return nil
  145. }
  146. func (s *allowAndDenyIpService) IsExistIp(ctx context.Context, hostId int64, Ip string) error {
  147. count, err := s.allowAndDenyIpRepository.GetIpCount(ctx, hostId, Ip)
  148. if err != nil {
  149. return err
  150. }
  151. if count > 0 {
  152. return fmt.Errorf("ip已存在,请勿重复添加")
  153. }
  154. return nil
  155. }