You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
5.2 KiB
PHP
165 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/** Canonical IPv4/IPv6 value used by the panel-owned public-network contract. */
|
|
final class CloudIpAddress
|
|
{
|
|
private function __construct(
|
|
private readonly string $binary,
|
|
private readonly string $text,
|
|
private readonly int $family
|
|
) {
|
|
}
|
|
|
|
public static function parse(mixed $value): self
|
|
{
|
|
if (!is_string($value) || $value === '' || trim($value) !== $value
|
|
|| str_contains($value, '%')) {
|
|
throw new \InvalidArgumentException('IP address is invalid.');
|
|
}
|
|
$binary = @inet_pton($value);
|
|
if ($binary === false || !in_array(strlen($binary), [4, 16], true)) {
|
|
throw new \InvalidArgumentException('IP address is invalid.');
|
|
}
|
|
$text = @inet_ntop($binary);
|
|
if (!is_string($text) || $text === '') {
|
|
throw new \InvalidArgumentException('IP address is invalid.');
|
|
}
|
|
return new self($binary, strtolower($text), strlen($binary) === 4 ? 4 : 6);
|
|
}
|
|
|
|
public static function fromBinary(string $binary): self
|
|
{
|
|
if (!in_array(strlen($binary), [4, 16], true)) {
|
|
throw new \InvalidArgumentException('Binary IP address is invalid.');
|
|
}
|
|
$text = @inet_ntop($binary);
|
|
if (!is_string($text) || $text === '') {
|
|
throw new \InvalidArgumentException('Binary IP address is invalid.');
|
|
}
|
|
return new self($binary, strtolower($text), strlen($binary) === 4 ? 4 : 6);
|
|
}
|
|
|
|
public function text(): string
|
|
{
|
|
return $this->text;
|
|
}
|
|
|
|
public function binary(): string
|
|
{
|
|
return $this->binary;
|
|
}
|
|
|
|
public function family(): int
|
|
{
|
|
return $this->family;
|
|
}
|
|
|
|
public function equals(self $other): bool
|
|
{
|
|
return $this->family === $other->family
|
|
&& hash_equals($this->binary, $other->binary);
|
|
}
|
|
|
|
public function compare(self $other): int
|
|
{
|
|
if ($this->family !== $other->family) {
|
|
throw new \InvalidArgumentException('IP families do not match.');
|
|
}
|
|
return strcmp($this->binary, $other->binary);
|
|
}
|
|
|
|
public function next(): self
|
|
{
|
|
$bytes = array_values(unpack('C*', $this->binary));
|
|
for ($index = count($bytes) - 1; $index >= 0; $index--) {
|
|
if ($bytes[$index] < 255) {
|
|
$bytes[$index]++;
|
|
return self::fromBinary(pack('C*', ...$bytes));
|
|
}
|
|
$bytes[$index] = 0;
|
|
}
|
|
throw new \OverflowException('IP address space is exhausted.');
|
|
}
|
|
|
|
public function isUnspecified(): bool
|
|
{
|
|
return $this->binary === str_repeat("\0", strlen($this->binary));
|
|
}
|
|
|
|
public function isLoopback(): bool
|
|
{
|
|
if ($this->family === 4) {
|
|
return ord($this->binary[0]) === 127;
|
|
}
|
|
return $this->binary === str_repeat("\0", 15) . "\1";
|
|
}
|
|
|
|
public function isMulticast(): bool
|
|
{
|
|
$first = ord($this->binary[0]);
|
|
return $this->family === 4 ? ($first & 0xf0) === 0xe0 : $first === 0xff;
|
|
}
|
|
|
|
public function isLinkLocal(): bool
|
|
{
|
|
if ($this->family === 4) {
|
|
return ord($this->binary[0]) === 169 && ord($this->binary[1]) === 254;
|
|
}
|
|
return ord($this->binary[0]) === 0xfe
|
|
&& (ord($this->binary[1]) & 0xc0) === 0x80;
|
|
}
|
|
|
|
public function isIpv4Mapped(): bool
|
|
{
|
|
return $this->family === 6
|
|
&& substr($this->binary, 0, 10) === str_repeat("\0", 10)
|
|
&& substr($this->binary, 10, 2) === "\xff\xff";
|
|
}
|
|
|
|
public function isUsableUnicast(): bool
|
|
{
|
|
return !$this->isUnspecified() && !$this->isLoopback()
|
|
&& !$this->isMulticast() && !$this->isLinkLocal()
|
|
&& !$this->isIpv4Mapped();
|
|
}
|
|
|
|
/** Public-network intents never accept private or special-use addresses. */
|
|
public function isPublicUnicast(): bool
|
|
{
|
|
if (!$this->isUsableUnicast()) {
|
|
return false;
|
|
}
|
|
if ($this->family === 6) {
|
|
// fc00::/7 unique-local addresses are not public Internet space.
|
|
return (ord($this->binary[0]) & 0xfe) !== 0xfc;
|
|
}
|
|
|
|
$number = unpack('Nvalue', $this->binary);
|
|
if (!is_array($number) || !isset($number['value'])) {
|
|
return false;
|
|
}
|
|
$value = (int) sprintf('%u', $number['value']);
|
|
foreach ([
|
|
['0.0.0.0', 8], ['10.0.0.0', 8], ['100.64.0.0', 10],
|
|
['127.0.0.0', 8], ['169.254.0.0', 16], ['172.16.0.0', 12],
|
|
['192.0.0.0', 24], ['192.0.2.0', 24], ['192.168.0.0', 16],
|
|
['198.18.0.0', 15], ['198.51.100.0', 24],
|
|
['203.0.113.0', 24], ['224.0.0.0', 4], ['240.0.0.0', 4],
|
|
] as [$network, $prefix]) {
|
|
$packed = inet_pton($network);
|
|
$baseParts = is_string($packed) ? unpack('Nvalue', $packed) : false;
|
|
if (!is_array($baseParts) || !isset($baseParts['value'])) {
|
|
return false;
|
|
}
|
|
$base = (int) sprintf('%u', $baseParts['value']);
|
|
$size = 2 ** (32 - $prefix);
|
|
if ($value >= $base && $value < $base + $size) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|