Forráskód Böngészése

feat(globalLimit): 实现删除全局限制功能

- 新增 GetIpCountListId 方法用于获取 IP计数列表 ID
- 在 cdn 服务中添加删除用户套餐和网站分组的方法
- 修改 gatewaygroup 仓库,移除 GetGatewayGroupByRuleId 方法
- 更新 globalLimit 服务,实现删除全局限制时清理相关资源
- 调整 wire_gen 以适应新的服务和仓库变化
fusu 1 hónapja
szülő
commit
ff7a1764c3

+ 4 - 4
cmd/server/wire/wire_gen.go

@@ -91,16 +91,16 @@ func NewWire(viperViper *viper.Viper, logger *log.Logger) (*app.App, func(), err
 	udpLimitService := service.NewUdpLimitService(serviceService, udpLimitRepository, requiredService, crawlerService, parserService, hostService)
 	udpLimitHandler := handler.NewUdpLimitHandler(handlerHandler, udpLimitService)
 	gatewayGroupService := service.NewGatewayGroupService(serviceService, gatewayGroupRepository, requiredService, parserService, requestService)
-	globalLimitService := service.NewGlobalLimitService(serviceService, globalLimitRepository, duedateService, crawlerService, viperViper, requiredService, parserService, hostService, tcpLimitService, udpLimitService, webLimitService, gatewayGroupService, hostRepository, gatewayGroupRepository, cdnService, cdnRepository)
+	allowAndDenyIpRepository := repository.NewAllowAndDenyIpRepository(repositoryRepository)
+	gateWayGroupIpService := service.NewGateWayGroupIpService(serviceService, gateWayGroupIpRepository, gatewayGroupRepository, requestService)
+	allowAndDenyIpService := service.NewAllowAndDenyIpService(serviceService, allowAndDenyIpRepository, gateWayGroupIpService, wafFormatterService)
+	globalLimitService := service.NewGlobalLimitService(serviceService, globalLimitRepository, duedateService, crawlerService, viperViper, requiredService, parserService, hostService, gatewayGroupService, hostRepository, gatewayGroupRepository, cdnService, cdnRepository, tcpforwardingRepository, udpForWardingRepository, webForwardingRepository, allowAndDenyIpService, allowAndDenyIpRepository, tcpforwardingService, udpForWardingService, webForwardingService)
 	globalLimitHandler := handler.NewGlobalLimitHandler(handlerHandler, globalLimitService)
 	adminRepository := repository.NewAdminRepository(repositoryRepository)
 	adminService := service.NewAdminService(serviceService, adminRepository)
 	adminHandler := handler.NewAdminHandler(handlerHandler, adminService)
 	gatewayGroupHandler := handler.NewGatewayGroupHandler(handlerHandler, gatewayGroupService)
-	gateWayGroupIpService := service.NewGateWayGroupIpService(serviceService, gateWayGroupIpRepository, gatewayGroupRepository, requestService)
 	gateWayGroupIpHandler := handler.NewGateWayGroupIpHandler(handlerHandler, gateWayGroupIpService)
-	allowAndDenyIpRepository := repository.NewAllowAndDenyIpRepository(repositoryRepository)
-	allowAndDenyIpService := service.NewAllowAndDenyIpService(serviceService, allowAndDenyIpRepository, gateWayGroupIpService, wafFormatterService)
 	allowAndDenyIpHandler := handler.NewAllowAndDenyIpHandler(handlerHandler, allowAndDenyIpService)
 	ccRepository := repository.NewCcRepository(repositoryRepository)
 	ccService := service.NewCcService(serviceService, ccRepository, webForwardingRepository)

+ 9 - 0
internal/repository/allowanddenyip.go

@@ -13,6 +13,7 @@ type AllowAndDenyIpRepository interface {
 	DeleteAllowAndDenyIps(ctx context.Context, id int64) error
 	GetAllowAndDenyIpsAllByHostId(ctx context.Context, hostId int64) ([]*model.AllowAndDenyIp, error)
 	GetIpCount(ctx context.Context,hostId int64,ip string) (int64, error)
+	GetIpCountListId(ctx context.Context,hostId int64) ([]int, error)
 }
 
 func NewAllowAndDenyIpRepository(
@@ -73,4 +74,12 @@ func (r *allowAndDenyIpRepository) GetIpCount(ctx context.Context,hostId int64,i
 		return 0, err
 	}
 	return count, nil
+}
+
+func (r *allowAndDenyIpRepository) GetIpCountListId(ctx context.Context,hostId int64) ([]int, error) {
+	var res []int
+	if err := r.DB(ctx).Model(&model.AllowAndDenyIp{}).Where("host_id = ?", hostId).Pluck("id", &res).Error; err != nil {
+		return nil, err
+	}
+	return res, nil
 }

+ 0 - 9
internal/repository/gatewaygroup.go

@@ -18,7 +18,6 @@ type GatewayGroupRepository interface {
 	DeleteGatewayGroup(ctx context.Context, id int) error
 	GetGatewayGroupWhereHostIdNull(ctx context.Context,operator int, count int) (int, error)
 	GetGatewayGroupByHostId(ctx context.Context, hostId int64) (*model.GatewayGroup, error)
-	GetGatewayGroupByRuleId(ctx context.Context, ruleId int64) (*model.GatewayGroup, error)
 	GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error)
 	EditGatewayGroupById(ctx context.Context, req *model.GatewayGroup) error
 }
