coolify/app/Http/Middleware/TrustHosts.php

49 lines
1.3 KiB
PHP
Raw Normal View History

2023-03-17 14:33:48 +00:00
<?php
namespace App\Http\Middleware;
use App\Models\InstanceSettings;
2023-03-17 14:33:48 +00:00
use Illuminate\Http\Middleware\TrustHosts as Middleware;
use Illuminate\Support\Facades\Cache;
use Spatie\Url\Url;
2023-03-17 14:33:48 +00:00
class TrustHosts extends Middleware
{
/**
* Get the host patterns that should be trusted.
*
* @return array<int, string|null>
*/
public function hosts(): array
{
$trustedHosts = [];
// Trust the configured FQDN from InstanceSettings (cached to avoid DB query on every request)
$fqdnHost = Cache::remember('instance_settings_fqdn_host', 300, function () {
try {
$settings = InstanceSettings::get();
if ($settings && $settings->fqdn) {
$url = Url::fromString($settings->fqdn);
$host = $url->getHost();
return $host ?: null;
}
} catch (\Exception $e) {
// If instance settings table doesn't exist yet (during installation),
// return null to fall back to APP_URL only
}
return null;
});
if ($fqdnHost) {
$trustedHosts[] = $fqdnHost;
}
// Trust all subdomains of APP_URL as fallback
$trustedHosts[] = $this->allSubdomainsOfApplicationUrl();
return array_filter($trustedHosts);
2023-03-17 14:33:48 +00:00
}
}