21

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.

7 Answers 7

7

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.

4
  • 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. Sep 4, 2010 at 8:06
  • Oh, and one other thing. You could ask the IT guy to setup WPAD. Then you coul just use th automatic proxy option.
    – Zoredache
    Sep 4, 2010 at 18:48
  • 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 :-) Sep 4, 2010 at 20:19
  • 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). Sep 6, 2010 at 19:46
5

In Ubuntu 10.10 the .pac script works well. As Zoredache explained, set up a webserver, put a pac file there, open System->Preferences->Network Proxy and set the "Autoconfiguration URL" to the pac file location.

Example of a PAC file that checks for one's own IP address: /var/www/selectProxy.pac:

function FindProxyForURL(url, host)
 {
 if (myIpAddress() == "192.168.22.63") { 
 return "PROXY 192.168.22.8:8080";
 }
 else {
 return "DIRECT";
 }
 }

In Network proxy prefs: the URL would be:

http://localhost/selectProxy.pac
3

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.

2

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/

1
  • 2
    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. Jul 13, 2012 at 18:40
0

I have the same problem. Please generalize any answers to include my situation.

I use my laptop at home on wireless: -- always eth1 interface static IP address (192.168.1.10)

When I go to work I plug it in: -- always eth0 interface dynamic IP address (10.10.xx.xx), AND a proxy: 10.10.123.123 port 8888

I want Ubuntu to automatically set the IP address and proxy based on which interface I'm using.

(I've messed around in /etc/network/interfaces [see "man interfaces" and /etc/resolv.conf to no avail.

2
  • 1
    If I understand: your Wifi card has a static IP - then it is already setup "automatically". And your wired network is set by DHCP - which is also automatic... Then what do you want to add to that, regarding the IP settings ? I understand the Proxy need, but not the IP. Sep 6, 2010 at 19:51
  • You know, I think you're right. I'm back at work after holdiays and it seems to be working automatically. I think that the only problem is the proxy. I'll investigate more tomorrow.
    – Bill
    Sep 9, 2010 at 23:13
0

I use the PAC file approach and it works well for browsing but not so well for many other applications that don't understand PAC files - I'm guessing that when an app asks for "proxy config" via the API, they just get the PAC file URL back if you are using one.

For user applications that either don't have proxy support or don't understand PAC files (like Empathy), I use tsocks (because I use an SSH tunnel for my proxy support).

Linking a JavaScript engine or a PAC parser to every application that supports basic proxy config seems like overkill. This seems to be a case that could benefit from some underlying platform support with an API call that actually interprets the PAC file and returns the result, instead of it's URL.

0

this is an old post at this point, but I came here looking for a way to do this and found a different answer. The answer I'm working with lies in a package called 'whereami' and requires you to define your work's sub-net in its detect.conf file and then define the proxy server on the whereami.conf file. Should work, and I'll post with any issues I have.

Good luck!

1
  • 1
    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... Mar 31, 2013 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .