Stabilize discovery workflow
This commit is contained in:
@@ -78,17 +78,44 @@ class NetworkRangeController
|
||||
|
||||
private function isValidCidr(string $cidr): bool
|
||||
{
|
||||
if (!str_contains($cidr, '/')) {
|
||||
if (!preg_match('#^(\d+\.\d+\.\d+\.\d+)/(\d{1,2})$#', $cidr, $matches)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
[$ip, $mask] = explode('/', $cidr, 2);
|
||||
$ip = $matches[1];
|
||||
$maskInt = (int)$matches[2];
|
||||
|
||||
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$maskInt = (int)$mask;
|
||||
return $maskInt >= 8 && $maskInt <= 32;
|
||||
if ($maskInt < 8 || $maskInt > 32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$long = ip2long($ip);
|
||||
if ($long === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$value = (int)sprintf('%u', $long);
|
||||
return $this->isInRange($value, '10.0.0.0', 8)
|
||||
|| $this->isInRange($value, '172.16.0.0', 12)
|
||||
|| $this->isInRange($value, '192.168.0.0', 16)
|
||||
|| $this->isInRange($value, '169.254.0.0', 16)
|
||||
|| $this->isInRange($value, '127.0.0.0', 8);
|
||||
}
|
||||
|
||||
private function isInRange(int $ip, string $network, int $mask): bool
|
||||
{
|
||||
$networkLong = ip2long($network);
|
||||
if ($networkLong === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$networkValue = (int)sprintf('%u', $networkLong);
|
||||
$maskValue = (0xFFFFFFFF << (32 - $mask)) & 0xFFFFFFFF;
|
||||
|
||||
return ($ip & $maskValue) === ($networkValue & $maskValue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user