Skip to content

Commit

Permalink
add hetzner ip exist check
Browse files Browse the repository at this point in the history
  • Loading branch information
philippderdiedas committed May 25, 2024
1 parent 65fb4db commit f774437
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions ddclient.in
Original file line number Diff line number Diff line change
Expand Up @@ -6401,42 +6401,50 @@ sub nic_hetzner_update {
next;
}
# Pull the ID out of the json, messy
my ($dns_rec_id) = map { ($_->{name} eq $hostname && $_->{type} eq $type) ? $_->{id} : ()} @{$response->{records}};
my ($dns_rec_id, $current_ip) = map { ($_->{name} eq $hostname && $_->{type} eq $type) ? ($_->{id}, $_->{value}) : ()} @{$response->{records}};

# Set domain
my $http_method="";
if ($dns_rec_id)
{
debug("updating %s: DNS '$type' record ID: $dns_rec_id", $domain);
$url = "https://$config{$key}{'server'}/records/$dns_rec_id";
$http_method = "PUT";
} else {
debug("creating %s: DNS '$type'", $domain);
$url = "https://$config{$key}{'server'}/records";
$http_method = "POST";
}
my $data = "{\"zone_id\":\"$zone_id\", \"name\": \"$hostname\", \"value\": \"$ip\", \"type\": \"$type\", \"ttl\": $config{$domain}{'ttl'}}";

$reply = geturl(proxy => opt('proxy'),
url => $url,
headers => $headers,
method => $http_method,
data => $data
);
unless ($reply && header_ok($domain, $reply)) {
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
next;
}
# Strip header
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
$response = eval {decode_json(${^MATCH})};
if ($response && $response->{record}) {
success("updating %s: IPv$ipv address set to %s", $domain, $ip);
if ($current_ip && $current_ip eq $ip) {
info("updating %s %s: IP is already %s, no update needed.", $hostname, $ipv, $ip);
$config{$domain}{"ipv$ipv"} = $ip;
$config{$domain}{'mtime'} = $now;
$config{$domain}{"status-ipv$ipv"} = 'good';
} else {
failed("updating %s: invalid json or result.", $domain);
}
else {
# Set domain
if ($dns_rec_id)
{
debug("updating %s: DNS '$type' record ID: $dns_rec_id", $domain);
$url = "https://$config{$key}{'server'}/records/$dns_rec_id";
$http_method = "PUT";
} else {
debug("creating %s: DNS '$type'", $domain);
$url = "https://$config{$key}{'server'}/records";
$http_method = "POST";
}
my $data = "{\"zone_id\":\"$zone_id\", \"name\": \"$hostname\", \"value\": \"$ip\", \"type\": \"$type\", \"ttl\": $config{$domain}{'ttl'}}";

$reply = geturl(proxy => opt('proxy'),
url => $url,
headers => $headers,
method => $http_method,
data => $data
);
unless ($reply && header_ok($domain, $reply)) {
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
next;
}
# Strip header
$reply =~ qr/{(?:[^{}]*|(?R))*}/mp;
$response = eval {decode_json(${^MATCH})};
if ($response && $response->{record}) {
success("updating %s: IPv$ipv address set to %s", $domain, $ip);
$config{$domain}{"ipv$ipv"} = $ip;
$config{$domain}{'mtime'} = $now;
$config{$domain}{"status-ipv$ipv"} = 'good';
} else {
failed("updating %s: invalid json or result.", $domain);
}
}
}
}
Expand Down

0 comments on commit f774437

Please sign in to comment.