Is anyone aware of any self-hosted (Open Source) Dynamic DNS services, that support devices behind NAT?
I'm aware that RouterOS supports DNS updates using the below, but you have to supply the IP.
Code: Select all
/tool dns-update
Thanks
/tool dns-update
<?php
require_once('Net/DNS2.php');
function do_update($user, $ip, &$error) {
if($user->type == 'server') {
$u = new Net_DNS2_Updater($user->zone, array('nameservers' => array($user->server)));
try {
$u->deleteAny($user->host, 'A');
$a = Net_DNS2_RR::fromString($user->host.' '.$user->ttl.' IN A '.$ip);
$u->add($a);
$u->signTSIG($user->key_name, $user->key_value);
$u->update();
return true;
} catch(Net_DNS2_Exception $e) {
$error = $e->getMessage();
return false;
}
} elseif($user->type == 'url') {
$url = $user->url.$ip;
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FAILONERROR => true
));
$data = curl_exec($ch);
$err = curl_errno($ch);
curl_close($ch);
if($err != 0) {
$error = 'curl error '.$err;
return false;
} else {
//FIXME: check $data for some string?
return true;
}
} else {
$error = 'unknown type';
return false;
}
}
if(!isset($_GET['user']) || !isset($_GET['pass'])) die('missing parameter(s)');
$ip = $_SERVER['REMOTE_ADDR'];
$data = json_decode(file_get_contents('data.json'));
foreach($data->users as $user) {
if($user->user == $_GET['user'] && $user->pass == $_GET['pass']) {
if($user->ip != $ip) {
if(do_update($user, $ip, $err)) {
$user->ip = $ip;
file_put_contents('data.json', json_encode($data));
die('updated');
} else {
die('update error: '.$err);
}
}
die('no update needed');
}
}
die('login error');
?>
{"users":[{
"user":"user1",
"pass":"pass",
"ip":"",
"type":"server",
"zone":"example.net",
"host":"dyndnshost.example.net",
"server":"192.0.2.1",
"ttl":60,
"key_name":"tsigkey",
"key_value":"supersecretkey"
},{
"user":"user2",
"pass":"pass",
"ip":"",
"type":"url",
"url":"http:\/\/dyndns.example.com\/update?user=xxx&pass=xxx&ip="
}]}