DtDNS: IP Tracker

From The Uniform Server Wiki
Jump to navigation Jump to search

 

DtDNS and automatic update

Introduction

In order to prevent excessive updates we need to know what IP address is currently being used by DtDNS knowing this it can be compared with your current IP address as seen from the Internet. If a mismatch is found an update can be performed.

Doing this prevents awakening a draconian dragon from banning your IP address. Generally if you update an IP that has not changed is considered an abuse or an attempt at denial of service. Although a few tests are permitted don’t push your luck.

This page looks at two methods you can use to track your old IP address (One currently being used by DtDNS).

Save to file

An obvious method, after updating IP address at DtDNS save that IP to a file.

Example code:

  • Create new text file dtdns_test_5.php in folder C:\dtdns_test\UniServer\plugins\dtdns_updater
  • Create new text file Run_dtdns_test_5.bat in folder C:\dtdns_test\UniServer\plugins\dtdns_updater
  • Content shown below:

dtdns_test_5.php

<?php
// Get current IP address from Internet
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com');
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($ch);
curl_close($ch);

if (empty($buffer)){
  // Need to add code and recover from this
}

else{
  // Extract IP address 
 if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){  
    $ip = $ipmatch[0]; // Save IP to variable
 }
}

// Print values
  print "IP from internet = $ip \n";           // Test code 
  set_ip_tracker($ip);                         // Save to file
  $ip_from_file = get_ip_tracker();
  print "IP from file     = $ip_from_file \n"; // Read from file


//=== Set IP Tracker ==================================
function set_ip_tracker($value){
  $fileName = "ip_tracker.txt"; 
  $fh = fopen($fileName, 'w') or die("can't open file"); 
  fwrite($fh, $value);                                   
  fclose($fh);                                           
}
//=============================== END Set IP Tracker ===

//=== Get IP Tracker ===================================
function get_ip_tracker(){
  $fileName = "ip_tracker.txt"; 
  $fh = fopen($fileName, 'r');             
  $Data = fread($fh, filesize($fileName)); 
  fclose($fh);                             
  return $Data;
}
//================================ END Get IP Tracker ===
?>

Run_dtdns_test_5.bat

COLOR B0
@echo off
cls
rem ### working directory current folder 
pushd %~dp0
..\..\usr\local\php\php.exe  dtdns_test_5.php
rem ### restore original working directory
pause
popd
EXIT

Run Test 5

  • Close servers
  • Double click on Run_dtdns_test_5.bat
  • Result: Current IP address displayed twice.

Top

Use PHP functtion gethostbyname()

Since the IP address is already stored on a DNS server it really does not make sense to store this IP in a file.

We know our host name pass this to PHP’s gethostbyname() function. This will contact a DNS server and return the dot formatted IPv4 address.

Using our example host name the function looks like this:

$ip = gethostbyname('books.effers.com');

Note: If the function fails for any reason the host name is returned instead of the IP address.

Top

Example Code

Substitute books.effers.com with your real host name

Create a new text file named dtdns_test_6.php in folder C:\dtdns_test\UniServer\plugins\dtdns_updaterwith the folowing content:

dtdns_test_6.php

<?php
// Get current IP address from Internet
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com');
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($ch);
curl_close($ch);

if (empty($buffer)){
  // Need to add code and recover from this
}

else{
  // Extract IP address 
 if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){  
    $ip = $ipmatch[0]; // Save IP to variable
 }
}

// Get IP from DNS server
$ip_dns = gethostbyname('books.effers.com');

// Print values
  print "IP current from Internet = $ip \n";      // Current IP 
  print "IP from DNS server       = $ip_dns \n";  // IP Last update 
?>

Create a new text file named Run_dtdns_test_6.bat in folder C:\dtdns_test\UniServer\plugins\dtdns_updaterwith the folowing content:

Run_dtdns_test_6.bat

COLOR B0
@echo off
cls

rem ### working directory current folder 
pushd %~dp0

..\..\usr\local\php\php.exe  dtdns_test_6.php

rem ### restore original working directory
pause
popd
EXIT

Top

Run Test 6

  • Close servers
  • Double click on Run_dtdns_test_6.bat
  • Result: Current IP address displayed followed by that stored on the DNS server.

Top

Summary

The above code snippet is the final piece required to build an automatic IP updater plugin.

Next page details the plugin code.

Top