Refactor Server.php to improve default 404 redirect setup

This commit is contained in:
Andras Bacsai
2024-09-23 22:57:24 +02:00
parent 21a5b4ce80
commit 480ae3de8a

View File

@@ -165,14 +165,13 @@ class Server extends BaseModel
$dynamic_conf_path = $this->proxyPath().'/dynamic'; $dynamic_conf_path = $this->proxyPath().'/dynamic';
$proxy_type = $this->proxyType(); $proxy_type = $this->proxyType();
$redirect_url = $this->proxy->redirect_url; $redirect_url = $this->proxy->redirect_url;
ray($proxy_type);
if ($proxy_type === ProxyTypes::TRAEFIK->value) { if ($proxy_type === ProxyTypes::TRAEFIK->value) {
$default_redirect_file = "$dynamic_conf_path/default_redirect_404.yaml"; $default_redirect_file = "$dynamic_conf_path/default_redirect_404.yaml";
} elseif ($proxy_type === 'CADDY') { } elseif ($proxy_type === ProxyTypes::CADDY->value) {
$default_redirect_file = "$dynamic_conf_path/default_redirect_404.caddy"; $default_redirect_file = "$dynamic_conf_path/default_redirect_404.caddy";
} }
if (empty($redirect_url)) { if (empty($redirect_url)) {
if ($proxy_type === 'CADDY') { if ($proxy_type === ProxyTypes::CADDY->value) {
$conf = ':80, :443 { $conf = ':80, :443 {
respond 404 respond 404
}'; }';
@@ -242,7 +241,7 @@ respond 404
$conf; $conf;
$base64 = base64_encode($conf); $base64 = base64_encode($conf);
} elseif ($proxy_type === 'CADDY') { } elseif ($proxy_type === ProxyTypes::CADDY->value) {
$conf = ":80, :443 { $conf = ":80, :443 {
redir $redirect_url redir $redirect_url
}"; }";
@@ -258,9 +257,6 @@ respond 404
"echo '$base64' | base64 -d | tee $default_redirect_file > /dev/null", "echo '$base64' | base64 -d | tee $default_redirect_file > /dev/null",
], $this); ], $this);
if (config('app.env') == 'local') {
ray($conf);
}
if ($proxy_type === 'CADDY') { if ($proxy_type === 'CADDY') {
$this->reloadCaddy(); $this->reloadCaddy();
} }
@@ -908,7 +904,7 @@ $schema://$host {
{ {
return Attribute::make( return Attribute::make(
get: function ($value) { get: function ($value) {
return preg_replace('/[^0-9a-zA-Z.-]/', '', $value); return preg_replace('/[^0-9a-zA-Z.:%-]/', '', $value);
} }
); );
} }