Merge pull request #42 from Harry-Xue/master

Update CloudFlare from API v1 to API v4.

Patch provided by @Harry-Xue by pull-request #42
This commit is contained in:
wimpunk 2016-05-25 08:47:27 +02:00
commit df883fd091

View file

@ -441,7 +441,7 @@ my %variables = (
'tcp' => setv(T_BOOL, 0, 1, 1, 0, undef), 'tcp' => setv(T_BOOL, 0, 1, 1, 0, undef),
}, },
'cloudflare-common-defaults' => { 'cloudflare-common-defaults' => {
'server' => setv(T_FQDNP, 1, 0, 1, 'www.cloudflare.com', undef), 'server' => setv(T_FQDNP, 1, 0, 1, 'api.cloudflare.com/client/v4', undef),
'zone' => setv(T_FQDN, 1, 0, 1, '', undef), 'zone' => setv(T_FQDN, 1, 0, 1, '', undef),
'static' => setv(T_BOOL, 0, 1, 1, 0, undef), 'static' => setv(T_BOOL, 0, 1, 1, 0, undef),
'wildcard' => setv(T_BOOL, 0, 1, 1, 0, undef), 'wildcard' => setv(T_BOOL, 0, 1, 1, 0, undef),
@ -638,7 +638,7 @@ my %services = (
'update' => \&nic_cloudflare_update, 'update' => \&nic_cloudflare_update,
'examples' => \&nic_cloudflare_examples, 'examples' => \&nic_cloudflare_examples,
'variables' => merge( 'variables' => merge(
{ 'server' => setv(T_FQDNP, 1, 0, 1, 'www.cloudflare.com', undef) }, { 'server' => setv(T_FQDNP, 1, 0, 1, 'api.cloudflare.com/client/v4', undef) },
{ 'min-interval' => setv(T_DELAY, 0, 0, 1, interval('5m'), 0),}, { 'min-interval' => setv(T_DELAY, 0, 0, 1, interval('5m'), 0),},
$variables{'cloudflare-common-defaults'}, $variables{'cloudflare-common-defaults'},
$variables{'service-common-defaults'}, $variables{'service-common-defaults'},
@ -1952,6 +1952,9 @@ sub geturl {
my $url = shift || ''; my $url = shift || '';
my $login = shift || ''; my $login = shift || '';
my $password = shift || ''; my $password = shift || '';
my $headers = shift || '';
my $method = shift || 'GET';
my $data = shift || '';
my ($peer, $server, $port, $default_port, $use_ssl); my ($peer, $server, $port, $default_port, $use_ssl);
my ($sd, $rq, $request, $reply); my ($sd, $rq, $request, $reply);
@ -1992,7 +1995,7 @@ sub geturl {
my $to = sprintf "%s%s", $server, $proxy ? " via proxy $peer:$port" : ""; my $to = sprintf "%s%s", $server, $proxy ? " via proxy $peer:$port" : "";
verbose("CONNECT:", "%s", $to); verbose("CONNECT:", "%s", $to);
$request = "GET "; $request = "$method ";
$request .= "http://$server" if $proxy; $request .= "http://$server" if $proxy;
$request .= "/$url HTTP/1.0\n"; $request .= "/$url HTTP/1.0\n";
$request .= "Host: $server\n"; $request .= "Host: $server\n";
@ -2001,7 +2004,10 @@ sub geturl {
$request .= "Authorization: Basic $auth\n" if $login || $password; $request .= "Authorization: Basic $auth\n" if $login || $password;
$request .= "User-Agent: ${program}/${version}\n"; $request .= "User-Agent: ${program}/${version}\n";
$request .= "Connection: close\n"; $request .= "Connection: close\n";
$request .= "$headers\n";
$request .= "Content-Length: ".length($data)."\n" if $data;
$request .= "\n"; $request .= "\n";
$request .= $data;
## make sure newlines are <cr><lf> for some pedantic proxy servers ## make sure newlines are <cr><lf> for some pedantic proxy servers
($rq = $request) =~ s/\n/\r\n/g; ($rq = $request) =~ s/\n/\r\n/g;
@ -4195,7 +4201,7 @@ The 'cloudflare' protocol is used by DNS service offered by www.cloudflare.com.
Configuration variables applicable to the 'cloudflare' protocol are: Configuration variables applicable to the 'cloudflare' protocol are:
protocol=cloudflare ## protocol=cloudflare ##
server=fqdn.of.service ## defaults to www.cloudflare.com server=fqdn.of.service ## defaults to api.cloudflare.com/client/v4
login=service-login ## login name and password registered with the service login=service-login ## login name and password registered with the service
password=service-password ## password=service-password ##
fully.qualified.host ## the host registered with the service. fully.qualified.host ## the host registered with the service.
@ -4232,6 +4238,10 @@ sub nic_cloudflare_update {
my $key = $hosts[0]; my $key = $hosts[0];
my $ip = $config{$key}{'wantip'}; my $ip = $config{$key}{'wantip'};
my $headers = "X-Auth-Email: $config{$key}{'login'}\n";
$headers .= "X-Auth-Key: $config{$key}{'password'}\n";
$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}$//;
@ -4240,13 +4250,11 @@ sub nic_cloudflare_update {
info("setting IP address to %s for %s", $ip, $domain); info("setting IP address to %s for %s", $ip, $domain);
verbose("UPDATE:","updating %s", $domain); verbose("UPDATE:","updating %s", $domain);
# Get domain ID # Get zone ID
my $url = "https://$config{$key}{'server'}/api_json.html?a=rec_load_all"; my $url = "https://$config{$key}{'server'}/zones?";
$url .= "&z=".$config{$key}{'zone'}; $url .= "name=".$config{$key}{'zone'};
$url .= "&email=".$config{$key}{'login'};
$url .= "&tkn=".$config{$key}{'password'};
my $reply = geturl(opt('proxy'), $url); my $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;
@ -4262,24 +4270,44 @@ sub nic_cloudflare_update {
} }
# Pull the ID out of the json, messy # Pull the ID out of the json, messy
my ($id) = map { $_->{name} eq $domain ? $_->{rec_id} : () } @{ $response->{response}->{recs}->{objs} }; my ($zone_id) = map { $_->{name} eq $config{$key}{'zone'} ? $_->{id} : () } @{ $response->{result} };
unless($id) { unless($zone_id) {
failed("updating %s: No domain ID found.", $domain); failed("updating %s: No zone ID found.", $config{$key}{'zone'});
next; next;
} }
info("zone ID is $zone_id");
# Get DNS record ID
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records?";
$url .= "type=A&name=$domain";
$reply = geturl(opt('proxy'), $url, undef, undef, $headers);
unless ($reply) {
failed("updating %s: Could not connect to %s.", $domain, $config{$key}{'server'});
last;
}
last if !header_ok($domain, $reply);
# Strip header
$reply =~ s/^.*?\n\n//s;
$response = JSON::Any->jsonToObj($reply);
if ($response->{result} eq 'error') {
failed ("%s", $response->{msg});
next;
}
# Pull the ID out of the json, messy
my ($dns_rec_id) = map { $_->{name} eq $domain ? $_->{id} : () } @{ $response->{result} };
unless($dns_rec_id) {
failed("updating %s: No DNS record ID found.", $domain);
next;
}
info("DNS record ID is $dns_rec_id");
# Set domain # Set domain
$url = "https://$config{$key}{'server'}/api_json.html?a=rec_edit&type=A"; $url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records/$dns_rec_id";
$url .= "&ttl=".$config{$key}{'ttl'}; my $data = "{\"content\":\"$ip\"}";
$url .= "&name=$hostname"; $reply = geturl(opt('proxy'), $url, undef, undef, $headers, "PATCH", $data);
$url .= "&z=".$config{$key}{'zone'};
$url .= "&id=".$id;
$url .= "&email=".$config{$key}{'login'};
$url .= "&tkn=".$config{$key}{'password'};
$url .= "&content=";
$url .= "$ip" if $ip;
$reply = geturl(opt('proxy'), $url);
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;