瀏覽代碼

fix(waf): 修复删除主机时未传入 UID 导致的 panic

- 在删除 TCP、UDP 和 Web 转发规则时添加了 Uid 参数
- 优化了 UDP 转发规则的获取逻辑,改为按主机 ID 获取
- 在处理 IP 数据时增加了空列表的判断,提高了代码的健壮性
fusu 2 周之前
父節點
當前提交
0e2c9d2afc
共有 3 個文件被更改,包括 17 次插入8 次删除
  1. 1 1
      internal/repository/udpforwarding.go
  2. 7 3
      internal/service/udpforwarding.go
  3. 9 4
      internal/task/waf.go

+ 1 - 1
internal/repository/udpforwarding.go

@@ -19,7 +19,7 @@ type UdpForWardingRepository interface {
 	DeleteUdpForwarding(ctx context.Context, id int64) error
 	GetUdpForwardingWafUdpIdById(ctx context.Context, id int) (int, error)
 	GetUdpForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
-	GetUdpForwardingWafUdpAllIds(ctx context.Context, udpId int) ([]int, error)
+	GetUdpForwardingWafUdpAllIds(ctx context.Context, hostId int) ([]int, error)
 	AddUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) (primitive.ObjectID, error)
 	EditUdpForwardingIps(ctx context.Context, req model.UdpForwardingRule) error
 	GetUdpForwardingIpsByID(ctx context.Context, udpId int) (*model.UdpForwardingRule, error)

+ 7 - 3
internal/service/udpforwarding.go

@@ -401,12 +401,16 @@ func (s *udpForWardingService) DeleteUdpForwarding(ctx context.Context, req v1.D
 			return err
 		}
 		var ips []string
-		ips, err = s.wafformatter.WashDeleteWafIp(ctx, ipData.BackendList)
-		if err != nil {
-			return err
+
+		if len(ipData.BackendList) > 0 {
+			ips, err = s.wafformatter.WashDeleteWafIp(ctx, ipData.BackendList)
+			if err != nil {
+				return err
+			}
 		}
 
 
+
 		if len(ips) > 0 {
 			ipsToDelist, err := s.wafformatter.WashDelIps(ctx, ips)
 			if err != nil {

+ 9 - 4
internal/task/waf.go

@@ -141,7 +141,7 @@ func (t *wafTask) setCdnWebsitesState(ctx context.Context, ids []int, enable boo
 			defer wg.Done()
 			// cdn.EditWebIsOn 的第二个参数 isBan, false=启用, true=禁用
 			// 所以 enable=true 对应 isBan=false
-			if err := t.cdn.EditWebIsOn(ctx, int64(id), !enable); err != nil {
+			if err := t.cdn.EditWebIsOn(ctx, int64(id), enable); err != nil {
 				errChan <- err
 			}
 		}(id)
@@ -521,29 +521,33 @@ func (t *wafTask) executeSinglePlanCleanup(ctx context.Context, limit model.Glob
 	if err != nil {
 		allErrors = multierror.Append(allErrors, err)
 	} else if len(tcpIds) > 0 {
-		if err := t.tcp.DeleteTcpForwarding(ctx, v1.DeleteTcpForwardingRequest{Ids: tcpIds, HostId: limit.HostId}); err != nil {
+		if err := t.tcp.DeleteTcpForwarding(ctx, v1.DeleteTcpForwardingRequest{Ids: tcpIds, HostId: limit.HostId,Uid: limit.Uid}); err != nil {
 			allErrors = multierror.Append(allErrors, err)
 		}
 	}
 
+
+
 	udpIds, err := t.udpForWardingRep.GetUdpForwardingWafUdpAllIds(ctx, limit.HostId)
 	if err != nil {
 		allErrors = multierror.Append(allErrors, err)
 	} else if len(udpIds) > 0 {
-		if err := t.udp.DeleteUdpForwarding(ctx, v1.DeleteUdpForwardingRequest{Ids: udpIds, HostId: limit.HostId}); err != nil {
+		if err := t.udp.DeleteUdpForwarding(ctx, v1.DeleteUdpForwardingRequest{Ids: udpIds, HostId: limit.HostId,Uid: limit.Uid}); err != nil {
 			allErrors = multierror.Append(allErrors, err)
 		}
 	}
 
+
 	webIds, err := t.webForWardingRep.GetWebForwardingWafWebAllIds(ctx, limit.HostId)
 	if err != nil {
 		allErrors = multierror.Append(allErrors, err)
 	} else if len(webIds) > 0 {
-		if err := t.web.DeleteWebForwarding(ctx, v1.DeleteWebForwardingRequest{Ids: webIds, HostId: limit.HostId}); err != nil {
+		if err := t.web.DeleteWebForwarding(ctx, v1.DeleteWebForwardingRequest{Ids: webIds, HostId: limit.HostId,Uid: limit.Uid}); err != nil {
 			allErrors = multierror.Append(allErrors, err)
 		}
 	}
 
+
 	// 只有在上述所有步骤都没有出错的情况下,才执行最终的数据库更新和Redis标记
 	if allErrors.ErrorOrNil() == nil {
 		err := t.gatewayIpRep.CleanIPByHostId(ctx, []int64{hostId})
@@ -557,6 +561,7 @@ func (t *wafTask) executeSinglePlanCleanup(ctx context.Context, limit model.Glob
 		}
 	}
 
+
 	return allErrors.ErrorOrNil()
 }