Skip to content

Commit ea49308

Browse files
myreveryjaapmarcus
andauthored
[Bug Fix] Optimize loading firewall rules (hestiacp#1858)
* Optimize loading firewall rules * Optimize loading firewall rules * Optimize loading firewall rules * Edited for get the default interface * Edited for get the default interface * Add extra rule to 1.4.2 On v-update-firewall the iptables.rules will be rewritten/generated and If this rule is skipped it will be not created https://github.com/myrevery/hestiacp/blob/f0c7c54261c289fb10ea283618dda71b7cddd320/bin/v-update-firewall#L79-L84 Co-authored-by: Jaap Marcus <9754650+jaapmarcus@users.noreply.github.com>
1 parent b3b0ac5 commit ea49308

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

bin/v-stop-firewall

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,30 @@ if [ -d "/etc/sysconfig" ]; then
7676
fi
7777
else
7878
/sbin/iptables-save > /etc/iptables.rules
79-
if dpkg-query -W -f'${Status}' "netplan*" 2>/dev/null | grep -q "ok installed"; then
80-
preup="/usr/lib/networkd-dispatcher/routable.d/50-ifup-hooks"
79+
if dpkg-query -W -f'${Status}' "netplan*" 2>/dev/null | grep -q "ok installed" && [ -d /etc/netplan ] && [ -n "$(ls -A /etc/netplan 2>/dev/null)" ]; then
80+
preup="/usr/lib/networkd-dispatcher/routable.d/10-hestia-iptables"
8181
if [ ! -e "$preup" ]; then
82-
for iface in $(ip token | awk -F 'dev ' '{print $2}'); do
83-
if [ -z "$interfaces" ]; then
84-
interfaces=" \"\$IFACE\"==\"$iface\""
85-
else
86-
interfaces="$interfaces || \"\$IFACE\"==\"$iface\" ";
87-
fi
88-
done
8982
IFS='%'
90-
echo '#!/bin/bash' > $preup
83+
echo '#!/bin/sh' > $preup
9184
echo '' >> $preup
92-
echo 'if [['$interfaces']]; then' >> $preup
85+
echo 'if [ "$IFACE" = "'$(ip route list | awk '/default .+/ {print $5}' | uniq)'" ]; then' >> $preup
86+
[ -x "$(which ipset)" ] && echo " ${HESTIA}/bin/v-update-firewall-ipset" >> $preup
9387
echo ' sleep 3' >> $preup
9488
echo ' /sbin/iptables-restore < /etc/iptables.rules' >> $preup
9589
echo 'fi' >> $preup
9690
echo "exit 0" >> $preup
9791
chmod +x $preup
9892
fi
9993
else
100-
preup="/etc/network/if-pre-up.d/iptables"
94+
preup="/etc/network/if-pre-up.d/hestia-iptables"
10195
if [ ! -e "$preup" ]; then
96+
IFS='%'
10297
echo '#!/bin/sh' > $preup
103-
echo "/sbin/iptables-restore < /etc/iptables.rules" >> $preup
98+
echo '' >> $preup
99+
echo 'if [ "$IFACE" = "'$(ip route list | awk '/default .+/ {print $5}' | uniq)'" ]; then' >> $preup
100+
[ -x "$(which ipset)" ] && echo " ${HESTIA}/bin/v-update-firewall-ipset" >> $preup
101+
echo ' /sbin/iptables-restore < /etc/iptables.rules' >> $preup
102+
echo 'fi' >> $preup
104103
echo "exit 0" >> $preup
105104
chmod +x $preup
106105
fi

bin/v-update-firewall

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,27 +183,30 @@ if [ -d "/etc/sysconfig" ]; then
183183
fi
184184
else
185185
/sbin/iptables-save > /etc/iptables.rules
186-
if dpkg-query -W -f'${Status}' "netplan*" 2>/dev/null | grep -q "ok installed"; then
187-
preup="/usr/lib/networkd-dispatcher/routable.d/50-ifup-hooks"
186+
if dpkg-query -W -f'${Status}' "netplan*" 2>/dev/null | grep -q "ok installed" && [ -d /etc/netplan ] && [ -n "$(ls -A /etc/netplan 2>/dev/null)" ]; then
187+
preup="/usr/lib/networkd-dispatcher/routable.d/10-hestia-iptables"
188188
if [ ! -e "$preup" ]; then
189189
IFS='%'
190-
echo '#!/bin/bash' > $preup
191-
echo '' >> $preup
192-
echo "${HESTIA}/bin/v-update-firewall-ipset" >> $preup
190+
echo '#!/bin/sh' > $preup
193191
echo '' >> $preup
194-
echo 'if [ "$IFACE" == "'$(/bin/ip token | awk -F 'dev ' '{print $2}')'" ]; then' >> $preup
192+
echo 'if [ "$IFACE" = "'$(ip route list | awk '/default .+/ {print $5}' | uniq)'" ]; then' >> $preup
193+
[ -x "$(which ipset)" ] && echo " ${HESTIA}/bin/v-update-firewall-ipset" >> $preup
195194
echo ' sleep 3' >> $preup
196195
echo ' /sbin/iptables-restore < /etc/iptables.rules' >> $preup
197196
echo 'fi' >> $preup
198197
echo "exit 0" >> $preup
199198
chmod +x $preup
200199
fi
201200
else
202-
preup="/etc/network/if-pre-up.d/iptables"
201+
preup="/etc/network/if-pre-up.d/hestia-iptables"
203202
if [ ! -e "$preup" ]; then
203+
IFS='%'
204204
echo '#!/bin/sh' > $preup
205-
echo "${HESTIA}/bin/v-update-firewall-ipset" >> $preup
206-
echo "/sbin/iptables-restore < /etc/iptables.rules" >> $preup
205+
echo '' >> $preup
206+
echo 'if [ "$IFACE" = "'$(ip route list | awk '/default .+/ {print $5}' | uniq)'" ]; then' >> $preup
207+
[ -x "$(which ipset)" ] && echo " ${HESTIA}/bin/v-update-firewall-ipset" >> $preup
208+
echo ' /sbin/iptables-restore < /etc/iptables.rules' >> $preup
209+
echo 'fi' >> $preup
207210
echo "exit 0" >> $preup
208211
chmod +x $preup
209212
fi

install/upgrade/versions/1.4.2.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# Hestia Control Panel upgrade script for target version 1.4.2
4+
5+
#######################################################################################
6+
####### Place additional commands below. #######
7+
#######################################################################################
8+
9+
# Optimize loading firewall rules
10+
if [ "$FIREWALL_SYSTEM" = "iptables" ]; then
11+
echo "[ * ] Fix the issue of loading firewall rules..."
12+
# Add rule to ensure the rule will be added when we update the firewall / /etc/iptables.rules
13+
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
14+
rm -f /usr/lib/networkd-dispatcher/routable.d/50-ifup-hooks /etc/network/if-pre-up.d/iptables
15+
$BIN/v-update-firewall
16+
fi

0 commit comments

Comments
 (0)