瀏覽代碼

feat(forwarding): 批量删除转发规则

- 修改 TCP、UDP 和 Web 转发的删除接口,支持批量删除
- 新增删除转发规则对应的 IP黑名单功能
- 优化删除逻辑,处理多个规则的删除并确保相关数据一致
fusu 1 月之前
父節點
當前提交
796caf7590

+ 3 - 1
api/v1/tcpForwarding.go

@@ -41,7 +41,9 @@ type TcpForwardingDataRequest struct {
 }
 }
 
 
 type DeleteTcpForwardingRequest struct {
 type DeleteTcpForwardingRequest struct {
-	Id   int `form:"id" json:"id" binding:"required"`
+	Ids   []int `form:"ids" json:"ids" binding:"required"`
+	Uid   int   `form:"uid" json:"uid" binding:"required"`
+	HostId   int   `form:"host_id" json:"host_id" binding:"required"`
 }
 }
 
 
 type TcpForwardingRequest struct {
 type TcpForwardingRequest struct {

+ 1 - 1
api/v1/udpForwarding.go

@@ -47,7 +47,7 @@ type UdpForwardingDataRequest struct {
 }
 }
 
 
 type DeleteUdpForwardingRequest struct {
 type DeleteUdpForwardingRequest struct {
-	Id   int `form:"id" json:"id" binding:"required"`
+	Ids   []int `form:"ids" json:"ids" binding:"required"`
 }
 }
 
 
 type UdpForwardingRequest struct {
 type UdpForwardingRequest struct {

+ 1 - 1
api/v1/webForwarding.go

@@ -59,7 +59,7 @@ type WebForwardingDataRequest struct {
 }
 }
 
 
 type DeleteWebForwardingRequest struct {
 type DeleteWebForwardingRequest struct {
-	Id   int `form:"id" json:"id" binding:"required"`
+	Ids   []int `form:"ids" json:"ids" binding:"required"`
 }
 }
 
 
 type WebForwardingRequest struct {
 type WebForwardingRequest struct {

+ 1 - 1
internal/handler/tcpforwarding.go

@@ -78,7 +78,7 @@ func (h *TcpforwardingHandler) DeleteTcpForwarding(ctx *gin.Context) {
 		return
 		return
 	}
 	}
 	defaults.SetDefaults(req)
 	defaults.SetDefaults(req)
-	 err := h.tcpforwardingService.DeleteTcpForwarding(ctx, req.Id)
+	 err := h.tcpforwardingService.DeleteTcpForwarding(ctx, *req)
 	if err != nil {
 	if err != nil {
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		return
 		return

+ 1 - 1
internal/handler/udpforwarding.go

@@ -78,7 +78,7 @@ func (h *UdpForWardingHandler) DeleteUdpForWarding(ctx *gin.Context) {
 		return
 		return
 	}
 	}
 	defaults.SetDefaults(req)
 	defaults.SetDefaults(req)
-	 err := h.udpForWardingService.DeleteUdpForwarding(ctx, req.Id)
+	 err := h.udpForWardingService.DeleteUdpForwarding(ctx, req.Ids)
 	if err != nil {
 	if err != nil {
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		return
 		return

+ 1 - 1
internal/handler/webforwarding.go

@@ -78,7 +78,7 @@ func (h *WebForwardingHandler) DeleteWebForwarding(ctx *gin.Context) {
 		return
 		return
 	}
 	}
 	defaults.SetDefaults(req)
 	defaults.SetDefaults(req)
-	 err := h.webForwardingService.DeleteWebForwarding(ctx, req.Id)
+	 err := h.webForwardingService.DeleteWebForwarding(ctx, req.Ids)
 	if err != nil {
 	if err != nil {
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		v1.HandleError(ctx, http.StatusInternalServerError, err, err.Error())
 		return
 		return

+ 18 - 2
internal/repository/tcpforwarding.go

@@ -20,7 +20,8 @@ type TcpforwardingRepository interface {
 	GetTcpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
 	GetTcpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
 	AddTcpforwardingIps(ctx context.Context,req model.TcpForwardingRule) (primitive.ObjectID, error)
 	AddTcpforwardingIps(ctx context.Context,req model.TcpForwardingRule) (primitive.ObjectID, error)
 	EditTcpforwardingIps(ctx context.Context, req model.TcpForwardingRule) error
 	EditTcpforwardingIps(ctx context.Context, req model.TcpForwardingRule) error
-	GetTcpForwardingByID(ctx context.Context, tcpId int) (*model.TcpForwardingRule, error)
+	GetTcpForwardingIpsByID(ctx context.Context, tcpId int) (*model.TcpForwardingRule, error)
+	DeleteTcpForwardingIpsById(ctx context.Context, tcpId int) error
 }
 }
 
 
 func NewTcpforwardingRepository(
 func NewTcpforwardingRepository(
@@ -149,7 +150,7 @@ func (r *tcpforwardingRepository) EditTcpforwardingIps(ctx context.Context, req
 
 
 }
 }
 
 
-func (r *tcpforwardingRepository) GetTcpForwardingByID(ctx context.Context, tcpId int) (*model.TcpForwardingRule, error) {
+func (r *tcpforwardingRepository) GetTcpForwardingIpsByID(ctx context.Context, tcpId int) (*model.TcpForwardingRule, error) {
 
 
 	collection := r.mongoDB.Collection("tcp_forwarding_rules")
 	collection := r.mongoDB.Collection("tcp_forwarding_rules")
 
 
@@ -165,4 +166,19 @@ func (r *tcpforwardingRepository) GetTcpForwardingByID(ctx context.Context, tcpI
 	}
 	}
 
 
 	return &res, nil
 	return &res, nil
+}
+
+func (r *tcpforwardingRepository) DeleteTcpForwardingIpsById(ctx context.Context, tcpId int) error {
+
+	collection := r.mongoDB.Collection("tcp_forwarding_rules")
+
+	 err := collection.Remove(ctx, bson.M{"tcp_id": tcpId})
+
+	if err != nil {
+		if errors.Is(err, mongo.ErrNoDocuments) {
+			return fmt.Errorf("记录不存在")
+		}
+		return fmt.Errorf("删除MongoDB文档失败: %w", err)
+	}
+	return nil
 }
 }

+ 19 - 2
internal/repository/udpforwarding.go

@@ -20,7 +20,8 @@ type UdpForWardingRepository interface {
 	GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
 	GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
 	AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error)
 	AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error)
 	EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error
 	EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error
-	GetTcpForwardingByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error)
+	GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error)
+	DeleteUdpForwardingIpsById(ctx context.Context, udpId int) error
 }
 }
 
 
 func NewUdpForWardingRepository(
 func NewUdpForWardingRepository(
@@ -146,7 +147,7 @@ func (r *udpForWardingRepository) EditUdpForwardingIps(ctx context.Context, req
 	return nil
 	return nil
 }
 }
 
 
-func (r *udpForWardingRepository) GetTcpForwardingByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error) {
+func (r *udpForWardingRepository) GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error) {
 
 
 	collection := r.mongoDB.Collection("udp_forwarding_rules")
 	collection := r.mongoDB.Collection("udp_forwarding_rules")
 	var result model.UdpForwardingRule
 	var result model.UdpForwardingRule
@@ -159,4 +160,20 @@ func (r *udpForWardingRepository) GetTcpForwardingByID(ctx context.Context, udpI
 	}
 	}
 	return &result, nil
 	return &result, nil
 
 
+}
+
+func (r *udpForWardingRepository) DeleteUdpForwardingIpsById(ctx context.Context, udpId int) error {
+
+	collection := r.mongoDB.Collection("udp_forwarding_rules")
+
+	 err := collection.Remove(ctx, bson.M{"host_id": udpId})
+
+	if err != nil {
+		if errors.Is(err, mongo.ErrNoDocuments) {
+			return fmt.Errorf("记录不存在")
+		}
+		return fmt.Errorf("删除MongoDB文档失败: %w", err)
+	}
+	return nil
+
 }
 }

