What happens is:
- The IPv4 A record gets deleted
- A second AAAA record (for the same domain) gets added with the new IP
My fault or?
Here’s the script:
<?php
//
// Authorisation and config variables
//
$headers = [
'X-Auth-Email: EMAIL',
'X-Auth-Key: API_KEY',
'Content-Type: application/json'
];
// Type, domain and record
$type = "AAAA";
$domain = "zkitzo.one";
$record = "zkitzo.one";
//
// Functions (https://www.php.net/manual/en/function.array-search.php#122288)
//
function array_recursive_search_key_map($needle, $haystack) {
foreach($haystack as $first_level_key=>$value) {
if ($needle === $value) {
return array($first_level_key);
} elseif (is_array($value)) {
$callback = array_recursive_search_key_map($needle, $value);
if ($callback) {
return array_merge(array($first_level_key), $callback);
}
}
}
return false;
}
function array_get_nested_value($keymap, $array)
{
$nest_depth = sizeof($keymap);
$value = $array;
for ($i = 0; $i < $nest_depth; $i++) {
$value = $value[$keymap[$i]];
}
return $value;
}
// IPv4 or IPv6 URL
if ($type == "A") {
$url = "https://api.ipify.org";
}
elseif ($type == "AAAA") {
$url = "https://api6.ipify.org";
}
// Save IP in a variable
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP version: ' . phpversion());
$ip = curl_exec($ch);
if(curl_errno($ch)) {
exit("Internet down? Error: " . curl_error($ch));
}
curl_close($ch);
// The data we want to modify if needed
$data = [
'type' => $type,
'name' => $record,
'content' => $ip
];
//
// Get ZoneID
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones?name=$domein");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(curl_errno($ch)) {
exit("Error: " . curl_error($ch));
}
curl_close($ch);
$json = json_decode($result, true);
// Set the ZoneID
$ZoneID = $json['result']['0']['id'];
echo "ZoneID: " . $ZoneID . PHP_EOL;
//
// Get DNSID
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/$ZoneID/dns_records?name=$record");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(curl_errno($ch)) {
exit("Error: " . curl_error($ch));
}
curl_close($ch);
$json = json_decode($result, true);
// var_dump($json);
$search_value = $type;
$array_keymap = array_recursive_search_key_map($search_value, $json);
// var_dump($array_keymap);
$array_nested_value = array_get_nested_value($array_keymap, $json);
// echo array_get_nested_value($array_keymap, $json) . PHP_EOL; // AAAA
// Set the DNSID
$DNSID = $json['result']['0']['id'];
echo "DNSID: " . $DNSID . PHP_EOL;
//
// Check if IP has to replaced
//
$cf_ip = $json[$array_keymap['0']][$array_keymap['1']]['content'];
if(empty($cf_ip)) {
exit("CloudFlare didn't send a IP... We'll try again soon!");
}
echo "CloudFlare IP: $cf_ip" . PHP_EOL;
echo "Current IP: $ip" . PHP_EOL;
if($cf_ip === $ip) {
echo "The IP doesn't have to be replaced!" . PHP_EOL;
}
else {
echo "The IP has to be replaced!" . PHP_EOL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/$ZoneID/dns_records/$DNSID");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(curl_errno($ch)) {
exit("Error: " . curl_error($ch) . PHP_EOL . "Something went wrong with replacing the IP!" . PHP_EOL);
}
else {
$json = json_decode($result, true);
// var_dump($json);
if ($json['success'] == false) {
exit("No success! " . $json['message'] . PHP_EOL);
}
echo "The IP has been replaced to $ip! " . PHP_EOL;
}
}