Saturday, April 20, 2013

What's My IP?

Why do you need/want to know your IP?

The links to all the code and tools used in this article are provided at the bottom.
I travel a lot, and even when I don't, I access the internet from various locations. Other than the varying bandwidths and limitations, there are adverse side effects to having a certain IP over another:

  1. Certain countries block access to certain content based on your geolocation - try accessing Hulu abroad, or BBC content in the US.
  2. Some information is blocked based on geographical location - even in the US. Some live streaming channels (like CNN) demand that you identify your local cable provider, in order to watch live TV.
  3. Some web sites completely change the user experience based on the perceived user location. Try going to Google.com from abroad, and by default you'll get a local version of the search site.
As a developer, it's important to me to know what's my IP, and where does a web site thinks I am. The problem is your laptop/device can only show your internal IP - the one allocated to you by your router or switch. To get your external IP, you need to access an external resource and have it tell you what IP does it see.

There are tons upon tons of web sites and extensions that do this for you today (try http://www.whatismyip.org/, or even try typing "what's my ip" into google), but I, being a great wheel re-inventer, wanted to solve my problem myself, and along the way, learn some more JavaScript and new tools.

The design

In order to get your IP, you need a server side component. I decided to use Node.js, as my entire logic can be easily implemented with several lines of JavaScript code. Also, I can host my solution on Heroku for free, and Git deployment means the entire app can be deployed and updated from command line.

For the geolocation info, I used a free service from IPinfoDB. The free tier gives you access to the 'Lite' service, meaning geolocation is not accurate. Sometimes you get a different zip code, city or even state. But it was good enough for my needs and I can always upgrade to the paid service, to get more accurate results.

For the client, at first I thought I'd just use the browser, but then decided it's a good chance to revisit my Chrome extension projects, and just build an extension that consumes the data returned from the server.

The server

Writing a web server in Node.js involves 3-5 lines of code. My entire app, including calls to the IPinfoDB service, results output, and (very minor) error handling, ended up being 40 lines long, in one file. I put all the other parameters in a separate config file, for easier editing. It was then easy to create a new Heroku site, commit my pages to Git and deploy. 

I then added a 'vanity domain name', so you can check out the site at http://myip.TravelingTechGuy.com. You can just go to this link and you'd get the JSON results (again, not really accurate, since it's free), or install the extension below.

The extension

A Chrome extension is basically a web app (HTML, CSS, JavaScript, and images) + a manifest file tying it all together, into a single .zip file. But the directory structure, and the manifest file are a hassle to write, and tasks get quite repetitive. I decided to use a new tool I learned about recently, called Yeoman

Yeoman allows you to bootstrap web development, by using templates (generators) of the desired result, in this case a Chrome extension, and uses Grunt to automate the build process. After installing Yeoman, and the Chrome extension generator, you can kick up the process by issuing a single command line. Few questions later, an you have a skeleton of an extension, that can actually run. All you need then is to fill the JavaScript files with logic, add styles to the CSS file, and add icons/images.
Issuing a grunt command 'builds' the extension - checks the JavaScript correctness (using JSHint), minimizes files, compresses images and zips everything up to an extension ready to be deployed.

All that remains then is to upload the extension to the Chrome web store. Just go to the store, log in, and go to the developer dashboard. You'll be asked for additional information, images, screen captures etc. before you can publish your extension.

To be able to access my server form the extension, I had to add a permission to the manifest file with the exact URL I'd be calling. That allows cross-site AJAX call from the extension's code.

The only challenge I encountered when implementing the extension's logic, was the copy function, that allows copying the IP to the clipboard. Turns out you can only copy text from input elements, so I had to create one. Then, it turns out the input element has to be visible for you to be able to copy from. Since that would have meant sticking an ugly text box in my popup, I just pushed that code to a background page. Finally, you need to add a permission to the manifest for clipboard access - it's called 'clipboardWrite'.

The apps

The code

The tools

Monday, August 29, 2011

Report the name of a JavaScript function

I do a LOT of JS development lately: straight up HTML5, PhoneGap for Android and iPhone and Node.JS on the server side. One command line I use in all environments is console.log.
console.log allows you to print out a string, a variable and even a complex object (just use JSON.stringify on it first) to the console view of:
  • Developer Tools in IE8 and higher (hit F12)
  • Developer Tools in Chrome (different keys for different OSs – nice, Google)
  • Firebug in Firefox (install as an extension)
Since advanced JS uses a lot of event calls, anonymous functions and multiple files, it’s hard at times to figure out where things go wrong. So I’ve gotten into the habit of adding an entrance printout at the beginning of each function. But it has gotten cumbersome and annoying after a while, so I searched for a more elegant solution. I found this StackOverflow question, and based on the replies, constructed this code:

var fname = /^function\s+([^(]+)/.exec(arguments.callee.toString())[1];
console.log(fname);


Now, this looks like a cumbersome code to paste into each and every function, so I wanted to write a general funnction I can call anywhere to print the current function name. The problem is, if you put this code into a function, say reportFunctionName() and call it, you get "getFunctionName" printed out. The answer was in the same SO post:

function reportName() {
  var fname = /^function\s+([^(]+)/.exec(reportName.caller.toString())[1];
  console.log(fname);
}


Note the use of caller instead of callee. Also note both methods would not work well for anonymous functions (duh - no name -> nothing to print :)

Finally, I started using Github's Gist feature to collect all my code snippets, so you can find both code pieces mentioned here and here respectively.

Monday, August 15, 2011

Ultraviolet Pig

If you got to this post hoping to read about the “Angry Birds” sequel “Ultraviolent Pigs” – you’re in the wrong place - please go to the Rovio siteSmile.
This is just another crazy electronics project I set myself to.

It all started while I was zombie-surfing the DealExtreme site. If you’re not familiar with the concept, it means browsing after 1am, no good reason other then to eke a couple of more minutes of browsing before the brain shuts down completely. Instead of muttering “brrraaaaains!” you mutter “beeedd”. Some of my best (and worst) ideas hit me during zombie surfing. But I digress.

So, as I said, while zs-ing DealExtreme, I came a cross a too-stupid-not-to-buy item: a pig LED flashlight for $0.78. And free shipping to boot! What’s the catch here, I wondered? And what the hell will I do with this useless, clearly non-kosher flashlight?


The Ultraviolet Pig
Here piggy piggy!

So I browsed further, and for $2.93 I got a bag full of ultraviolet LEDs.

20 UV LEDs
Not the best UV LEDs out there - but the cheapest
After 20-25 days (hey, you 'pay' for free shipping, you get it delivered by a lame horse that has to swim across the Pacific blindfolded), I finally got an envelope containing the items.

It took less than 5 minutes to take the pig apart (please don’t ever associate this sentence with me Smile) and replace the 2 simple LEDs with the UV ones. And that’s when the fun starts.


Other than enabling me to see hidden marks on a $20 bills and a driver’s license, the most useful thing you can do with UV light is protect your stuff.
Do you have a favorite chair that you think gets replaced with a crappy one when you leave your desk? Did someone swipe your beloved Swingline stapler and you can’t prove it? Just mark your stuff with UV ink – completely invisible to the naked eye – but not to good ol’ Mr. UVP. Observe:

Now you don't

This is a simple metal mint box, easily swiped and easy to deny. But when you turn UVP on it, the truth comes to light, in 3 colors (you can also see the UV pens in the photo):

Now you see it


So, yes, not really practical unless you can retrieve the stolen goods, but quite good at identifying your stuff, or secretly communicating under the nose of pig-less people (think about passing notes in a boring meeting).

The stats:
  • Usefulness: 3/5 stars – not that amazing, and not too techy. Not a single line of code needed
  • Fun factor: 5/5. Oh yeah!
  • How easy it is to build: very. Just get your pig, LEDs (you need at least 2) and follow your instincts. Or watch this youtube video where someone demonstrates how to take it apart
  • Total project cost: $3.71 – and I still have about 18 LEDs waiting for the next 9 pigs

Warning: DO NOT STARE DIRECTLY INTO UV LIGHT.
Your eyes cannot see it well, hence your pupils will not shrink properly to protect them. Few minutes are OK, more is not recommended.

Thursday, July 7, 2011

The 5-minutes Twilio App

This week, I interviewed for a position at Twilio. Twilio sells an API that enables your web applications and mobile app to use voice and SMS messaging capabilities. You are charged a small fee per incoming/outgoing call/message, and you get full backend support, including IVR, call statistics and the ability to reply automatically based on content and context.

The recruiter suggested I build something using the Twilio API. Since I have so many APIs running in my head these days, I was a bit hesitant at first to learn another one, especially if I didn’t see an immediate use for it. I was further less impressed by the use of XML (over JSON) in the API. But I gave it a shot, and within 5 minutes of starting I had my one (and only) Twilio API written: the SMS Calculator (no tm or c need apply). One minute after that and I had it tested. 5 minutes after twitting about it, someone asked me if he can demo my app in a code meetup. Once I said ‘yes’, my app officially became open sourced.

So, without further ado, here’s the code to my app, and the steps you need to take to create your 5-minutes Twilio app in 5 easy steps:

1. Go to twilio.com and create a free account. All you need is an email and a password.
You get free $30 on your account to use for testing. Good for 3000 SMS messages.

2. Paste the following PHP code into a text file and name it calc.php

<?php
 $body = $_REQUEST['Body'];
 $expression = explode(" ", $body, 3);
 $a = intval($expression[0]);
 $b = intval($expression[2]);
 $result = 0;
 switch($expression[1]) {
  case "+":
   $result = $a + $b;
   break;
  case "-":
   $result = $a - $b;
   break;
  case "*":
   $result = $a * $b;
   break;
  case "/":
   if($b != 0)
    $result = $a / $b;
   else
    $result = "Division by 0";
   break;
  default:
   $result = "ERROR";
   break;
 }
 $reply = "You asked: $body. The reply is: $result";
 header("content-type: text/xml");
 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
 <Sms><?php echo $reply ?></Sms>
</Response>

Code explanation:
This is a rudimentary, "stoopid", expression calculator. It expects an operand-space-operator-space-operand syntax (i.e. 2 + 2), does not use any regular expressions and does not check for correctness. The only error it checks for explicitly is division by 0 (and that was added only because I had points deducted for a similar exercise years ago by an evil TA).
  • We get the SMS body in the $_REQUEST[‘Body’] HTTP variable.
  • We proceed to split that string, using space as a delimiter. I limit the split to 3 strings.
  • We parse the 2 expected integers
  • We attempt to parse the operand (only the main 4 are supported)
  • We calculate the result and compose the reply. I could probably use more string tricks here, but what’s the use?
  • Finally, we compose the SMS XML that Twilio expects and output it.

3. Put the page on a web server that supports PHP. There are plenty of free hosting choices if you don’t have one ready (off the top of my head, try Zymic)

4. In your Twilio account page, scroll down to the section “Developer Tools”. Input the full path to the calc.php page in the “SMS URL” field, and notice the phone number and PIN you were allocated.

Twilio

At this point you’re actually done. Step 5 is the testing.

5. Using your phone, send an SMS message to the phone number. Start by putting in your PIN number (only required with test accounts) and then add an expression. It should look like:
xxxx-xxxx
25 / 5
You should receive an immediate reply, echoing the given expression and the result. For the full free experience, test using your Google Voice account. Google provides free SMS texts in the US and Canada, and the Twilio number is a US one.

I will certainly keep Twilio in mind for future uses in my apps, but as for now, I'm just happy that it took me longer to write this post than to write the code.

My next step: slapping an Eliza or an 8-ball app at the end of this :)

Update:

Andrew Watson from Twilio asked to borrow my sample for his presentation. Here's a link to the Google Doc Andrew has published. Thanks Andrew!

Oh, and it seems like I won't be working for Twilio after all, so I'm free to play with other new toys :)

Thursday, May 19, 2011

Display JSON results

JSON (JavaScript Object Notation) is a great straight forward way to transfer information from one application/server to another. It's string-based, array-oriented, and does not carry with it the redundancy of XML or (shudder) SOAP. And the best feature in a web application: it's immediately transferable to a JavaScript object, without the need for (an explicit) parsing.

Add to the mix jQuery - a JavaScript framework that makes using JavaScript and writing dynamic web pages a snap. Once I picked it up, I couldn't go back. jQuery is now embraced by many - including Microsoft and Google, who include it in their default web templates, and provide hosted CDN versions of its latest versions.

jQuery shines when it comes to AJAX transactions - you can get a JSON result from a web service using one instruction! Going over the reply is also a snatch. I found myself many times just needing to dump the results on the page - either for debugging purposes, or so I could copy/paste them somewhere.

Here is a short code snippet that I use to get a JSON result from a local web service (a remote request requires JSONP - future post), and dumps the results in a form of a list. It assumes a single-level array reply and that you have a <DIV> element on the page called "result":


        


Explanation:
  1. Line 1 includes version 1.5.2 of jQuery from the Microsoft CDN
  2. Line 4 will make sure the function will run when the document has been fully loaded and drawn
  3. Line 5 calls the JSON service and sends the results to a function
  4. Line 8 iterates over the result object and creates an array of <LI> elements
  5. Line 14 creates a new <UL> element, applies a CSS style to it, attaches the <LI> elements and places it inside the result <DIV>
That simple!