Skip to content

Commit 5581300

Browse files
authored
Refactor and fixes for handling system IP/Interfaces (hestiacp#3605)
* Refactor and fixes for handling system IP/Interfaces * Minor updates
1 parent 04e3297 commit 5581300

17 files changed

+291
-239
lines changed

bin/v-add-sys-ip

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
#!/bin/bash
2-
# info: add system ip address
2+
# info: add system IP address
33
# options: IP NETMASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
44
#
5-
# example: v-add-sys-ip 216.239.32.21 255.255.255.0
5+
# example: v-add-sys-ip 203.0.113.1 255.255.255.0
66
#
7-
# This function adds ip address into a system. It also creates rc scripts. You
8-
# can specify ip name which will be used as root domain for temporary aliases.
7+
# This function adds IP address into a system. It also creates rc scripts. You
8+
# can specify IP name which will be used as root domain for temporary aliases.
99
# For example, if you set a1.myhosting.com as name, each new domain created on
10-
# this ip will automatically receive alias $domain.a1.myhosting.com. Of course
11-
# you must have wildcard record *.a1.myhosting.com pointed to ip. This feature
10+
# this IP will automatically receive alias $domain.a1.myhosting.com. Of course
11+
# you must have wildcard record *.a1.myhosting.com pointed to IP. This feature
1212
# is very handy when customer wants to test domain before dns migration.
1313

1414
#----------------------------------------------------------#
1515
# Variables & Functions #
1616
#----------------------------------------------------------#
1717

18+
# Argument definition
19+
ip="${1// /}"
20+
netmask="$2"
21+
1822
# Get interface name
19-
iface=$(/bin/ip token | awk -F 'dev ' '{print $2}')
23+
# First try to detect which interface the IP address resides on
24+
iface="$(ip -d -j addr show | jq --arg IP "$ip" -r '.[] | if .addr_info[].local == $IP then .ifname else empty end')"
25+
# If that fails, detect the default interface as a fallback
26+
if [ -z "$iface" ]; then
27+
iface="$(ip -d -j route show | jq -r '.[] | if .dst == "default" then .dev else empty end')"
28+
fi
2029

21-
# Argument definition
22-
ip=${1// /}
23-
netmask=$2
2430
iface="${3-$iface}"
2531
user="${4-admin}"
2632
ip_status="${5-shared}"
27-
ip_name=$6
28-
nat_ip=$7
33+
ip_name="$6"
34+
nat_ip="$7"
2935

3036
# Includes
3137
# shellcheck source=/etc/hestiacp/hestia.conf
@@ -65,31 +71,26 @@ check_hestia_demo_mode
6571
# Action #
6672
#----------------------------------------------------------#
6773

68-
cidr=$(convert_netmask $netmask)
69-
broadcast=$(get_broadcast $ip $netmask)
74+
cidr="$(convert_netmask "$netmask")"
75+
broadcast="$(get_broadcast "$ip" "$netmask")"
7076

71-
sys_ip_check=$(/sbin/ip addr | grep "$ip")
77+
sys_ip_check="$(ip addr | grep -w "$ip")"
7278
if [ -z "$sys_ip_check" ]; then
73-
# Adding sys ip
74-
/sbin/ip addr add $ip/$cidr dev $iface \
75-
broadcast $broadcast label $iface
79+
# Adding system IP
80+
ip addr add "$ip/$cidr" dev "$iface" broadcast "$broadcast" label "$iface"
7681

7782
# Check if netplan is in use and generate configuration file
78-
if [ ! -z $(which netplan) ]; then
79-
if [ ! -z "$(netplan generate --mapping "$iface" | grep networkd)" ]; then
80-
netplan=1
81-
else
82-
netplan=0
83-
fi
83+
if [ -n "$(netplan generate --mapping "$iface" 2> /dev/null | grep networkd)" ]; then
84+
netplan="true"
8485
else
85-
netplan=0
86+
netplan="false"
8687
fi
8788

88-
if [ "$netplan" == "1" ]; then
89+
if [ "$netplan" = "true" ]; then
8990
if [ -f "/etc/netplan/60-hestia.yaml" ]; then
9091
sys_ip=" - $ip/$cidr"
9192
else
92-
sys_ip="# Added by hestia, please do not edit the file manually!"
93+
sys_ip="# Added by Hestia, please do not edit the file manually!"
9394
sys_ip="$sys_ip\nnetwork:"
9495
sys_ip="$sys_ip\n version: 2"
9596
sys_ip="$sys_ip\n renderer: networkd"
@@ -99,7 +100,7 @@ if [ -z "$sys_ip_check" ]; then
99100
sys_ip="$sys_ip\n - $ip/$cidr"
100101
fi
101102
IFS='%'
102-
echo -e $sys_ip >> /etc/netplan/60-hestia.yaml
103+
echo -e "$sys_ip" >> /etc/netplan/60-hestia.yaml
103104
unset IFS
104105
else
105106
sys_ip="\n# Added by Hestia Control Panel"
@@ -112,11 +113,9 @@ if [ -z "$sys_ip_check" ]; then
112113
fi
113114

114115
# Generating timestamp
115-
time_n_date=$(date +'%T %F')
116-
time=$(echo "$time_n_date" | cut -f 1 -d \ )
117-
date=$(echo "$time_n_date" | cut -f 2 -d \ )
116+
new_timestamp
118117

119-
# Adding hestia ip
118+
# Adding Hestia IP
120119
echo "OWNER='$user'
121120
STATUS='$ip_status'
122121
NAME='$ip_name'
@@ -130,30 +129,30 @@ DATE='$date'" > $HESTIA/data/ips/$ip
130129
chmod 660 $HESTIA/data/ips/$ip
131130

132131
# WEB support
133-
if [ ! -z "$WEB_SYSTEM" ]; then
132+
if [ -n "$WEB_SYSTEM" ]; then
134133
web_conf="/etc/$WEB_SYSTEM/conf.d/$ip.conf"
135-
rm -f $web_conf
134+
rm -f "$web_conf"
136135

137136
if [ "$WEB_SYSTEM" = 'httpd' ] || [ "$WEB_SYSTEM" = 'apache2' ]; then
138137
if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
139-
echo "NameVirtualHost $ip:$WEB_PORT" > $web_conf
138+
echo "NameVirtualHost $ip:$WEB_PORT" > "$web_conf"
140139
fi
141-
echo "Listen $ip:$WEB_PORT" >> $web_conf
142-
cat $HESTIA_INSTALL_DIR/apache2/unassigned.conf >> $web_conf
143-
sed -i 's/directIP/'$ip'/g' $web_conf
144-
sed -i 's/directPORT/'$WEB_PORT'/g' $web_conf
140+
echo "Listen $ip:$WEB_PORT" >> "$web_conf"
141+
cat $HESTIA_INSTALL_DIR/apache2/unassigned.conf >> "$web_conf"
142+
sed -i 's/directIP/'$ip'/g' "$web_conf"
143+
sed -i 's/directPORT/'$WEB_PORT'/g' "$web_conf"
145144

146145
elif [ "$WEB_SYSTEM" = 'nginx' ]; then
147-
cp -f $HESTIA_INSTALL_DIR/nginx/unassigned.inc $web_conf
148-
sed -i 's/directIP/'$ip'/g' $web_conf
146+
cp -f $HESTIA_INSTALL_DIR/nginx/unassigned.inc "$web_conf"
147+
sed -i 's/directIP/'$ip'/g' "$web_conf"
149148
fi
150149

151150
if [ "$WEB_SSL" = 'mod_ssl' ]; then
152151
if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
153-
sed -i "1s/^/NameVirtualHost $ip:$WEB_SSL_PORT\n/" $web_conf
152+
sed -i "1s/^/NameVirtualHost $ip:$WEB_SSL_PORT\n/" "$web_conf"
154153
fi
155-
sed -i "1s/^/Listen $ip:$WEB_SSL_PORT\n/" $web_conf
156-
sed -i 's/directSSLPORT/'$WEB_SSL_PORT'/g' $web_conf
154+
sed -i "1s/^/Listen $ip:$WEB_SSL_PORT\n/" "$web_conf"
155+
sed -i 's/directSSLPORT/'$WEB_SSL_PORT'/g' "$web_conf"
157156
fi
158157
fi
159158

@@ -169,24 +168,24 @@ if [ -n "$PROXY_SYSTEM" ]; then
169168
# mod_extract_forwarded
170169
fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
171170
if [ -e "$fw_conf" ]; then
172-
ips=$(grep 'MEFaccept ' $fw_conf | grep -v '#' | head -n1)
173-
sed -i "s/$ips/$ips $ip/g" $fw_conf
171+
ips=$(grep 'MEFaccept ' "$fw_conf" | grep -v '#' | head -n1)
172+
sed -i "s/$ips/$ips $ip/g" "$fw_conf"
174173
fi
175174

176175
# mod_rpaf
177176
rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
178177
if [ -e "$rpaf_conf" ]; then
179-
rpaf_str=$(grep RPAFproxy_ips $rpaf_conf)
180-
[ -z "$rpaf_str" ] && sed -i 's|</IfModule>|RPAFproxy_ips\n</IfModule>|' $rpaf_conf && rpaf_str='RPAFproxy_ips'
178+
rpaf_str="$(grep RPAFproxy_ips "$rpaf_conf")"
179+
[ -z "$rpaf_str" ] && sed -i 's|</IfModule>|RPAFproxy_ips\n</IfModule>|' "$rpaf_conf" && rpaf_str='RPAFproxy_ips'
181180
rpaf_str="$rpaf_str $ip"
182-
sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" $rpaf_conf
181+
sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" "$rpaf_conf"
183182
fi
184183

185-
#mod_remoteip
184+
# mod_remoteip
186185
remoteip_conf="/etc/$WEB_SYSTEM/mods-enabled/remoteip.conf"
187186
if [ -e "$remoteip_conf" ]; then
188-
if [ $(grep -ic "$ip" $remoteip_conf) -eq 0 ]; then
189-
sed -i "s/<\/IfModule>/RemoteIPInternalProxy $ip\n<\/IfModule>/g" $remoteip_conf
187+
if [ "$(grep -ic "$ip" "$remoteip_conf")" -eq "0" ]; then
188+
sed -i "s/<\/IfModule>/RemoteIPInternalProxy $ip\n<\/IfModule>/g" "$remoteip_conf"
190189
fi
191190
fi
192191
fi

bin/v-change-sys-ip-name

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/bin/bash
2-
# info: change ip name
2+
# info: change IP name
33
# options: IP NAME
44
#
5-
# example: v-change-sys-ip-name 80.122.52.70 acme.com
5+
# example: v-change-sys-ip-name 203.0.113.1 acme.com
66
#
7-
# This function for changing dns domain associated with ip.
7+
# This function for changing dns domain associated with IP.
88

99
#----------------------------------------------------------#
1010
# Variables & Functions #
1111
#----------------------------------------------------------#
1212

1313
# Argument definition
14-
ip=$1
15-
ip_name=$2
14+
ip="$1"
15+
ip_name="$2"
1616

1717
# Includes
1818
# shellcheck source=/etc/hestiacp/hestia.conf
@@ -40,7 +40,7 @@ check_hestia_demo_mode
4040
# Action #
4141
#----------------------------------------------------------#
4242

43-
# Changing ip name
43+
# Changing IP name
4444
update_ip_value '$NAME' "$ip_name"
4545

4646
#----------------------------------------------------------#

bin/v-change-sys-ip-nat

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/bin/bash
2-
# info: change ip nat address
2+
# info: change NAT IP address
33
# options: IP NAT_IP [RESTART]
44
#
5-
# example: v-change-sys-ip-nat 185.209.50.140 10.110.104.205
5+
# example: v-change-sys-ip-nat 10.0.0.1 203.0.113.1
66
#
7-
# This function for changing nat ip associated with ip.
7+
# This function for changing NAT IP associated with IP.
88

99
#----------------------------------------------------------#
1010
# Variables & Functions #
1111
#----------------------------------------------------------#
1212

1313
# Argument definition
14-
ip=$1
15-
nat_ip=$2
16-
restart=$3
14+
ip="$1"
15+
nat_ip="$2"
16+
restart="$3"
1717

1818
# Includes
1919
# shellcheck source=/etc/hestiacp/hestia.conf
@@ -45,59 +45,59 @@ check_hestia_demo_mode
4545
if [ -z "$(grep NAT= $HESTIA/data/ips/$ip)" ]; then
4646
sed -i "s/^TIME/NAT='$nat_ip'\nTIME/g" $HESTIA/data/ips/$ip
4747
old=''
48-
new=$nat_ip
48+
new="$nat_ip"
4949
else
50-
old=$(get_ip_value '$NAT')
51-
new=$nat_ip
50+
old="$(get_ip_value '$NAT')"
51+
new="$nat_ip"
5252
sed -i "s/NAT=.*/NAT='$new'/" $HESTIA/data/ips/$ip
5353
if [ -z "$nat_ip" ]; then
54-
new=$ip
54+
new="$ip"
5555
fi
5656
fi
5757

5858
# Updating WEB configs
5959
if [ -n "$old" ] && [ -n "$WEB_SYSTEM" ]; then
6060
for user in $($BIN/v-list-sys-users plain); do
6161
sed -i "s/$old/$new/" $HESTIA/data/users/$user/web.conf
62-
$BIN/v-rebuild-web-domains $user no
62+
$BIN/v-rebuild-web-domains "$user" no
6363
done
6464
$BIN/v-restart-dns "$restart"
6565
fi
6666

6767
# Updating DNS configs
6868
if [ -n "$old" ] && [ -n "$DNS_SYSTEM" ]; then
6969
for user in $($BIN/v-list-sys-users plain); do
70-
sed -i "s/$old/$new/" $HESTIA/data/users/$user/dns.conf
71-
if ls $HESTIA/data/users/$user/dns/*.conf 1> /dev/null 2>&1; then
70+
sed -i "s/$old/$new/" "$HESTIA/data/users/$user/dns.conf"
71+
if ls $HESTIA/data/users/$user/dns/*.conf > /dev/null 2>&1; then
7272
sed -i "s/$old/$new/" $HESTIA/data/users/$user/dns/*.conf
7373
fi
74-
$BIN/v-rebuild-dns-domains $user no
74+
$BIN/v-rebuild-dns-domains "$user" no
7575
done
7676
$BIN/v-restart-dns "$restart"
7777
fi
7878

7979
# Updating FTP
8080
if [ -n "$old" ] && [ -n "$FTP_SYSTEM" ]; then
81-
conf=$(find /etc -name $FTP_SYSTEM.conf)
82-
if [ -e "$conf" ]; then
83-
sed -i "s/$old/$new/g" $conf
81+
ftp_conf="$(find /etc -maxdepth 2 -name "$FTP_SYSTEM.conf")"
82+
if [ -e "$ftp_conf" ]; then
83+
sed -i "s/$old/$new/g" "$ftp_conf"
8484
if [ "$FTP_SYSTEM" = 'vsftpd' ]; then
85-
check_pasv=$(grep pasv_address $conf)
85+
check_pasv="$(grep pasv_address "$ftp_conf")"
8686
if [ -z "$check_pasv" ] && [ -n "$nat_ip" ]; then
87-
echo "pasv_address=$nat_ip" >> $conf
87+
echo "pasv_address=$nat_ip" >> "$ftp_conf"
8888
fi
8989
if [ -n "$check_pasv" ] && [ -z "$nat_ip" ]; then
90-
sed -i "/pasv_address/d" $conf
90+
sed -i "/pasv_address/d" "$ftp_conf"
9191
fi
9292
if [ -n "$check_pasv" ] && [ -n "$nat_ip" ]; then
93-
sed -i "s/pasv_address=.*/pasv_address='$nat_ip'/g" $conf
93+
sed -i "s/pasv_address=.*/pasv_address='$nat_ip'/g" "$ftp_conf"
9494
fi
9595
fi
9696
fi
9797
if [ "$FTP_SYSTEM" = 'proftpd' ]; then
98-
conf="/etc/$FTP_SYSTEM/conf.d/external_ip.conf"
98+
ext_ip_conf="/etc/$FTP_SYSTEM/conf.d/external_ip.conf"
9999
content="MasqueradeAddress ${nat_ip}"
100-
echo "$content" > $conf
100+
echo "$content" > "$ext_ip_conf"
101101
fi
102102
$BIN/v-restart-ftp "$restart"
103103
fi

bin/v-change-sys-ip-owner

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/bin/bash
2-
# info: change ip owner
2+
# info: change IP owner
33
# options: IP USER
44
#
5-
# example: v-change-sys-ip-owner 91.198.136.14 admin
5+
# example: v-change-sys-ip-owner 203.0.113.1 admin
66
#
7-
# This function of changing ip address ownership.
7+
# This function of changing IP address ownership.
88

99
#----------------------------------------------------------#
1010
# Variables & Functions #
1111
#----------------------------------------------------------#
1212

1313
# Argument definition
14-
ip=$1
15-
user=$2
14+
ip="$1"
15+
user="$2"
1616

1717
# shellcheck source=/etc/hestiacp/hestia.conf
1818
source /etc/hestiacp/hestia.conf
@@ -44,7 +44,7 @@ check_hestia_demo_mode
4444
# Action #
4545
#----------------------------------------------------------#
4646

47-
# Changing ip owner
47+
# Changing IP owner
4848
ip_owner=$(get_ip_value '$OWNER')
4949
if [ "$ip_owner" != "$user" ]; then
5050
update_ip_value '$OWNER' "$user"
@@ -79,7 +79,7 @@ fi
7979

8080
# Set status to dedicated if owner is not admin
8181
ip_status="$(get_ip_value '$STATUS')"
82-
if [ "$user" != 'admin' ] && [ "$ip_status" == 'shared' ]; then
82+
if [ "$user" != 'admin' ] && [ "$ip_status" = 'shared' ]; then
8383
$BIN/v-change-sys-ip-status "$ip" 'dedicated'
8484
fi
8585

0 commit comments

Comments
 (0)