Skip to content

Commit

Permalink
fix: 防火墙关闭状态下ping状态报错
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 25, 2024
1 parent 5ac43df commit 302d0f1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/data/safe.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package data

import (
"errors"
"fmt"
"github.com/TheTNB/panel/pkg/firewall"
"strings"

"github.com/spf13/cast"
Expand Down Expand Up @@ -61,8 +62,8 @@ func (r *safeRepo) UpdateSSH(port uint, status bool) error {

func (r *safeRepo) GetPingStatus() (bool, error) {
out, err := shell.Execf(`firewall-cmd --list-rich-rules`)
if err != nil {
return true, errors.New(out)
if err != nil { // 可能防火墙已关闭等
return true, nil
}

if !strings.Contains(out, `rule protocol value="icmp" drop`) {
Expand All @@ -73,7 +74,14 @@ func (r *safeRepo) GetPingStatus() (bool, error) {
}

func (r *safeRepo) UpdatePingStatus(status bool) error {
var err error
fw, err := firewall.NewFirewall().Status()
if err != nil {
return err
}
if !fw {
return fmt.Errorf("failed to update ping status: firewalld is not running")
}

if status {
_, err = shell.Execf(`firewall-cmd --permanent --remove-rich-rule='rule protocol value=icmp drop'`)
} else {
Expand Down

0 comments on commit 302d0f1

Please sign in to comment.