Merge pull request #2 from roberthawdon/cloudflare-json

Cloudflare json
This commit is contained in:
Robert Hawdon 2014-10-03 21:39:24 +01:00
commit 0b43bc84ba
3 changed files with 91 additions and 87 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ patches
release release
.svn .svn
.cvsignore .cvsignore
*~

View file

@ -23,6 +23,7 @@ Dynamic DNS services currently supported include:
ChangeIP - See http://www.changeip.com/ for details ChangeIP - See http://www.changeip.com/ for details
dtdns - See http://www.dtdns.com/ for details dtdns - See http://www.dtdns.com/ for details
nsupdate - See nsupdate(1) and ddns-confgen(8) for details nsupdate - See nsupdate(1) and ddns-confgen(8) for details
CloudFlare - See https://www.cloudflare.com/ for defails
DDclient now supports many of cable/dsl broadband routers. DDclient now supports many of cable/dsl broadband routers.
@ -37,8 +38,9 @@ REQUIREMENTS:
- one or more accounts from one of the dynamic DNS services - one or more accounts from one of the dynamic DNS services
- Perl 5.004 or later - Perl 5.014 or later
(you need the IO::Socket::SSL perl library for ssl-support) (you need the IO::Socket::SSL perl library for ssl-support
and JSON::Any perl library for JSON support)
- Linux or probably any common Unix system - Linux or probably any common Unix system

139
ddclient
View file

