Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS #11

Merged
merged 2 commits into from
Jun 16, 2024
Merged

DNS #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/Filament/Admin/Resources/DnsSettingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
43 changes: 43 additions & 0 deletions app/Listeners/UpdateBindDnsRecords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Listeners;

use App\Events\DnsSettingSaved;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class UpdateBindDnsRecords implements ShouldQueue
{
/**
* Handle the event.
*
* @param \App\Events\DnsSettingSaved $event
* @return void
*/
public function handle(DnsSettingSaved $event)
{
$dnsSetting = $event->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());
}
}
}
22 changes: 22 additions & 0 deletions app/Models/DnsSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

namespace App\Models;

use App\Events\DnsSettingSaved;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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.
*
Expand All @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions app/Providers/DnsSettingSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Events;

use App\Models\DnsSetting;
use Illuminate\Foundation\Events\Dispatchable;

class DnsSettingSaved
{
use Dispatchable;

/**
* The DnsSetting instance.
*
* @var \App\Models\DnsSetting
*/
public $dnsSetting;

/**
* Create a new event instance.
*
* @param \App\Models\DnsSetting $dnsSetting
* @return void
*/
public function __construct(DnsSetting $dnsSetting)
{
$this->dnsSetting = $dnsSetting;
}
}
6 changes: 6 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class EventServiceProvider extends ServiceProvider
{
use App\Events\DnsSettingSaved;
use App\Listeners\UpdateBindDnsRecords;

/**
* The event to listener mappings for the application.
*
Expand All @@ -18,6 +21,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
DnsSettingSaved::class => [
UpdateBindDnsRecords::class,
],
];

/**
Expand Down
Loading