Goodbye Free Dyn DNS

I have a home server. I don’t direct the general public to this server but I do need to occasionally SSH to it. I know where that server is on the internet via Dyn DNS, that is, until I got the email announcing that they are discontinuing their free service. (Something is happening to the internet; say goodbye to free) Instead of trying to find another free dynamic DNS service I decided to write a couple simple PHP scripts and host them on my website.

The first script saves theĀ $_SERVER[‘REMOTE_ADDR’] to a file. I named the file using a URL parameter for location. Using cron from my home server, I curl the script every hour. So if I need to know my home IP address to SSH I can access this file to copy and paste to putty.

<?php
	date_default_timezone_set('America/New_York');
	$clientip = $_SERVER['REMOTE_ADDR'];
	if(isset($_GET['loc']) && preg_match("/^[A-Z0-9]+$/i",$_GET['loc'])) {
		echo "location: ".$_GET['loc']."n";
		$myFile = $_SERVER["APPL_PHYSICAL_PATH"]."tmp/".$_GET['loc']."ip.html";
		$fh = fopen($myFile, 'w') or die("can't open file");
		$stringData = "<!doctype html><html><head><title>location: ".$_GET['loc']."</title></head><body>Updated on ".date('Y-m-d H:i:s').
		" EST <br/>".$clientip."</body></html>";
		fwrite($fh, $stringData);
		fclose($fh);
	} else {
		echo "location: unknownn";
	}
	echo $clientip;
?>

The second script provides the redirection for a web browser. I pass two URL parameters, location and port, which get concatenated with the saved IP address. Just save a bookmark or create a link to the redirect script with the correct URL parameters.

<?php
//redirect client to ipaddress:port/location
$port = '';
if(isset($_GET['port']) && preg_match("/^[0-9]{1,5}$/",$_GET['port'],$matches)) {
	$port = ':'.$_GET['port'];
}
if(isset($_GET['loc']) && preg_match("/^[A-Z0-9]+$/i",$_GET['loc'])) {
	//get ip
	$filename = $_SERVER["APPL_PHYSICAL_PATH"]."tmp/".$_GET['loc']."ip.html";
	$fh = fopen($filename, 'r') or die("can't open file");
	$contents = fread($fh, filesize($filename));
	fclose($fh);
	if(preg_match("/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/",$contents,$matches)) {
		header('Location: http://'.$matches[0].$port.'/'.$_GET['loc'].'/');
		exit;
	} else {
		echo 'Location match not found';
	}
} else {
	echo 'I need a location to redirect';
}
?>

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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