fix: Fixed the fault in determining the firewall port forwarding version

This commit is contained in:
ssongliu 2024-11-21 23:19:05 +08:00
parent 28597721f2
commit 4936654b0c
3 changed files with 12 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
) )
@ -118,6 +119,9 @@ func (f *Firewall) ListPort() ([]FireInfo, error) {
} }
func (f *Firewall) ListForward() ([]FireInfo, error) { func (f *Firewall) ListForward() ([]FireInfo, error) {
if err := f.EnableForward(); err != nil {
global.LOG.Errorf("init port forward failed, err: %v", err)
}
stdout, err := cmd.Exec("firewall-cmd --zone=public --list-forward-ports") stdout, err := cmd.Exec("firewall-cmd --zone=public --list-forward-ports")
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -52,7 +52,7 @@ func (iptables *Iptables) Check() error {
} }
func (iptables *Iptables) NatNewChain() error { func (iptables *Iptables) NatNewChain() error {
return iptables.runf("-t nat -N %s", NatChain) return iptables.runf("-N %s", NatChain)
} }
func (iptables *Iptables) NatAppendChain() error { func (iptables *Iptables) NatAppendChain() error {

View File

@ -6,6 +6,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
) )
@ -108,6 +109,12 @@ func (f *Ufw) ListForward() ([]FireInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
panelChian, _ := cmd.Execf("%s iptables -t nat -L -n | grep 'Chain 1PANEL'", iptables.CmdStr)
if len(strings.ReplaceAll(panelChian, "\n", "")) == 0 {
if err := f.EnableForward(); err != nil {
global.LOG.Errorf("init port forward failed, err: %v", err)
}
}
rules, err := iptables.NatList() rules, err := iptables.NatList()
if err != nil { if err != nil {
return nil, err return nil, err