mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-18 13:29:03 +08:00
parent
15bc6cf884
commit
6aca579854
1 changed files with 26 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/agent/buserr"
|
||||
"github.com/1Panel-dev/1Panel/agent/global"
|
||||
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
|
||||
)
|
||||
|
||||
|
|
@ -167,7 +168,8 @@ func (f *Ufw) RichRules(rule FireInfo, operation string) error {
|
|||
return buserr.New("ErrCmdIllegal")
|
||||
}
|
||||
|
||||
ruleStr := fmt.Sprintf("%s insert 1 %s ", f.CmdStr, rule.Strategy)
|
||||
insertNum := f.loadInsertNum(rule, operation)
|
||||
ruleStr := fmt.Sprintf("%s insert %d %s ", f.CmdStr, insertNum, rule.Strategy)
|
||||
if operation == "remove" {
|
||||
ruleStr = fmt.Sprintf("%s delete %s ", f.CmdStr, rule.Strategy)
|
||||
}
|
||||
|
|
@ -252,3 +254,26 @@ func (f *Ufw) loadInfo(line string, fireType string) FireInfo {
|
|||
|
||||
return itemInfo
|
||||
}
|
||||
|
||||
func (f *Ufw) loadInsertNum(rule FireInfo, operation string) int {
|
||||
if !strings.Contains(rule.Address, ":") || operation == "remove" {
|
||||
return 1
|
||||
}
|
||||
rules, err := cmd.RunDefaultWithStdoutBashCf("%s status numbered", f.CmdStr)
|
||||
if err != nil {
|
||||
global.LOG.Errorf("load ufw rules failed, err: %v", err)
|
||||
return 1
|
||||
}
|
||||
lines := strings.Split(rules, "\n")
|
||||
i := 1
|
||||
for _, item := range lines {
|
||||
fields := strings.Fields(item)
|
||||
if len(fields) < 4 {
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(item, "(v6)") {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue