Files
panel/app/Filament/Resources/NodeResource/Pages/CreateNode.php

175 lines
6.5 KiB
PHP
Raw Normal View History

2024-03-24 01:48:03 -04:00
<?php
namespace App\Filament\Resources\NodeResource\Pages;
use App\Filament\Resources\NodeResource;
2024-03-31 00:14:48 -04:00
use App\Models\Node;
use Filament\Actions\Action;
2024-03-24 14:42:36 -04:00
use Filament\Forms;
use Filament\Notifications\Notification;
2024-03-24 01:48:03 -04:00
use Filament\Resources\Pages\CreateRecord;
2024-03-25 10:26:57 -04:00
use Illuminate\Support\HtmlString;
2024-03-24 01:48:03 -04:00
class CreateNode extends CreateRecord
{
protected static string $resource = NodeResource::class;
2024-03-24 14:42:36 -04:00
2024-03-31 01:06:49 -04:00
protected static bool $canCreateAnother = false;
2024-03-31 00:14:48 -04:00
2024-04-11 00:45:26 -04:00
protected ?string $subheading = 'which is a machine that runs your Servers';
2024-04-09 21:01:46 -04:00
2024-03-24 14:42:36 -04:00
public function form(Forms\Form $form): Forms\Form
{
return $form
2024-03-25 10:26:57 -04:00
->columns(4)
2024-03-24 14:42:36 -04:00
->schema([
Forms\Components\TextInput::make('fqdn')
2024-03-25 10:26:57 -04:00
->columnSpan(2)
2024-03-24 14:42:36 -04:00
->required()
->autofocus()
->live(debounce: 500)
2024-03-31 01:06:49 -04:00
->rule('prohibited', fn ($state) => is_ip($state) && request()->isSecure())
2024-03-25 10:26:57 -04:00
->label(fn ($state) => is_ip($state) ? 'IP Address' : 'Domain Name')
->placeholder(fn ($state) => is_ip($state) ? '192.168.1.1' : 'node.example.com')
2024-04-04 18:58:19 -04:00
->helperText(function ($state) {
if (is_ip($state)) {
if (request()->isSecure()) {
return '
Your panel is currently secured via an SSL certificate and that means your nodes require one too.
You must use a domain name, because you cannot get SSL certificates for IP Addresses
';
}
return '';
}
return "
This is the domain name that points to your node's IP Address.
If you've already set up this, you can verify it by checking the next field!
";
})
2024-03-25 10:26:57 -04:00
->hintColor('danger')
->hint(function ($state) {
if (is_ip($state) && request()->isSecure()) {
2024-03-31 01:06:49 -04:00
return 'You cannot connect to an IP Address over SSL';
2024-03-24 14:42:36 -04:00
}
2024-03-25 10:26:57 -04:00
return '';
2024-03-24 14:42:36 -04:00
})
2024-03-25 10:26:57 -04:00
->afterStateUpdated(function (Forms\Set $set, ?string $state) {
2024-04-04 18:59:34 -04:00
$set('dns', null);
2024-04-09 21:01:46 -04:00
$set('ip', null);
2024-03-31 00:14:48 -04:00
[$subdomain] = str($state)->explode('.', 2);
if (!is_numeric($subdomain)) {
$set('name', $subdomain);
}
2024-04-04 18:59:34 -04:00
if (!$state || is_ip($state)) {
$set('dns', null);
return;
}
2024-04-09 21:01:46 -04:00
$validRecords = gethostbynamel($state);
if ($validRecords) {
2024-04-04 18:59:34 -04:00
$set('dns', true);
2024-04-09 21:01:46 -04:00
$set('ip', collect($validRecords)->first());
2024-04-04 18:59:34 -04:00
return;
}
$set('dns', false);
2024-03-25 10:26:57 -04:00
})
->maxLength(191),
2024-03-24 14:42:36 -04:00
2024-04-09 21:01:46 -04:00
Forms\Components\TextInput::make('ip')
->disabled()
->hidden(),
2024-04-04 18:59:34 -04:00
Forms\Components\ToggleButtons::make('dns')
->label('DNS Record Check')
2024-04-09 18:45:43 -04:00
->helperText('This lets you know if your DNS record correctly points to an IP Address.')
2024-04-04 18:59:34 -04:00
->disabled()
->inline()
->default(null)
2024-04-09 21:01:46 -04:00
->hint(fn (Forms\Get $get) => $get('ip'))
->hintColor('success')
2024-04-04 18:59:34 -04:00
->options([
true => 'Valid',
false => 'Invalid'
])
->colors([
true => 'success',
false => 'danger'
]),
2024-03-24 14:42:36 -04:00
Forms\Components\TextInput::make('daemonListen')
2024-04-04 18:58:40 -04:00
->columnSpan(1)
2024-03-24 14:42:36 -04:00
->label('Port')
2024-03-25 10:26:57 -04:00
->helperText('If you are running the daemon behind Cloudflare you should set the daemon port to 8443 to allow websocket proxying over SSL.')
2024-03-24 14:42:36 -04:00
->minValue(0)
->maxValue(65536)
->default(8080)
->required()
->integer(),
2024-04-04 18:59:12 -04:00
Forms\Components\TextInput::make('name')
2024-04-09 18:45:43 -04:00
->label('Display Name')
2024-04-04 18:59:12 -04:00
->columnSpan(2)
->required()
->regex('/[a-zA-Z0-9_\.\- ]+/')
2024-04-09 18:45:43 -04:00
->helperText('This name is for display only and can be changed later.')
2024-04-04 18:59:12 -04:00
->maxLength(100),
2024-04-04 18:59:42 -04:00
2024-03-24 14:42:36 -04:00
Forms\Components\ToggleButtons::make('scheme')
->label('Communicate over SSL')
2024-04-04 18:59:42 -04:00
->columnSpan(2)
2024-03-24 14:42:36 -04:00
->required()
->inline()
->helperText(function (Forms\Get $get) {
if (request()->isSecure()) {
2024-04-09 18:45:43 -04:00
return new HtmlString('Your Panel is using a secure SSL connection,<br>so your Daemon must too.');
2024-03-24 14:42:36 -04:00
}
2024-03-25 10:26:57 -04:00
if (is_ip($get('fqdn'))) {
2024-03-24 14:42:36 -04:00
return 'An IP address cannot use SSL.';
}
return '';
})
2024-03-31 01:06:49 -04:00
->disableOptionWhen(fn (string $value): bool => $value === 'http' && request()->isSecure())
2024-03-24 14:42:36 -04:00
->options([
'http' => 'HTTP',
2024-03-30 02:15:56 -04:00
'https' => 'HTTPS (SSL)',
2024-03-24 14:42:36 -04:00
])
->colors([
'http' => 'warning',
'https' => 'success',
])
->icons([
2024-03-30 02:15:56 -04:00
'http' => 'tabler-lock-open-off',
'https' => 'tabler-lock',
2024-03-24 14:42:36 -04:00
])
2024-03-31 00:11:56 -04:00
->default(fn () => request()->isSecure() ? 'https' : 'http'),
2024-04-04 18:59:12 -04:00
2024-03-30 20:32:48 -04:00
Forms\Components\Textarea::make('description')
->hidden()
->columnSpanFull()
->rows(5),
2024-04-04 20:16:14 -04:00
2024-03-31 00:14:48 -04:00
Forms\Components\Hidden::make('skipValidation')->default(true),
2024-03-24 14:42:36 -04:00
]);
}
2024-03-31 01:07:03 -04:00
protected function getRedirectUrlParameters(): array
{
return [
2024-04-04 20:16:14 -04:00
'tab' => '-configuration-tab',
2024-03-31 01:07:03 -04:00
];
}
2024-03-24 01:48:03 -04:00
}