@@ -91,14 +90,6 @@ func (r *gatewayGroupRepository) GetGatewayGroupByHostId(ctx context.Context, ho
 	return &res, nil
 }
 
-func (r *gatewayGroupRepository) GetGatewayGroupByRuleId(ctx context.Context, ruleId int64) (*model.GatewayGroup, error) {
-	res := model.GatewayGroup{}
-	if err := r.DB(ctx).Where("rule_id = ?", ruleId).Find(&res).Error; err != nil {
-		return nil, err
-	}
-	return &res, nil
-
-}
 
 func (r *gatewayGroupRepository) GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error) {
 	var res []model.GatewayGroup

+ 44 - 0
internal/service/cdn.go

@@ -54,6 +54,10 @@ type CdnService interface {
 	EditHTTPWebWebsocket(ctx context.Context,websocketId int64,websocketJSON []byte) error
 	// 启用/禁用网站
 	EditWebIsOn(ctx context.Context,serverId int64,isOn bool) error
+	// 删除已购套餐
+	DelUserPlan(ctx context.Context,planId int64) error
+	// 删除网站分组
+	DelServerGroup(ctx context.Context,serverId int64) error
 }
 
 func NewCdnService(
@@ -951,4 +955,44 @@ func (s *cdnService) EditWebIsOn(ctx context.Context,serverId int64,isOn bool) e
 		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
 	}
 	return nil
+}
+
+// 删除已购套餐
+func (s *cdnService) DelUserPlan(ctx context.Context,planId int64) error {
+	formData := map[string]interface{}{
+		"userPlanId": planId,
+	}
+	apiUrl := s.Url + "UserPlanService/deleteUserPlan"
+	resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
+	if err != nil {
+		return err
+	}
+	var res v1.GeneralResponse[any]
+	if err := json.Unmarshal(resBody, &res); err != nil {
+		return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
+	}
+	if res.Code != 200 {
+		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
+	}
+	return nil
+}
+
+// 删除网站分组
+func (s *cdnService) DelServerGroup(ctx context.Context,serverId int64) error {
+	formData := map[string]interface{}{
+		"serverId": serverId,
+	}
+	apiUrl := s.Url + "ServerService/deleteServer"
+	resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
+	if err != nil {
+		return err
+	}
+	var res v1.GeneralResponse[any]
+	if err := json.Unmarshal(resBody, &res); err != nil {
+		return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
+	}
+	if res.Code != 200 {
+		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
+	}
+	return nil
 }

+ 118 - 9
internal/service/globallimit.go

