How to make my proxy settings change depending on the network I connect to?

32,250

Solution 1

I can think of one way, but setting it up will be a bit obscure.

Basically you could use a PAC file

Install a web server on your system, any tiny web server will do, you don't need a huge system like apache.

Create a file wpad.dat with PAC directives that match based on your source address, and then configure your system to use the correct proxy.

In firefox, configure the proxy to point at your local PAC file. It would probably be something like http://localhost/wpad.dat.

Your PAC file might look somewhat like this (untested). See here for more PAC examples.

function FindProxyForURL(url, host) {   
  // If on a internal/LAN IP address, send traffic direct.
  if (isInNet(myIpAddress(), "10.10.1.0", "255.255.255.0"))
  {        
    return "PROXY 1.2.3.4:8080; PROXY 4.5.6.7:8080; DIRECT";
  }
  else
  {
    return "DIRECT";
  }
}

I have never tried it, and I am not at a system to test, but you may even be able to specify the PAC file using a file:// URL in firefox, which would mean you could skip setting up the web server.

Of course there is also the quick and easy solution, but it does require a little effort on your part as you move between locations. Install the Quick Proxy Firefox extension, and just click the button on your tool bar to toggle the proxy on or off. If you are willing to deal with this with a Firefox extension you can also try FoxyProxy, it supports setting up multiple proxy profiles, and you can easily switch between profiles.

Solution 2

As an addition to Zoredache's answer, you could use a script in /etc/network/if-up.d to generate a snippet in /etc/profile.d/ containing your proxy shell variables. Note that this will only work with new shells.

Additionally, this method might be used to generate a WPAD-file, to which you could point any browsers supporting this.

Solution 3

This is a pretty old post, but I found this. Have a look: http://marin.jb.free.fr/proxydriver/

You can install the .deb package provided for Ubuntu. This is basically a shell script that changes the environment variables when your network changes. You can configure the settings for each network by editing the config files (automatically) created in /etc/proxydriver.d/

Share:
32,250
Rob Bazinet
Author by

Rob Bazinet

Updated on September 17, 2022

Comments

  • Rob Bazinet
    Rob Bazinet over 1 year

    My company's corporate network requires me to set a network proxy to access the net, but when I am anywhere else, I don't need it. The proxy settings in Ubuntu (System -> Preferences -> Proxy server) allowed me to create "locations" that I can manually select. Then I have a "default" location (with no proxy) and a "work" location (with my company's proxy in it).

    Is there a way to make Ubuntu automatically select the "work" location based on the connection I'm using? I thought I could use the IP subnet (very specific) to detect where I am, but I have no idea how to set it up...

    Edit: I really need to have the proxy settings set at the system level. All my network connections (IMAP, SMTP, chat, etc) need to go through the proxy. Not only the web browser.

  • Rob Bazinet
    Rob Bazinet over 13 years
    Interesting... I'll have to test. I will tell you how it turns out. I think the Firefox extension won't do though: the Proxy is used for all protocols, not only HTTP. Then I need it also for Empathy, Evolution, everything.
  • Zoredache
    Zoredache over 13 years
    Oh, and one other thing. You could ask the IT guy to setup WPAD. Then you coul just use th automatic proxy option.
  • Rob Bazinet
    Rob Bazinet over 13 years
    Well... making IT do anything is not an option I'm afraid. I can make a request, but I certainly won't see anything happen. No business justification means no resource to do it. And I'll be quicker to do it myself than to write down a justification :-)
  • Rob Bazinet
    Rob Bazinet over 13 years
    Ok. After a little more reading, I'm not satisfied with this solution. It seems it would work (I have not tried implementing it), but it would not be available system-wide. It would work only for the web browser, since (according to findproxyforurl.com/index.html) the system is supported only in web browser (because it relies on Javascript).
  • Eliah Kagan
    Eliah Kagan almost 12 years
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • Eliah Kagan
    Eliah Kagan about 11 years
    Welcome to Ask Ubuntu! So... how would someone actually do this? Can you provide instructions? Right now this doesn't really the answer the question...