allowanddenyip.go 4.7 KB

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