fix: 解决编辑端口规则不能保存的问题 (#7100)
Some checks are pending
Build Test / build-linux-binary (push) Waiting to run
Build / SonarCloud (push) Waiting to run
sync2gitee / repo-sync (push) Waiting to run

This commit is contained in:
igophper 2024-11-17 22:52:02 +08:00 committed by GitHub
parent eb36a0f8cb
commit c6ea06153a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -323,21 +323,58 @@ func (u *FirewallService) OperateForwardRule(req dto.ForwardRuleOperate) error {
} }
rules, _ := client.ListForward() rules, _ := client.ListForward()
i := 0
for _, rule := range rules {
shouldKeep := true
for i := range req.Rules {
reqRule := &req.Rules[i]
if reqRule.TargetIP == "" {
reqRule.TargetIP = "127.0.0.1"
}
if reqRule.Operation == "remove" {
for _, proto := range strings.Split(reqRule.Protocol, "/") {
if reqRule.Port == rule.Port &&
reqRule.TargetPort == rule.TargetPort &&
reqRule.TargetIP == rule.TargetIP &&
proto == rule.Protocol {
shouldKeep = false
break
}
}
}
}
if shouldKeep {
rules[i] = rule
i++
}
}
rules = rules[:i]
for _, rule := range rules { for _, rule := range rules {
for _, reqRule := range req.Rules { for _, reqRule := range req.Rules {
if reqRule.Operation == "remove" { if reqRule.Operation == "remove" {
continue continue
} }
if reqRule.TargetIP == "" {
reqRule.TargetIP = "127.0.0.1" for _, proto := range strings.Split(reqRule.Protocol, "/") {
} if reqRule.Port == rule.Port &&
if reqRule.Port == rule.Port && reqRule.TargetPort == rule.TargetPort && reqRule.TargetIP == rule.TargetIP { reqRule.TargetPort == rule.TargetPort &&
return constant.ErrRecordExist reqRule.TargetIP == rule.TargetIP &&
proto == rule.Protocol {
return constant.ErrRecordExist
}
} }
} }
} }
sort.SliceStable(req.Rules, func(i, j int) bool { sort.SliceStable(req.Rules, func(i, j int) bool {
if req.Rules[i].Operation == "remove" && req.Rules[j].Operation != "remove" {
return true
}
if req.Rules[i].Operation != "remove" && req.Rules[j].Operation == "remove" {
return false
}
n1, _ := strconv.Atoi(req.Rules[i].Num) n1, _ := strconv.Atoi(req.Rules[i].Num)
n2, _ := strconv.Atoi(req.Rules[j].Num) n2, _ := strconv.Atoi(req.Rules[j].Num)
return n1 > n2 return n1 > n2