Commit adb3081e authored by Franco Fichtner's avatar Franco Fichtner

dyndns: update cloudflare service, closes #423

Taken from: https://forum.pfsense.org/index.php?topic=87436

(cherry picked from commit 55586d3c)
parent b70fc853
......@@ -607,11 +607,43 @@
curl_setopt($ch, CURLOPT_URL, $server);
break;
case 'cloudflare':
$dnsServer ='www.cloudflare.com';
$dnsServer ='api.cloudflare.com';
$dnsHost = str_replace(' ','', $this->_dnsHost);
$URL = "https://{$dnsServer}/api.html?a=DIUP&email={$this->_dnsUser}&tkn={$this->_dnsPass}&ip={$this->_dnsIP}&hosts={$dnsHost}";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $URL);
$host_names = explode('.', $dnsHost);
$bottom_host_name = $host_names[count($host_names) - 2] . '.' . $host_names[count($host_names) - 1];
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'
));
// Get zone ID
$getZoneId = "https://{$dnsServer}/client/v4/zones/?name={$bottom_host_name}";
curl_setopt($ch, CURLOPT_URL, $getZoneId);
$output = json_decode(curl_exec($ch));
$zone = $output->result[0]->id;
if ($zone){ // If zone ID was found get host ID
$getHostId = "https://{$dnsServer}/client/v4/zones/{$zone}/dns_records?name={$this->_dnsHost}";
curl_setopt($ch, CURLOPT_URL, $getHostId);
$output = json_decode(curl_exec($ch));
$host = $output->result[0]->id;
if ($host){ // If host ID was found update host
$hostData = array(
"content" => "{$this->_dnsIP}",
"type" => "A",
"name" => "{$this->_dnsHost}",
"proxiable" => false,
"proxied" => false
);
$data_json = json_encode($hostData);
$updateHostId = "https://{$dnsServer}/client/v4/zones/{$zone}/dns_records/{$host}";
curl_setopt($ch, CURLOPT_URL, $updateHostId);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
}
}
break;
case 'eurodns':
if ($this->_dnsVerboseLog)
......@@ -1111,35 +1143,20 @@
$status = "Dynamic DNS: (Error) Result did not match.";
break;
case 'cloudflare':
// recieve multipe results
$data = explode("\n",$data);
$lines = count($data)-1;
// loop over the lines
for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++){
$resp = $data[$pos];
if (preg_match('/UAUTH/i', $resp)) {
$status = "Dynamic DNS: The username specified is not authorized to update this hostname and domain.";
} else if (preg_match('/NOHOST/i', $resp)) {
$status = "Dynamic DNS: No valid FQDN (fully qualified domain name) was specified";
} else if (preg_match('/INVLDHST/i', $resp)) {
$status = "Dynamic DNS: 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.";
} else if (preg_match('/INVLDIP/i', $resp)) {
$status = "Dynamic DNS: The IP address given is not valid.";
} else if (preg_match('/DUPHST/i', $resp)) {
$status = "Dynamic DNS: Duplicate values exist for a record. Only single values for records are supported currently.";
} else if (preg_match('/NOUPDATE/i', $resp)) {
$status = "Dynamic DNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
$successful_update = true; //success if it is the same so that it saves
} else if (preg_match('/OK/i', $resp)) {
$status = "Dynamic DNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
$successful_update = true;
} else {
$status = "Dynamic DNS: (Unknown Response)";
log_error("Dynamic DNS: PAYLOAD: {$resp}");
$this->_debug($resp);
}
log_error($status);
$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.";
}
elseif (($output->success) && (!$output->result[0]->id)) {
$status = "Dynamic DNS ({$this->_dnsHost}): ERROR - Zone or Host ID was not found, check your hostname.";
}
else {
$status = "Dynamic DNS ({$this->_dnsHost}): UNKNOWN ERROR - {$output->errors[0]->message}";
log_error("Dynamic DNS ({$this->_dnsHost}): PAYLOAD: {$data}");
}
break;
case 'eurodns':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment