@@ -2,21 +2,30 @@ package firewall
22
33import (
44 "fmt"
5+ "os"
56
67 "github.com/1Panel-dev/1Panel/agent/app/dto"
8+ "github.com/1Panel-dev/1Panel/agent/app/repo"
79 "github.com/1Panel-dev/1Panel/agent/app/service"
10+ "github.com/1Panel-dev/1Panel/agent/constant"
811 "github.com/1Panel-dev/1Panel/agent/global"
912 "github.com/1Panel-dev/1Panel/agent/utils/firewall"
1013 firewallClient "github.com/1Panel-dev/1Panel/agent/utils/firewall/client"
1114 "github.com/1Panel-dev/1Panel/agent/utils/firewall/client/iptables"
1215)
1316
1417func Init () {
18+ if ! needInit () {
19+ return
20+ }
21+ global .LOG .Info ("initializing firewall settings..." )
1522 client , err := firewall .NewFirewallClient ()
1623 if err != nil {
1724 return
1825 }
1926 clientName := client .Name ()
27+
28+ settingRepo := repo .NewISettingRepo ()
2029 if clientName == "ufw" || clientName == "iptables" {
2130 if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelForward , iptables .ForwardFileName ); err != nil {
2231 global .LOG .Errorf ("load forward rules from file failed, err: %v" , err )
@@ -30,65 +39,91 @@ func Init() {
3039 global .LOG .Errorf ("load postrouting rules from file failed, err: %v" , err )
3140 return
3241 }
33- if err := firewallClient .EnableIptablesForward (); err != nil {
34- global .LOG .Errorf ("enable iptables forward failed, err: %v" , err )
35- return
36- }
3742 global .LOG .Infof ("loaded iptables rules for forward from file successfully" )
43+
44+ iptablesForwardStatus , _ := settingRepo .GetValueByKey ("IptablesForwardStatus" )
45+ if iptablesForwardStatus == constant .StatusEnable {
46+ if err := firewallClient .EnableIptablesForward (); err != nil {
47+ global .LOG .Errorf ("enable iptables forward failed, err: %v" , err )
48+ return
49+ }
50+ }
3851 }
52+
3953 if clientName == "ufw" {
4054 _ = iptables .UnbindChain (iptables .FilterTab , iptables .ChainInput , iptables .Chain1PanelBasicAfter )
4155 _ = iptables .UnbindChain (iptables .FilterTab , iptables .ChainInput , iptables .Chain1PanelBasicBefore )
4256 _ = iptables .UnbindChain (iptables .FilterTab , iptables .ChainInput , iptables .Chain1PanelBasic )
4357 _ = iptables .UnbindChain (iptables .FilterTab , iptables .ChainInput , iptables .Chain1PanelInput )
4458 _ = iptables .UnbindChain (iptables .FilterTab , iptables .ChainOutput , iptables .Chain1PanelOutput )
4559 }
46- if clientName == "iptables" {
47- if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelBasicBefore , iptables .BasicBeforeFileName ); err != nil {
48- global .LOG .Errorf ("load basic before rules from file failed, err: %v" , err )
49- return
50- }
51- if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelBasic , iptables .BasicFileName ); err != nil {
52- global .LOG .Errorf ("load basic rules from file failed, err: %v" , err )
53- return
54- }
55- if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelBasicAfter , iptables .BasicAfterFileName ); err != nil {
56- global .LOG .Errorf ("load basic after rules from file failed, err: %v" , err )
57- return
58- }
59- if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelInput , iptables .InputFileName ); err != nil {
60- global .LOG .Errorf ("load input rules from file failed, err: %v" , err )
61- return
62- }
63- if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelOutput , iptables .OutputFileName ); err != nil {
64- global .LOG .Errorf ("load output rules from file failed, err: %v" , err )
65- return
66- }
67- global .LOG .Infof ("loaded iptables rules for basic, input and output from file successfully" )
6860
69- panelPort := service .LoadPanelPort ()
70- if len (panelPort ) == 0 {
71- global .LOG .Errorf ("find 1panel service port failed" )
72- return
73- }
74- if err := iptables .AddRule (iptables .FilterTab , iptables .Chain1PanelBasicBefore , fmt .Sprintf ("-p tcp -m tcp --dport %v -j ACCEPT" , panelPort )); err != nil {
75- global .LOG .Errorf ("add port accept rule %v failed, err: %v" , panelPort , err )
61+ if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelBasicBefore , iptables .BasicBeforeFileName ); err != nil {
62+ global .LOG .Errorf ("load basic before rules from file failed, err: %v" , err )
63+ return
64+ }
65+ if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelBasic , iptables .BasicFileName ); err != nil {
66+ global .LOG .Errorf ("load basic rules from file failed, err: %v" , err )
67+ return
68+ }
69+ if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelBasicAfter , iptables .BasicAfterFileName ); err != nil {
70+ global .LOG .Errorf ("load basic after rules from file failed, err: %v" , err )
71+ return
72+ }
73+ panelPort := service .LoadPanelPort ()
74+ if len (panelPort ) == 0 {
75+ global .LOG .Errorf ("find 1panel service port failed" )
76+ return
77+ }
78+ if err := iptables .AddRule (iptables .FilterTab , iptables .Chain1PanelBasicBefore , fmt .Sprintf ("-p tcp -m tcp --dport %v -j ACCEPT" , panelPort )); err != nil {
79+ global .LOG .Errorf ("add port accept rule %v failed, err: %v" , panelPort , err )
80+ return
81+ }
82+ global .LOG .Infof ("loaded iptables rules for basic from file successfully" )
83+ iptablesService := service.IptablesService {}
84+ iptablesStatus , _ := settingRepo .GetValueByKey ("IptablesStatus" )
85+ if iptablesStatus == constant .StatusEnable {
86+ if err := iptablesService .Operate (dto.IptablesOp {Operate : "bind-base" }); err != nil {
87+ global .LOG .Errorf ("bind base chains failed, err: %v" , err )
7688 return
7789 }
90+ }
7891
79- iptablesService := service.IptablesService {}
80- if err := iptablesService .Operate (dto.IptablesOp {Operate : "bind-base" }); err != nil {
81- global .LOG .Errorf ("bind base chains failed, err: %v" , err )
92+ if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelInput , iptables .InputFileName ); err != nil {
93+ global .LOG .Errorf ("load input rules from file failed, err: %v" , err )
94+ return
95+ }
96+ if err := iptables .LoadRulesFromFile (iptables .FilterTab , iptables .Chain1PanelOutput , iptables .OutputFileName ); err != nil {
97+ global .LOG .Errorf ("load output rules from file failed, err: %v" , err )
98+ return
99+ }
100+ global .LOG .Infof ("loaded iptables rules for input and output from file successfully" )
101+ iptablesInputStatus , _ := settingRepo .GetValueByKey ("IptablesInputStatus" )
102+ if iptablesInputStatus == constant .StatusEnable {
103+ if err := iptablesService .Operate (dto.IptablesOp {Name : iptables .Chain1PanelInput , Operate : "bind" }); err != nil {
104+ global .LOG .Errorf ("bind input chains failed, err: %v" , err )
82105 return
83106 }
107+ }
108+ iptablesOutputStatus , _ := settingRepo .GetValueByKey ("IptablesOutputStatus" )
109+ if iptablesOutputStatus == constant .StatusEnable {
84110 if err := iptablesService .Operate (dto.IptablesOp {Name : iptables .Chain1PanelOutput , Operate : "bind" }); err != nil {
85111 global .LOG .Errorf ("bind output chains failed, err: %v" , err )
86112 return
87113 }
88- if err := iptablesService .Operate (dto.IptablesOp {Name : iptables .Chain1PanelInput , Operate : "bind" }); err != nil {
89- global .LOG .Errorf ("bind input chains failed, err: %v" , err )
90- return
91- }
92114 }
115+ }
93116
117+ func needInit () bool {
118+ file , err := os .OpenFile ("/run/1panel_boot_mark" , os .O_RDWR | os .O_CREATE | os .O_EXCL , 0644 )
119+ if err != nil {
120+ if os .IsExist (err ) {
121+ return false
122+ }
123+ global .LOG .Errorf ("check boot mark file failed: %v" , err )
124+ return true
125+ }
126+ defer file .Close ()
127+ fmt .Fprintf (file , "Boot Mark for 1panel\n " )
128+ return true
94129}
0 commit comments