@ -13,14 +13,19 @@
# Support for multiple IP numbers added by # Support for multiple IP numbers added by
# Astaro AG, Ingo Schwarze <ischwarze-OOs/4mkCeqbQT0dZR+AlfA@public.gmane.org> September 16, 2008 # Astaro AG, Ingo Schwarze <ischwarze-OOs/4mkCeqbQT0dZR+AlfA@public.gmane.org> September 16, 2008
# #
# Modified to work with Cloudflare by Robert Ian Hawdon 2012-07-16: http://robertianhawdon.me.uk/ # Support for multiple domain support for Namecheap by Robert Ian Hawdon 2010-09-03: https://robertianhawdon.me.uk/
#
# Initial Cloudflare support by Ian Pye, updated by Robert Ian Hawdon 2012-07-16
# Further updates by Peter Roberts to support the new API 2013-09-26, 2014-06-22: http://blog.peter-r.co.uk/
#
# #
###################################################################### ######################################################################
require 5.004; require 5.014;
use strict; use strict;
use Getopt::Long; use Getopt::Long;
use Sys::Hostname; use Sys::Hostname;
use IO::Socket; use IO::Socket;
use JSON::Any;
# my ($VERSION) = q$Revision: 161 $ =~ /(\d+)/; # my ($VERSION) = q$Revision: 161 $ =~ /(\d+)/;
@ -437,6 +442,7 @@ my %variables = (
}, },
'cloudflare-common-defaults' => { 'cloudflare-common-defaults' => {
'server' => setv(T_FQDNP, 1, 0, 1, 'www.cloudflare.com', undef), 'server' => setv(T_FQDNP, 1, 0, 1, 'www.cloudflare.com', 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),
'mx' => setv(T_OFQDN, 0, 1, 1, '', undef), 'mx' => setv(T_OFQDN, 0, 1, 1, '', undef),
@ -618,7 +624,7 @@ my @opt = (
"usage: ${program} [options]", "usage: ${program} [options]",
"options are:", "options are:",
[ "daemon", "=s", "-daemon delay : run as a daemon, specify delay as an interval." ], [ "daemon", "=s", "-daemon delay : run as a daemon, specify delay as an interval." ],
+ [ "foreground", "!", "-foreground : do not fork" ], [ "foreground", "!", "-foreground : do not fork" ],
[ "proxy", "=s", "-proxy host : use 'host' as the HTTP proxy" ], [ "proxy", "=s", "-proxy host : use 'host' as the HTTP proxy" ],
[ "server", "=s", "-server host : update DNS information on 'host'" ], [ "server", "=s", "-server host : update DNS information on 'host'" ],
[ "protocol", "=s", "-protocol type : update protocol used" ], [ "protocol", "=s", "-protocol type : update protocol used" ],
@ -3907,8 +3913,6 @@ EoINSTR3
## ##
## written by Ian Pye ## written by Ian Pye
## ##
## https://www.cloudflare.com/api.html?a=DIUP&u=myemail@mydomain.com&tkn=SecretPass&ip=192.168.10.4&hosts=example.com
##
###################################################################### ######################################################################
sub nic_cloudflare_examples { sub nic_cloudflare_examples {
return <<EoEXAMPLE; return <<EoEXAMPLE;
@ -3926,12 +3930,14 @@ Configuration variables applicable to the 'cloudflare' protocol are:
Example ${program}.conf file entries: Example ${program}.conf file entries:
## single host update ## single host update
protocol=cloudflare, \\ protocol=cloudflare, \\
zone=dns.zone, \\
login=my-cloudflare.com-login, \\ login=my-cloudflare.com-login, \\
password=my-cloudflare.com-secure-token \\ password=my-cloudflare.com-secure-token \\
myhost.com myhost.com
## multiple host update to the custom DNS service ## multiple host update to the custom DNS service
protocol=cloudflare, \\ protocol=cloudflare, \\
zone=dns.zone, \\
login=my-cloudflare.com-login, \\ login=my-cloudflare.com-login, \\
password=my-cloudflare.com-secure-token \\ password=my-cloudflare.com-secure-token \\
my-toplevel-domain.com,my-other-domain.com my-toplevel-domain.com,my-other-domain.com
@ -3944,90 +3950,85 @@ sub nic_cloudflare_update {
debug("\nnic_cloudflare_update -------------------"); debug("\nnic_cloudflare_update -------------------");
## group hosts with identical attributes together ## group hosts with identical attributes together
my %groups = group_hosts_by([ @_ ], [ qw(ssh login password server wildcard mx backupmx) ]); my %groups = group_hosts_by([ @_ ], [ qw(ssh login password server wildcard mx backupmx zone) ]);
## each host is in a group by itself
##my %groups = map { $_ => [ $_ ] } @_;
my %errors = (
'E_NOUPDATE' => 'No changes made to the hostname(s). Continual updates with no changes lead to blocked clients.',
'E_NOHOST' => 'No valid FQDN (fully qualified domain name) was specified',
'E_INVLDHST'=> 'An invalid hostname was specified. This may be due to the fact the hostname has not been created in the system. Creating new host names via clients is not supported.',
'E_UNAUTH' => 'The username specified is not authorized to update this hostname and domain.',
'E_INVLDIP' => 'The IP address given is not valid.',
'E_DUPHST' => 'Duplicate values exist for a record. Only single values for records are supported currently.',
);
## update each set of hosts that had similar configurations ## update each set of hosts that had similar configurations
foreach my $sig (keys %groups) { foreach my $sig (keys %groups) {
my @hosts = @{$groups{$sig}}; my @hosts = @{$groups{$sig}};
my $hosts = join(',', @hosts); my $hosts = join(',', @hosts);
my $h = $hosts[0]; my $key = $hosts[0];
my $ip = $config{$h}{'wantip'}; my $ip = $config{$key}{'wantip'};
delete $config{$_}{'wantip'} foreach @hosts; # FQDNs
for my $domain (@hosts) {
my $hostname = $domain =~ s/\.$config{$key}{zone}$//r;
delete $config{$domain}{'wantip'};
info("setting IP address to %s for %s", $ip, $hosts); info("setting IP address to %s for %s", $ip, $domain);
verbose("UPDATE:","updating %s", $hosts); verbose("UPDATE:","updating %s", $domain);
my $url; # Get domain ID
$url = "https://$config{$h}{'server'}/api.html?a=DIUP"; my $url = "https://$config{$key}{'server'}/api_json.html?a=rec_load_all";
$url .= "&hosts=$hosts"; $url .= "&z=".$config{$key}{'zone'};
$url .= "&u=".$config{$h}{'login'}; $url .= "&email=".$config{$key}{'login'};
$url .= "&tkn=".$config{$h}{'password'}; $url .= "&tkn=".$config{$key}{'password'};
$url .= "&ip=";
$url .= "$ip" if $ip;
my $reply = geturl(opt('proxy'), $url); my $reply = geturl(opt('proxy'), $url);
if (!defined($reply) || !$reply) { unless ($reply) {
failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $domain, $config{$key}{'server'});
last; last;
} }
last if !header_ok($hosts, $reply); last if !header_ok($domain, $reply);
my @reply = split /\n/, $reply; # Strip header
my @body = (); $reply =~ s/^.*?\n\n//s;
my $in_header = 1; my $response = JSON::Any->jsonToObj($reply);
foreach my $line (@reply) { if ($response->{result} eq 'error') {
if ($line eq "") { failed ("%s", $response->{msg});
$in_header = 0; next;
} elsif (!$in_header) {
push(@body, $line);
}
} }
if ($reply =~ /E_UNAUTH/) { # Pull the ID out of the json, messy
failed ("%s", $errors{"E_UNAUTH"}); my ($id) = map { $_->{name} eq $domain ? $_->{rec_id} : () } @{ $response->{response}->{recs}->{objs} };
} elsif ($reply =~ /E_NOHOST/) { unless($id) {
failed ("%s", $errors{"E_NOHOST"}); failed("updating %s: No domain ID found.", $domain);
} elsif ($reply =~ /E_INVLDHST/) { next;
failed ("%s", $errors{"E_INVLDHST"}); }
} elsif ($reply =~ /E_INVLDIP/) {
failed ("%s", $errors{"E_INVLDIP"}); # Set domain
} elsif ($reply =~ /E_DUPHST/) { $url = "https://$config{$key}{'server'}/api_json.html?a=rec_edit&type=A&ttl=1";
failed ("%s", $errors{"E_DUPHST"}); $url .= "&name=$hostname";
$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) {
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'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});
} else { } else {
foreach my $line (@body) { success ("%s -- Updated Successfully to %s", $domain, $ip);
my @res = split / /, $line;
if ($res[1] eq "E_NOUPDATE") {
$config{$res[0]}{'ip'} = $ip;
$config{$res[0]}{'mtime'} = $now;
$config{$res[0]}{'status'} = 'good';
warning ("%s -- %s", $res[0], $errors{"E_NOUPDATE"});
} elsif ($res[1] eq "OK") {
$config{$res[0]}{'ip'} = $ip;
$config{$res[0]}{'mtime'} = $now;
$config{$res[0]}{'status'} = 'good';
success ("%s -- Updated Successfully to %s", $res[0], $ip);
}
} }
# Cache
$config{$key}{'ip'} = $ip;
$config{$key}{'mtime'} = $now;
$config{$key}{'status'} = 'good';
} }
} }
} }
######################################################################
# vim: ai ts=4 sw=4 tw=78 :
__END__ __END__