|
|
@@ -1,31 +1,37 @@
|
|
|
#!/bin/bash
|
|
|
-# info: add system ip address
|
|
|
+# info: add system IP address
|
|
|
# options: IP NETMASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
|
|
|
#
|
|
|
-# example: v-add-sys-ip 216.239.32.21 255.255.255.0
|
|
|
+# example: v-add-sys-ip 203.0.113.1 255.255.255.0
|
|
|
#
|
|
|
-# This function adds ip address into a system. It also creates rc scripts. You
|
|
|
-# can specify ip name which will be used as root domain for temporary aliases.
|
|
|
+# This function adds IP address into a system. It also creates rc scripts. You
|
|
|
+# can specify IP name which will be used as root domain for temporary aliases.
|
|
|
# For example, if you set a1.myhosting.com as name, each new domain created on
|
|
|
-# this ip will automatically receive alias $domain.a1.myhosting.com. Of course
|
|
|
-# you must have wildcard record *.a1.myhosting.com pointed to ip. This feature
|
|
|
+# this IP will automatically receive alias $domain.a1.myhosting.com. Of course
|
|
|
+# you must have wildcard record *.a1.myhosting.com pointed to IP. This feature
|
|
|
# is very handy when customer wants to test domain before dns migration.
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
|
# Variables & Functions #
|
|
|
#----------------------------------------------------------#
|
|
|
|
|
|
+# Argument definition
|
|
|
+ip="${1// /}"
|
|
|
+netmask="$2"
|
|
|
+
|
|
|
# Get interface name
|
|
|
-iface=$(/bin/ip token | awk -F 'dev ' '{print $2}')
|
|
|
+# First try to detect which interface the IP address resides on
|
|
|
+iface="$(ip -d -j addr show | jq --arg IP "$ip" -r '.[] | if .addr_info[].local == $IP then .ifname else empty end')"
|
|
|
+# If that fails, detect the default interface as a fallback
|
|
|
+if [ -z "$iface" ]; then
|
|
|
+ iface="$(ip -d -j route show | jq -r '.[] | if .dst == "default" then .dev else empty end')"
|
|
|
+fi
|
|
|
|
|
|
-# Argument definition
|
|
|
-ip=${1// /}
|
|
|
-netmask=$2
|
|
|
iface="${3-$iface}"
|
|
|
user="${4-admin}"
|
|
|
ip_status="${5-shared}"
|
|
|
-ip_name=$6
|
|
|
-nat_ip=$7
|
|
|
+ip_name="$6"
|
|
|
+nat_ip="$7"
|
|
|
|
|
|
# Includes
|
|
|
# shellcheck source=/etc/hestiacp/hestia.conf
|
|
|
@@ -65,31 +71,26 @@ check_hestia_demo_mode
|
|
|
# Action #
|
|
|
#----------------------------------------------------------#
|
|
|
|
|
|
-cidr=$(convert_netmask $netmask)
|
|
|
-broadcast=$(get_broadcast $ip $netmask)
|
|
|
+cidr="$(convert_netmask "$netmask")"
|
|
|
+broadcast="$(get_broadcast "$ip" "$netmask")"
|
|
|
|
|
|
-sys_ip_check=$(/sbin/ip addr | grep "$ip")
|
|
|
+sys_ip_check="$(ip addr | grep -w "$ip")"
|
|
|
if [ -z "$sys_ip_check" ]; then
|
|
|
- # Adding sys ip
|
|
|
- /sbin/ip addr add $ip/$cidr dev $iface \
|
|
|
- broadcast $broadcast label $iface
|
|
|
+ # Adding system IP
|
|
|
+ ip addr add "$ip/$cidr" dev "$iface" broadcast "$broadcast" label "$iface"
|
|
|
|
|
|
# Check if netplan is in use and generate configuration file
|
|
|
- if [ ! -z $(which netplan) ]; then
|
|
|
- if [ ! -z "$(netplan generate --mapping "$iface" | grep networkd)" ]; then
|
|
|
- netplan=1
|
|
|
- else
|
|
|
- netplan=0
|
|
|
- fi
|
|
|
+ if [ -n "$(netplan generate --mapping "$iface" 2> /dev/null | grep networkd)" ]; then
|
|
|
+ netplan="true"
|
|
|
else
|
|
|
- netplan=0
|
|
|
+ netplan="false"
|
|
|
fi
|
|
|
|
|
|
- if [ "$netplan" == "1" ]; then
|
|
|
+ if [ "$netplan" = "true" ]; then
|
|
|
if [ -f "/etc/netplan/60-hestia.yaml" ]; then
|
|
|
sys_ip=" - $ip/$cidr"
|
|
|
else
|
|
|
- sys_ip="# Added by hestia, please do not edit the file manually!"
|
|
|
+ sys_ip="# Added by Hestia, please do not edit the file manually!"
|
|
|
sys_ip="$sys_ip\nnetwork:"
|
|
|
sys_ip="$sys_ip\n version: 2"
|
|
|
sys_ip="$sys_ip\n renderer: networkd"
|
|
|
@@ -99,7 +100,7 @@ if [ -z "$sys_ip_check" ]; then
|
|
|
sys_ip="$sys_ip\n - $ip/$cidr"
|
|
|
fi
|
|
|
IFS='%'
|
|
|
- echo -e $sys_ip >> /etc/netplan/60-hestia.yaml
|
|
|
+ echo -e "$sys_ip" >> /etc/netplan/60-hestia.yaml
|
|
|
unset IFS
|
|
|
else
|
|
|
sys_ip="\n# Added by Hestia Control Panel"
|
|
|
@@ -112,11 +113,9 @@ if [ -z "$sys_ip_check" ]; then
|
|
|
fi
|
|
|
|
|
|
# Generating timestamp
|
|
|
-time_n_date=$(date +'%T %F')
|
|
|
-time=$(echo "$time_n_date" | cut -f 1 -d \ )
|
|
|
-date=$(echo "$time_n_date" | cut -f 2 -d \ )
|
|
|
+new_timestamp
|
|
|
|
|
|
-# Adding hestia ip
|
|
|
+# Adding Hestia IP
|
|
|
echo "OWNER='$user'
|
|
|
STATUS='$ip_status'
|
|
|
NAME='$ip_name'
|
|
|
@@ -130,30 +129,30 @@ DATE='$date'" > $HESTIA/data/ips/$ip
|
|
|
chmod 660 $HESTIA/data/ips/$ip
|
|
|
|
|
|
# WEB support
|
|
|
-if [ ! -z "$WEB_SYSTEM" ]; then
|
|
|
+if [ -n "$WEB_SYSTEM" ]; then
|
|
|
web_conf="/etc/$WEB_SYSTEM/conf.d/$ip.conf"
|
|
|
- rm -f $web_conf
|
|
|
+ rm -f "$web_conf"
|
|
|
|
|
|
if [ "$WEB_SYSTEM" = 'httpd' ] || [ "$WEB_SYSTEM" = 'apache2' ]; then
|
|
|
if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
|
|
|
- echo "NameVirtualHost $ip:$WEB_PORT" > $web_conf
|
|
|
+ echo "NameVirtualHost $ip:$WEB_PORT" > "$web_conf"
|
|
|
fi
|
|
|
- echo "Listen $ip:$WEB_PORT" >> $web_conf
|
|
|
- cat $HESTIA_INSTALL_DIR/apache2/unassigned.conf >> $web_conf
|
|
|
- sed -i 's/directIP/'$ip'/g' $web_conf
|
|
|
- sed -i 's/directPORT/'$WEB_PORT'/g' $web_conf
|
|
|
+ echo "Listen $ip:$WEB_PORT" >> "$web_conf"
|
|
|
+ cat $HESTIA_INSTALL_DIR/apache2/unassigned.conf >> "$web_conf"
|
|
|
+ sed -i 's/directIP/'$ip'/g' "$web_conf"
|
|
|
+ sed -i 's/directPORT/'$WEB_PORT'/g' "$web_conf"
|
|
|
|
|
|
elif [ "$WEB_SYSTEM" = 'nginx' ]; then
|
|
|
- cp -f $HESTIA_INSTALL_DIR/nginx/unassigned.inc $web_conf
|
|
|
- sed -i 's/directIP/'$ip'/g' $web_conf
|
|
|
+ cp -f $HESTIA_INSTALL_DIR/nginx/unassigned.inc "$web_conf"
|
|
|
+ sed -i 's/directIP/'$ip'/g' "$web_conf"
|
|
|
fi
|
|
|
|
|
|
if [ "$WEB_SSL" = 'mod_ssl' ]; then
|
|
|
if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
|
|
|
- sed -i "1s/^/NameVirtualHost $ip:$WEB_SSL_PORT\n/" $web_conf
|
|
|
+ sed -i "1s/^/NameVirtualHost $ip:$WEB_SSL_PORT\n/" "$web_conf"
|
|
|
fi
|
|
|
- sed -i "1s/^/Listen $ip:$WEB_SSL_PORT\n/" $web_conf
|
|
|
- sed -i 's/directSSLPORT/'$WEB_SSL_PORT'/g' $web_conf
|
|
|
+ sed -i "1s/^/Listen $ip:$WEB_SSL_PORT\n/" "$web_conf"
|
|
|
+ sed -i 's/directSSLPORT/'$WEB_SSL_PORT'/g' "$web_conf"
|
|
|
fi
|
|
|
fi
|
|
|
|
|
|
@@ -169,24 +168,24 @@ if [ -n "$PROXY_SYSTEM" ]; then
|
|
|
# mod_extract_forwarded
|
|
|
fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
|
|
|
if [ -e "$fw_conf" ]; then
|
|
|
- ips=$(grep 'MEFaccept ' $fw_conf | grep -v '#' | head -n1)
|
|
|
- sed -i "s/$ips/$ips $ip/g" $fw_conf
|
|
|
+ ips=$(grep 'MEFaccept ' "$fw_conf" | grep -v '#' | head -n1)
|
|
|
+ sed -i "s/$ips/$ips $ip/g" "$fw_conf"
|
|
|
fi
|
|
|
|
|
|
# mod_rpaf
|
|
|
rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
|
|
|
if [ -e "$rpaf_conf" ]; then
|
|
|
- rpaf_str=$(grep RPAFproxy_ips $rpaf_conf)
|
|
|
- [ -z "$rpaf_str" ] && sed -i 's|</IfModule>|RPAFproxy_ips\n</IfModule>|' $rpaf_conf && rpaf_str='RPAFproxy_ips'
|
|
|
+ rpaf_str="$(grep RPAFproxy_ips "$rpaf_conf")"
|
|
|
+ [ -z "$rpaf_str" ] && sed -i 's|</IfModule>|RPAFproxy_ips\n</IfModule>|' "$rpaf_conf" && rpaf_str='RPAFproxy_ips'
|
|
|
rpaf_str="$rpaf_str $ip"
|
|
|
- sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" $rpaf_conf
|
|
|
+ sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" "$rpaf_conf"
|
|
|
fi
|
|
|
|
|
|
- #mod_remoteip
|
|
|
+ # mod_remoteip
|
|
|
remoteip_conf="/etc/$WEB_SYSTEM/mods-enabled/remoteip.conf"
|
|
|
if [ -e "$remoteip_conf" ]; then
|
|
|
- if [ $(grep -ic "$ip" $remoteip_conf) -eq 0 ]; then
|
|
|
- sed -i "s/<\/IfModule>/RemoteIPInternalProxy $ip\n<\/IfModule>/g" $remoteip_conf
|
|
|
+ if [ "$(grep -ic "$ip" "$remoteip_conf")" -eq "0" ]; then
|
|
|
+ sed -i "s/<\/IfModule>/RemoteIPInternalProxy $ip\n<\/IfModule>/g" "$remoteip_conf"
|
|
|
fi
|
|
|
fi
|
|
|
fi
|