Skip to content

Commit 6a8783b

Browse files
committed
small fixes and don't allow 0.0.0.0 or :: as ip
1 parent ca5fcf1 commit 6a8783b

3 files changed

Lines changed: 35 additions & 29 deletions

File tree

subdomains/src/Filament/Server/Resources/Subdomains/SubdomainResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function canAccess(): bool
3737
/** @var Server $server */
3838
$server = Filament::getTenant();
3939

40-
return parent::canAccess() && $server->allocation && CloudflareDomain::count() > 0;
40+
return parent::canAccess() && $server->allocation && $server->allocation->ip !== '0.0.0.0' && $server->allocation->ip !== '::' && CloudflareDomain::count() > 0;
4141
}
4242

4343
public static function getNavigationLabel(): string

subdomains/src/Models/CloudflareDomain.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CloudflareDomain extends Model
1515
{
1616
protected $fillable = [
1717
'name',
18+
'cloudflare_id',
1819
];
1920

2021
protected static function boot(): void
@@ -42,7 +43,7 @@ public function fetchCloudflareId(): void
4243

4344
if (count($zones) > 0) {
4445
$this->update([
45-
'cloudflare_id' => $zones[0]->id,
46+
'cloudflare_id' => $zones[0]['id'],
4647
]);
4748
}
4849
}

subdomains/src/Models/Subdomain.php

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Subdomain extends Model implements HasLabel
2424
protected $fillable = [
2525
'name',
2626
'record_type',
27+
'cloudflare_id',
2728
'domain_id',
2829
'server_id',
2930
];
@@ -47,12 +48,12 @@ protected static function boot(): void
4748

4849
public function domain(): BelongsTo
4950
{
50-
return $this->BelongsTo(CloudflareDomain::class, 'domain_id');
51+
return $this->belongsTo(CloudflareDomain::class, 'domain_id');
5152
}
5253

5354
public function server(): BelongsTo
5455
{
55-
return $this->BelongsTo(Server::class);
56+
return $this->belongsTo(Server::class);
5657
}
5758

5859
public function getLabel(): string|Htmlable|null
@@ -62,42 +63,46 @@ public function getLabel(): string|Htmlable|null
6263

6364
protected function createOnCloudflare(): void
6465
{
65-
if (!$this->server->allocation || $this->cloudflare_id) {
66+
if (!$this->server->allocation || $this->server->allocation->ip === '0.0.0.0' || $this->server->allocation->ip === '::') {
6667
return;
6768
}
6869

69-
$response = Http::cloudflare()->post("zones/{$this->domain->cloudflare_id}/dns_records", [
70-
'name' => $this->name,
71-
'ttl' => 120,
72-
'type' => $this->record_type,
73-
'comment' => 'Created by Pelican Subdomains plugin',
74-
'content' => $this->server->allocation->ip,
75-
'proxied' => false,
76-
])->json();
77-
78-
if ($response['success']) {
79-
$dnsRecord = $response['result'];
80-
81-
$this->updateQuietly([
82-
'cloudflare_id' => $dnsRecord->id,
83-
]);
70+
if (!$this->cloudflare_id) {
71+
$response = Http::cloudflare()->post("zones/{$this->domain->cloudflare_id}/dns_records", [
72+
'name' => $this->name,
73+
'ttl' => 120,
74+
'type' => $this->record_type,
75+
'comment' => 'Created by Pelican Subdomains plugin',
76+
'content' => $this->server->allocation->ip,
77+
'proxied' => false,
78+
])->json();
79+
80+
if ($response['success']) {
81+
$dnsRecord = $response['result'];
82+
83+
$this->updateQuietly([
84+
'cloudflare_id' => $dnsRecord['id'],
85+
]);
86+
}
8487
}
8588
}
8689

8790
protected function updateOnCloudflare(): void
8891
{
89-
if (!$this->server->allocation || !$this->cloudflare_id) {
92+
if (!$this->server->allocation || $this->server->allocation->ip === '0.0.0.0' || $this->server->allocation->ip === '::') {
9093
return;
9194
}
9295

93-
Http::cloudflare()->patch("zones/{$this->domain->cloudflare_id}/dns_records/{$this->cloudflare_id}", [
94-
'name' => $this->name,
95-
'ttl' => 120,
96-
'type' => $this->record_type,
97-
'comment' => 'Created by Pelican Subdomains plugin',
98-
'content' => $this->server->allocation->ip,
99-
'proxied' => false,
100-
]);
96+
if ($this->cloudflare_id) {
97+
Http::cloudflare()->patch("zones/{$this->domain->cloudflare_id}/dns_records/{$this->cloudflare_id}", [
98+
'name' => $this->name,
99+
'ttl' => 120,
100+
'type' => $this->record_type,
101+
'comment' => 'Created by Pelican Subdomains plugin',
102+
'content' => $this->server->allocation->ip,
103+
'proxied' => false,
104+
]);
105+
}
101106
}
102107

103108
protected function deleteOnCloudflare(): void

0 commit comments

Comments
 (0)