fix(yeswiki-installer) : replace DNS lookup with ip pings

This commit is contained in:
mrflos 2023-04-21 10:25:37 +03:00
parent 2dbc525925
commit 7cd4c7de9f
2 changed files with 16 additions and 13 deletions

View file

@ -2,27 +2,30 @@
require 'vendor/autoload.php';
function checkDNS($domain, $withWww = false, $noip6 = false)
function checkIP($domain, $withWww = false, $noip6 = false)
{
if (!preg_match('/(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/', strtolower($domain))) {
throw new Exception('not valid domain : "'.$domain.'".');
}
$currentip = dns_get_record($domain, DNS_A + DNS_AAAA);
$output = shell_exec('ping -c1 -4 '.$domain);
preg_match_all('/PING.*\(.*((\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}).*\)/m', $output, $matches, PREG_SET_ORDER, 0);
$currentip = $matches[0][1] ?? null;
if (empty($currentip)) {
throw new Exception('the domain '.$domain.' was not found in DNS record.');
throw new Exception('the domain '.$domain.' has no ip v4.');
}
if (empty($currentip[0]['ip'])) {
throw new Exception('the domain '.$domain.' has no ip v4 in DNS record.');
}
if ($currentip[0]['ip'] !== $_SERVER['ip4']) {
throw new Exception('the current ip v4 address of '.$domain.' is '.$currentip[0]['ip'].'. it should be '.$_SERVER['ip4']);
if ($currentip !== $_SERVER['ip4']) {
throw new Exception('the current ip v4 address of '.$domain.' is '.$currentip.'. it should be '.$_SERVER['ip4']);
}
if (!$noip6) {
if (empty($currentip[0]['ipv6'])) {
throw new Exception('the domain '.$domain.' has no ip v6 in DNS record.');
$output = shell_exec('ping -c1 -6 '.$domain);
preg_match_all('/PING.*\((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\)/m', $output, $matches, PREG_SET_ORDER, 0);
var_dump($output,$matches);
$currentip6 = $matches[0][1] ?? null;
if (empty($currentip6)) {
throw new Exception('the domain '.$domain.' has no ip v6.');
}
if ($currentip[1]['ipv6'] !== $_SERVER['ip6']) {
throw new Exception('the current ip v6 address of '.$domain.' is '.$currentip[1]['ipv6'].'. it should be '.$_SERVER['ip6']);
if ($currentip6 !== $_SERVER['ip6']) {
throw new Exception('the current ip v6 address of '.$domain.' is '.$currentip6.'. it should be '.$_SERVER['ip6']);
}
}
if ($withWww) {

View file

@ -84,7 +84,7 @@ if (0 == posix_getuid()) {
//$isFullDomain = !preg_match('/.'.$_SERVER['maindomain'].'$/isU', $domain, $matches, PREG_OFFSET_CAPTURE, 0);
$isFullDomain = !preg_match('/^([a-zA-Z0-9]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9])\.)([a-zA-Z0-9]{1,2}([-a-zA-Z0-9]{0,252}[a-zA-Z0-9])?)\.([a-zA-Z]{2,63})$/isU', $domain, $matches, PREG_OFFSET_CAPTURE, 0);
if (!$nossl) {
checkDNS($domain, $isFullDomain, $noip6);
checkIP($domain, $isFullDomain, $noip6);
}
checkIfInstalled($domain);
$needHerse = checkHerse($herseUser, $hersePass);