mirror of
https://github.com/coollabsio/coolify.git
synced 2026-11-14 08:42:38 -04:00
Co-authored-by: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Co-authored-by: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: rosslh <ross@rosshill.ca>
47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
class ValidServerIp implements ValidationRule
|
|
{
|
|
/**
|
|
* Accepts a valid IPv4 address, IPv6 address (optionally in brackets), or RFC 1123 hostname.
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if (empty($value)) {
|
|
return;
|
|
}
|
|
|
|
$trimmed = trim($value);
|
|
|
|
if (filter_var($trimmed, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
return;
|
|
}
|
|
|
|
if (filter_var($trimmed, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
|
return;
|
|
}
|
|
|
|
// A bracketed IPv6 address ([2a01:4f8::1]); it is stored without brackets.
|
|
if (str_starts_with($trimmed, '[') && str_ends_with($trimmed, ']')
|
|
&& filter_var(substr($trimmed, 1, -1), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
|
return;
|
|
}
|
|
|
|
// Delegate hostname validation to ValidHostname
|
|
$hostnameRule = new ValidHostname;
|
|
$failed = false;
|
|
$hostnameRule->validate($attribute, $trimmed, function () use (&$failed) {
|
|
$failed = true;
|
|
});
|
|
|
|
if ($failed) {
|
|
$fail('The :attribute must be a valid IPv4 address, IPv6 address, or hostname.');
|
|
}
|
|
}
|
|
}
|