Skip to content
Merged
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
69 changes: 69 additions & 0 deletions src/lib/dyn_dns/HetznerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

require_once __DIR__ . '/BaseProvider.php';

class HetznerProvider extends BaseProvider {
public function updateIp(string $ip): bool {
$apiToken = $this->credentials['hetznerApiToken'] ?? null;
$zoneId = $this->credentials['hetznerZoneId'] ?? null;

if (!$apiToken || !$zoneId) {
$this->logger->error("Missing credentials for Hetzner: {$this->domain}");
return false;
}

$recordsUrl = "https://dns.hetzner.com/api/v1/records?zone_id=$zoneId";
$headers = [
"Auth-API-Token: $apiToken",
"Content-Type: application/json"
];

$ch = curl_init($recordsUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers
]);
$resp = curl_exec($ch);
if (!$resp) {
$this->logger->error("Failed Hetzner DNS fetch for {$this->domain}");
return false;
}

$data = json_decode($resp, true)['records'] ?? [];
$record = null;
foreach ($data as $r) {
if ($r['name'] === $this->domain || $r['name'] === str_replace('.'.$this->domain, '', $this->domain)) {
$record = $r;
break;
}
}

if (!$record) {
$this->logger->error("No DNS record found for {$this->domain}");
return false;
}

$updateUrl = "https://dns.hetzner.com/api/v1/records/{$record['id']}";
$payload = json_encode([
'value' => $ip,
'name' => $record['name'],
'type' => $record['type'],
'ttl' => 120,
'zone_id' => $zoneId
]);

curl_setopt_array($ch, [
CURLOPT_URL => $updateUrl,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $payload
]);
$resp = curl_exec($ch);
curl_close($ch);

$ok = strpos($resp, '"record"') !== false;
if ($ok) $this->logger->info("Hetzner updated {$this->domain} to $ip");
else $this->logger->error("Hetzner update failed for {$this->domain}: $resp");

return $ok;
}
}
2 changes: 2 additions & 0 deletions src/lib/dyn_dns_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_once __DIR__ . '/dyn_dns/StratoProvider.php';
require_once __DIR__ . '/dyn_dns/NamecheapProvider.php';
require_once __DIR__ . '/dyn_dns/CloudflareProvider.php';
require_once __DIR__ . '/dyn_dns/HetznerProvider.php';

$domainManager = new PersistentEntityManager(Domain::class, $logger, DB, 'domains');
$domains = $domainManager->list([], ['domain' => 'ASC']);
Expand All @@ -33,6 +34,7 @@
'strato' => StratoProvider::class,
'namecheap' => NamecheapProvider::class,
'cloudflare' => CloudflareProvider::class,
'hetzner' => HetznerProvider::class,
default => null,
};

Expand Down
5 changes: 5 additions & 0 deletions src/public/domains.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
<option value="strato">Strato</option>
<option value="namecheap">Namecheap</option>
<option value="cloudflare">Cloudflare</option>
<option value="hetzner">Hetzner</option>
</select>

<div id="providerFields"></div>
Expand Down Expand Up @@ -348,6 +349,10 @@
cloudflare: [
{ label: 'API Token', id: 'cfApiToken', type: 'text' },
{ label: 'Zone ID', id: 'cfZoneId', type: 'text' }
],
hetzner: [
{ label: 'API Token', id: 'hetznerApiToken', type: 'text' },
{ label: 'Zone ID', id: 'hetznerZoneId', type: 'text' }
]
};

Expand Down
Loading