DNSPark DDNS update with ColdFusion

This website is hosted in a server that has been laying under my TV stand for the past couple of years. I blogged about my initial setup, and even though the setup has improved a bit since then, I still use DNSPark.com to host my DNS records. By having a Dynamic IP, occasionally my IP address changes. Since I have not been running any DDNS update clients on my server as any other normal person would do, I had to update the new IP address directly in the DNSPark.com admin interface. Here I’m going to sort this out with ColdFusion.

My server is behind a NAT Router which, although quite adamantly owns my real IP and my server, has no clue about the IP it actually uses to connect to the rest of the world. I can’t use CGI variables or java.net.InetAddress in this case. However, DNSPark offers a web-based IP detection page at http://ipdetect.dnspark.net with the limitation of calling it no more than once every 10 minute interval. http://checkip.dyndns.org offers the same function and I’m sure they also have some sort of usage limitation. Following Code, fetch the IP address from either one of above URLs.

   1: <cfhttp url="http://checkip.dyndns.org" method="get"></cfhttp>
   2: <cfset myip = listfirst(listlast(cfhttp.Filecontent,':'),' <')>
Show/Hide Line Numbers . Full Screen . Plain

I’m behind a LinkSys E series router and, like most routers, the admin interface displays the real IP it’s currently occupying. With bit of fishing around on the admin section HTML page, here I was able to fetch out the real IP directly from the router without being a burden to a third party service.

   1: <cfhttp url="http://192.168.1.1/Status_Router.asp" username="admin" password="[mypassword]" method="get">
   2: <cfset myip    = listgetat(mid(cfhttp.Filecontent, find('var wan_ip',cfhttp.Filecontent),40),2,'"')>
Show/Hide Line Numbers . Full Screen . Plain

Now having the real IP address, all we have to do is call DNSPark API URL with the correct information (documentation here). Bellow is the complete Code, and I used cfschedule to run this page every 10 minute, check if the IP address is changed, if true, Call DNSPark with the new IP. Also I add a text log on this, because I’m sort of turning into a log freak.

   1: <cfset myhostnames = "[host names separated by comma]">
   2: <cfset DDNSUser = "[DNS Park DDNS User Name]">
   3: <cfset DDNPW    = "[DNS Park DDNS Password]">
   4: <cfset LogFilePath = "#FileExists('/DNSparklog.txt')#">
   5: <cfset URLhere    = "http://localhost/dnspark.cfm">
   6: <!--- *********************************************************** --->
   7: <!--- Fetch the real IP from dnspark.net                         --->
   8: <!--- *********************************************************** --->
   9: <cfhttp url="http://ipdetect.dnspark.net" method="get"></cfhttp>
  10: <cfset myip = listfirst(listlast(cfhttp.Filecontent,':'),' <')>
  11: <cfset logstring = ArrayNew(1)>
  12: <cfparam name="url.oldip" default="#myip#">
  13: <cfset ArrayAppend(logstring,myip)>
  14: <cfif myip neq url.oldip>
  15:  <!--- IP changed, lets talk to DNSpark --->
  16:  <cfset ArrayAppend(logstring,'SENT')>
  17:  <cftry>
  18:  <cfhttp username="#DDNSUser#" password="#DDNPW#" url="https://www.dnspark.net/api/dynamic/update.php?hostname=#myhostnames#&ip=#myip#" method="get"></cfhttp>
  19:  <cfset url.oldip = myip>
  20:  <cfset Filecontent = replacenocase(cfhttp.Filecontent,chr(10),' ','all')>
  21:  <cfswitch expression="#listfirst(Filecontent,' ')#">
  22:  <cfcase value="ok,nochange">
  23:     <!--- update successful --->
  24:     <cfset ArrayAppend(logstring,Filecontent)>
  25:  </cfcase>
  26:  <cfcase value="nofqdn,nohost,abuse,unauth,blocked,notdyn">
  27:     <cfset ArrayAppend(logstring,Filecontent)>
  28:     <!--- DNSPark retuned an error message. This is a god palace to add email notification to yourself. --->
  29:  </cfcase>
  30:  <cfdefaultcase>
  31:     <!--- DNSPark retuned Something else - posibly possibly an server error --->
  32:     <cfset url.oldip = "">
  33:     <cfset ArrayAppend(logstring,'OTHER #cfhttp.Statuscode#')>
  34:  </cfdefaultcase>
  35:  </cfswitch>
  36:  <cfcatch>
  37:     <cfset url.oldip = "">
  38:     <cfset ArrayAppend(logstring,'ERROR')>
  39:  </cfcatch>
  40:  </cftry>
  41: </cfif>
  42: <!--- Save the log --->
  43: <cfset ArrayAppend(logstring,"#dateformat(now(),'dd/mm/yy')# #TimeFormat(now(),'HH:mm:ss')#")>
  44: <cffile action="append" file="e:\iis\cflove.org\warehouse\DNSPark.txt" output="#ArrayToList(logstring,' | ')#">
  45: <!--- Let's Create a Schedule --->
  46: <cfschedule    
  47:     action    = "update"    
  48:     task    = "dnspark-update"    
  49:     operation = "HTTPRequest"    
  50:     startDate = "#dateformat(now(),'mm/dd/yyyy')#"    
  51:     startTime = "#timeformat( DateAdd('n',10,now()) ,'HH:mm')#"
  52:     url    = "#URLhere#?oldup=#url.oldip#"    
  53:     interval = "once">
Show/Hide Line Numbers . Full Screen . Plain

You may want to run this page inside your localhost because if the IP to your domain changed, the cfschedule may lose access to the page too.