Kaynağa Gözat

refactor(service): 重构 AllowAndDenyIpService 和 TcpforwardingService

- 更新 AllowAndDenyIpService 依赖,将 GateWayGroupIpService 替换为 GatewayipRepository
- 新增 Gatewayip
fusu 3 hafta önce
ebeveyn
işleme
9c8b791c10

+ 6 - 0
internal/repository/gatewayip.go

@@ -19,6 +19,7 @@ type GatewayipRepository interface {
 	DeleteGatewayipByHostId(ctx context.Context, hostId int64) error
 	GetIpWhereHostIdNull(ctx context.Context,req v1.GlobalLimitRequireResponse) error
 	CleanIPByHostId(ctx context.Context, hostId []int64) error
+	GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64) ([]string, error)
 }
 
 func NewGatewayipRepository(
@@ -122,4 +123,9 @@ func (r *gatewayipRepository) GetIpWhereHostIdNull(ctx context.Context,req v1.Gl
 
 func (r *gatewayipRepository) CleanIPByHostId(ctx context.Context, hostId []int64) error {
 	return r.DB(ctx).Model(&model.Gatewayip{}).Where("host_id IN ?", hostId).Update("host_id", 0).Error
+}
+
+func (r *gatewayipRepository) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64) ([]string, error) {
+	var req []string
+	return req, r.DB(ctx).Where("host_id = ?", hostId).Pluck("ip", &req).Error
 }

+ 8 - 6
internal/service/allowanddenyip.go

@@ -18,23 +18,23 @@ type AllowAndDenyIpService interface {
 func NewAllowAndDenyIpService(
     service *Service,
     allowAndDenyIpRepository repository.AllowAndDenyIpRepository,
-	gatewayGroupIp GateWayGroupIpService,
 	wafformatter WafFormatterService,
+	gatewayIpRep repository.GatewayipRepository,
 
 ) AllowAndDenyIpService {
 	return &allowAndDenyIpService{
 		Service:        service,
 		allowAndDenyIpRepository: allowAndDenyIpRepository,
-		gatewayGroupIp: gatewayGroupIp,
 		wafformatter : wafformatter,
+		gatewayIpRep : gatewayIpRep,
 	}
 }
 
 type allowAndDenyIpService struct {
 	*Service
 	allowAndDenyIpRepository repository.AllowAndDenyIpRepository
-	gatewayGroupIp GateWayGroupIpService
 	wafformatter WafFormatterService
+	gatewayIpRep repository.GatewayipRepository
 }
 
 func (s *allowAndDenyIpService) GetAllowAndDenyIp(ctx context.Context, id int64) (*model.AllowAndDenyIp, error) {
@@ -61,7 +61,7 @@ func (s *allowAndDenyIpService) AddAllowAndDenyIps(ctx context.Context, req v1.A
 	}
 
 
-	gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
+	gatewayGroupIps, err := s.gatewayIpRep.GetGatewayipOnlyIpByHostIdAll(ctx, int64(req.HostId))
 	if err != nil {
 		return err
 	}
@@ -70,6 +70,8 @@ func (s *allowAndDenyIpService) AddAllowAndDenyIps(ctx context.Context, req v1.A
 	if req.AllowOrDeny == 1 {
 		color = "white"
 	}
+
+
 	for _, v := range gatewayGroupIps {
 		go s.wafformatter.PublishIpWhitelistTask([]string{req.Ip}, "add",v,color)
 	}
@@ -92,7 +94,7 @@ func (s *allowAndDenyIpService) EditAllowAndDenyIps(ctx context.Context, req v1.
 	}
 
 
-	gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
+	gatewayGroupIps, err := s.gatewayIpRep.GetGatewayipOnlyIpByHostIdAll(ctx, int64(req.HostId))
 	if err != nil {
 		return err
 	}
@@ -131,7 +133,7 @@ func (s *allowAndDenyIpService) EditAllowAndDenyIps(ctx context.Context, req v1.
 func (s *allowAndDenyIpService) DeleteAllowAndDenyIps(ctx context.Context, req v1.DelAllowAndDenyIpRequest) error {
 
 	for _, id := range req.Ids {
-		gatewayGroupIps, err := s.gatewayGroupIp.GetGateWayGroupIpByHostId(ctx, req.HostId)
+		gatewayGroupIps, err := s.gatewayIpRep.GetGatewayipOnlyIpByHostIdAll(ctx, int64(req.HostId))
 		if err != nil {
 			return err
 		}

+ 15 - 1
internal/service/tcpforwarding.go

@@ -32,6 +32,7 @@ func NewTcpforwardingService(
 	wafformatter WafFormatterService,
 	cdn CdnService,
 	proxy ProxyService,
+	gatewayIpRep repository.GatewayipRepository,
 ) TcpforwardingService {
 	return &tcpforwardingService{
 		Service:                 service,
@@ -44,6 +45,7 @@ func NewTcpforwardingService(
 		wafformatter:            wafformatter,
 		cdn:                     cdn,
 		proxy:                   proxy,
+		gatewayIpRep:            gatewayIpRep,
 	}
 }
 
@@ -58,6 +60,7 @@ type tcpforwardingService struct {
 	wafformatter WafFormatterService
 	cdn CdnService
 	proxy ProxyService
+	gatewayIpRep repository.GatewayipRepository
 }
 
 
@@ -133,9 +136,20 @@ func (s *tcpforwardingService) prepareWafData(ctx context.Context, req *v1.TcpFo
 	if err != nil {
 		return RequireResponse{}, v1.WebsiteSend{}, err
 	}
-	if require.GatewayGroupId == 0 || require.Uid == 0 {
+
+
+	gatewayIps, err := s.gatewayIpRep.GetGatewayipOnlyIpByHostIdAll(ctx, int64(req.HostId))
+	if err != nil {
+		return RequireResponse{}, v1.WebsiteSend{}, err
+	}
+	require.GatewayIps = gatewayIps
+
+
+	if require.GatewayIps == nil || require.Uid == 0 {
 		return RequireResponse{}, v1.WebsiteSend{}, fmt.Errorf("请先配置实例")
 	}
+
+
 	var jsonData v1.TypeJSON
 	jsonData.IsOn = true
 	for _, v := range require.GatewayIps {