2022-10-09 16:19:16 -06:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-10-09 16:19:16 -06:00
|
|
|
namespace Pterodactyl\Rules;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Closure;
|
2022-10-09 16:19:16 -06:00
|
|
|
use Illuminate\Contracts\Validation\DataAwareRule;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
|
use Illuminate\Support\Arr;
|
2022-10-09 16:19:16 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
class Fqdn implements DataAwareRule, ValidationRule
|
2022-10-09 16:19:16 -06:00
|
|
|
{
|
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
|
|
|
*/
|
2022-10-09 16:19:16 -06:00
|
|
|
protected array $data = [];
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2022-10-09 16:19:16 -06: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
|
2022-10-09 16:19:16 -06:00
|
|
|
*/
|
|
|
|
|
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
|
2022-10-09 16:19:16 -06:00
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-09 16:19:16 -06:00
|
|
|
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.
|
2022-10-09 16:19:16 -06:00
|
|
|
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.');
|
2022-10-09 16:19:16 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return;
|
2022-10-09 16:19:16 -06:00
|
|
|
}
|
|
|
|
|
|
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.
|
2022-10-09 16:19:16 -06:00
|
|
|
$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;
|
2022-10-09 16:19:16 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$fail('The :attribute could not be resolved to a valid IP address.');
|
2022-10-09 16:19:16 -06:00
|
|
|
}
|
|
|
|
|
}
|