Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Change location automatically based on network' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Change location automatically based on network
Authored by: Lliwynd on May 17, '07 08:50:36PM
Hi,

I wanted something like this for a while. In general my network config doesn't change (it's DHCP based), except for the proxy configuration.

The solution I came up with was to write my own proxy.pac file that works for all locations, install it, and I'm done.

The proxy.pac file is a javascript function that gets called by the browser to find the proxy to use. I downloaded the proxy.pac files for all the different proxies I wanted to use, and then used a text editor to load them all into one file. I then created my own javascript function to do the switching. The result looks like this:



function FindProxyForURLLocationA(url, host) {
    // stuff from the location A proxy.pac FindProxyForURL() function
}

function FindProxyForURLLocationB(url, host) {
    // stuff from the location B proxy.pac FindProxyForURL() function
}

function FindProxyForURL(url, host) {
	myip = myIpAddress();

	result = "";

	if (isPlainHostName(host) || host=="127.0.0.1" || host == "localhost") {
		result = "DIRECT";
	} else if (isInNet(myip, "120.120.0.0" , "255.255.0.0")) {
		result = FindProxyForURLLocationA(url, host);
	} else if (isInNet(myip, "120.121.0.0", "255.255.0.0")) {
		result = FindProxyForURLLocationA(url, host);
	} else if (isInNet(myip, "130.120.0.0", "255.255.0.0")) {
		result = FindProxyForURLLocationB(url, host);
	} else {
		result = "DIRECT";
	}
	
	// if (!shExpMatch(url, "http:*")) {
	// 	result = result + "; DIRECT";
	// }
	
      // Return a proxy like this: "PROXY proxy.mydomain.net:3128"

	return result;
}

This approach has the added advantage that it works for all OS's.



[ Reply to This | # ]