-
-
Save montchr/52b86134e0ae42b79cc6190a41f57e2b to your computer and use it in GitHub Desktop.
A DDNS service provider script for Synology NAS (based off https://github.com/cpascal/syno-ddns-linode/blob/master/linode.php)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -d open_basedir=/usr/syno/bin/ddns | |
<?php | |
if ($argc !== 5) { | |
echo 'badparam'; | |
exit(); | |
} | |
// Entered into DSM (Control Panel > External Access > DDNS) | |
$username = (string) $argv[1]; // Resource ID | |
$password = (string) $argv[2]; // API Key | |
$hostname = (string) $argv[3]; // DomainID.domain (e.g. 1234567.henrik.io) | |
$ip = (string) $argv[4]; // External address | |
$token = $password; | |
$domainId = explode('.', $hostname)[0]; | |
$resourceId = $username; | |
// only for IPv4 format | |
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | |
echo "badparam"; | |
exit(); | |
} | |
function linode_request($curl, $token, $uri, $fields = [], $type = 'GET') | |
{ | |
$BASE_URL = 'https://api.linode.com/v4'; | |
$HEADERS = [ | |
'Authorization: Bearer '.$token, | |
'Content-type: application/json', | |
]; | |
curl_setopt($curl, CURLOPT_URL, $BASE_URL.$uri); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, $HEADERS); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
if (count($fields) > 0) { | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields)); | |
array_push($HEADERS, 'Accept: application/json'); | |
} else { | |
curl_setopt($curl, CURLOPT_POST, false); | |
} | |
$res = curl_exec($curl); | |
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
$error = null; | |
switch ($http_status) { | |
case 200: break; | |
case 401: $error = 'badauth'; break; | |
default : $error = '911'; break; | |
} | |
if ($error !== null) { | |
echo $error; | |
curl_close($curl): | |
exit(-1); | |
} | |
return json_decode($res, true); | |
} | |
$req = curl_init(); | |
// | |
$json = linode_request($req, $token, 'GET', "/domains/$domainId/records"); | |
if ($json['results'] < 1) { | |
echo 'nohost'; | |
curl_close($req); | |
exit(); | |
} | |
$record = null; | |
foreach($json['data'] as $row) { | |
if ($row['type'] == 'A' && (string) $row['id'] == $resourceId) { | |
$record = $row['target']; | |
} | |
} | |
if ($record === null) { | |
echo 'nohost'; | |
curl_close($req); | |
exit(); | |
} | |
if ($record == $ip) { | |
echo 'nochg'; | |
curl_close($req); | |
exit(); | |
} | |
// | |
linode_request($req, $token, 'PUT', "/domains/$domainId/records/$resourceId", [ 'target' => $ip ]); | |
curl_close($req); | |
echo 'good'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment