Browse Source

IPV6: Add web domain. Bash+PHP

asmcc 3 years ago
parent
commit
8622f9d5da
3 changed files with 56 additions and 18 deletions
  1. 26 12
      bin/v-add-web-domain
  2. 8 2
      web/add/web/index.php
  3. 22 4
      web/templates/pages/add_web.php

+ 26 - 12
bin/v-add-web-domain

@@ -1,8 +1,8 @@
 #!/bin/bash
 # info: add web domain
-# options: USER DOMAIN [IP] [RESTART] [ALIASES] [PROXY_EXTENSIONS]
+# options: USER DOMAIN [IPV4] [IPV6] [RESTART] [ALIASES] [PROXY_EXTENSIONS]
 #
-# example: v-add-web-domain admin wonderland.com 192.18.22.43 yes www.wonderland.com
+# example: v-add-web-domain admin wonderland.com 192.18.22.43 1111:2222:3333::1 yes www.wonderland.com
 #
 # This function adds virtual host to a server. In cases when ip is
 # undefined in the script, "default" template will be used. The alias of
@@ -20,9 +20,10 @@ user=$1
 domain=$2
 domain_idn=$2
 ip=$3
-restart=$4 # will be moved to the end soon
-aliases=$5
-proxy_ext=$6
+ipv6=$4
+restart=$5 # will be moved to the end soon
+aliases=$6
+proxy_ext=$7
 
 # Includes
 # shellcheck source=/etc/hestiacp/hestia.conf
@@ -33,6 +34,8 @@ source $HESTIA/func/main.sh
 source $HESTIA/func/domain.sh
 # shellcheck source=/usr/local/hestia/func/ip.sh
 source $HESTIA/func/ip.sh
+# shellcheck source=/usr/local/hestia/func/ipv6.sh
+source $HESTIA/func/ipv6.sh
 # shellcheck source=/usr/local/hestia/func/syshealth.sh
 source $HESTIA/func/syshealth.sh
 # load config file
@@ -49,8 +52,8 @@ domain_utf=$(idn2 --quiet -d "$domain_idn")
 #----------------------------------------------------------#
 
 is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
-check_args '2' "$#" 'USER DOMAIN [IP] [RESTART] [ALIASES] [PROXY_EXTENSIONS]'
-is_format_valid 'user' 'domain' 'aliases' 'ip' 'proxy_ext'
+check_args '2' "$#" 'USER DOMAIN [IPV4] [IPV6] [RESTART] [ALIASES] [PROXY_EXTENSIONS]'
+is_format_valid 'user' 'domain' 'aliases' 'ip' 'ipv6' 'proxy_ext'
 is_object_valid 'user' 'USER' "$user"
 is_object_unsuspended 'user' 'USER' "$user"
 is_package_full 'WEB_DOMAINS'
@@ -60,10 +63,10 @@ if [ "$aliases" != "none" ]; then
 	is_package_full 'WEB_ALIASES'
 fi
 
-if [ "$($BIN/v-list-web-domain $user $domain_utf plain | cut -f 1) " != "$domain" ]; then
+if [ "$($BIN/v-list-web-domain $user $domain_utf shell | sed -n '/DOMAIN/s/DOMAIN:[\t ]*//p')" != "$domain" ]; then
 	is_domain_new 'web' "$domain_utf,$aliases"
 fi
-if [ "$($BIN/v-list-web-domain $user $domain_idn plain | cut -f 1) " != "$domain" ]; then
+if [ "$($BIN/v-list-web-domain $user $domain_idn shell | sed -n '/DOMAIN/s/DOMAIN:[\t ]*//p')" != "$domain" ]; then
 	is_domain_new 'web' "$domain_idn,$aliases"
 else
 	is_domain_new 'web' "$domain,$aliases"
@@ -80,7 +83,17 @@ is_base_domain_owner "$domain,$aliases"
 
 if [ -n "$ip" ]; then
 	is_ip_valid "$ip" "$user"
-else
+fi
+
+if [ -n "$ipv6" ]; then
+	is_ipv6_valid "$ipv6" "$user"
+fi
+
+if [ -z "$ip" -a -z "$ipv6" ]; then
+	get_user_ipv6
+fi
+
+if [ -z "$ipv6" ]; then
 	get_user_ip
 fi
 
@@ -205,7 +218,8 @@ fi
 #----------------------------------------------------------#
 
 # Increasing counters
-increase_ip_value "$local_ip"
+[ -n "$ip" ] && increase_ip_value "$local_ip"
+[ -n "$ipv6" ] && increase_ipv6_value "$local_ipv6"
 increase_user_value "$user" '$U_WEB_DOMAINS'
 increase_user_value "$user" '$U_WEB_ALIASES' "$alias_number"
 
