Skip to content

Instantly share code, notes, and snippets.

@namikiri
Created August 24, 2018 21:57
Show Gist options
  • Save namikiri/82fe683c5ec31be4881b0fb33cea51bf to your computer and use it in GitHub Desktop.
Save namikiri/82fe683c5ec31be4881b0fb33cea51bf to your computer and use it in GitHub Desktop.
Yandex Dynamic DNS Updater
<?php
/*
Yandex DNS automatic update script.
Uses Yandex's DNS as a DDNS service as they provide configurable TTL value.
Synopsis:
updateDomain (string $domain, string $apiKey, [array $blacklist = []])
Where:
$domain : your domain name
$apiKey : an API key provided by Yandex.
The API key should be obtained here:
https://pddimp.yandex.ru/api2/admin/get_token
$blacklist : An array of subdomains which should not be updated
Example:
updateDomain ('asterleen.com', 'IMMAYANDEXKEYYARRRSUXMYD1CK13371488228LOLAZAZA', ['dont', 'update', 'us']);
Process of domain update will be commented to stdout.
*/
define ('YAPI_ADDR', 'https://pddimp.yandex.ru/api2/admin/dns/');
function curl_request ($method, $type, $fields, $apiKey)
{
$curl = curl_init();
if($curl)
{
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_setopt($curl, CURLOPT_URL, YAPI_ADDR.$method.($type == 'get' ? '?'.http_build_query($fields) : ''));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_USERAGENT, 'SoCute/1.3.3.7');
if ($type == 'post')
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
}
// Yapi Auth
curl_setopt($curl, CURLOPT_HTTPHEADER, array('PddToken: '.$apiKey));
$out = curl_exec($curl);
curl_close($curl);
return (empty ($out)) ? false : $out;
} else
return false;
}
function getMyIp()
{
$ipservice = 'http://icanhazip.com';
$myIP = trim(file_get_contents($ipservice));
if (empty($myIP) || $myIP == '127.0.0.1')
return false;
else
return $myIP;
}
function updateDomain($domain, $apiKey, $blacklist = [])
{
$myIP = getMyIp();
if (empty($myIP))
die ('IP Getting Error, exiting!');
$raw = curl_request ('list', 'get', array('domain' => $domain), $apiKey);
if (!$raw)
die ('Could not make request!');
$json = json_decode($raw, true);
if (!$json)
die ('Could not parse JSON!');
if ($json['success'] == 'error')
{
die ('Some API error occured: '.$json['error']);
}
$records = $json['records'];
foreach ($records as $rec)
{
if ($rec['type'] == 'A') // Only A records can be updated
{
$sub = $rec['subdomain'];
if (!in_array($sub, $blacklist)) // Checking black list
{
if ($rec['content'] == $myIP) // Why must we do this if we are already up to date?
{
echo ('Record '.$sub." is already set, ignore\n");
continue;
}
$id = $rec['record_id'];
$res_update = curl_request('edit', 'post', Array('domain' => $domain, 'record_id' => $id, 'content' => $myIP, 'ttl' => 90), $apiKey);
if (!$res_update)
echo ('Could not update record '.$id.' ['.$sub."]!\n");
$json_update = json_decode($res_update, true);
if (!$json_update)
echo ('Could not parse JSON for record '.$id.' ['.$sub."]!\n");
if ($json_update['success'] == 'ok')
echo ('Record update for '.$sub." done (ok)\n");
else
echo ('Record update for '.$sub.' FAILED ('.$json_update['error'].")\n");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment