Cloudflare: Allow updating IPv4 and IPv6 at the same time

This commit is contained in:
Sandro Jäckel 2020-01-01 04:22:11 +01:00
parent aea03be92f
commit 763463eaf3
No known key found for this signature in database
GPG key ID: 3AF5A43A3EECC2E5

View file

@ -4906,6 +4906,7 @@ sub nic_cloudflare_update {
my $hosts = join(',', @hosts);
my $key = $hosts[0];
my $ip = $config{$key}{'wantip'};
my $ipv6 = $config{$key}{'wantipv6'};
my $headers = "X-Auth-Email: $config{$key}{'login'}\n";
$headers .= "X-Auth-Key: $config{$key}{'password'}\n";
@ -4915,8 +4916,10 @@ sub nic_cloudflare_update {
for my $domain (@hosts) {
(my $hostname = $domain) =~ s/\.$config{$key}{zone}$//;
delete $config{$domain}{'wantip'};
delete $config{$domain}{'wantipv6'};
info("setting IP address to %s for %s", $ip, $domain);
info("setting IPv6 address to %s for %s", $ipv6, $domain);
verbose("UPDATE:","updating %s", $domain);
# Get zone ID
@ -4946,12 +4949,13 @@ sub nic_cloudflare_update {
}
info("zone ID is $zone_id");
foreach my $ip_addr ($ip, $ipv6) {
# Get DNS record ID
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records?";
if (is_ipv6($ip)) {
$url .= "type=AAAA&name=$domain";
} else {
if ($ip_addr eq $ip) {
$url .= "type=A&name=$domain";
} elsif ($ip_addr eq $ipv6) {
$url .= "type=AAAA&name=$domain";
}
$reply = geturl(opt('proxy'), $url, undef, undef, $headers);
@ -4979,7 +4983,7 @@ sub nic_cloudflare_update {
# Set domain
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records/$dns_rec_id";
my $data = "{\"content\":\"$ip\"}";
my $data = "{\"content\":\"$ip_addr\"}";
$reply = geturl(opt('proxy'), $url, undef, undef, $headers, "PATCH", $data);
unless ($reply) {
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
@ -4993,12 +4997,13 @@ sub nic_cloudflare_update {
if (!defined $response || !defined $response->{result}) {
failed ("invalid json or result.");
} else {
success ("%s -- Updated Successfully to %s", $domain, $ip);
success ("%s -- Updated Successfully to %s", $domain, $ip_addr);
}
}
# Cache
$config{$key}{'ip'} = $ip;
$config{$key}{'ipv6'} = $ipv6;
$config{$key}{'mtime'} = $now;
$config{$key}{'status'} = 'good';
}