|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Rules; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Illuminate\Contracts\Validation\Rule; |
| 7 | +use Illuminate\Contracts\Validation\DataAwareRule; |
| 8 | + |
| 9 | +class Fqdn implements Rule, DataAwareRule |
| 10 | +{ |
| 11 | + protected array $data = []; |
| 12 | + protected string $message = ''; |
| 13 | + protected ?string $schemeField = null; |
| 14 | + |
| 15 | + /** |
| 16 | + * @param array $data |
| 17 | + */ |
| 18 | + public function setData($data): self |
| 19 | + { |
| 20 | + $this->data = $data; |
| 21 | + |
| 22 | + return $this; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Validates that the value provided resolves to an IP address. If a scheme is |
| 27 | + * specified when this rule is created additional checks will be applied. |
| 28 | + * |
| 29 | + * @param string $attribute |
| 30 | + * @param mixed $value |
| 31 | + * @return bool |
| 32 | + */ |
| 33 | + public function passes($attribute, $value) |
| 34 | + { |
| 35 | + if (filter_var($value, FILTER_VALIDATE_IP)) { |
| 36 | + // Check if the scheme is set to HTTPS. |
| 37 | + // |
| 38 | + // Unless someone owns their IP blocks and decides to pay who knows how much for a |
| 39 | + // custom SSL cert, IPs will not be able to use HTTPS. This should prevent most |
| 40 | + // home users from making this mistake and wondering why their node is not working. |
| 41 | + if ($this->schemeField && Arr::get($this->data, $this->schemeField) === 'https') { |
| 42 | + $this->message = 'The :attribute must not be an IP address when HTTPS is enabled.'; |
| 43 | + |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + // Lookup A and AAAA DNS records for the FQDN. Note, this function will also resolve CNAMEs |
| 51 | + // for us automatically, there is no need to manually resolve them here. |
| 52 | + // |
| 53 | + // The error suppression is intentional, see https://bugs.php.net/bug.php?id=73149 |
| 54 | + $records = @dns_get_record($value, DNS_A + DNS_AAAA); |
| 55 | + // If no records were returned fall back to trying to resolve the value using the hosts DNS |
| 56 | + // resolution. This will not work for IPv6 which is why we prefer to use `dns_get_record` |
| 57 | + // first. |
| 58 | + if (!empty($records) || filter_var(gethostbyname($value), FILTER_VALIDATE_IP)) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + $this->message = 'The :attribute could not be resolved to a valid IP address.'; |
| 63 | + |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + public function message(): string |
| 68 | + { |
| 69 | + return $this->message; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns a new instance of the rule with a defined scheme set. |
| 74 | + */ |
| 75 | + public static function make(string $schemeField = null): self |
| 76 | + { |
| 77 | + return tap(new static(), function ($fqdn) use ($schemeField) { |
| 78 | + $fqdn->schemeField = $schemeField; |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
0 commit comments