Skip to content

Commit 8342115

Browse files
asmccasmcc
andauthored
IPV6: Preparation for IPV6 compatibility of backend hestia-nginx (hestiacp#3882)
Co-authored-by: asmcc <root@test.hestiacp.com>
1 parent cb0371b commit 8342115

File tree

7 files changed

+70
-45
lines changed

7 files changed

+70
-45
lines changed

bin/v-add-firewall-chain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ source $HESTIA/func/firewall.sh
3131
source_conf "$HESTIA/conf/hestia.conf"
3232

3333
# Get hestia port by reading nginx.conf
34-
hestiaport=$(grep -m 1 'listen' $HESTIA/nginx/conf/nginx.conf | awk '{print $2}' | sed "s|;||")
34+
hestiaport=$(sed -ne "/listen/{s/.*listen[^0-9]*\([0-9][0-9]*\)[ \t]*ssl\;/\1/p;q}" "$HESTIA/nginx/conf/nginx.conf")
3535
if [ -z "$hestiaport" ]; then
3636
hestiaport=8083
3737
fi

bin/v-add-sys-ip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ source $HESTIA/func/main.sh
4242
source $HESTIA/func/ip.sh
4343
# shellcheck source=/usr/local/hestia/func/domain.sh
4444
source $HESTIA/func/domain.sh
45+
# shellcheck source=/usr/local/hestia/func/syshealth.sh
46+
source "$HESTIA/func/syshealth.sh"
4547
# load config file
4648
source_conf "$HESTIA/conf/hestia.conf"
4749

@@ -193,6 +195,9 @@ if [ -n "$PROXY_SYSTEM" ]; then
193195
fi
194196
fi
195197

198+
# Adapt Port Listing in HESTIA NGINX Backend
199+
syshealth_adapt_hestia_nginx_listen_ports
200+
196201
#----------------------------------------------------------#
197202
# Hestia #
198203
#----------------------------------------------------------#
@@ -227,6 +232,12 @@ if [ -n "$FIREWALL_SYSTEM" ]; then
227232
$BIN/v-update-firewall
228233
fi
229234

235+
# Restarting hestia service if nginx backend config was changed
236+
if [ "$NGINX_BCONF_CHANGED" = "yes" -a -f "/etc/init.d/hestia" ]; then
237+
systemctl restart hestia > /dev/null 2>&1
238+
check_result $? "Hestia backend restart failed" > /dev/null
239+
fi
240+
230241
# Logging
231242
$BIN/v-log-action "system" "Info" "Network" "Added new IP address to the system (IP: $ip)."
232243
log_event "$OK" "$ARGUMENTS"

bin/v-change-sys-port

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ NGINX_CONFIG="$HESTIA/nginx/conf/nginx.conf"
1818
# shellcheck source=/etc/hestiacp/hestia.conf
1919
source /etc/hestiacp/hestia.conf
2020
# shellcheck source=/usr/local/hestia/func/main.sh
21-
source $HESTIA/func/main.sh
21+
source "$HESTIA/func/main.sh"
2222
# load config file
2323
source_conf "$HESTIA/conf/hestia.conf"
2424

@@ -55,18 +55,18 @@ check_hestia_demo_mode
5555
#----------------------------------------------------------#
5656

5757
# Get original port
58-
LISTEN_ROWS=$(cat ${NGINX_CONFIG} | grep -c "listen")
59-
ORIGINAL_PORT=$(cat ${NGINX_CONFIG} | grep -m1 "listen" | sed 's/[^0-9]*//g')
58+
LISTEN_ROWS=$(cat "$NGINX_CONFIG" | grep -c "listen")
59+
ORIGINAL_PORT=$(sed -ne "/listen/{s/.*listen[^0-9]*\([0-9][0-9]*\)[ \t]*ssl\;/\1/p;q}" "$NGINX_CONFIG")
6060

6161
# Check if port is different to nginx.conf
6262
if [ "$ORIGINAL_PORT" = "$PORT" ]; then
6363
# Nothing to do, exit
6464
exit
6565
else
6666
# Set new port in config via v-change-sys-config-value
67-
$BIN/v-change-sys-config-value "BACKEND_PORT" "$PORT"
67+
"$BIN/v-change-sys-config-value" "BACKEND_PORT" "$PORT"
6868
# Replace port in config files.
69-
sed -i "s/\(listen[ \t]*.*[: \t]\)[0-9][0-9]*\([^0-9]*ssl\;$\)/\1$PORT\2/" ${NGINX_CONFIG}
69+
sed -i "s/\(listen[ \t]*.*[: \t]\)[0-9][0-9]*\([^0-9]*ssl\;$\)/\1$PORT\2/" "$NGINX_CONFIG"
7070
if [ -d /etc/roundcube/ ]; then
7171
sed -i "/password_hestia_port/c\$rcmail_config['password_hestia_port'] = '$PORT';" /etc/roundcube/plugins/password/config.inc.php
7272
fi
@@ -75,21 +75,21 @@ else
7575
fi
7676

7777
if [ -f "$HESTIA/data/firewall/rules.conf" ]; then
78-
sed -i "/COMMENT='HESTIA'/c\RULE='2' ACTION='ACCEPT' PROTOCOL='TCP' PORT='$PORT' IP='0.0.0.0/0' COMMENT='HESTIA' SUSPENDED='no' TIME='07:40:16' DATE='2014-05-25'" $HESTIA/data/firewall/rules.conf
78+
sed -i "/COMMENT='HESTIA'/c\RULE='2' ACTION='ACCEPT' PROTOCOL='TCP' PORT='$PORT' IP='0.0.0.0/0' COMMENT='HESTIA' SUSPENDED='no' TIME='07:40:16' DATE='2014-05-25'" "$HESTIA/data/firewall/rules.conf"
7979
fi
8080

81-
if [ -f $HESTIA/data/firewall/chains.conf ]; then
82-
sed -i "/CHAIN='HESTIA'/c\CHAIN='HESTIA' PORT='$PORT' PROTOCOL='TCP'" $HESTIA/data/firewall/chains.conf
81+
if [ -f "$HESTIA/data/firewall/chains.conf" ]; then
82+
sed -i "/CHAIN='HESTIA'/c\CHAIN='HESTIA' PORT='$PORT' PROTOCOL='TCP'" "$HESTIA/data/firewall/chains.conf"
8383
fi
8484

8585
# Restart services
8686
if [ -n "$FIREWALL_SYSTEM" ] && [ "$FIREWALL_SYSTEM" != no ]; then
87-
$BIN/v-restart-service iptables
87+
"$BIN/v-restart-service" iptables
8888
fi
8989

9090
# Check if Hestia is running
9191
if [[ $(ps -eaf | grep -i hestia | sed '/^$/d' | wc -l) -gt 1 ]]; then
92-
$BIN/v-restart-service hestia
92+
"$BIN/v-restart-service" hestia
9393
fi
9494
fi
9595

@@ -98,9 +98,9 @@ fi
9898
#----------------------------------------------------------#
9999

100100
# Logging
101-
$BIN/v-log-action "system" "Warning" "System" "Hestia Control Panel backend port changed (New Value: $PORT, Old Value: $ORIGINAL_PORT)."
101+
"$BIN/v-log-action" "system" "Warning" "System" "Hestia Control Panel backend port changed (New Value: $PORT, Old Value: $ORIGINAL_PORT)."
102102
if [ $LISTEN_ROWS -gt 1 ]; then
103-
$BIN/v-log-action "system" "Warning" "System" "Hestia Control Panel backend port: Use first of $LISTEN_ROWS listened ports in ${NGINX_CONFIG}"
103+
"$BIN/v-log-action" "system" "Warning" "System" "Hestia Control Panel backend port: Use first of $LISTEN_ROWS listened ports in $NGINX_CONFIG"
104104
fi
105105
log_event "$OK" "$ARGUMENTS"
106106

func/syshealth.sh

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function syshealth_repair_system_config() {
285285

286286
# Backend port
287287
if [[ -z $(check_key_exists 'BACKEND_PORT') ]]; then
288-
ORIGINAL_PORT=$(cat $HESTIA/nginx/conf/nginx.conf | grep -m 1 "listen" | sed 's/[^0-9]*//g')
288+
ORIGINAL_PORT=$(sed -ne "/listen/{s/.*listen[^0-9]*\([0-9][0-9]*\)[ \t]*ssl\;/\1/p;q}" "$HESTIA/nginx/conf/nginx.conf")
289289
echo "[ ! ] Adding missing variable to hestia.conf: BACKEND_PORT ('$ORIGINAL_PORT')"
290290
$BIN/v-change-sys-config-value 'BACKEND_PORT' $ORIGINAL_PORT
291291
fi
@@ -451,9 +451,9 @@ function syshealth_repair_system_config() {
451451
$BIN/v-change-sys-config-value "USE_SERVER_SMTP" "false"
452452
fi
453453

454-
if [[ -z $(check_key_exists 'SERVER_SMTP_HOST') ]]; then
454+
if [[ -z $(check_key_exists 'SERVER_SMTP_PORT') ]]; then
455455
echo "[ ! ] Adding missing variable to hestia.conf: SERVER_SMTP_PORT ('')"
456-
$BIN/v-change-sys-config-value "SERVER_SMTP_HOST" ""
456+
$BIN/v-change-sys-config-value "SERVER_SMTP_PORT" ""
457457
fi
458458

459459
if [[ -z $(check_key_exists 'SERVER_SMTP_HOST') ]]; then
@@ -575,3 +575,40 @@ function syshealth_repair_system_cronjobs() {
575575
$BIN/v-add-cron-job 'admin' '*/5' '*' '*' '*' '*' "sudo $BIN/v-update-sys-rrd" '' 'no'
576576
$BIN/v-restart-cron
577577
}
578+
579+
# Adapt Port Listing in HESTIA NGINX Backend
580+
# Activates or deactivates port listing on IPV4 or/and IPV6 network interfaces
581+
function syshealth_adapt_hestia_nginx_listen_ports() {
582+
# Detect "physical" NICs only (virtual NICs created by Docker, WireGuard etc. are excluded)
583+
physical_nics="$(ip -d -j link show | jq -r '.[] | if .link_type == "loopback" // .linkinfo.info_kind then empty else .ifname end')"
584+
if [ -z "$physical_nics" ]; then
585+
physical_nics="$(ip -d -j link show | jq -r '.[] | if .link_type == "loopback" then empty else .ifname end')"
586+
fi
587+
for nic in $physical_nics; do
588+
if [ -z "$ipv4_scope_global" ]; then
589+
ipv4_scope_global="$(ip -4 -d -j addr show "$nic" | jq -r '.[] | select(length > 0) | .addr_info[] | if .scope == "global" then .local else empty end')"
590+
fi
591+
if [ -z "$ipv6_scope_global" ]; then
592+
ipv6_scope_global="$(ip -6 -d -j addr show "$nic" | jq -r '.[] | select(length > 0) | .addr_info[] | if .scope == "global" then .local else empty end')"
593+
fi
594+
done
595+
596+
# Adapt port listing in nginx.conf depended on availability of IPV4 and IPV6 network interface
597+
NGINX_BCONF_CHANGED=""
598+
NGINX_BCONF="/usr/local/hestia/nginx/conf/nginx.conf"
599+
NGINX_BCONF_TEMP="/tmp/nginx.conf"
600+
cp "$NGINX_BCONF" "$NGINX_BCONF_TEMP"
601+
if [ -z "$ipv4_scope_global" ]; then
602+
sed -i 's/^\([ \t]*listen[ \t]*[0-9]\{1,5\}.*\)/#\1/' "$NGINX_BCONF"
603+
else
604+
sed -i 's/#\([ \t]*listen[ \t]*[0-9]\{1,5\}.*\)/\1/' "$NGINX_BCONF"
605+
fi
606+
if [ -z "$ipv6_scope_global" ]; then
607+
sed -i 's/^\([ \t]*listen[ \t]*\[\:\:\]\:[0-9]\{1,5\}.*\)/#\1/' "$NGINX_BCONF"
608+
else
609+
sed -i 's/#\([ \t]*listen[ \t]*\[\:\:\]\:[0-9]\{1,5\}.*\)/\1/' "$NGINX_BCONF"
610+
fi
611+
cmp --silent "$NGINX_BCONF" "$NGINX_BCONF_TEMP"
612+
[ $? -ne 0 ] && NGINX_BCONF_CHANGED="yes"
613+
rm -f "$NGINX_BCONF_TEMP" > /dev/null 2>&1
614+
}

src/deb/nginx/hestia

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,6 @@ set -e
3131

3232
. /etc/profile.d/hestia.sh
3333

34-
adapt_nginx_config() {
35-
# Detect "physical" NICs only (virtual NICs created by Docker, WireGuard etc. are excluded)
36-
physical_nics="$(ip -d -j link show | jq -r '.[] | if .link_type == "loopback" // .linkinfo.info_kind then empty else .ifname end')"
37-
if [ -z "$physical_nics" ]; then
38-
physical_nics="$(ip -d -j link show | jq -r '.[] | if .link_type == "loopback" then empty else .ifname end')"
39-
fi
40-
for nic in $physical_nics; do
41-
if [ -z "$ipv4_scope_global" ]; then
42-
ipv4_scope_global="$(ip -4 -d -j addr show "$nic" | jq -r '.[] | select(length > 0) | .addr_info[] | if .scope == "global" then .local else empty end')"
43-
fi
44-
if [ -z "$ipv6_scope_global" ]; then
45-
ipv6_scope_global="$(ip -6 -d -j addr show "$nic" | jq -r '.[] | select(length > 0) | .addr_info[] | if .scope == "global" then .local else empty end')"
46-
fi
47-
done
48-
49-
if [ -n "$ipv4_scope_global" ]; then
50-
sed -i 's/#IPV4\([ \t]*listen[ \t]*[0-9]\{1,5\}.*\)/\1/' "$NGINX_CONF"
51-
else
52-
sed -i 's/^\([ \t]*listen[ \t]*[0-9]\{1,5\}.*\)/#IPV4\1/' "$NGINX_CONF"
53-
fi
54-
if [ -n "$ipv6_scope_global" ]; then
55-
sed -i 's/#IPV6\([ \t]*listen[ \t]*\[\:\:\]\:[0-9]\{1,5\}.*\)/\1/' "$NGINX_CONF"
56-
else
57-
sed -i 's/^\([ \t]*listen[ \t]*\[\:\:\]\:[0-9]\{1,5\}.*\)/#IPV6\1/' "$NGINX_CONF"
58-
fi
59-
}
60-
6134
update_nginx_resolver() {
6235
if grep -qw "1.0.0.1 8.8.4.4 1.1.1.1 8.8.8.8" "$NGINX_CONF"; then
6336
for nameserver in $(grep -is '^nameserver' /etc/resolv.conf | cut -d' ' -f2 | tr '\r\n' ' ' | xargs); do
@@ -77,7 +50,6 @@ update_nginx_resolver() {
7750
}
7851

7952
start_nginx() {
80-
#adapt_nginx_config
8153
update_nginx_resolver
8254
start-stop-daemon --start --quiet --pidfile $NGINX_PID \
8355
--retry 5 --exec $NGINX_DAEMON --oknodo

src/deb/nginx/nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ http {
116116
# Vhost
117117
server {
118118
listen 8083 ssl;
119+
listen [::]:8083 ssl;
119120
server_name _;
120121
root /usr/local/hestia/web;
121122
# Fix error "The plain HTTP request was sent to HTTPS port"

src/deb/nginx/postinst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ fi
1010
update-rc.d hestia defaults > /dev/null
1111
invoke-rc.d hestia start || true
1212

13+
# Adapt Port Listing in HESTIA NGINX Backend
14+
source /usr/local/hestia/func/syshealth.sh # Load syshealth functions
15+
syshealth_adapt_hestia_nginx_listen_ports # Adapt port listing
16+
1317
# Run triggers only on updates
1418
if [ ! -e "/usr/local/hestia/data/users/admin" ]; then
1519
exit
@@ -37,7 +41,7 @@ source /usr/local/hestia/conf/hestia.conf
3741
source /usr/local/hestia/install/upgrade/upgrade.conf
3842

3943
# Reset backend port
40-
if [ ! -z "$BACKEND_PORT" ]; then
44+
if [ -n "$BACKEND_PORT" ]; then
4145
/usr/local/hestia/bin/v-change-sys-port $BACKEND_PORT
4246
fi
4347

0 commit comments

Comments
 (0)