Can I make Windows 7 change my proxy settings based on my network connection?

Solution 1:

You could set this up with a PAC (Proxy Auto Configuration script). They're small scripts written in Javascript that let you specify various connection rules. Most modern desktop web browsers can read them with no problem, I have the same one set up in both Internet Explorer and Firefox.

A minimal PAC file that always sends you to the proxy server proxy.companyname on port 81:

function FindProxyForURL(url, host)
{
     return "PROXY proxy.companyname:8080";
}

And a slightly more complicated example of a PAC file that checks which network you're on by checking what IP address range you've picked up. In this example all client PCs in your company are given an IP address in the 10.10.* range by DHCP and access the internet via the proxy server proxy.companyname on port 81.

//Turn debug mode on by setting debug variable to any non-empty string
//Note that "" is an empty string, and that "0", "1" & "test" are not empty strings.


function FindProxyForURL(url, host)
  {
    var ip = myIpAddress();
    var debug = "";

    if(debug)
    {
        alert("proxy.pac IP=" + ip + "  HOST=" + host + "  URL=" + url);
    }

      // All client PCs on the internal network will have a 10.10.x.x address, check if I have this
     if (shExpMatch(ip, "10.10.*"))
     {
             if(debug)
          {
                 alert("Proxy for " + url);
             }
     return "PROXY proxy.companyname:81";
     }

     // If you have a different IP then this isn't the company network
     else 
     {
             if(debug)
          {
      alert("You are not at CompanyName so going direct for " + url);
             } 
            return "DIRECT";
     }

  }

The script also has a debug mode that you can use to test that it is working and which mode (PROXY or DIRECT connect) it thinks your current connection should be.

You could also make a more complex script that checks things like if the server you're connecting to is on your internal company network (and thus wants a DIRECT connection, even though your PC is currently on the company network) this is where you'd put anything you normally put in IE's proxy exceptions box, by expanding it like so:

//Turn debug mode on by setting debug variable to any non-empty string
//Note that "" is an empty string, and that "0", "1" & "test" are not empty strings.


function FindProxyForURL(url, host)
  {
    var ip = myIpAddress();
    var debug = "";

    if(debug)
    {
        alert("proxy.pac IP=" + ip + "  HOST=" + host + "  URL=" + url);
    }

    // Detect if trying to connect to internal/intranet host
    if(isPlainHostName(host) || dnsDomainIs(host, "intranet.companyname"))
    {
 if(debug)
 {
            alert("Direct for " + url);    
     }
    return "DIRECT";   
    }

    // Detect if trying to connect to URLs on the internal network via internal DNS suffix or IP address
    if(shExpMatch(url, "*.companyname") || shExpMatch(host, "10.100.*"))
    {
 if(debug)
 {
     alert("Direct for " + url);
 } 
        return "DIRECT";    
    } 

     // All client PCs on the internal network will have a 10.10.x.x address, check if I have this
     if (shExpMatch(ip, "10.10.*"))
     {
             if(debug)
          {
                 alert("Proxy for " + url);
             }
     return "PROXY proxy.companyname:8080";
     }

     // If you have a different IP then this isn't the company network
     else 
     {
             if(debug)
          {
      alert("You are not at CompanyName so going direct for " + url);
             } 
            return "DIRECT";
     }

  }

Use this file by saving it somewhere on your PC's C drive as something like proxy.pac, then for Internet Explorer, go to Tools -> Options (or Control Panel -> Internet Options) then click the Connections tab, click the LAN Settings button. Now take the tick out of the "Use a proxy server" box at the bottom, and instead put a tick in "Use automatic configuration script" and type in the address of your PAC file using a file:// URL like file://C:/proxy.pac.

IE proxy settings

Alternatively for Firefox go to Tools -> Options, click the Advanced tab, click the Network tab, click the Settings button, click the "Automatic proxy configuration URL" setting, again using a file:/// URL (note that in Firefox there are 3 slashes after the colon, eg file:///C:/proxy.pac.

Firefox proxy settings


Note that Internet Explorer 11 has changed this behaviour and you now can't load a PAC file from a 'file://' URL, you must use an 'HTTP://' or 'HTTPS://' URL.

In Internet Explorer 11, the WinINET team has disabled WinINET’s support for file:// based scripts to promote interoperability across network stacks. Corporations are advised to instead host their proxy configuration scripts on a HTTP or HTTPS server.

There is a work-around that can re-enable this feature for the moment, but you are not advised to use it, as future updates could disable this too.

As a temporary workaround, this block can be removed by setting the following registry key:

Key:   HKLM\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\
Value: EnableLegacyAutoProxyFeatures
Type:  REG_DWORD
Data:  1

Keep in mind that this should only be a temporary measure, as this block was introduced for good reasons, and removing the block won’t magically fix your WinHTTP-based applications.

Note also that after making this change IE11 you will need to change the address setting for your auto-config script as IE 11 only accepts one syntax for this (see the bottom of the linked article on the IE Internals blog below).

However, as noted, it is unlikely that this registry key workaround will continue to work in future versions of Internet Explorer, so you are much better off finding a way to hold the PAC on a web server. The Windows built-in IIS web server would do, and there are more minimal ones out there that would work for installing on your laptop too.

More info on the IE Internals blog entry Understanding Web Proxy Configuration.

Solution 2:

Use Free IP Switcher 1.0

alt text

Description:

Whenever mobile user moves to different network, has to change particular IP address, gateway address, subnet mask, proxy server and default printer, etc., to particular network environment. In order to do it, he has to remember TCP/IP settings information of every network. Also, he has to reboot the computer to apply it to the system and to access the Internet.

Don't waste your time reconfiguring computer network settings every time you change your location. Free IP Switcher is a perfect solution for you. It switches between preconfigured network settings with a single click and without any reboot. With Free IP Switcher you can automatically change all network settings on your pc: IP address, network masks, default gateway, DNS server, wins server, proxy settings, default printer and more.

Features:

  • Switch IP network settings "on the fly" without restarting
  • Switch Internet Explorer proxy-server settings
  • Switch your default printer
  • Switch your computer name and workgroup
  • Import current network settings to the setting
  • System-tray icon support, auto-start with windows
  • 100% Spyware FREE, NOT contain any Spyware, Adware or Viruses

Solution 3:

You have other (paid) alternatives to switching network configurations, like Mobile Net Switch or NetSetMan