DtDNS: Plugin 2

From The Uniform Server Wiki
Jump to navigation Jump to search

 

DtDNS and automatic update

A Twist

While writing this tutorial it became apparent two functions were ideal candidates for adding to the core functionality.

The two functions now integrated provide additional diagnostics. A side effect concept of a plugin faded away and no longer made sense. Hence the DtDNS automatic IP updater is included as a standard component within the plugin's folder. It requires configuring and enabling for CRON operation.

This page details how to configure and enable CRON operation.

Script

The final script shown below:

UniServer\plugins\dtdns_updater\dtdns_updater.php

<?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
###############################################################################
*/
//error_reporting(0); // Disable PHP errors and warnings
                      // Comment to Enable for testing

chdir(dirname(__FILE__)); // Change wd to this files location
include_once "../../unicon/main/includes/config.inc.php";
include_once "../../unicon/main/includes/functions.php";

run_location_tracker();  // Have servers moved if moved update configuration
print"\n";

//######################## 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";   // Change
$id_pw[] = "books.effers.com,fred123";   // Change or delete
$id_pw[] = "books.effers.com,fred123";   // Change or delete

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

$test = false; // 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 saved from DNS server
  $ip_dns = gethostbyname($hostname);  // Get IP address of hostname
  if($ip_dns == $hostname){            // Is hostname returned (not IP address) 
    return false;                      // yes: failed to get IP address
  }
  else{                                // no: IP obtained  
    return true;                       // set success 
  }
}
//============================================== END Get IP from DNS server ===

//=== 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 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);       // curl_exec ouputs a string 
$buffer = curl_exec($ch);                        // run above, save 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 ===
?>

Manual Update

You can run this batch file to manually force an update, remember to leave around ten minuets before performing another update.

The final batch script shown below:

UniServer\plugins\dtdns_updater\Run_dtdns_updater.bat

COLOR B0
@echo off
cls

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

..\..\usr\local\php\php.exe  dtdns_updater.php

rem ### restore original working directory
pause
popd
EXIT

Top

Cron

Cron provides a periodic tick Uniform Server's default is every ten minutes.

DtDNS automatic updater has been pre-configured however it is disabled by default.

To enable Cron edit file UniServer\unicon\main\run_cron.php

Locate these two line in section List of scripts to be run

  // $cmd = 'start ..\..\usr\local\php\php-win.exe ..\..\plugins\dtdns_updater\dtdns_updater.php';
  // pclose(popen($cmd,'r'));             // Start detatched process  

and uncomment as shown below

  $cmd = 'start ..\..\usr\local\php\php-win.exe ..\..\plugins\dtdns_updater\dtdns_updater.php';
  pclose(popen($cmd,'r'));             // Start detatched process  

Notes:

  • When the server is first started Cron is run and the IP addreess updated if different
  • Every ten minuets Cron is run and the IP address are checked if different are updated.
  • Cron only runs when the servers are running however you can force an update using the batch file mentioned above.

Top

Summary

Interesting twist to this tutorial started life as a plugin only to become an integrated component.

Thought I would leave the tutorial as is it contains useful information should you want to design an IP updater for another provider.

Top