@@ -145,7 +145,7 @@ func (s *IptablesFilterService) AddRule(req dto.IptablesFilterRuleOperate) error
145145 if (req .Action == "DROP" || req .Action == "REJECT" ) &&
146146 req .Protocol == "" && req .SourceIP == "" && req .DestIP == "" &&
147147 req .SourcePort == 0 && req .DestPort == 0 {
148- return fmt .Errorf ("不允许添加无条件 %s 规则,这会锁定系统 " , req .Action )
148+ return fmt .Errorf ("Iptables Rule Security Check: unconditional %s rules are not allowed, this may lock you out of the system " , req .Action )
149149 }
150150
151151 ctx := context .Background ()
@@ -298,7 +298,7 @@ func (s *IptablesFilterService) ApplyFirewall() error {
298298 if (p .Action == "DROP" || p .Action == "REJECT" ) &&
299299 p .Protocol == "" && p .SourceIP == "" && p .DestIP == "" &&
300300 p .SrcPort == 0 && p .DstPort == 0 {
301- return fmt .Errorf ("链 %s 包含无条件 %s 规则,不允许应用 " , Chain1PanelInput , p .Action )
301+ return fmt .Errorf ("Chain %s includes unconditional %s rule, not allowed to apply " , Chain1PanelInput , p .Action )
302302 }
303303 item = item .Next ()
304304 }
@@ -312,59 +312,21 @@ func (s *IptablesFilterService) ApplyFirewall() error {
312312 if (p .Action == "DROP" || p .Action == "REJECT" ) &&
313313 p .Protocol == "" && p .SourceIP == "" && p .DestIP == "" &&
314314 p .SrcPort == 0 && p .DstPort == 0 {
315- return fmt .Errorf ("链 %s 包含无条件 %s 规则,不允许应用 " , Chain1PanelOutput , p .Action )
315+ return fmt .Errorf ("Chain %s includes unconditional %s rule, not allowed to apply " , Chain1PanelOutput , p .Action )
316316 }
317317 item = item .Next ()
318318 }
319319 }
320320
321- // 检查 INPUT 链是否已有跳转规则
322- inputChains , _ := s .iptablesClient .ReadFilter ([]string {ChainInput })
323- hasInputRule := false
324- if inputChain , ok := inputChains [ChainInput ]; ok {
325- item := inputChain .FirstRule
326- for item != nil {
327- if item .P .Action == Chain1PanelInput {
328- hasInputRule = true
329- break
330- }
331- item = item .Next ()
332- }
333- }
334-
335- // 应用到 INPUT 链
336- if ! hasInputRule {
337- if err := s .iptablesClient .Run (client .FilterTab , fmt .Sprintf ("-I %s 1 -j %s" , ChainInput , Chain1PanelInput )); err != nil {
338- return fmt .Errorf ("failed to apply %s to %s: %w" , Chain1PanelInput , ChainInput , err )
339- }
340- global .LOG .Infof ("Applied %s to %s chain" , Chain1PanelInput , ChainInput )
341- } else {
342- global .LOG .Infof ("%s already applied to %s chain" , Chain1PanelInput , ChainInput )
321+ if err := s .iptablesClient .Setup1PanelFirewallChains ("input" ); err != nil {
322+ return fmt .Errorf ("failed to apply %s to %s: %w" , Chain1PanelInput , ChainInput , err )
343323 }
324+ global .LOG .Infof ("Applied %s to %s chain" , Chain1PanelInput , ChainInput )
344325
345- // 检查 OUTPUT 链是否已有跳转规则
346- outputChains , _ := s .iptablesClient .ReadFilter ([]string {ChainOutput })
347- hasOutputRule := false
348- if outputChain , ok := outputChains [ChainOutput ]; ok {
349- item := outputChain .FirstRule
350- for item != nil {
351- if item .P .Action == Chain1PanelOutput {
352- hasOutputRule = true
353- break
354- }
355- item = item .Next ()
356- }
357- }
358-
359- // 应用到 OUTPUT 链
360- if ! hasOutputRule {
361- if err := s .iptablesClient .Run (client .FilterTab , fmt .Sprintf ("-I %s 1 -j %s" , ChainOutput , Chain1PanelOutput )); err != nil {
362- return fmt .Errorf ("failed to apply %s to %s: %w" , Chain1PanelOutput , ChainOutput , err )
363- }
364- global .LOG .Infof ("Applied %s to %s chain" , Chain1PanelOutput , ChainOutput )
365- } else {
366- global .LOG .Infof ("%s already applied to %s chain" , Chain1PanelOutput , ChainOutput )
326+ if err := s .iptablesClient .Setup1PanelFirewallChains ("output" ); err != nil {
327+ return fmt .Errorf ("failed to apply %s to %s: %w" , Chain1PanelInput , ChainInput , err )
367328 }
329+ global .LOG .Infof ("Applied %s to %s chain" , Chain1PanelInput , ChainInput )
368330
369331 return nil
370332}
0 commit comments