+ 17 - 2
internal/repository/webforwarding.go

@@ -22,7 +22,8 @@ type WebForwardingRepository interface {
 	GetWebForwardingDomainCountByHostId(ctx context.Context, hostId int) (int64, []string, error)
 	GetWebForwardingDomainCountByHostId(ctx context.Context, hostId int) (int64, []string, error)
 	AddWebForwardingIps(ctx context.Context, req model.WebForwardingRule) (primitive.ObjectID, error)
 	AddWebForwardingIps(ctx context.Context, req model.WebForwardingRule) (primitive.ObjectID, error)
 	EditWebForwardingIps(ctx context.Context, req model.WebForwardingRule) error
 	EditWebForwardingIps(ctx context.Context, req model.WebForwardingRule) error
-	GetWebForwardingByID(ctx context.Context, webId int) (*model.WebForwardingRule, error)
+	GetWebForwardingIpsByID(ctx context.Context, webId int) (*model.WebForwardingRule, error)
+	DeleteWebForwardingIpsById(ctx context.Context, webId int) error
 }
 }
 
 
 func NewWebForwardingRepository(
 func NewWebForwardingRepository(
@@ -162,7 +163,7 @@ func (r *webForwardingRepository) EditWebForwardingIps(ctx context.Context, req
 	return nil
 	return nil
 }
 }
 
 
-func (r *webForwardingRepository) GetWebForwardingByID(ctx context.Context, webId int) (*model.WebForwardingRule, error) {
+func (r *webForwardingRepository) GetWebForwardingIpsByID(ctx context.Context, webId int) (*model.WebForwardingRule, error) {
 
 
 	// 获取集合
 	// 获取集合
 	collection := r.mongoDB.Collection("web_forwarding_rules")
 	collection := r.mongoDB.Collection("web_forwarding_rules")
@@ -186,3 +187,17 @@ func (r *webForwardingRepository) GetWebForwardingByID(ctx context.Context, webI
 	return &rule, nil
 	return &rule, nil
 }
 }
 
 
+func (r *webForwardingRepository) DeleteWebForwardingIpsById(ctx context.Context, webId int) error {
+
+	collection := r.mongoDB.Collection("web_forwarding_rules")
+
+	 err := collection.Remove(ctx, bson.M{"web_id": webId})
+
+	if err != nil {
+		if errors.Is(err, mongo.ErrNoDocuments) {
+			return fmt.Errorf("记录不存在")
+		}
+		return fmt.Errorf("删除MongoDB文档失败: %w", err)
+	}
+	return nil
+}

+ 19 - 14
internal/service/tcpforwarding.go

@@ -15,7 +15,7 @@ type TcpforwardingService interface {
 	GetTcpforwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.TcpForwardingDataRequest, error)
 	GetTcpforwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.TcpForwardingDataRequest, error)
 	AddTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest)  error
 	AddTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest)  error
 	EditTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest)  error
 	EditTcpForwarding(ctx context.Context, req *v1.TcpForwardingRequest)  error
