mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/1Panel-dev/1Panel.git
synced 2026-09-20 16:13:59 +08:00
Port forwarding no longer shares the filter client. FilterClient keeps only
filter capabilities, and forwarding gets its own adapter, service and boot
replay:
- utils/firewall/forwarding holds the provider adapters. firewalld uses native
forward-port, ufw and iptables share the NAT implementation moved out of
client/iptables/forward.go.
- service/forwarding.go owns base info, search, operate, enable and replay.
The API keeps its routes and dispatches on name/type/operate.
- init/firewall replays forwarding through that service instead of loading NAT
rule files inline.
Also adds 1PANEL_FORWARD to the IptablesOp name enum: the frontend already
sends {"name":"1PANEL_FORWARD","operate":"init-forward"} and the validator
rejected it with 400 before reaching the service. Besides that, the only
observable difference is that a forward-tab search no longer triggers the
port/address record cleanup goroutine on the side.
122 lines
4.1 KiB
Go
122 lines
4.1 KiB
Go
package firewall
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
|
"github.com/1Panel-dev/1Panel/agent/app/repo"
|
|
"github.com/1Panel-dev/1Panel/agent/app/service"
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall"
|
|
"github.com/1Panel-dev/1Panel/agent/utils/firewall/client/iptables"
|
|
)
|
|
|
|
func Init() {
|
|
if !needInit() {
|
|
return
|
|
}
|
|
InitPingStatus()
|
|
global.LOG.Info("initializing firewall settings...")
|
|
client, err := firewall.NewFirewallClient()
|
|
if err != nil {
|
|
return
|
|
}
|
|
clientName := client.Name()
|
|
if err := service.NewIForwardingService().Replay(); err != nil {
|
|
global.LOG.Errorf("replay forwarding rules failed, err: %v", err)
|
|
return
|
|
}
|
|
|
|
if clientName != "iptables" {
|
|
return
|
|
}
|
|
settingRepo := repo.NewISettingRepo()
|
|
if err := iptables.LoadRulesFromFile(iptables.FilterTab, iptables.Chain1PanelBasicBefore, iptables.BasicBeforeFileName); err != nil {
|
|
global.LOG.Errorf("load basic before rules from file failed, err: %v", err)
|
|
return
|
|
}
|
|
if err := iptables.LoadRulesFromFile(iptables.FilterTab, iptables.Chain1PanelBasic, iptables.BasicFileName); err != nil {
|
|
global.LOG.Errorf("load basic rules from file failed, err: %v", err)
|
|
return
|
|
}
|
|
if err := iptables.LoadRulesFromFile(iptables.FilterTab, iptables.Chain1PanelBasicAfter, iptables.BasicAfterFileName); err != nil {
|
|
global.LOG.Errorf("load basic after rules from file failed, err: %v", err)
|
|
return
|
|
}
|
|
panelPort := service.LoadPanelPort()
|
|
if len(panelPort) == 0 {
|
|
global.LOG.Errorf("find 1panel service port failed")
|
|
return
|
|
}
|
|
if err := iptables.AddRule(iptables.FilterTab, iptables.Chain1PanelBasicBefore, "-p", "tcp", "-m", "tcp", "--dport", panelPort, "-j", "ACCEPT"); err != nil {
|
|
global.LOG.Errorf("add port accept rule %v failed, err: %v", panelPort, err)
|
|
return
|
|
}
|
|
global.LOG.Infof("loaded iptables rules for basic from file successfully")
|
|
iptablesService := service.IptablesService{}
|
|
iptablesStatus, _ := settingRepo.GetValueByKey("IptablesStatus")
|
|
if iptablesStatus == constant.StatusEnable {
|
|
if err := iptablesService.Operate(dto.IptablesOp{Operate: "bind-base-without-init"}); err != nil {
|
|
global.LOG.Errorf("bind base chains failed, err: %v", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
if err := iptables.LoadRulesFromFile(iptables.FilterTab, iptables.Chain1PanelInput, iptables.InputFileName); err != nil {
|
|
global.LOG.Errorf("load input rules from file failed, err: %v", err)
|
|
return
|
|
}
|
|
if err := iptables.LoadRulesFromFile(iptables.FilterTab, iptables.Chain1PanelOutput, iptables.OutputFileName); err != nil {
|
|
global.LOG.Errorf("load output rules from file failed, err: %v", err)
|
|
return
|
|
}
|
|
global.LOG.Infof("loaded iptables rules for input and output from file successfully")
|
|
iptablesInputStatus, _ := settingRepo.GetValueByKey("IptablesInputStatus")
|
|
if iptablesInputStatus == constant.StatusEnable {
|
|
if err := iptablesService.Operate(dto.IptablesOp{Name: iptables.Chain1PanelInput, Operate: "bind"}); err != nil {
|
|
global.LOG.Errorf("bind input chains failed, err: %v", err)
|
|
return
|
|
}
|
|
}
|
|
iptablesOutputStatus, _ := settingRepo.GetValueByKey("IptablesOutputStatus")
|
|
if iptablesOutputStatus == constant.StatusEnable {
|
|
if err := iptablesService.Operate(dto.IptablesOp{Name: iptables.Chain1PanelOutput, Operate: "bind"}); err != nil {
|
|
global.LOG.Errorf("bind output chains failed, err: %v", err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func needInit() bool {
|
|
file, err := os.OpenFile("/run/1panel_boot_mark", os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644)
|
|
if err != nil {
|
|
if os.IsExist(err) {
|
|
return false
|
|
}
|
|
global.LOG.Errorf("check boot mark file failed: %v", err)
|
|
return true
|
|
}
|
|
defer file.Close()
|
|
fmt.Fprintf(file, "Boot Mark for 1panel\n")
|
|
return true
|
|
}
|
|
|
|
func InitPingStatus() {
|
|
global.LOG.Info("initializing ban ping status from settings...")
|
|
status := firewall.LoadPingStatus()
|
|
statusInDB, _ := repo.NewISettingRepo().GetValueByKey("BanPing")
|
|
if statusInDB == status {
|
|
return
|
|
}
|
|
|
|
enable := "1"
|
|
if statusInDB == constant.StatusDisable {
|
|
enable = "0"
|
|
}
|
|
if err := firewall.UpdatePingStatus(enable); err != nil {
|
|
global.LOG.Errorf("initialize ping status failed: %v", err)
|
|
}
|
|
}
|