fix: 修改防火墙状态判断方式 (#3258)

pull/3260/head
ssongliu 2023-12-11 10:48:06 +08:00 committed by GitHub
parent fcba07d37a
commit 2aea2d3dfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View File

@ -6,6 +6,7 @@ type Fail2BanBaseInfo struct {
IsExist bool `json:"isExist"`
Version string `json:"version"`
Port int `json:"port"`
MaxRetry int `json:"maxRetry"`
BanTime string `json:"banTime"`
FindTime string `json:"findTime"`

View File

@ -9,7 +9,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/utils/systemctl"
"github.com/1Panel-dev/1Panel/backend/utils/firewall"
"github.com/1Panel-dev/1Panel/backend/utils/toolbox"
)
@ -100,16 +100,18 @@ func (u *Fail2BanService) Operate(operation string) error {
func (u *Fail2BanService) UpdateConf(req dto.Fail2BanUpdate) error {
if req.Key == "banaction" {
switch req.Value {
case "firewallcmd-ipset":
isActive, _ := systemctl.IsActive("firewalld")
if !isActive {
return buserr.WithName("ErrBanAction", "firewalld")
if req.Value == "firewallcmd-ipset" || req.Value == "ufw" {
client, err := firewall.NewFirewallClient()
if err != nil {
return err
}
case "ufw":
isActive, _ := systemctl.IsActive("ufw")
if !isActive {
return buserr.WithName("ErrBanAction", "ufw")
status, _ := client.Status()
if status != "running" {
service := "firewalld"
if req.Value == "ufw" {
service = "ufw"
}
return buserr.WithName("ErrBanAction", service)
}
}
}
@ -209,6 +211,11 @@ func (u *Fail2BanService) OperateSSHD(req dto.Fail2BanSet) error {
}
func loadFailValue(line string, baseInfo *dto.Fail2BanBaseInfo) {
if strings.HasPrefix(line, "port") {
itemValue := strings.ReplaceAll(line, "port", "")
itemValue = strings.ReplaceAll(itemValue, "=", "")
baseInfo.Port, _ = strconv.Atoi(strings.TrimSpace(itemValue))
}
if strings.HasPrefix(line, "maxretry") {
itemValue := strings.ReplaceAll(line, "maxretry", "")
itemValue = strings.ReplaceAll(itemValue, "=", "")