DtDNS and automatic update

Plugin code

Our plugin IP updater for DtDNS is nearly complete; all that is required is to assemble code previously covered.

User configuration should be easy I assume a user can use a text editor.

Yep! User configuration is always a real problem even with fancy GUI.

General

Any solution needs to be general, at DtDNS you can have up to five free host names these need to be taken into account.

User configuration requires adding a single line for each hostname created at DtDNS. A line has the following format:

  • $id_pw[] = "hostname,password";

It creates an array element, containing a string composed of hostname (id) and account password.

Assuming a user creates five free accounts there will be five elements for example

  • $id_pw[] = "books1.dtdns.net,fred234";
  • $id_pw[] = "books2.dtdns.net,fred234";
  • $id_pw[] = "books3.dtdns.net,fred234";
  • $id_pw[] = "books4.dtdns.net,fred234";
  • $id_pw[] = "books5.dtdns.net,fred234";

The array $id_pw is scanned line by line using function foreach() each line (array element) is split into its component parts.

  • $idpw_array[0] = hostname
  • $idpw_array[1] = password

Core code has been separated into a number of functions and the above parts passed as parameters.

  • get_ip_dns($idpw_array[0] - Obtains the IP address from DNS server
  • get_ip_current() - Obtains the current IP address of the server as seen from the Internet
  • dtdns_update($idpw_array[0],$idpw_array[1]) - Requests an IP update at DtDNS

The foreach() loop is the main work horse calling functions where appropriate.

Assemble Code

The following code should be easy to understand, it contains comments explaining what each line performs.

Testing assumes you have created some real host names at DtDNS.

Create a new file plug.php in folder C:\dtdns_test\UniServer\plugins\dtdns_updater with the following content:

<?php
/*
###############################################################################
# Name: The Uniform Server DtDNS Updater v 1.0
# Developed By: The Uniform Server Development Team
# Modified Last By: Mike Gleaves (Ric) 
# Web: http://www.uniformserver.com
###############################################################################
*/

//######################## User Configuration #################################
// Add as many lines as required with the following format:
//   $id_pw[] = "hostname,password";
// For example:
// $id_pw[] = "books.effers.com,fred123";
// $id_pw[] = "power.dtdns.net,gun22powder";
//-----------------------------------------------------------------------------

$id_pw[] = "books.effers.com,fred123";


//###################### END User Configuration ###############################

$ip_dns     = "";   // Global variable
$ip_current = "";   // Global variable
$test       = true; // Test true = display IP address and host names
                    // false = no display

foreach ($id_pw as $value) {                        // Iteratate through array
  $idpw_array = explode(",",$value);                // Split id pw pairs

  if(get_ip_dns($idpw_array[0])){                   // Was IP from DNS returned
    if(get_ip_current()){                           // Yes: Current IP returned   

      if($test){ // Test code
       print "DNS IP = $ip_dns CURRENT IP = $ip_current $idpw_array[0] <br/>\n";
      }

      if($ip_dns != $ip_current){                   // Are IP's different
     //   dtdns_update($idpw_array[0],$idpw_array[1]);// yes: Update required
      }
    }
  }
}// End foreach

//=== Get IP from DNS server =================================
// Input:  ID = Hostname:
// Output: Return value true  = IP was obtained
// Output: Return value false = IP not obtained or error
// Output: $ip_dns either IP address or host name

function get_ip_dns($hostname){
  global $ip_dns;                     // IP address
  $ip_dns = gethostbyname($hostname); // Get IP
  if($ip_dns == $hostname){           // Is hostname returned 
    return false;                     // yes: failed to get IP
  }
  else{                               // no: IP obtained
    return true;                      // set success
  }
}
//============================= END Get IP from DNS server ===

//=== Get current IP as seen from Internet =================================
// Input:  None:
// Output: Return value true  = IP was obtained
// Output: Return value false = IP not obtained or error
// Output: $ip_current either IP address or blank

function get_ip_current(){
  global $ip_current;                     // IP address

  $ch=curl_init();                                      // Get handle
  curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com'); // Page to get
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);           // Give up after 10s
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);            // Force text string op
  $buffer = curl_exec($ch);                             // Run above save to buffer
  curl_close($ch);                                      // Cose resource

  if (empty($buffer)){                                  // Is buff empty
    return false;                                       // yes: failed to get ip
  }
  else{                                                 // no: page obtained
   // Extract IP address 
   if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){
     $ip_current = $ipmatch[0];                         // Save IP to variable
     return true;                                       // Set success
   }
   else{                                                // No match must be error
     return false;                                      // hence failed
   }
  }
}
//============================= END Get current IP as seen from Internet ===

//=== DtDNS Host Update Page ==================================================
// Input:  $id = Hostname:
// Input:  $pw = User password
// Output: Return value true = sucess false = failed

  
function dtdns_update($id,$pw){
$id_val     = urlencode($id);                   // User Host on DtDNS
$pw_val     = urlencode($pw);                   // User password
$client_val = urlencode('UniServerV1');         // Optional - But added it
$url = 'https://www.dtdns.com/api/autodns.cfm'; // DtDNS update page

// Build complete URL
$str= $url."?id=".$id_val."&pw=".$pw_val."&client=".$client_val;

// Access page using Curl SSL
$ch=curl_init();                                 // Initialize Curl get handle
curl_setopt($ch,CURLOPT_URL,$str);               // Set the Curl URL option 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // no verify
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);      // timeout set to 10 sceonds
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);       // force curl_exec to ouput string 
$buffer = curl_exec($ch);                        // run above, save returned page to buffer
curl_close($ch);                                 // Close Curl frees memory

// Test returned page 
if(preg_match("/now\spoints\sto/",$buffer)){     // Is returne value "now points to"
 return true;                                    // yes: update OK
}
else{                                            // no: Failed
 return false;
}
}
//============================================== END DtDNS Host Update Page ===
?>

Create a new file Run_plug.bat in folder C:\dtdns_test\UniServer\plugins\dtdns_updater with the following content:

COLOR B0
@echo off
cls

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

..\..\usr\local\php\php.exe  plug.php

rem ### restore original working directory
pause
popd
EXIT

Top

Run batch file

Double click on Run_plug.bat there is no need to have the servers running.

With $test = true; You will see the following if you have a host name of books.effers.com

DNS IP = 188.109.187.84 CURRENT IP = 188.109.187.84 books.effers.com <br/>
Press any key to continue . . .

All the host names are scanned if there is a difference their IP addresses are updated.

Top

Summary

Although you can run the above script manually it is preferable to enable CRON and run the script periodically. DtDNS recommends something like 10 minuets between updates otherwise false updating may occur. It takes worldwide propagation of all DNS servers around this time, in the States it’s faster.

At this stage I would have provided details regarding CRON however things change.

Next page explains the deviation.

Top