Author: jaywll
Using JavaScript to Identify Whether a Server Exists
Recently, for reasons Iām sure Iāll write about in the
future, I needed to find a way to use JavaScript to test if either of two
web-locations are accessible ā my home intranet (which would mean the user is on
my network), or the corporate intranet of the company for which I work (which
would mean the user is on my organizationās network). The page doing this test
is on the public web.
My solution for doing this test was simple. Since neither
resource is accessible publicly I put a small JavaScript file on each, then I
use AJAX and jQuery to try and fetch it. If
thatās successful, I know the user has access to whichever intranet site served
the request and my page can react accordingly.
If neither request is successful I donāt have to do
anything, but the user doesnāt see any errors unless they choose to take a look
in the browser console.

This all worked wonderfully until I enabled SSL on the page
that needs to run these tests, then it immediately fell apart.

Both requests fail, because a page served over HTTPS is
blocked from asynchronously fetching content over an insecure connection. Which
makes sense, but really throws a spanner into the works for me: neither my home
nor corporate intranet sites are available outside the confines of their safe
networks, so neither support HTTPS.
My first attempt at getting around this was to simply change
the URL prefix for each from http:// to https:// and see what happened. Neither
site supports that protocol, but is the error that comes back different for a
site which exists but canāt respond, vs. a site which doesnāt exist? It appears
so!

Sadly, my joy at having solved the problem was extremely
short lived. The browser can tell the difference and reports as much in the
console, but JavaScript doesnāt have access to the error reported in the
console. As far as my code was concerned, both scenario was still identical
with a HTTP response code of 0 and the status description worryingly generic āerror.ā
We are getting closer to the solution I landed on, however.
The next thing I tried was specifying the port in the URL. I used the https://
prefix to avoid the āmixed contentā error, but appended :80 after the hostname
to specify a port that the server was actually listening on.

This was what I was looking for. Neither server is capable
of responding to a HTTPS request on port 80, but the server that doesnāt exist
immediately returns an error (with a status code of 0 and the generic āerrorā
as the descriptive text), but the server that is accessible simply doesnāt
respond. Eventually the request times out with a status code of 0 but a status
description, crucially, of ātimeout.ā
From that, I built my imperfect but somewhat workable
solution. I fire a request off to each address, both of which are going to
fail. One fails immediately which indicates the server doesnāt exist, and the
other times-out (which I can check for in my JavaScript), indicating that the
server exists and I can react accordingly.
Itās not a perfect solution. I set the timeout limit in my
code to five seconds, which means a āsuccessfulā result canāt possibly come
back in less time than that. Iād like to reduce that time, but when I
originally had it set at 2.5 seconds I was occasionally getting a
false-positive on my corporate network caused by, yāknow, an actual timeout
from a request that took longer than that to return in an error state.
Nevertheless if you have a use-case like mine and you need
to test whether a server exists from the client perspective (i.e. the response
from doing the check server-side is irrelevant), I know of no other way. As for
me, Iām still on the lookout for a more elegant design. Iām next going to try
and figure out a reliable way to identify if the user is connected to my home
or corporate network based on their IP address. That way I can do a quick
server-side check and return an immediate result.
Itās good to have this to fall back on, though, and for now
at least it appears to be working.
Inception Is A Prequel To The Matrix … No, Seriously
Stanford quantifies the privacy-stripping power ofĀ metadata
The best $1,000 projector
Flexbox Patterns: Building Blocks for Your Design | NOUPE
10 Answers In Fake Guest Book At Florida Airbnb That Will Make You Laugh Everytime – Get Very Funny
Late Night Links – Sunday May 8th, 2016
Itās late night links time again! Believe it or not, weāre still playing catchup from the weeks I took a hiatus. We should be good after today though, so letās get this thing going.
- Trying to Drive Your Car Off a Tow Truck is as Stupid as it Sounds
- Virtual Desktop for VR is a Glimpse at a Future Without Monitors
This actually looks really cool. I want to try it. - Donāt Blink or You Might Miss the Fastest Formula 1 Pit Stop Crew at Work
- New Rideshare Company Steps Up in Calgary
Iām listening… - British Man Successfully Gets Dominos Delivered to His Moving Train
- Make a Temperature Indicator for You Faucet with Thermochromatic Pigment
Alright then, maybe I will. - Sonyās Google Cast Speakers are Ready to Put Music in Any Room
- ATB Creates New Support for Alberta Entrepreneurs
- Passenger Mistakes Math Equations forĀ āTerrorist Code,ā Gets Economics Professor Escorted Off Flight
And just like that, weāre not only done but also up to date! Until next time then, internet.
Late Night Links – Sunday May 1st, 2016
Itās a beautiful start to May here in #YYC, and Iām sitting on my deck writing this post while Flo is out doing Flo things. Sheāll be back before long, so letās get going and get this done.
- Windows 10 Will Bring Your Android Phone Notifications to Your PC
Thatās great and all, but I already have them through pushbullet. - Koh Lipe // The Maldives of Thailand
- Super Trivia About Super Troopers
Now I kind of want to go back inside and watch this. - High River RCMP Chase Allegedly Impaired, Machete Wielding Suspect Through Farmland
- WhatsApp Completes End-to-End Encryption Rollout
- Four Lawn Mowing Tips for Keeping a Lush Lawn Without Wasting Water
My lawn has definite tinge of brown to it right now… - Game Boy Mod Plays Nearly Any Classic Nintendo Game
- Sick Beat ft. Dying Car Alarm
- The First Fan to Try the $100,000 Goal Promotion Makes it Look Easy
- Capān Crunch Chicken Strips
Oh…? - That Emoji Does Not Mean What You Think it Means
- Letās Encrypt Free HTTPS Certification Push Exits Beta
- Get You
- Wacoās Finest
- If You Work More Than 50 Hours a Week, Youāre Probably Not Getting More Done
I was definitely pushing more than that for a while there. Back to normal now, though.
…and weāre done for another week! Until next week then folks, same time, same place.
Late Night Links – Sunday April 24th, 2016
Itās the long awaited return of late night links! With things getting especially busy recently #lnl really fell by the wayside, but weāre back! Letās dive in.
- Nikonās A900 is the First Coolpix Point-and-Shoot with 4K
I am looking for a good point-and-shoot camera… - Microsoft App Logs You Into Windows 10 Using Your Phone
- Kevin Spacey Crashed a Kevin Spacey Themed Night at a Florida Panthers Game
- Guy Solves Three Rubikās Cubes in Under 20 Seconds, While Juggling Them
- XKCD #1659: Tire Swing
- Poorly Drawn Lines: The Package
- 14 Apps Every Modern Gentleman Should Have on His Phone
- Chinaās Xiaomi Unveils a $150 Smartphone-Controlled Rice Cooker
I do want a new small rice cooker, but I was really looking more in the $15 range. - Mix Italian Dressing with Barbecue Sauce for the Perfect Marinade for Any Meat
Really? - Cleartext Limits Your Writing to the 1,000 Most Common Words in English
- Ballsy Brit Gets a Photo with His Planeās Hijacker
- FÅkasu is a Simple Timer Based on the Ideal 52/17 Productivity Schedule
I am not familiar with this 52/17 productivity schedule, but this kind of thing interests me, so I think Iām going to learn more.
And weāre done! Or rather weāre not, because Iāve built up quite a backlog of this stuff. But thatās enough for now.
Until next week then!