package service import ( "context" v1 "github.com/go-nunu/nunu-layout-advanced/api/v1" "github.com/go-nunu/nunu-layout-advanced/internal/model" "github.com/go-nunu/nunu-layout-advanced/internal/repository" ) type AllowAndDenyIpService interface { GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) AddAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error EditAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error } func NewAllowAndDenyIpService( service *Service, allowAndDenyIpRepository repository.AllowAndDenyIpRepository, gatewayGroupIp GateWayGroupIpService, wafformatter WafFormatterService, ) AllowAndDenyIpService { return &allowAndDenyIpService{ Service: service, allowAndDenyIpRepository: allowAndDenyIpRepository, gatewayGroupIp: gatewayGroupIp, wafformatter : wafformatter, } } type allowAndDenyIpService struct { *Service allowAndDenyIpRepository repository.AllowAndDenyIpRepository gatewayGroupIp GateWayGroupIpService wafformatter WafFormatterService } func (s *allowAndDenyIpService) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) { res, err := s.allowAndDenyIpRepository.GetAllowAndDenyIp(ctx, id) if err != nil { return nil, err } return res, nil } func (s *allowAndDenyIpService) GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error) { res, err := s.allowAndDenyIpRepository.GetAllowAndDenyIpsAllByHostId(ctx, hostId) if err != nil { return nil, err } return res, nil } func (s *allowAndDenyIpService) AddAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error { gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId) if err != nil { return err } color := "black" if req.AllowOrDeny == 1 { color = "white" } for _, v := range gatewayGroupIps { go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color) } if err := s.allowAndDenyIpRepository.AddAllowAndDenyIps(ctx, model.AllowAndDenyIp{ Ip: req.Ip, HostId: req.HostId, AllowOrDeny: req.AllowOrDeny, Uid: req.Uid, }); err != nil { return err } return nil } func (s *allowAndDenyIpService) EditAllowAndDenyIps(ctx context.Context, req v1.AllowAndDenyIpRequest) error { gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId) if err != nil { return err } color := "black" if req.AllowOrDeny == 1 { color = "white" } oldIp, err := s.GetAllowAndDenyIp(ctx, int64(req.Id)) if err != nil { return err } if oldIp.Ip != req.Ip { for _, v := range gatewayGroupIps { go s.wafformatter.PublishIpWhitelistTask([]string{oldIp.Ip}, "del",v,color) } } for _, v := range gatewayGroupIps { go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color) } if err := s.allowAndDenyIpRepository.EditAllowAndDenyIps(ctx, model.AllowAndDenyIp{ Id: req.Id, Ip: req.Ip, HostId: req.HostId, AllowOrDeny: req.AllowOrDeny, Uid: req.Uid, }); err != nil { return err } return nil } func (s *allowAndDenyIpService) DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error { for _, id := range req.Ids { gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId) if err != nil { return err } ip, err := s.GetAllowAndDenyIp(ctx, int64(id)) if err != nil { return err } color := "black" if ip.AllowOrDeny == 1 { color = "white" } for _, v := range gatewayGroupIps { go s.wafformatter.PublishIpWhitelistTask([]string{ip.Ip}, "del",v,color) } if err := s.allowAndDenyIpRepository.DeleteAllowAndDenyIps(ctx, int64(id)); err != nil { return err } } return nil }