Files

73 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Rules;
2026-10-05 18:10:18 -04:00
use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
2026-10-05 18:10:18 -04:00
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;
2026-10-05 18:10:18 -04:00
class Fqdn implements DataAwareRule, ValidationRule
{
2026-10-05 18:10:18 -04:00
/**
2026-10-11 19:54:55 -04:00
* @var array<string, mixed>
2026-10-05 18:10:18 -04:00
*/
protected array $data = [];
2026-10-05 18:10:18 -04:00
protected ?string $schemeField = null;
/**
2026-10-05 18:10:18 -04:00
* Returns a new instance of the rule with a defined scheme set.
*/
public static function make(?string $schemeField = null): self
{
return tap(new self, function (self $fqdn) use ($schemeField): void {
$fqdn->schemeField = $schemeField;
});
}
/**
2026-10-11 19:54:55 -04:00
* @param array<string, mixed> $data
*/
public function setData($data): self
{
$this->data = $data;
return $this;
}
/**
* Validates that the value provided resolves to an IP address. If a scheme is
* specified when this rule is created additional checks will be applied.
*/
2026-10-05 18:10:18 -04:00
public function validate(string $attribute, mixed $value, Closure $fail): void
{
2026-10-05 18:10:18 -04:00
if (! is_string($value)) {
$fail('The :attribute could not be resolved to a valid IP address.');
return;
}
if (filter_var($value, FILTER_VALIDATE_IP)) {
2026-10-05 18:10:18 -04:00
// Unless someone owns their IP blocks and pays for a custom certificate, an IP
// cannot serve HTTPS, so refuse the combination before the node fails to connect.
if ($this->schemeField && Arr::get($this->data, $this->schemeField) === 'https') {
2026-10-05 18:10:18 -04:00
$fail('The :attribute must not be an IP address when HTTPS is enabled.');
}
2026-10-05 18:10:18 -04:00
return;
}
2026-10-05 18:10:18 -04:00
// dns_get_record resolves CNAMEs for us; the suppression is intentional,
// see https://bugs.php.net/bug.php?id=73149. gethostbyname is the IPv4-only fallback.
$records = @dns_get_record($value, DNS_A + DNS_AAAA);
2026-10-05 18:10:18 -04:00
if (! empty($records) || filter_var(gethostbyname($value), FILTER_VALIDATE_IP)) {
return;
}
2026-10-05 18:10:18 -04:00
$fail('The :attribute could not be resolved to a valid IP address.');
}
}