lasthourcloud/app/Http/Livewire/Server/Proxy.php

96 lines
3.1 KiB
PHP
Raw Normal View History

2023-05-03 05:23:45 +00:00
<?php
namespace App\Http\Livewire\Server;
2023-05-12 18:42:39 +00:00
use App\Actions\Proxy\CheckProxySettingsInSync;
2023-05-03 08:27:44 +00:00
use App\Actions\Proxy\InstallProxy;
2023-05-15 11:45:37 +00:00
use App\Enums\ProxyTypes;
use Illuminate\Support\Str;
2023-05-03 05:23:45 +00:00
use App\Models\Server;
use Livewire\Component;
class Proxy extends Component
{
public Server $server;
2023-05-15 11:45:37 +00:00
public ProxyTypes $selectedProxy = ProxyTypes::TRAEFIK_V2;
public $proxy_settings = null;
2023-06-22 12:18:17 +00:00
public string|null $redirect_url = null;
2023-05-03 05:23:45 +00:00
2023-06-02 13:15:12 +00:00
protected $listeners = ['serverValidated', 'saveConfiguration'];
2023-06-22 12:18:17 +00:00
public function mount()
{
$this->redirect_url = $this->server->proxy->redirect_url;
}
2023-05-15 19:14:45 +00:00
public function serverValidated()
{
2023-06-02 13:15:12 +00:00
$this->server->refresh();
2023-05-15 19:14:45 +00:00
}
2023-06-08 06:39:00 +00:00
public function switchProxy()
{
2023-06-20 18:19:31 +00:00
$this->server->proxy->type = null;
2023-06-08 06:39:00 +00:00
$this->server->save();
}
2023-05-15 11:45:37 +00:00
public function installProxy()
2023-05-03 05:23:45 +00:00
{
2023-05-16 09:39:18 +00:00
if (
2023-06-20 18:19:31 +00:00
$this->server->proxy->last_applied_settings &&
$this->server->proxy->last_saved_settings !== $this->server->proxy->last_applied_settings
2023-05-16 09:39:18 +00:00
) {
$this->saveConfiguration($this->server);
}
2023-05-03 08:27:44 +00:00
$activity = resolve(InstallProxy::class)($this->server);
2023-05-03 05:23:45 +00:00
$this->emit('newMonitorActivity', $activity->id);
}
2023-06-02 13:15:12 +00:00
public function setProxy(string $proxy_type)
2023-05-15 11:45:37 +00:00
{
2023-06-20 18:19:31 +00:00
$this->server->proxy->type = $proxy_type;
$this->server->proxy->status = 'exited';
2023-05-15 11:45:37 +00:00
$this->server->save();
}
public function stopProxy()
{
2023-05-24 13:25:08 +00:00
instant_remote_process([
2023-05-15 11:45:37 +00:00
"docker rm -f coolify-proxy",
], $this->server);
2023-06-20 18:19:31 +00:00
$this->server->proxy->status = 'exited';
2023-05-15 11:45:37 +00:00
$this->server->save();
}
2023-06-22 12:18:17 +00:00
public function saveConfiguration()
2023-05-15 11:45:37 +00:00
{
try {
$proxy_path = config('coolify.proxy_config_path');
2023-05-15 19:48:36 +00:00
$this->proxy_settings = Str::of($this->proxy_settings)->trim()->value;
2023-05-15 11:45:37 +00:00
$docker_compose_yml_base64 = base64_encode($this->proxy_settings);
2023-06-22 12:18:17 +00:00
$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();
2023-05-24 13:25:08 +00:00
instant_remote_process([
2023-05-15 11:45:37 +00:00
"echo '$docker_compose_yml_base64' | base64 -d > $proxy_path/docker-compose.yml",
2023-06-22 12:18:17 +00:00
], $this->server);
$this->server->refresh();
setup_default_redirect_404(redirect_url: $this->server->proxy->redirect_url, server: $this->server);
2023-06-22 08:04:39 +00:00
$this->emit('success', 'Proxy configuration saved.');
2023-05-15 11:45:37 +00:00
} catch (\Exception $e) {
2023-06-09 13:55:21 +00:00
return general_error_handler(err: $e);
2023-05-15 11:45:37 +00:00
}
}
2023-05-15 20:17:31 +00:00
public function resetProxy()
2023-05-03 05:23:45 +00:00
{
2023-05-15 11:45:37 +00:00
try {
2023-05-15 20:06:08 +00:00
$this->proxy_settings = resolve(CheckProxySettingsInSync::class)($this->server, true);
2023-05-15 11:45:37 +00:00
} catch (\Exception $e) {
2023-06-09 13:55:21 +00:00
return general_error_handler(err: $e);
2023-05-15 11:45:37 +00:00
}
2023-05-03 05:23:45 +00:00
}
2023-05-15 20:17:31 +00:00
public function checkProxySettingsInSync()
{
try {
$this->proxy_settings = resolve(CheckProxySettingsInSync::class)($this->server);
} catch (\Exception $e) {
2023-06-09 13:55:21 +00:00
return general_error_handler(err: $e);
2023-05-15 20:17:31 +00:00
}
}
2023-05-03 05:23:45 +00:00
}