How To Fake Dynamic DNS

I have everything I need to keep tabs on my home IP address without signing up for a DDNS service like No-IP or DynDNS. My Synology NAS always knows its address when it’s online, and because it’s essentially a little Linux box, there’s no reason it can’t tell my other sites where to find it.

In my case, I want to be able to reach my home IP address by visiting a certain URL on either my remote Linux or Windows host — something like www.mydomain.com/myhomeip. But I don’t really want to hide the IP address like a real DDNS would, and I don’t think my hosting providers would even accommodate that. So I rolled my own.

On the remote web host, I created a few PHP files to receive the notifications. common.php does most of the work:

<?php
$file = 'ip.txt';
$pw = 'some_password_here';
$default = 'www.theFrankes.com';

function redirect() {
  global $file, $default;
  $ip = file_get_contents($file);
  if( isValid( $ip ) ) {
    header( "Location: http://$ip" );
  } else {
    header( "Location: http://$default" );
  }
}

function setIp() {
  global $file, $pw;
  $ip = $_SERVER['REMOTE_ADDR'];
  if( isValid( $ip ) & ( $_GET["pw"] == $pw ) ) {
    $fh = fopen($file, 'w') or die("can't open file");
    fwrite($fh, $ip);
    fclose($fh);
  }
}

function isValid( $ip ) {
  return preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $ip );
}
?>

My index page calls redirect(), which reads in the IP address and sends the visitor to it if it’s valid. There’s another page that calls setIp(), and that’s the page that my home computer is going to call, passing in the “password” at the top.

I used putty to log into the Synology server and stopped the cron job.

/usr/syno/etc/rc.d/S04crond.sh stop

Then I edited  /etc/crontab to call a shell script in admin’s home directory every few hours. Don’t copy/paste this into the file using putty, but rather type it in directly and use only one tab to separate each column. Typing it directly prevents special (invisible) characters from getting into the file, which could cause it to be overwritten by the default file upon reboot.

*       */4     *       *       *       root   /volume1/homes/admin/cron/every4hrs.sh

In the shell script, I used wget to make the web request. curl would work just as well. I could have just added this command to the crontab file, but it’s easier to manage a shell script in the home directory. I can also extend it more easily at a later date. The shell script simply contains:

/usr/syno/bin/wget --tries=2 "http://www.mydomain.com/myhomeip/ipsetter.php?pw=some_password_here" -O -

The Synology DiskStation makes a working copy of the crontab when it starts up, and stores it in /var/spool/cron/crontabs/root, so to be safe I just rebooted the device rather than just starting the service up again. When I started the service using “S04crond.sh start”, it would work only until the DiskStation rebooted, and after that it would default back to the original. Rebooting the device right away seems to have fixed that problem.

Leave a Reply

Your email address will not be published. Required fields are marked *