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,17 +4906,20 @@ sub nic_cloudflare_update {
my $hosts = join(',', @hosts); my $hosts = join(',', @hosts);
my $key = $hosts[0]; my $key = $hosts[0];
my $ip = $config{$key}{'wantip'}; my $ip = $config{$key}{'wantip'};
my $ipv6 = $config{$key}{'wantipv6'};
my $headers = "X-Auth-Email: $config{$key}{'login'}\n"; my $headers = "X-Auth-Email: $config{$key}{'login'}\n";
$headers .= "X-Auth-Key: $config{$key}{'password'}\n"; $headers .= "X-Auth-Key: $config{$key}{'password'}\n";
$headers .= "Content-Type: application/json"; $headers .= "Content-Type: application/json";
# FQDNs # FQDNs
for my $domain (@hosts) { for my $domain (@hosts) {
(my $hostname = $domain) =~ s/\.$config{$key}{zone}$//; (my $hostname = $domain) =~ s/\.$config{$key}{zone}$//;
delete $config{$domain}{'wantip'}; delete $config{$domain}{'wantip'};
delete $config{$domain}{'wantipv6'};
info("setting IP address to %s for %s", $ip, $domain); 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); verbose("UPDATE:","updating %s", $domain);
# Get zone ID # Get zone ID
@ -4946,59 +4949,61 @@ sub nic_cloudflare_update {
} }
info("zone ID is $zone_id"); info("zone ID is $zone_id");
# Get DNS record ID foreach my $ip_addr ($ip, $ipv6) {
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records?"; # Get DNS record ID
if (is_ipv6($ip)) { $url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records?";
$url .= "type=AAAA&name=$domain"; if ($ip_addr eq $ip) {
} else { $url .= "type=A&name=$domain";
$url .= "type=A&name=$domain"; } elsif ($ip_addr eq $ipv6) {
} $url .= "type=AAAA&name=$domain";
}
$reply = geturl(opt('proxy'), $url, undef, undef, $headers); $reply = geturl(opt('proxy'), $url, undef, undef, $headers);
unless ($reply) { unless ($reply) {
failed("updating %s: Could not connect to %s.", $domain, $config{$key}{'server'}); failed("updating %s: Could not connect to %s.", $domain, $config{$key}{'server'});
last; last;
} }
last if !header_ok($domain, $reply); last if !header_ok($domain, $reply);
# Strip header # Strip header
$reply =~ s/^.*?\n\n//s; $reply =~ s/^.*?\n\n//s;
$response = eval {decode_json($reply)}; $response = eval {decode_json($reply)};
if (!defined $response || !defined $response->{result}) { if (!defined $response || !defined $response->{result}) {
failed ("invalid json or result."); failed ("invalid json or result.");
next; next;
} }
# Pull the ID out of the json, messy # Pull the ID out of the json, messy
my ($dns_rec_id) = map { $_->{name} eq $domain ? $_->{id} : () } @{ $response->{result} }; my ($dns_rec_id) = map { $_->{name} eq $domain ? $_->{id} : () } @{ $response->{result} };
unless($dns_rec_id) { unless($dns_rec_id) {
failed("updating %s: No DNS record ID found.", $domain); failed("updating %s: No DNS record ID found.", $domain);
next; next;
} }
info("DNS record ID is $dns_rec_id"); info("DNS record ID is $dns_rec_id");
# Set domain # Set domain
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records/$dns_rec_id"; $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); $reply = geturl(opt('proxy'), $url, undef, undef, $headers, "PATCH", $data);
unless ($reply) { unless ($reply) {
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'}); failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
last; last;
} }
last if !header_ok($domain, $reply); last if !header_ok($domain, $reply);
# Strip header
$reply =~ s/^.*?\n\n//s;
$response = eval {decode_json($reply)};
if (!defined $response || !defined $response->{result}) {
failed ("invalid json or result.");
} else {
success ("%s -- Updated Successfully to %s", $domain, $ip);
# Strip header
$reply =~ s/^.*?\n\n//s;
$response = eval {decode_json($reply)};
if (!defined $response || !defined $response->{result}) {
failed ("invalid json or result.");
} else {
success ("%s -- Updated Successfully to %s", $domain, $ip_addr);
}
} }
# Cache # Cache
$config{$key}{'ip'} = $ip; $config{$key}{'ip'} = $ip;
$config{$key}{'ipv6'} = $ipv6;
$config{$key}{'mtime'} = $now; $config{$key}{'mtime'} = $now;
$config{$key}{'status'} = 'good'; $config{$key}{'status'} = 'good';
} }