-	DeleteTcpForwarding(ctx context.Context, Id int) error
+	DeleteTcpForwarding(ctx context.Context, req v1.DeleteTcpForwardingRequest) error
 }
 }
 
 
 func NewTcpforwardingService(
 func NewTcpforwardingService(
@@ -69,7 +69,7 @@ func (s *tcpforwardingService) GetTcpforwarding(ctx context.Context, req v1.GetF
 	})
 	})
 
 
 	g.Go(func() error {
 	g.Go(func() error {
-		res, e := s.tcpforwardingRepository.GetTcpForwardingByID(gCtx, req.Id)
+		res, e := s.tcpforwardingRepository.GetTcpForwardingIpsByID(gCtx, req.Id)
 		if e != nil {
 		if e != nil {
 			return fmt.Errorf("GetTcpforwardingIps failed: %w", e)
 			return fmt.Errorf("GetTcpforwardingIps failed: %w", e)
 		}
 		}
@@ -259,19 +259,24 @@ func (s *tcpforwardingService) EditTcpForwarding(ctx context.Context, req *v1.Tc
 	return  nil
 	return  nil
 }
 }
 
 
-func (s *tcpforwardingService) DeleteTcpForwarding(ctx context.Context, Id int)  error {
-	wafTcpId, err := s.tcpforwardingRepository.GetTcpforwardingWafTcpIdById(ctx, Id)
-	if err != nil {
-		return  err
-	}
-	_, err = s.crawler.DeleteRule(ctx, wafTcpId, "admin/delete/waf_tcp?page=1&__pageSize=10&__sort=waf_tcp_id&__sort_type=desc")
-	if err != nil {
-		return err
-	}
+func (s *tcpforwardingService) DeleteTcpForwarding(ctx context.Context, req v1.DeleteTcpForwardingRequest)  error {
+	for _, Id := range req.Ids {
+		wafTcpId, err := s.tcpforwardingRepository.GetTcpforwardingWafTcpIdById(ctx, Id)
+		if err != nil {
+			return  err
+		}
+		_, err = s.crawler.DeleteRule(ctx, wafTcpId, "admin/delete/waf_tcp?page=1&__pageSize=1000000&__sort=waf_tcp_id&__sort_type=desc")
+		if err != nil {
+			return err
+		}
 
 
-	if err = s.tcpforwardingRepository.DeleteTcpforwarding(ctx, int64(Id)); err != nil {
-		return  err
-	}
+		if err = s.tcpforwardingRepository.DeleteTcpforwarding(ctx, int64(Id)); err != nil {
+			return  err
+		}
 
 
+		if err = s.tcpforwardingRepository.DeleteTcpForwardingIpsById(ctx, Id); err != nil {
+			return  err
+		}
+	}
 	return  nil
 	return  nil
 }
 }

+ 20 - 13
internal/service/udpforwarding.go

@@ -15,7 +15,7 @@ type UdpForWardingService interface {
 	GetUdpForWarding(ctx context.Context,req v1.GetForwardingRequest) (v1.UdpForwardingDataRequest, error)
 	GetUdpForWarding(ctx context.Context,req v1.GetForwardingRequest) (v1.UdpForwardingDataRequest, error)
 	AddUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
 	AddUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
 	EditUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
 	EditUdpForwarding(ctx context.Context, req *v1.UdpForwardingRequest) error
-	DeleteUdpForwarding(ctx context.Context, Id int) error
+	DeleteUdpForwarding(ctx context.Context, Ids []int) error
 }
 }
 
 
 func NewUdpForWardingService(
 func NewUdpForWardingService(
@@ -75,7 +75,7 @@ func (s *udpForWardingService) GetUdpForWarding(ctx context.Context,req v1.GetFo
 		return nil
 		return nil
 	})
 	})
 	g.Go(func() error {
 	g.Go(func() error {
-		res, e := s.udpForWardingRepository.GetTcpForwardingByID(gCtx, req.Id)
+		res, e := s.udpForWardingRepository.GetUdpForwardingIpsByID(gCtx, req.Id)
 		if e != nil {
 		if e != nil {
 			return fmt.Errorf("GetUdpForWardingByID failed: %w", e)
 			return fmt.Errorf("GetUdpForWardingByID failed: %w", e)
 		}
 		}
@@ -277,17 +277,24 @@ func (s *udpForWardingService) EditUdpForwarding(ctx context.Context, req *v1.Ud
 	return nil
 	return nil
 }
 }
 
 
-func (s *udpForWardingService) DeleteUdpForwarding(ctx context.Context, Id int) error {
-	wafUdpId, err := s.udpForWardingRepository.GetUdpForwardingWafUdpIdById(ctx, Id)
-	if err != nil {
-		return err
-	}
-	_, err = s.crawler.DeleteRule(ctx, wafUdpId, "admin/delete/waf_udp?page=1&__pageSize=10&__sort=waf_udp_id&__sort_type=desc")
-	if err != nil {
-		return err
-	}
-	if err = s.udpForWardingRepository.DeleteUdpForwarding(ctx, int64(Id)); err != nil {
-		return err
+func (s *udpForWardingService) DeleteUdpForwarding(ctx context.Context, Ids []int) error {
+	for _, id := range Ids {
+		wafUdpId, err := s.udpForWardingRepository.GetUdpForwardingWafUdpIdById(ctx, id)
+		if err != nil {
+			return err
+		}
+		_, err = s.crawler.DeleteRule(ctx, wafUdpId, "admin/delete/waf_udp?page=1&__pageSize=10&__sort=waf_udp_id&__sort_type=desc")
+		if err != nil {
+			return err
+		}
+
+		if err = s.udpForWardingRepository.DeleteUdpForwarding(ctx, int64(id)); err != nil {
+			return err
+		}
+
+		if err = s.udpForWardingRepository.DeleteUdpForwardingIpsById(ctx, id); err != nil {
+			return err
+		}
 	}
 	}
 	return nil
 	return nil
 }
 }

+ 19 - 13
internal/service/webforwarding.go

@@ -16,7 +16,7 @@ type WebForwardingService interface {
 	GetWebForwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.WebForwardingDataRequest, error)
 	GetWebForwarding(ctx context.Context, req v1.GetForwardingRequest) (v1.WebForwardingDataRequest, error)
 	AddWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error
 	AddWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error
 	EditWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error
 	EditWebForwarding(ctx context.Context, req *v1.WebForwardingRequest) error
-	DeleteWebForwarding(ctx context.Context, Id int) error
+	DeleteWebForwarding(ctx context.Context, Ids []int) error
 }
 }
 
 
 func NewWebForwardingService(
 func NewWebForwardingService(
@@ -90,7 +90,7 @@ func (s *webForwardingService) GetWebForwarding(ctx context.Context, req v1.GetF
 	})
 	})
 
 
 	g.Go(func() error {
 	g.Go(func() error {
-		res, e := s.webForwardingRepository.GetWebForwardingByID(ctx, req.Id)
+		res, e := s.webForwardingRepository.GetWebForwardingIpsByID(ctx, req.Id)
 		if e != nil {
 		if e != nil {
 			return fmt.Errorf("GetWebForwardingByID failed: %w", e)
 			return fmt.Errorf("GetWebForwardingByID failed: %w", e)
 		}
 		}
@@ -329,17 +329,23 @@ func (s *webForwardingService) EditWebForwarding(ctx context.Context, req *v1.We
 	return nil
 	return nil
 }
 }
 
 
-func (s *webForwardingService) DeleteWebForwarding(ctx context.Context, Id int) error {
-	wafWebId, err := s.webForwardingRepository.GetWebForwardingWafWebIdById(ctx, Id)
-	if err != nil {
-		return err
-	}
-	_, err = s.crawler.DeleteRule(ctx, wafWebId, "admin/delete/waf_web?page=1&__pageSize=10&__sort=waf_web_id&__sort_type=desc")
-	if err != nil {
-		return err
-	}
-	if err = s.webForwardingRepository.DeleteWebForwarding(ctx, int64(Id)); err != nil {
-		return err
+func (s *webForwardingService) DeleteWebForwarding(ctx context.Context, Ids []int) error {
+	for _, Id := range Ids {
+		wafWebId, err := s.webForwardingRepository.GetWebForwardingWafWebIdById(ctx, Id)
+		if err != nil {
+			return err
+		}
+		_, err = s.crawler.DeleteRule(ctx, wafWebId, "admin/delete/waf_web?page=1&__pageSize=10&__sort=waf_web_id&__sort_type=desc")
+		if err != nil {
+			return err
+		}
+		if err = s.webForwardingRepository.DeleteWebForwarding(ctx, int64(Id)); err != nil {
+			return err
+		}
+		if err = s.webForwardingRepository.DeleteWebForwardingIpsById(ctx, Id); err != nil {
+			return err
+		}
 	}
 	}
+
 	return nil
 	return nil
 }
 }