From 402931862f4c28f1daf94c2c55da4e801e304fec Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:36:47 +0800 Subject: [PATCH] fix: Fix the issue where iptables description does not take effect (#10947) --- agent/app/dto/firewall.go | 9 ++-- agent/app/repo/host.go | 29 +++++++++---- agent/app/service/firewall.go | 8 ++-- agent/app/service/iptables.go | 4 +- frontend/src/api/interface/host.ts | 8 +++- .../src/views/host/firewall/advance/index.vue | 43 ++++++++++++++++--- .../host/firewall/advance/operate/index.vue | 4 +- frontend/src/views/host/firewall/ip/index.vue | 17 ++++++-- .../src/views/host/firewall/port/index.vue | 17 ++++++-- 9 files changed, 105 insertions(+), 34 deletions(-) diff --git a/agent/app/dto/firewall.go b/agent/app/dto/firewall.go index a5150b521..f926ed9d2 100644 --- a/agent/app/dto/firewall.go +++ b/agent/app/dto/firewall.go @@ -50,8 +50,11 @@ type ForwardRuleOperate struct { type UpdateFirewallDescription struct { Type string `json:"type"` - Address string `json:"address"` - Port string `json:"port"` + Chain string `json:"chain"` + SrcIP string `json:"srcIP"` + DstIP string `json:"dstIP"` + SrcPort string `json:"srcPort"` + DstPort string `json:"dstPort"` Protocol string `json:"protocol"` Strategy string `json:"strategy" validate:"required,oneof=accept drop"` @@ -96,7 +99,7 @@ type IptablesRuleOp struct { SrcPort uint `json:"srcPort"` DstIP string `json:"dstIP"` DstPort uint `json:"dstPort"` - Strategy string `json:"strategy" validate:"required,oneof=ACCEPT DROP REJECT"` + Strategy string `json:"strategy" validate:"required,oneof=accept drop reject"` Description string `json:"description"` } diff --git a/agent/app/repo/host.go b/agent/app/repo/host.go index 0d399229b..374dbcddf 100644 --- a/agent/app/repo/host.go +++ b/agent/app/repo/host.go @@ -57,16 +57,27 @@ func (h *HostRepo) SaveFirewallRecord(firewall *model.Firewall) error { return global.DB.Save(firewall).Error } var data model.Firewall - if firewall.Type == "port" { - _ = global.DB.Where("type = ? AND dst_port = ? AND protocol = ? AND src_ip = ? AND strategy = ?", "port", firewall.DstPort, firewall.Protocol, firewall.SrcIP, firewall.Strategy).First(&data) - if data.ID != 0 { - firewall.ID = data.ID - } - } else { + switch firewall.Type { + case "port": + _ = global.DB.Where("type = ? AND dst_port = ? AND protocol = ? AND src_ip = ? AND strategy = ?", "port", + firewall.DstPort, + firewall.Protocol, + firewall.SrcIP, + firewall.Strategy, + ).First(&data).Error + case "ip": _ = global.DB.Where("type = ? AND src_ip = ? AND strategy = ?", "address", firewall.SrcIP, firewall.Strategy).First(&data) - if data.ID != 0 { - firewall.ID = data.ID - } + default: + _ = global.DB.Where("type = ? AND chain = ? AND src_port = ? AND dst_port = ? AND protocol = ? AND src_ip = ? AND dst_ip = ? AND strategy = ?", + firewall.Type, + firewall.Chain, + firewall.SrcPort, + firewall.DstPort, + firewall.Protocol, + firewall.SrcIP, + firewall.DstIP, + firewall.Strategy, + ).First(&data).Error } return global.DB.Save(firewall).Error } diff --git a/agent/app/service/firewall.go b/agent/app/service/firewall.go index b6b566988..a00c44216 100644 --- a/agent/app/service/firewall.go +++ b/agent/app/service/firewall.go @@ -475,9 +475,11 @@ func (u *FirewallService) UpdateAddrRule(req dto.AddrRuleUpdate) error { func (u *FirewallService) UpdateDescription(req dto.UpdateFirewallDescription) error { firewall := model.Firewall{ Type: req.Type, - Chain: iptables.Chain1PanelBasic, - SrcIP: req.Address, - DstPort: req.Port, + Chain: req.Chain, + SrcIP: req.SrcIP, + DstIP: req.DstIP, + SrcPort: req.SrcPort, + DstPort: req.DstPort, Protocol: req.Protocol, Strategy: req.Strategy, Description: req.Description, diff --git a/agent/app/service/iptables.go b/agent/app/service/iptables.go index 0df6d62ee..56f3e952e 100644 --- a/agent/app/service/iptables.go +++ b/agent/app/service/iptables.go @@ -100,9 +100,9 @@ func (s *IptablesService) OperateRule(req dto.IptablesRuleOp) error { Chain: req.Chain, Protocol: req.Protocol, SrcIP: req.SrcIP, - SrcPort: fmt.Sprintf("%v", req.SrcPort), + SrcPort: policy.SrcPort, DstIP: req.DstIP, - DstPort: fmt.Sprintf("%v", req.DstPort), + DstPort: policy.DstPort, Strategy: req.Strategy, Description: req.Description, } diff --git a/frontend/src/api/interface/host.ts b/frontend/src/api/interface/host.ts index e35937c30..98d3285e8 100644 --- a/frontend/src/api/interface/host.ts +++ b/frontend/src/api/interface/host.ts @@ -95,8 +95,12 @@ export namespace Host { [key: string]: any; } export interface UpdateDescription { - address: string; - port: string; + type: string; + chain: string; + srcIP: string; + dstIP: string; + srcPort: string; + dstPort: string; protocol: string; strategy: string; description: string; diff --git a/frontend/src/views/host/firewall/advance/index.vue b/frontend/src/views/host/firewall/advance/index.vue index f9c2dd2ac..ec7c3fa21 100644 --- a/frontend/src/views/host/firewall/advance/index.vue +++ b/frontend/src/views/host/firewall/advance/index.vue @@ -96,11 +96,13 @@