<?php12namespace Pterodactyl\Rules;34use Illuminate\Support\Arr;5use Illuminate\Contracts\Validation\Rule;6use Illuminate\Contracts\Validation\DataAwareRule;78class Fqdn implements Rule, DataAwareRule9{10protected array $data = [];11protected string $message = '';12protected ?string $schemeField = null;1314/**15* @param array $data16*/17public function setData($data): self18{19$this->data = $data;2021return $this;22}2324/**25* Validates that the value provided resolves to an IP address. If a scheme is26* specified when this rule is created additional checks will be applied.27*28* @param string $attribute29*/30public function passes($attribute, $value): bool31{32if (filter_var($value, FILTER_VALIDATE_IP)) {33// Check if the scheme is set to HTTPS.34//35// Unless someone owns their IP blocks and decides to pay who knows how much for a36// custom SSL cert, IPs will not be able to use HTTPS. This should prevent most37// home users from making this mistake and wondering why their node is not working.38if ($this->schemeField && Arr::get($this->data, $this->schemeField) === 'https') {39$this->message = 'The :attribute must not be an IP address when HTTPS is enabled.';4041return false;42}4344return true;45}4647// Lookup A and AAAA DNS records for the FQDN. Note, this function will also resolve CNAMEs48// for us automatically, there is no need to manually resolve them here.49//50// The error suppression is intentional, see https://bugs.php.net/bug.php?id=7314951$records = @dns_get_record($value, DNS_A + DNS_AAAA);52// If no records were returned fall back to trying to resolve the value using the hosts DNS53// resolution. This will not work for IPv6 which is why we prefer to use `dns_get_record`54// first.55if (!empty($records) || filter_var(gethostbyname($value), FILTER_VALIDATE_IP)) {56return true;57}5859$this->message = 'The :attribute could not be resolved to a valid IP address.';6061return false;62}6364public function message(): string65{66return $this->message;67}6869/**70* Returns a new instance of the rule with a defined scheme set.71*/72public static function make(?string $schemeField = null): self73{74return tap(new static(), function ($fqdn) use ($schemeField) {75$fqdn->schemeField = $schemeField;76});77}78}798081