fix: 解决 UFW 防火墙规则为空时插入规则失败的问题 (#2816)

Refs #2794
This commit is contained in:
ssongliu 2023-11-06 10:54:39 +08:00 committed by GitHub
parent cd5658adab
commit 262ddb5f12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -183,6 +183,13 @@ func (f *Ufw) RichRules(rule FireInfo, operation string) error {
stdout, err := cmd.Exec(ruleStr)
if err != nil {
if strings.Contains(stdout, "ERROR: Invalid position") {
stdout, err := cmd.Exec(strings.ReplaceAll(ruleStr, "insert 1 ", ""))
if err != nil {
return fmt.Errorf("%s rich rules (%s), failed, err: %s", operation, ruleStr, stdout)
}
return nil
}
return fmt.Errorf("%s rich rules (%s), failed, err: %s", operation, ruleStr, stdout)
}
return nil