@@ -33,14 +33,19 @@ func NewGlobalLimitService(
 	required RequiredService,
 	parser ParserService,
 	host HostService,
-	tcpLimit TcpLimitService,
-	udpLimit UdpLimitService,
-	webLimit WebLimitService,
 	gateWayGroup GatewayGroupService,
 	hostRep repository.HostRepository,
 	gateWayGroupRep repository.GatewayGroupRepository,
 	cdnService CdnService,
 	cdnRep repository.CdnRepository,
+	tcpforwardingRep repository.TcpforwardingRepository,
+	udpForWardingRep repository.UdpForWardingRepository,
+	webForWardingRep repository.WebForwardingRepository,
+	allowAndDeny AllowAndDenyIpService,
+	allowAndDenyRep repository.AllowAndDenyIpRepository,
+	tcpforwarding TcpforwardingService,
+	udpForWarding UdpForWardingService,
+	webForWarding WebForwardingService,
 ) GlobalLimitService {
 	return &globalLimitService{
 		Service:               service,
@@ -51,14 +56,19 @@ func NewGlobalLimitService(
 		required:              required,
 		parser:                parser,
 		host:                  host,
-		tcpLimit:              tcpLimit,
-		udpLimit:              udpLimit,
-		webLimit:              webLimit,
 		gateWayGroup:          gateWayGroup,
 		hostRep:                hostRep,
 		gateWayGroupRep:       gateWayGroupRep,
 		cdnService:            cdnService,
 		cdnRep:                cdnRep,
+		tcpforwardingRep:      tcpforwardingRep,
+		udpForWardingRep:      udpForWardingRep,
+		webForWardingRep:      webForWardingRep,
+		allowAndDeny:          allowAndDeny,
+		allowAndDenyRep:       allowAndDenyRep,
+		tcpforwarding:         tcpforwarding,
+		udpForWarding:         udpForWarding,
+		webForWarding:         webForWarding,
 	}
 }
 
@@ -71,14 +81,19 @@ type globalLimitService struct {
 	required              RequiredService
 	parser                ParserService
 	host                  HostService
-	tcpLimit              TcpLimitService
-	udpLimit              UdpLimitService
-	webLimit              WebLimitService
 	gateWayGroup          GatewayGroupService
 	hostRep               repository.HostRepository
 	gateWayGroupRep       repository.GatewayGroupRepository
 	cdnService            CdnService
 	cdnRep                repository.CdnRepository
+	tcpforwardingRep      repository.TcpforwardingRepository
+	udpForWardingRep      repository.UdpForWardingRepository
+	webForWardingRep      repository.WebForwardingRepository
+	allowAndDeny          AllowAndDenyIpService
+	allowAndDenyRep       repository.AllowAndDenyIpRepository
+	tcpforwarding         TcpforwardingService
+	udpForWarding         UdpForWardingService
+	webForWarding         WebForwardingService
 }
 
 func (s *globalLimitService) GetCdnUserId(ctx context.Context, uid int64) (int64, error) {
@@ -380,8 +395,102 @@ func (s *globalLimitService) EditGlobalLimit(ctx context.Context, req v1.GlobalL
 
 
 func (s *globalLimitService) DeleteGlobalLimit(ctx context.Context, req v1.GlobalLimitRequest) error {
+	oldData, err := s.globalLimitRepository.GetGlobalLimitByHostId(ctx, int64(req.HostId))
+	if err != nil {
+		return err
+	}
+
+	tcpIds, err := s.tcpforwardingRep.GetTcpForwardingAllIdsByID(ctx,req.HostId)
+	if err != nil {
+		return err
+	}
+	udpIds, err := s.udpForWardingRep.GetUdpForwardingWafUdpAllIds(ctx,req.HostId)
+	if err != nil {
+		return err
+	}
+	webIds, err := s.webForWardingRep.GetWebForwardingWafWebAllIds(ctx,req.HostId)
+	if err != nil {
+		return err
+	}
+
+	BwIds, err := s.allowAndDenyRep.GetIpCountListId(ctx, int64(req.HostId))
+	if err != nil {
+		return err
+	}
+
+	// 删除网站
+	g, gCtx := errgroup.WithContext(ctx)
+	g.Go(func() error {
+		e := s.tcpforwarding.DeleteTcpForwarding(ctx,v1.DeleteTcpForwardingRequest{
+			Ids: tcpIds,
+			Uid: req.Uid,
+			HostId: req.HostId,
+		})
+		if e != nil {
+			return fmt.Errorf("删除TCP转发失败: %w", e)
+		}
+		return nil
+	})
+
+	g.Go(func() error {
+		e := s.udpForWarding.DeleteUdpForwarding(ctx,udpIds)
+		if e != nil {
+			return fmt.Errorf("删除UDP转发失败: %w", e)
+		}
+		return nil
+	})
+
+	g.Go(func() error {
+		e := s.webForWarding.DeleteWebForwarding(ctx,webIds)
+		if e != nil {
+			return fmt.Errorf("删除WEB转发失败: %w", e)
+		}
+		return nil
+	})
+
+	// 删除套餐
+	g.Go(func() error {
+		e := s.cdnService.DelUserPlan(gCtx, int64(oldData.RuleId))
+		if e != nil {
+			return fmt.Errorf("删除套餐失败: %w", e)
+		}
+		return nil
+	})
+	// 删除网站分组
+	g.Go(func() error {
+		e := s.cdnService.DelServerGroup(gCtx, int64(oldData.GroupId))
+		if e != nil {
+			return fmt.Errorf("删除网站分组失败: %w", e)
+		}
+		return nil
+	})
+
+	if err = g.Wait(); err != nil {
+		return err
+	}
+
+
+
 	if err := s.globalLimitRepository.DeleteGlobalLimitByHostId(ctx, int64(req.HostId)); err != nil {
 		return err
 	}
+	if err := s.gateWayGroupRep.EditGatewayGroup(ctx,&model.GatewayGroup{
+		Id: oldData.GatewayGroupId,
+		HostId: 0,
+	}); err != nil {
+		return err
+	}
+
+	// 删除黑白名单
+	err = s.allowAndDeny.DeleteAllowAndDenyIps(ctx, v1.DelAllowAndDenyIpRequest{
+		HostId: req.HostId,
+		Ids:    BwIds,
+		Uid:    req.Uid,
+	})
+	if err != nil {
+		return err
+	}
+
+
 	return nil
 }