We help IT Professionals succeed at work.

Configuring a proxy pac file (Java)

Medium Priority
4,892 Views
Last Modified: 2013-12-07
Hi All,

Were trying to write a pac file to accomplish the following
if the ip address is 192.168.10.0 with a mask of 255.255.255.0 or the ip address is 192.168.12.0 with a mask of 255.255.255.0 and the url is http or https then use the proxy (192.168.10.20 on port 8080) http proxy

if the ip address is 192.168.10.0 with a mask of 255.255.255.0 or the ip address is 192.168.12.0 with a mask of 255.255.255.0 and the url is ftp. then use the proxy (192.168.10.20 on port 1080) socks proxy

Ive guessed at the code below which obviously doesnt work. Could anyone please assist? Im sure the logic is OK its just the java Im not getting right.

TIA for any ideas


function FindProxyForURL(url, host)
{
if (          	(isInNet(myIpAddress(), "192.168.10.0", "255.255.255.0") ||
          	isInNet(myIpAddress(), "192.168.12.0", "255.255.255.0")) &&
		(shExpMatch(url, "http:*") ||
		shExpMatch(url, https:*"))
return "PROXY 192.168.10.20:8080;" 
 
else if (          	(isInNet(myIpAddress(), "192.168.10.0", "255.255.255.0") ||
          	isInNet(myIpAddress(), "192.168.12.0", "255.255.255.0")) &&
		(shExpMatch(url, "ftp.*"))
 
return "SOCKS 192.168.10.20:1080;"
else
return "DIRECT";
}

Open in new window

Comment
Watch Question

ksivananthVice President
Commented:
for java apps, you need to specify them as system properties, for e.g.,

http.proxyHost (default: <none>)
http.proxyPort (default: 80 if http.proxyHost specified)
http.nonProxyHosts (default: <none>

check this, http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016
Commented:
Try the following:
var ip = myIpAddress();
var reAddress = /192\.168\.1[02]\..+/;
var reProtocolHttp = /https?:.+/;
var reProtocolFtp = /ftp:.+/;
 
if (reAddress.test(ip) && reProtocolHttp.test(url)) {
	return "PROXY 192.168.10.20:8080";
}
 
else if (reAddress.test(ip) && reProtocolFtp.test(url)) {
	return "SOCKS 192.168.10.20:1080";
}
else {
	return "DIRECT";
}

Open in new window

Author

Commented:
Hi all,

Thanks for the input and sorry for the delayed response.

The requirements changed and we ended up going with the following, i.e. send http via proxy and all else direct (since our proxy isnt that good :) )

Im going to leave the q open for a while for any comments then award points appropriately if thats OK with all?

function FindProxyForURL(url, host)
{
// variable strings to return
var proxy_yes = "PROXY 192.168.10.20:8080";
var proxy_no = "DIRECT";
// Exceptions for protocols that dont proxy well
if (shExpMatch(url, "https://*")) { return proxy_no; }
if (shExpMatch(url, "ftp://*")) { return proxy_no; }
// If on the following networks then use proxy
if (isInNet(myIpAddress(), "192.168.10.0", "255.255.255.0")){ return proxy_yes; }
if (isInNet(myIpAddress(), "192.168.12.0", "255.255.255.0")){ return proxy_yes; }
// Send everything else direct
else
return "DIRECT";
}

Open in new window

Author

Commented:
No more comments!

Thanks for the help. Will award points accordingly to who I feel made the best contributions, hope thats OK.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.