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:
commit
df883fd091
1 changed files with 52 additions and 24 deletions
76
ddclient
76
ddclient
|
@ -441,7 +441,7 @@ my %variables = (
|
|||
'tcp' => setv(T_BOOL, 0, 1, 1, 0, undef),
|
||||
},
|
||||
'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),
|
||||
'static' => 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,
|
||||
'examples' => \&nic_cloudflare_examples,
|
||||
'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),},
|
||||
$variables{'cloudflare-common-defaults'},
|
||||
$variables{'service-common-defaults'},
|
||||
|
@ -1952,6 +1952,9 @@ sub geturl {
|
|||
my $url = shift || '';
|
||||
my $login = shift || '';
|
||||
my $password = shift || '';
|
||||
my $headers = shift || '';
|
||||
my $method = shift || 'GET';
|
||||
my $data = shift || '';
|
||||
my ($peer, $server, $port, $default_port, $use_ssl);
|
||||
my ($sd, $rq, $request, $reply);
|
||||
|
||||
|
@ -1992,7 +1995,7 @@ sub geturl {
|
|||
my $to = sprintf "%s%s", $server, $proxy ? " via proxy $peer:$port" : "";
|
||||
verbose("CONNECT:", "%s", $to);
|
||||
|
||||
$request = "GET ";
|
||||
$request = "$method ";
|
||||
$request .= "http://$server" if $proxy;
|
||||
$request .= "/$url HTTP/1.0\n";
|
||||
$request .= "Host: $server\n";
|
||||
|
@ -2001,7 +2004,10 @@ sub geturl {
|
|||
$request .= "Authorization: Basic $auth\n" if $login || $password;
|
||||
$request .= "User-Agent: ${program}/${version}\n";
|
||||
$request .= "Connection: close\n";
|
||||
$request .= "$headers\n";
|
||||
$request .= "Content-Length: ".length($data)."\n" if $data;
|
||||
$request .= "\n";
|
||||
$request .= $data;
|
||||
|
||||
## make sure newlines are <cr><lf> for some pedantic proxy servers
|
||||
($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:
|
||||
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
|
||||
password=service-password ##
|
||||
fully.qualified.host ## the host registered with the service.
|
||||
|
@ -4232,6 +4238,10 @@ sub nic_cloudflare_update {
|
|||
my $key = $hosts[0];
|
||||
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
|
||||
for my $domain (@hosts) {
|
||||
(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);
|
||||
verbose("UPDATE:","updating %s", $domain);
|
||||
|
||||
# Get domain ID
|
||||
my $url = "https://$config{$key}{'server'}/api_json.html?a=rec_load_all";
|
||||
$url .= "&z=".$config{$key}{'zone'};
|
||||
$url .= "&email=".$config{$key}{'login'};
|
||||
$url .= "&tkn=".$config{$key}{'password'};
|
||||
# Get zone ID
|
||||
my $url = "https://$config{$key}{'server'}/zones?";
|
||||
$url .= "name=".$config{$key}{'zone'};
|
||||
|
||||
my $reply = geturl(opt('proxy'), $url);
|
||||
my $reply = geturl(opt('proxy'), $url, undef, undef, $headers);
|
||||
unless ($reply) {
|
||||
failed("updating %s: Could not connect to %s.", $domain, $config{$key}{'server'});
|
||||
last;
|
||||
|
@ -4262,24 +4270,44 @@ sub nic_cloudflare_update {
|
|||
}
|
||||
|
||||
# Pull the ID out of the json, messy
|
||||
my ($id) = map { $_->{name} eq $domain ? $_->{rec_id} : () } @{ $response->{response}->{recs}->{objs} };
|
||||
unless($id) {
|
||||
failed("updating %s: No domain ID found.", $domain);
|
||||
my ($zone_id) = map { $_->{name} eq $config{$key}{'zone'} ? $_->{id} : () } @{ $response->{result} };
|
||||
unless($zone_id) {
|
||||
failed("updating %s: No zone ID found.", $config{$key}{'zone'});
|
||||
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;
|
||||
}
|
||||
|
||||
# Set domain
|
||||
$url = "https://$config{$key}{'server'}/api_json.html?a=rec_edit&type=A";
|
||||
$url .= "&ttl=".$config{$key}{'ttl'};
|
||||
$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;
|
||||
# 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");
|
||||
|
||||
$reply = geturl(opt('proxy'), $url);
|
||||
# Set domain
|
||||
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records/$dns_rec_id";
|
||||
my $data = "{\"content\":\"$ip\"}";
|
||||
$reply = geturl(opt('proxy'), $url, undef, undef, $headers, "PATCH", $data);
|
||||
unless ($reply) {
|
||||
failed("updating %s: Could not connect to %s.", $domain, $config{$domain}{'server'});
|
||||
last;
|
||||
|
|
Loading…
Reference in a new issue