Leaving your work proxy at work

June 16th, 2007 by grant · 4 Comments

»How to use proxy.pac scripting to make your browser choose the right proxy settings to use. When you're at work it uses your work proxy; at home it doesn't.

 

The problem

If you have a company laptop and your company forces you to use a proxy server you probably know about the mild annoyance of bringing your laptop home and finding that the internet doesn’t work because your browsers are setup to use a proxy that it can no longer find. So you probably go into your preferences/options and force the browser to connect directly to the internet, then switch it back when you get back to work the next day. Rinse, repeat, annoy.

Well, as it turns out most major browsers like Internet Explorer, Firefox, and Opera all allow you to use a bit of javascript to automatically set the proxy server to different things under different circumstances. You save this javascript code into a “proxy.pac” file and then you just let your browser know where it is.

Creating a simple proxy.pac

So open up your favorite text editor and lets create a proxy.pac. The javascript in this file should just be one function, called FindProxyForURL. It always will give you two parameters to work with and it should always return a string with a proxy (or several) to use. Below is a very, very simple version that will always try to use the proxy server unless it cannot connect, the it will try a direct connection:

function FindProxyForURL(url, host)
{
   return "PROXY proxy.myworplace.com:1080; DIRECT";
}

Using day and time features

If you generally work the same hours every day and have a pretty set schedule then your proxy.pac will be fairly simple. We want to use a direct connection whenever we are hitting the local machine, on weekends, and during certain hours on the weekdays. Otherwise it will try to use the named proxy as a starting point. Regardless of which method is picked, if no connection can be made the browser should try the other method, since we just ordered them differently in the if-else statment.

function FindProxyForURL(url, host)
{
  if( host == "localhost" ||
  	host == "127.0.0.1" ||  //bypass for local hosts
    weekdayRange("SAT","SUN") ||   //bypass on saturday or sunday
    timeRange(18, 7) )  //bypass after 6pm and before 7am
  {
    return "DIRECT; PROXY proxy.mycompany.com:1080";
    /*with both the DIRECT and the PROXY listed it will try the
    	first and then fall back on the second*/
  } else {
    return "PROXY proxy.mycompany.com:1080; DIRECT";
  }
}

Proxy by IP address

If you have a different IP address at work than you do at home then you can use that to filter off of instead of the time. We’ll just split the IP and if the first number isn’t equal to the first number of your IP at work then you must be elsewhere. We’ll also make it a bit more elegant and define our proxy server at the top of the file.

function FindProxyForURL(url, host)
{
  var myProxy = "proxy.mycompany.com:1080";
  var ipSubs = myIpAddress().split(".");
 
  if( host == "localhost" ||
  	host == "127.0.0.1" ||
  	ipSubs[0] != "172" ) {
    return "DIRECT; PROXY " + myProxy;
  } else {
    return "PROXY " + myProxy + "; DIRECT";
  }
}

Setting up the browser

So you’ve got your file, but now what? You need to find a place to put it (it doesn’t matter where, as long as you know where it is located at). For our example we’ll say that it is located at “C:\stuff\proxy.pac”.

To setup Internet Explorer:

  1. Go to Tools->Internet Options
  2. Go to the Connections Tab
  3. Click the LAN Settings button
  4. Check the box labeled “Use automatic configuration script”
  5. Type in the address using this format: file://c:/stuff/proxy.pac
  6. Keep clicking “OK” until you get back to the main screen

To setup Firefox (v2):

  1. Go to Tools->Options (Edit->Preferences on Linux)
  2. Go to the Network tab
  3. Click on the Settings button
  4. Click the radio button labeled “Automatic proxy configuration URL”
  5. Type in the address using this format: file:///c:/stuff/proxy.pac
  6. Keep clicking “OK” until you get back to the main screen

To setup Opera:
Just setup IE and Opera is smart enough to grab that file and use it.

Conclusion and Resources

You’ll probably have a little trial and error period, but you should be able to get something that works for you and will keep you from having to go into your preferences/options.

Tags: Browsers · Tips

 

4 responses so far ↓

  • 1 Nasreen begum // Mar 10, 2010 at 11:00 pm

    Exactly what i was looking for, but will this work for Mac as well?

    Most of the websites I searched on this instructs to put the proxy.pac file in the server itself. Hence, it it possible to not touch the server at all and just configure this from the client side?

    Thanks!

  • 2 Grant // Mar 30, 2010 at 3:46 pm

    Nasreen,
    It should work on Macs as well. Most sites suggest putting it on the server because then all your users can use it without having to create their own copies.

    Of course if you are just setting this up for yourself you can use it locally, just drop it somewhere on your drive and edit your browser’s preferences.

  • 3 Chris // Dec 4, 2010 at 8:07 pm

    This does NOT work for Firefox on Vista, Windows 7 or apparently Ubuntu Maverick. All of these systems return an IPv6 address instead of an IPv4 address. I would point out a correction, but Firefox on Maverick is *always* returning ::1 (localhost in IPv6) to the myIpAddress() function and I have been pulling my hair out trying to find out why.

  • 4 Leon // Jan 23, 2011 at 9:43 pm

    @Chris,

    I got this working by setting network.dns.disableIPV6 to false via about:config in firefox

Leave a Comment


8 − two =