diff --git a/app/Filament/Admin/Resources/DnsSettingResource.php b/app/Filament/Admin/Resources/DnsSettingResource.php index 2d745f31..99fe7540 100644 --- a/app/Filament/Admin/Resources/DnsSettingResource.php +++ b/app/Filament/Admin/Resources/DnsSettingResource.php @@ -26,9 +26,12 @@ public static function form(Form $form): Form Forms\Components\TextInput::make('domain_id') ->required() ->numeric(), - Forms\Components\TextInput::make('record_type') - ->required() - ->maxLength(255), + Forms\Components\Select::make('record_type') + ->options([ + 'A' => 'A', + 'MX' => 'MX', + ]) + ->required(), Forms\Components\TextInput::make('name') ->required() ->maxLength(255), diff --git a/app/Listeners/UpdateBindDnsRecords.php b/app/Listeners/UpdateBindDnsRecords.php new file mode 100644 index 00000000..fcd27793 --- /dev/null +++ b/app/Listeners/UpdateBindDnsRecords.php @@ -0,0 +1,43 @@ +dnsSetting; + + try { + $domain = $dnsSetting->domain->name; + $recordType = $dnsSetting->record_type; + $recordName = $dnsSetting->name; + $recordValue = $dnsSetting->value; + $ttl = $dnsSetting->ttl; + + $zoneFile = "./bind/records/{$domain}.db"; + + if ($recordType === 'A') { + $recordLine = "{$recordName} {$ttl} IN A {$recordValue}"; + } elseif ($recordType === 'MX') { + $recordLine = "{$domain}. {$ttl} IN MX 10 {$recordValue}."; + } + + file_put_contents($zoneFile, $recordLine . "\n", FILE_APPEND); + + exec('docker-compose restart bind9'); + } catch (\Exception $e) { + Log::error('Failed to update Bind9 DNS records: ' . $e->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/Models/DnsSetting.php b/app/Models/DnsSetting.php index b4182b1f..43298d9b 100644 --- a/app/Models/DnsSetting.php +++ b/app/Models/DnsSetting.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\DnsSettingSaved; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -9,6 +10,18 @@ class DnsSetting extends Model { use HasFactory; + /** + * The "booted" method of the model. + * + * @return void + */ + protected static function booted() + { + static::saved(function ($dnsSetting) { + event(new DnsSettingSaved($dnsSetting)); + }); + } + /** * The table associated with the model. * @@ -28,6 +41,15 @@ class DnsSetting extends Model 'value', 'ttl', ]; + + /** + * The validation rules for the model attributes. + * + * @var array + */ + public $rules = [ + 'record_type' => 'required|in:A,MX', + ]; /** * The attributes that should be cast. diff --git a/app/Providers/DnsSettingSaved.php b/app/Providers/DnsSettingSaved.php new file mode 100644 index 00000000..45c37a5c --- /dev/null +++ b/app/Providers/DnsSettingSaved.php @@ -0,0 +1,29 @@ +dnsSetting = $dnsSetting; + } +} \ No newline at end of file diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 2d65aac0..29ed54c5 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -9,6 +9,9 @@ class EventServiceProvider extends ServiceProvider { + use App\Events\DnsSettingSaved; + use App\Listeners\UpdateBindDnsRecords; + /** * The event to listener mappings for the application. * @@ -18,6 +21,9 @@ class EventServiceProvider extends ServiceProvider Registered::class => [ SendEmailVerificationNotification::class, ], + DnsSettingSaved::class => [ + UpdateBindDnsRecords::class, + ], ]; /**