duckdns: Update multiple hosts simultaneously

This commit is contained in:
Richard Hansen 2024-07-21 20:05:52 -04:00
parent 1eccfb8c77
commit 7bdb554e36
2 changed files with 26 additions and 19 deletions

View file

@ -86,6 +86,8 @@ repository history](https://github.com/ddclient/ddclient/commits/master).
records. [#690](https://github.com/ddclient/ddclient/pull/690) records. [#690](https://github.com/ddclient/ddclient/pull/690)
* `domeneshop`: Add IPv6 support. * `domeneshop`: Add IPv6 support.
[#719](https://github.com/ddclient/ddclient/pull/719) [#719](https://github.com/ddclient/ddclient/pull/719)
* `duckdns`: Multiple hosts with the same IP address are now updated together.
[#719](https://github.com/ddclient/ddclient/pull/719)
### Bug fixes ### Bug fixes

View file

@ -6341,38 +6341,43 @@ EoEXAMPLE
###################################################################### ######################################################################
sub nic_duckdns_update { sub nic_duckdns_update {
debug("\nnic_duckdns_update -------------------"); debug("\nnic_duckdns_update -------------------");
for my $h (@_) { for my $group (group_hosts_by(\@_, qw(password server wantipv4 wantipv6))) {
my $ipv4 = delete $config{$h}{'wantipv4'}; my @hosts = @{$group->{hosts}};
my $ipv6 = delete $config{$h}{'wantipv6'}; my %groupcfg = %{$group->{cfg}};
info("$h: setting IPv4 address to $ipv4") if $ipv4; my $hosts = join(',', @hosts);
info("$h: setting IPv6 address to $ipv6") if $ipv6; my $ipv4 = $groupcfg{'wantipv4'};
my $url = "https://$config{$h}{'server'}/update?domains=$h&token=$config{$h}{'password'}"; my $ipv6 = $groupcfg{'wantipv6'};
delete $config{$_}{'wantipv4'} for @hosts;
delete $config{$_}{'wantipv6'} for @hosts;
info("$hosts: setting IPv4 address to $ipv4") if $ipv4;
info("$hosts: setting IPv6 address to $ipv6") if $ipv6;
my $url = "https://$groupcfg{'server'}/update?domains=$hosts&token=$groupcfg{'password'}";
$url .= "&ip=$ipv4" if $ipv4; $url .= "&ip=$ipv4" if $ipv4;
$url .= "&ipv6=$ipv6" if $ipv6; $url .= "&ipv6=$ipv6" if $ipv6;
my $reply = geturl(proxy => opt('proxy'), url => $url); my $reply = geturl(proxy => opt('proxy'), url => $url);
if (!defined($reply) || !$reply) { if (!defined($reply) || !$reply) {
failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'}); failed("$hosts: Could not connect to $groupcfg{'server'}");
next; next;
} }
next if !header_ok($h, $reply); next if !header_ok($hosts, $reply);
(my $body = $reply) =~ s/^.*?\n\n//s or do { (my $body = $reply) =~ s/^.*?\n\n//s or do {
failed("$h: Invalid response from server"); failed("$hosts: Invalid response from server");
next; next;
}; };
chomp($body); chomp($body);
if ($body ne 'OK') { if ($body ne 'OK') {
$config{$h}{'status-ipv4'} = 'failed' if $ipv4; failed("$hosts: Server said: $body");
$config{$h}{'status-ipv6'} = 'failed' if $ipv6;
failed("updating %s: Server said: '%s'", $h, $body);
next; next;
} }
$config{$h}{'ipv4'} = $ipv4 if $ipv4; for my $h (@hosts) {
$config{$h}{'ipv6'} = $ipv6 if $ipv6; $config{$h}{'ipv4'} = $ipv4 if $ipv4;
$config{$h}{'mtime'} = $now; $config{$h}{'ipv6'} = $ipv6 if $ipv6;
$config{$h}{'status-ipv4'} = 'good' if $ipv4; $config{$h}{'mtime'} = $now;
$config{$h}{'status-ipv6'} = 'good' if $ipv6; $config{$h}{'status-ipv4'} = 'good' if $ipv4;
success("updating %s: good: IPv4 address set to %s", $h, $ipv4) if $ipv4; $config{$h}{'status-ipv6'} = 'good' if $ipv6;
success("updating %s: good: IPv6 address set to %s", $h, $ipv6) if $ipv6; }
success("$hosts: good: IPv4 address set to $ipv4") if $ipv4;
success("$hosts: good: IPv6 address set to $ipv6") if $ipv6;
} }
} }