From 448fa200a4156214ec29828b1067df21c9929441 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 22 Jun 2023 14:18:17 +0200 Subject: [PATCH] wip --- app/Http/Livewire/Server/Proxy.php | 16 +- app/Http/Livewire/Settings/Configuration.php | 8 +- app/Models/Server.php | 1 + bootstrap/helpers/proxy.php | 217 ++++++++++++------ .../views/livewire/server/proxy.blade.php | 6 +- 5 files changed, 166 insertions(+), 82 deletions(-) diff --git a/app/Http/Livewire/Server/Proxy.php b/app/Http/Livewire/Server/Proxy.php index 96268eb79..6c34fd682 100644 --- a/app/Http/Livewire/Server/Proxy.php +++ b/app/Http/Livewire/Server/Proxy.php @@ -15,8 +15,13 @@ class Proxy extends Component public ProxyTypes $selectedProxy = ProxyTypes::TRAEFIK_V2; public $proxy_settings = null; + public string|null $redirect_url = null; protected $listeners = ['serverValidated', 'saveConfiguration']; + public function mount() + { + $this->redirect_url = $this->server->proxy->redirect_url; + } public function serverValidated() { $this->server->refresh(); @@ -52,17 +57,20 @@ class Proxy extends Component $this->server->proxy->status = 'exited'; $this->server->save(); } - public function saveConfiguration(Server $server) + public function saveConfiguration() { try { $proxy_path = config('coolify.proxy_config_path'); $this->proxy_settings = Str::of($this->proxy_settings)->trim()->value; $docker_compose_yml_base64 = base64_encode($this->proxy_settings); - $server->proxy->last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value; - $server->save(); + $this->server->proxy->last_saved_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value; + $this->server->proxy->redirect_url = $this->redirect_url; + $this->server->save(); instant_remote_process([ "echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml", - ], $server); + ], $this->server); + $this->server->refresh(); + setup_default_redirect_404(redirect_url: $this->server->proxy->redirect_url, server: $this->server); $this->emit('success', 'Proxy configuration saved.'); } catch (\Exception $e) { return general_error_handler(err: $e); diff --git a/app/Http/Livewire/Settings/Configuration.php b/app/Http/Livewire/Settings/Configuration.php index 7db42883a..217ccb47e 100644 --- a/app/Http/Livewire/Settings/Configuration.php +++ b/app/Http/Livewire/Settings/Configuration.php @@ -15,7 +15,7 @@ class Configuration extends Component public $do_not_track; public $is_auto_update_enabled; public $is_registration_enabled; - protected string $dynamic_config_path; + protected string $dynamic_config_path = '/data/coolify/proxy/dynamic'; protected Server $server; protected $rules = [ @@ -117,9 +117,6 @@ class Configuration extends Component "rm -f $file", ], $this->server); } else { - $url = Url::fromString($this->settings->default_redirect_404); - $host = $url->getHost(); - $schema = $url->getScheme(); $traefik_dynamic_conf = [ 'http' => [ @@ -200,10 +197,9 @@ class Configuration extends Component $this->validate(); $this->settings->save(); - $this->dynamic_config_path = '/data/coolify/proxy/dynamic'; $this->server = Server::findOrFail(0); $this->setup_instance_fqdn(); - $this->setup_default_redirect_404(); + setup_default_redirect_404(redirect_url: $this->settings->default_redirect_404, server: $this->server); if ($this->settings->fqdn || $this->settings->default_redirect_404) { dispatch(new InstanceProxyCheckJob()); } diff --git a/app/Models/Server.php b/app/Models/Server.php index 5382f6dfd..8ee417fe7 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -87,6 +87,7 @@ class Server extends BaseModel static public function ownedByCurrentTeam(array $select = ['*']) { $selectArray = collect($select)->concat(['id']); + ray(Server::whereTeamId(session('currentTeam')->id)->with('settings')->select($selectArray->all())->get()); return Server::whereTeamId(session('currentTeam')->id)->with('settings')->select($selectArray->all()); } diff --git a/bootstrap/helpers/proxy.php b/bootstrap/helpers/proxy.php index f9fc68d3f..c029ae4a3 100644 --- a/bootstrap/helpers/proxy.php +++ b/bootstrap/helpers/proxy.php @@ -1,85 +1,160 @@ standaloneDockers)->map(function ($docker) { - return $docker['network']; - })->unique(); - if ($networks->count() === 0) { - $networks = collect(['coolify']); - } - $array_of_networks = collect([]); - $networks->map(function ($network) use ($array_of_networks) { - $array_of_networks[$network] = [ - "external" => true, - ]; - }); - $config = [ - "version" => "3.8", - "networks" => $array_of_networks->toArray(), - "services" => [ - "traefik" => [ - "container_name" => "coolify-proxy", - "image" => "traefik:v2.10", - "restart" => "always", - "extra_hosts" => [ - "host.docker.internal:host-gateway", +function getProxyConfiguration(Server $server) +{ + $proxy_path = config('coolify.proxy_config_path'); + if (isDev()) { + $proxy_path = $proxy_path . '/testing-host-1/'; + } + $networks = collect($server->standaloneDockers)->map(function ($docker) { + return $docker['network']; + })->unique(); + if ($networks->count() === 0) { + $networks = collect(['coolify']); + } + $array_of_networks = collect([]); + $networks->map(function ($network) use ($array_of_networks) { + $array_of_networks[$network] = [ + "external" => true, + ]; + }); + $config = [ + "version" => "3.8", + "networks" => $array_of_networks->toArray(), + "services" => [ + "traefik" => [ + "container_name" => "coolify-proxy", + "image" => "traefik:v2.10", + "restart" => "always", + "extra_hosts" => [ + "host.docker.internal:host-gateway", + ], + "networks" => $networks->toArray(), + "ports" => [ + "80:80", + "443:443", + "8080:8080", + ], + "healthcheck" => [ + "test" => "wget -qO- http://localhost:80/ping || exit 1", + "interval" => "4s", + "timeout" => "2s", + "retries" => 5, + ], + "volumes" => [ + "/var/run/docker.sock:/var/run/docker.sock:ro", + "{$proxy_path}:/traefik", + ], + "command" => [ + "--ping=true", + "--ping.entrypoint=http", + "--api.dashboard=true", + "--api.insecure=true", + "--entrypoints.http.address=:80", + "--entrypoints.https.address=:443", + "--providers.docker=true", + "--providers.docker.exposedbydefault=false", + "--providers.file.directory=/traefik/dynamic/", + "--providers.file.watch=true", + "--certificatesresolvers.letsencrypt.acme.httpchallenge=true", + "--certificatesresolvers.letsencrypt.acme.storage=/traefik/acme.json", + "--certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=http", + ], + "labels" => [ + "traefik.enable=true", + "traefik.http.routers.traefik.entrypoints=http", + "traefik.http.routers.traefik.middlewares=traefik-basic-auth@file", + "traefik.http.routers.traefik.service=api@internal", + "traefik.http.services.traefik.loadbalancer.server.port=8080", + // Global Middlewares + "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https", + "traefik.http.middlewares.gzip.compress=true", + ], + ], + ], + ]; + if (isDev()) { + $config['services']['traefik']['command'][] = "--log.level=debug"; + } + return Yaml::dump($config, 4, 2); +} +function setup_default_redirect_404(string $redirect_url, Server $server) +{ + $traefik_dynamic_conf_path = '/data/coolify/proxy/dynamic'; + $traefik_default_redirect_file = "$traefik_dynamic_conf_path/default_redirect_404.yaml"; + if (empty($redirect_url)) { + remote_process([ + "rm -f $traefik_default_redirect_file", + ], $server); + } else { + $traefik_dynamic_conf = [ + 'http' => + [ + 'routers' => + [ + 'catchall' => + [ + 'entryPoints' => [ + 0 => 'http', + 1 => 'https', + ], + 'service' => 'noop', + 'rule' => "HostRegexp(`{catchall:.*}`)", + 'priority' => 1, + 'middlewares' => [ + 0 => 'redirect-regexp@file', + ], ], - "networks" => $networks->toArray(), - "ports" => [ - "80:80", - "443:443", - "8080:8080", + ], + 'services' => + [ + 'noop' => + [ + 'loadBalancer' => + [ + 'servers' => + [ + 0 => + [ + 'url' => '', + ], + ], + ], ], - "healthcheck" => [ - "test" => "wget -qO- http://localhost:80/ping || exit 1", - "interval" => "4s", - "timeout" => "2s", - "retries" => 5, - ], - "volumes" => [ - "/var/run/docker.sock:/var/run/docker.sock:ro", - "{$proxy_path}:/traefik", - ], - "command" => [ - "--ping=true", - "--ping.entrypoint=http", - "--api.dashboard=true", - "--api.insecure=true", - "--entrypoints.http.address=:80", - "--entrypoints.https.address=:443", - "--providers.docker=true", - "--providers.docker.exposedbydefault=false", - "--providers.file.directory=/traefik/dynamic/", - "--providers.file.watch=true", - "--certificatesresolvers.letsencrypt.acme.httpchallenge=true", - "--certificatesresolvers.letsencrypt.acme.storage=/traefik/acme.json", - "--certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=http", - ], - "labels" => [ - "traefik.enable=true", - "traefik.http.routers.traefik.entrypoints=http", - "traefik.http.routers.traefik.middlewares=traefik-basic-auth@file", - "traefik.http.routers.traefik.service=api@internal", - "traefik.http.services.traefik.loadbalancer.server.port=8080", - // Global Middlewares - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https", - "traefik.http.middlewares.gzip.compress=true", + ], + 'middlewares' => + [ + 'redirect-regexp' => + [ + 'redirectRegex' => + [ + 'regex' => '(.*)', + 'replacement' => $redirect_url, + 'permanent' => false, + ], ], ], ], ]; - if (isDev()) { - $config['services']['traefik']['command'][] = "--log.level=debug"; + $yaml = Yaml::dump($traefik_dynamic_conf, 12, 2); + $yaml = + "# This file is automatically generated by Coolify.\n" . + "# Do not edit it manually (only if you know what are you doing).\n\n" . + $yaml; + + $base64 = base64_encode($yaml); + ray("mkdir -p $traefik_dynamic_conf_path"); + remote_process([ + "mkdir -p $traefik_dynamic_conf_path", + "echo '$base64' | base64 -d > $traefik_default_redirect_file", + ], $server); + + if (config('app.env') == 'local') { + ray($yaml); } - return Yaml::dump($config, 4, 2); } } diff --git a/resources/views/livewire/server/proxy.blade.php b/resources/views/livewire/server/proxy.blade.php index 1bbf2fbef..03e7f1874 100644 --- a/resources/views/livewire/server/proxy.blade.php +++ b/resources/views/livewire/server/proxy.blade.php @@ -9,7 +9,7 @@ @isset($proxy_settings) @if ($selectedProxy->value === 'TRAEFIK_V2') -
+

Proxy

Save @@ -25,6 +25,10 @@
Configuration out of sync. Restart to get the new configs.
@endif + @if ($server->id !== 0) + + @endif