@@ -215,7 +229,7 @@ time=$(echo "$time_n_date" | cut -f 1 -d \ )
 date=$(echo "$time_n_date" | cut -f 2 -d \ )
 
 # Adding domain in web.conf
-echo "DOMAIN='$domain' IP='$ip' IP6='' CUSTOM_DOCROOT='' ALIAS='$ALIAS' TPL='$WEB_TEMPLATE'\
+echo "DOMAIN='$domain' IP='$ip' IP6='$ipv6' CUSTOM_DOCROOT='' ALIAS='$ALIAS' TPL='$WEB_TEMPLATE'\
  SSL='no' SSL_FORCE='no' SSL_HOME='same' LETSENCRYPT='no' FTP_USER='' FTP_MD5=''\
  BACKEND='$BACKEND_TEMPLATE' PROXY='$PROXY_TEMPLATE' PROXY_EXT='$PROXY_EXT'\
  STATS='' STATS_USER='' STATS_CRYPT='' U_DISK='0' U_BANDWIDTH='0'\

+ 8 - 2
web/add/web/index.php

@@ -16,8 +16,8 @@ if (!empty($_POST["ok"])) {
 	if (empty($_POST["v_domain"])) {
 		$errors[] = _("domain");
 	}
-	if (empty($_POST["v_ip"])) {
-		$errors[] = _("ip");
+	if (empty($_POST["v_ip"]) && empty($_POST["v_ipv6"])) {
+		$errors[] = _("ipv4 and ipv6");
 	}
 
 	if (!empty($errors[0])) {
@@ -37,6 +37,8 @@ if (!empty($_POST["ok"])) {
 
 	// Define domain ip address
 	$v_ip = quoteshellarg($_POST["v_ip"]);
+	// Define domain ipv6 address
+	$v_ipv6 = quoteshellarg($_POST["v_ipv6"]);
 
 	// Using public IP instead of internal IP when creating DNS
 	// Gets public IP from 'v-list-user-ips' command (that reads /hestia/data/ips/ip), precisely from 'NAT' field
@@ -78,6 +80,8 @@ if (!empty($_POST["ok"])) {
 				quoteshellarg($v_domain) .
 				" " .
 				$v_ip .
+				" " .
+				$v_ipv6 .
 				" 'yes'",
 			$output,
 			$return_var,
@@ -103,6 +107,8 @@ if (!empty($_POST["ok"])) {
 				quoteshellarg($v_domain) .
 				" " .
 				$v_public_ip .
+				" " .
+				$v_ipv6 .
 				" '' '' '' '' '' '' '' '' 'no'",
 			$output,
 			$return_var,

+ 22 - 4
web/templates/pages/add_web.php

@@ -44,13 +44,31 @@
 					<input type="text" class="form-control" name="v_domain" id="v_domain" value="<?= htmlentities(trim($v_domain, "'")) ?>">
 				</div>
 				<div class="u-mb20">
-					<label for="v_ip" class="form-label"><?= _("IP Address") ?></label>
+					<label for="v_ip" class="form-label"><?= _("IPV4 Address") ?></label>
 					<select class="form-select" name="v_ip" id="v_ip">
+					<option value="">clear</option>
 						<?php
 							foreach ($ips as $ip => $value) {
-								$display_ip = htmlentities(empty($value['NAT']) ? $ip : "{$value['NAT']}");
-								$ip_selected = (!empty($v_ip) && $ip == $_POST['v_ip']) ? 'selected' : '';
-								echo "\t\t\t\t<option value=\"{$ip}\" {$ip_selected}>{$display_ip}</option>\n";
+								if ($value['VERSION']==4) {
+									$display_ip = htmlentities(empty($value['NAT']) ? $ip : "{$value['NAT']}");
+									$ip_selected = (!empty($v_ip) && $ip == $_POST['v_ip']) ? 'selected' : '';
+									echo "\t\t\t\t<option value=\"{$ip}\" {$ip_selected}>{$display_ip}</option>\n";
+								}
+							}
+						?>
+					</select>
+				</div>
+				<div class="u-mb20">
+					<label for="v_ipv6" class="form-label"><?= _("IPV6 Address") ?></label>
+					<select class="form-select" name="v_ipv6" id="v_ipv6">
+					<option value="">clear</option>
+						<?php
+							foreach ($ips as $ipv6 => $value) {
+								if ($value['VERSION']==6) {
+									$display_ipv6 = $ipv6;
+									$ipv6_selected = (!empty($v_ipv6) && $ipv6 == $_POST['v_ipv6']) ? 'selected' : '';
+									echo "\t\t\t\t<option value=\"{$ipv6}\" {$ipv6_selected}>{$display_ipv6}</option>\n";
+								}
 							}
 						?>
 					</select>