From a3913891d57f16b0a8fa0852cc50c3d5a2244a7a Mon Sep 17 00:00:00 2001 From: Andreas Rupper <61623186+andreas-rupper@users.noreply.github.com> Date: Sat, 13 Jun 2020 22:32:41 +0200 Subject: [PATCH] Updated Cloudflare to use token auth and update ttl (#1726) --- .../src/etc/inc/plugins.inc.d/dyndns.inc | 6 +- .../inc/plugins.inc.d/dyndns/phpDynDNS.inc | 133 +++++++++++------- dns/dyndns/src/www/services_dyndns_edit.php | 23 ++- 3 files changed, 106 insertions(+), 56 deletions(-) diff --git a/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns.inc b/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns.inc index 02847b288..68b129280 100644 --- a/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns.inc +++ b/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns.inc @@ -100,8 +100,10 @@ function dyndns_list() 'azure' => 'Azure DNS', 'azurev6' => 'Azure DNS (v6)', 'citynetwork' => 'City Network', - 'cloudflare' => 'CloudFlare', - 'cloudflare-v6' => 'CloudFlare (v6)', + 'cloudflare' => 'Cloudflare', + 'cloudflare-v6' => 'Cloudflare (v6)', + 'cloudflare-token' => 'Cloudflare w/API token', + 'cloudflare-token-v6' => 'Cloudflare w/API token (v6)', 'custom' => 'Custom', 'custom-v6' => 'Custom (v6)', 'dhs' => 'DHS', diff --git a/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns/phpDynDNS.inc b/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns/phpDynDNS.inc index 1b0c7b698..7623744b8 100644 --- a/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns/phpDynDNS.inc +++ b/dns/dyndns/src/etc/inc/plugins.inc.d/dyndns/phpDynDNS.inc @@ -26,8 +26,10 @@ * - DNS-O-Matic (dnsomatic.com) * - Custom dynamic DNS (any URL) * - Custom dynamic DNS IPv6 (any URL) - * - CloudFlare (www.cloudflare.com) - * - CloudFlare IPv6 (www.cloudflare.com) + * - Cloudflare (www.cloudflare.com) + * - Cloudflare IPv6 (www.cloudflare.com) + * - Cloudflare API token (www.cloudflare.com) + * - Cloudflare API token IPv6 (www.cloudflare.com) * - Eurodns (eurodns.com) * - GratisDNS (gratisdns.dk) * - City Network (citynetwork.se) @@ -58,47 +60,49 @@ * - _debug() * - _checkIP() * +----------------------------------------------------+ - * DynDNS Dynamic - Last Tested: 12 July 2005 - * DynDNS Static - Last Tested: NEVER - * DynDNS Custom - Last Tested: NEVER - * No-IP - Last Tested: 20 July 2008 - * HN.org - Last Tested: 12 July 2005 - * EasyDNS - Last Tested: 20 July 2008 - * DHS - Last Tested: 12 July 2005 - * ZoneEdit - Last Tested: NEVER - * Dyns - Last Tested: NEVER - * ODS - Last Tested: 02 August 2005 - * FreeDNS - Last Tested: 23 Feb 2011 - * Loopia - Last Tested: NEVER - * StaticCling - Last Tested: 27 April 2006 - * DNSexit - Last Tested: 20 July 2008 - * Namecheap - Last Tested: 31 August 2010 - * HE.net - Last Tested: 7 July 2013 - * HE.net IPv6 - Last Tested: 7 July 2013 - * HE.net Tunnel - Last Tested: 28 June 2011 - * SelfHost - Last Tested: 26 December 2011 - * Amazon Route53 - Last Tested: 01 April 2012 - * DNS-O-Matic - Last Tested: 9 September 2010 - * CloudFlare - Last Tested: 16 April 2019 - * CloudFlare IPv6 - Last Tested: 16 April 2019 - * Eurodns - Last Tested: 25 July 2018 - * GratisDNS - Last Tested: 26 January 2020 - * OVH DynHOST - Last Tested: NEVER - * City Network - Last Tested: 13 November 2013 - * Duck DNS - Last Tested: 04 March 2015 - * Google Domains - Last Tested: 20 February 2017 - * STRATO - Last Tested: 09 May 2017 - * 3322 - Last Tested: 26 May 2017 - * Oray - Last Tested: 26 May 2017 - * regfish - Last Tested: 15 August 2017 - * regfish v6 - Last Tested: 15 August 2017 - * Amazon Route53 v6 - Last Tested: 19 November 2017 - * dynv6 - Last Tested: 25 June 2019 - * dynv6 v6 - Last Tested: 25 June 2019 - * DigitalOcean - Last Tested: 25 June 2019 - * Azure DNS - Last Tested: 16 October 2019 - * Linode - Last Tested: 25 February 2020 - * Linode v6 - Last Tested: 25 February 2020 + * DynDNS Dynamic - Last Tested: 12 July 2005 + * DynDNS Static - Last Tested: NEVER + * DynDNS Custom - Last Tested: NEVER + * No-IP - Last Tested: 20 July 2008 + * HN.org - Last Tested: 12 July 2005 + * EasyDNS - Last Tested: 20 July 2008 + * DHS - Last Tested: 12 July 2005 + * ZoneEdit - Last Tested: NEVER + * Dyns - Last Tested: NEVER + * ODS - Last Tested: 02 August 2005 + * FreeDNS - Last Tested: 23 Feb 2011 + * Loopia - Last Tested: NEVER + * StaticCling - Last Tested: 27 April 2006 + * DNSexit - Last Tested: 20 July 2008 + * Namecheap - Last Tested: 31 August 2010 + * HE.net - Last Tested: 7 July 2013 + * HE.net IPv6 - Last Tested: 7 July 2013 + * HE.net Tunnel - Last Tested: 28 June 2011 + * SelfHost - Last Tested: 26 December 2011 + * Amazon Route53 - Last Tested: 01 April 2012 + * DNS-O-Matic - Last Tested: 9 September 2010 + * Cloudflare - Last Tested: 16 April 2019 + * Cloudflare IPv6 - Last Tested: 16 April 2019 + * Cloudflare w/API token - Last Tested: 13 June 2020 + * Cloudflare w/API token v6 - Last Tested: NEVER + * Eurodns - Last Tested: 25 July 2018 + * GratisDNS - Last Tested: 26 January 2020 + * OVH DynHOST - Last Tested: NEVER + * City Network - Last Tested: 13 November 2013 + * Duck DNS - Last Tested: 04 March 2015 + * Google Domains - Last Tested: 20 February 2017 + * STRATO - Last Tested: 09 May 2017 + * 3322 - Last Tested: 26 May 2017 + * Oray - Last Tested: 26 May 2017 + * regfish - Last Tested: 15 August 2017 + * regfish v6 - Last Tested: 15 August 2017 + * Amazon Route53 v6 - Last Tested: 19 November 2017 + * dynv6 - Last Tested: 25 June 2019 + * dynv6 v6 - Last Tested: 25 June 2019 + * DigitalOcean - Last Tested: 25 June 2019 + * Azure DNS - Last Tested: 16 October 2019 + * Linode - Last Tested: 25 February 2020 + * Linode v6 - Last Tested: 25 February 2020 * +====================================================+ * * @author E.Kristensen @@ -251,6 +255,16 @@ class updatedns $this->_error(9); } break; + case 'cloudflare-token': + case 'cloudflare-token-v6': + if (!$dnsPass) { + $this->_error(4); + } elseif (!$dnsHost) { + $this->_error(5); + } elseif (!$dnsTTL) { + $this->_error(9); + } + break; default: if (!$dnsUser) { $this->_error(3); @@ -271,6 +285,7 @@ class updatedns case 'linode-v6': case 'regfish-v6': case 'route53-v6': + case 'cloudflare-token-v6': $this->_useIPv6 = true; break; default: @@ -319,6 +334,8 @@ class updatedns case 'citynetwork': case 'cloudflare': case 'cloudflare-v6': + case 'cloudflare-token': + case 'cloudflare-token-v6': case 'custom': case 'custom-v6': case 'dhs': @@ -749,21 +766,35 @@ class updatedns break; case 'cloudflare': case 'cloudflare-v6': + case 'cloudflare-token': + case 'cloudflare-token-v6': $baseUrl = 'https://api.cloudflare.com/client/v4'; $fqdn = str_replace(' ', '', $this->_dnsHost); $recordType = ($this->_useIPv6) ? 'AAAA' : 'A'; + $ttlData = intval($this->_dnsTTL) < 1 ? 1 : intval($this->_dnsTTL); $hostData = array( "content" => "{$this->_dnsIP}", "type" => $recordType, - "name" => $fqdn + "name" => $fqdn, + "ttl" => $ttlData ); + // Determine if service is token based or user/password based and define appropriate header + if (strpos($this->_dnsService, 'token') !== false) { + $headerAuth = array( + "Authorization: Bearer {$this->_dnsPass}", + 'Content-Type: application/json' + ); + } else { + $headerAuth = array( + "X-Auth-Email: {$this->_dnsUser}", + "X-Auth-Key: {$this->_dnsPass}", + 'Content-Type: application/json' + ); + } + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_HTTPHEADER, array( - "X-Auth-Email: {$this->_dnsUser}", - "X-Auth-Key: {$this->_dnsPass}", - 'Content-Type: application/json' - )); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headerAuth); // Get all zone info $zonesUrl = "$baseUrl/zones"; @@ -1322,12 +1353,14 @@ class updatedns break; case 'cloudflare': case 'cloudflare-v6': + case 'cloudflare-token': + case 'cloudflare-token-v6': $output = json_decode($data); if ($output->result->content === $this->_dnsIP) { $status = "Dynamic DNS: (Success) {$this->_dnsHost} updated to {$this->_dnsIP}"; $successful_update = true; } elseif ($output->errors[0]->code === 9103) { - $status = "Dynamic DNS ({$this->_dnsHost}): ERROR - Invalid Credentials! Don't forget to use API Key for password field with CloudFlare."; + $status = "Dynamic DNS ({$this->_dnsHost}): ERROR - Invalid Credentials! Don't forget to use API Key for password field with Cloudflare."; } elseif (($output->success) && (!$output->result[0]->id)) { $status = "Dynamic DNS ({$this->_dnsHost}): ERROR - Zone ID was not found."; } else { diff --git a/dns/dyndns/src/www/services_dyndns_edit.php b/dns/dyndns/src/www/services_dyndns_edit.php index bb327f5d3..bf3d92e87 100644 --- a/dns/dyndns/src/www/services_dyndns_edit.php +++ b/dns/dyndns/src/www/services_dyndns_edit.php @@ -75,7 +75,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { } $input_errors = array(); $pconfig = $_POST; - if(($pconfig['type'] == "freedns" || $pconfig['type'] == "linode" || $pconfig['type'] == "linode-v6" || $pconfig['type'] == "namecheap") && $pconfig['username'] == "") { + if(($pconfig['type'] == "freedns" || $pconfig['type'] == "linode" || $pconfig['type'] == "linode-v6" || $pconfig['type'] == "namecheap" || $pconfig['type'] == "cloudflare-token" || $pconfig['type'] == "cloudflare-token-v6") && $pconfig['username'] == "") { $pconfig['username'] = "none"; } @@ -114,6 +114,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { switch ($pconfig['type']) { case 'cloudflare': case 'cloudflare-v6': + case 'cloudflare-token': + case 'cloudflare-token-v6': case 'eurodns': case 'googledomains': case 'linode': @@ -137,6 +139,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { $input_errors[] = gettext("The username contains invalid characters."); } + if ((string)((int)$pconfig['ttl']) != $pconfig['ttl']) { + $input_errors[] = gettext("The TTL value needs to be a valid integer number."); + } if (count($input_errors) == 0) { $dyndns = array(); @@ -212,6 +217,15 @@ include("head.inc"); case "azurev6": $(".type_azure").show(); break; + case 'cloudflare': + case 'cloudflare-v6': + $(".type_default").show(); + $(".type_cloudflare").show(); + break; + case 'cloudflare-token': + case 'cloudflare-token-v6': + $(".type_cloudflare").show(); + break; default: $(".type_default").show(); break; @@ -339,7 +353,7 @@ include("head.inc"); = gettext("Verify SSL peer") ?> -