Wednesday, January 22, 2014

Avoiding Dynamic DNS using D, cURL, and Gmail


After buying a C720 to use as a remote keyboard/screen for my home computer, I quickly realized I needed an easy way to sync my files between the two computers.  Since google drive still doesn't support Linux, my choices were to sign up for a free dynamic dns service or to constantly check my home IP and copy it to my laptop.  I'd been looking for more excuses to code in D, and so I decided to write a simple dynamic dns replacement using cURL and one of my junk mail Gmail accounts.

The concept behind a dynamic dns service is fairly simple.  For a client computer to connect to a host computer, the client needs to know the host's IP.  If the host's computer doesn't have a statically set IP, the client computer can't connect, because the IP is constantly changing.  To avoid this, use a third computer, with a static IP, to pass the current IP from the host to the client.  That's essentially what my code does, but instead of using a service like dynDNS I used Gmail and two programs (a host and a client) to send/receive the new IP.

Using D and cURL made writing the host and client executables fairy simple.  For the host executable, I needed to write some code that kept track of the current IP and previous IPs, and updated the IP address when it changed.  To track the IP address, I had the host program check and update a csv file with the date stamp of the last time an IP address changed.  To check the current IP I used ipecho.net.  I then scheduled the program to run every 25 minutes with cron and was done.  (If you're interested in setting this up yourself I would recommend scheduling the task for 25-30 minutes to avoid abusing the ipecho website. It's enough time so that if you're at a coffee shop and lose access to your home computer during one of those thousand coffee pee breaks, you won't have to wait around too long for the updated IP to arrive).

The client program is just as simple as the host program, but relies on some prior configuration in gmail.  In order to keep the client progam simple, I relied on the gmail automated filter options to take the email with the new IP from the host program, and automatically file it away to a unique, predetermined, folder.  This filter can be anything that ensures that /only/ the IP update emails are filtered away.  Since I'm using a junk email address, I ensured that the filter checked the to and from address lines (both are set to me) and the title of the email (an arbitrary long name).  The other nice side effect of this setup is that the newest email always has the most recent IP.  Getting the email is then simple; just use the curl imap support to get mail and we're done!  The IP is saved in a file called "ip_address.txt".  

One other step I chose to implement was a pseudo-cipher for the IP address.  Initially I was afraid that Google would see my IP, or that someone else may intercept the IP in transit and attempt to hack me.  After further reflection both seem a bit unlikely, so I included a version of the code that just sends and receives the IP from gmail.  The one fun part of the ciphered IP address is that it has a sort of 'mad-lib' quality.  To make the cipher I wrote a lengthy sentence, and replaced all of the words with their grammatical equivalents.  For example:

"Google should really get google drive working on Linux" ==> becomes ==>
"noun should really verb noun noun gerund on noun"

I then just created an array with 11 words for each array type, and picked the word by adding the ip number to and offset, and taking it modulus 11 (11 for 0-10, where 10 is null).  It was fun to make, but in the end not necessary.  It's all contained in the configuration file, which also holds the email username, password, filter name, email subject name.

I've been running the code for a few weeks now and it works pretty great.  This obviously isn't a perfect replacement for those who want to host a webserver at their home address, but it's convenient for easy file syncing and ssh sessions.  Also, I'm sure there are probably a few issues that will crop up, but I've been pleased with the success of the project.  It's nice working on a project that I actually use in my day-to-day life :).

Compiling the Code
I ended up using LDC (D llvm compiler) for my Makefile, but any other compiler should work as well, as long as curl is installed.

Source

2 comments:

  1. But, if you don't have information related to recovery, which is associated with your account, then you will be taken to the account retrieval survey.gmail support services

    ReplyDelete