Skip to content

Commit 3ba1362

Browse files
committed
fix: Fix ufw IPv6 rule configuration issue
1 parent 15bc6cf commit 3ba1362

File tree

1 file changed

+26
-1
lines changed
  • agent/utils/firewall/client

1 file changed

+26
-1
lines changed

agent/utils/firewall/client/ufw.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/1Panel-dev/1Panel/agent/buserr"
8+
"github.com/1Panel-dev/1Panel/agent/global"
89
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
910
)
1011

@@ -167,7 +168,8 @@ func (f *Ufw) RichRules(rule FireInfo, operation string) error {
167168
return buserr.New("ErrCmdIllegal")
168169
}
169170

170-
ruleStr := fmt.Sprintf("%s insert 1 %s ", f.CmdStr, rule.Strategy)
171+
insertNum := f.loadInsertNum(rule, operation)
172+
ruleStr := fmt.Sprintf("%s insert %d %s ", f.CmdStr, insertNum, rule.Strategy)
171173
if operation == "remove" {
172174
ruleStr = fmt.Sprintf("%s delete %s ", f.CmdStr, rule.Strategy)
173175
}
@@ -252,3 +254,26 @@ func (f *Ufw) loadInfo(line string, fireType string) FireInfo {
252254

253255
return itemInfo
254256
}
257+
258+
func (f *Ufw) loadInsertNum(rule FireInfo, operation string) int {
259+
if !strings.Contains(rule.Address, ":") || operation == "remove" {
260+
return 1
261+
}
262+
rules, err := cmd.RunDefaultWithStdoutBashCf("%s status numbered", f.CmdStr)
263+
if err != nil {
264+
global.LOG.Errorf("load ufw rules failed, err: %v", err)
265+
return 1
266+
}
267+
lines := strings.Split(rules, "\n")
268+
i := 1
269+
for _, item := range lines {
270+
fields := strings.Fields(item)
271+
if len(fields) < 4 {
272+
continue
273+
}
274+
if !strings.Contains(item, "(v6)") {
275+
i++
276+
}
277+
}
278+
return i
279+
}

0 commit comments

Comments
 (0)