From f7744372352bc99530f11835661a8c1e4c91b133 Mon Sep 17 00:00:00 2001 From: philippderdiedas <56478008+philippderdiedas@users.noreply.github.com> Date: Sat, 25 May 2024 01:36:17 +0000 Subject: [PATCH 1/3] add hetzner ip exist check --- ddclient.in | 70 +++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/ddclient.in b/ddclient.in index f6bf5ae..ebad305 100755 --- a/ddclient.in +++ b/ddclient.in @@ -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); + } } } } From 963868f1b519a05dc6149e6322d727f510cdd3fb Mon Sep 17 00:00:00 2001 From: philippderdiedas <56478008+philippderdiedas@users.noreply.github.com> Date: Tue, 16 Jul 2024 02:08:49 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Richard Hansen --- ddclient.in | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ddclient.in b/ddclient.in index ebad305..957cb65 100755 --- a/ddclient.in +++ b/ddclient.in @@ -6409,8 +6409,7 @@ sub nic_hetzner_update { $config{$domain}{"ipv$ipv"} = $ip; $config{$domain}{'mtime'} = $now; $config{$domain}{"status-ipv$ipv"} = 'good'; - } - else { + } else { # Set domain if ($dns_rec_id) { From 3477e36b8824551704cf587ed6afbfa48d5802ca Mon Sep 17 00:00:00 2001 From: philippderdiedas <56478008+philippderdiedas@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:28:59 +0000 Subject: [PATCH 3/3] fix syntax --- ddclient.in | 95 ++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/ddclient.in b/ddclient.in index 3aed169..adabdb6 100755 --- a/ddclient.in +++ b/ddclient.in @@ -6406,12 +6406,52 @@ sub nic_hetzner_update { info("updating %s: setting IPv$ipv address to %s", $domain, $ip); $config{$domain}{"status-ipv$ipv"} = 'failed'; - # Get DNS 'A' or 'AAAA' record ID - $url = "https://$config{$domain}{'server'}/records?zone_id=$zone_id"; + # Get DNS 'A' or 'AAAA' record ID + $url = "https://$config{$domain}{'server'}/records?zone_id=$zone_id"; + $reply = geturl(proxy => opt('proxy'), + url => $url, + headers => $headers + ); + 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})}; + unless ($response && $response->{records}) { + failed("updating %s: invalid json or result.", $domain); + next; + } + # Pull the ID out of the json, messy + my ($dns_rec_id, $current_ip) = map { ($_->{name} eq $hostname && $_->{type} eq $type) ? ($_->{id}, $_ ->{value}) : ()} @{$response->{records}}; + + my $http_method=""; + 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 { + # Set domain + if ($dns_rec_id) + { + debug("updating %s: DNS '$type' record ID: $dns_rec_id", $domain); + $url = "https://$config{$domain}{'server'}/records/$dns_rec_id"; + $http_method = "PUT"; + } else { + debug("creating %s: DNS '$type'", $domain); + $url = "https://$config{$domain}{'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 - ); + 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; @@ -6419,54 +6459,13 @@ sub nic_hetzner_update { # Strip header $reply =~ qr/{(?:[^{}]*|(?R))*}/mp; $response = eval {decode_json(${^MATCH})}; - unless ($response && $response->{records}) { - failed("updating %s: invalid json or result.", $domain); - next; - } - # Pull the ID out of the json, messy - my ($dns_rec_id, $current_ip) = map { ($_->{name} eq $hostname && $_->{type} eq $type) ? ($_->{id}, $_ ->{value}) : ()} @{$response->{records}}; - - my $http_method=""; - if ($current_ip && $current_ip eq $ip) { - info("updating %s %s: IP is already %s, no update needed.", $hostname, $ipv, $ip); + 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 { - # Set domain - if ($dns_rec_id) - { - debug("updating %s: DNS '$type' record ID: $dns_rec_id", $domain); - $url = "https://$config{$domain}{'server'}/records/$dns_rec_id"; - $http_method = "PUT"; - } else { - debug("creating %s: DNS '$type'", $domain); - $url = "https://$config{$domain}{'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); - } + failed("updating %s: invalid json or result.", $domain); } } }