Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set nonProxyHosts for a SOCKS proxy?

Tags:

I want to set the nonProxyHosts list for a SOCKS5 proxy, i.e. the list of hostnames to which a direct connection should be used.

As the oracle docs describe, there are options named http.nonProxyHosts and ftp.nonProxyHosts to set proxy exclusions for HTTP and FTP, but there is no specific setting for SOCKS proxies.

I tried http.nonProxyHosts, but this doesn't affect SOCKS connections.

The SOCKS proxy is set up via:

System.setProperty("socksProxyHost", "192.168.10.10");
System.setProperty("socksProxyPort", "3128");

But this causes that even DB connections to localhost are using the SOCKS proxy, which is unacceptable.

How is this supposed to be used? How can I exclude certain hosts from the proxified connections?

like image 868
unwichtich Avatar asked Aug 22 '14 11:08

unwichtich


People also ask

How to set proxy for Java application?

Configure Proxies through the Java Control PanelIn the Java Control Panel, under the General tab, click on Network Settings. Select the Use Browser Settings checkbox. Click OK to save your changes. Close all browser windows.

How does a Socks proxy work?

A SOCKS5 proxy is an alternative to a VPN. It routes packets between a server and a client using a proxy server. This means that your real IP address is hidden and you access the internet with an address provided to you by a proxy provider.

What is Dhttp nonProxyHosts?

nonProxyHosts indicates the hosts which should be connected to directly and not through the proxy server. The value can be a list of hosts, each separated by a |, and in addition a wildcard character (*) can be used for matching. For example: -Dhttp.nonProxyHosts=*.foo.com|localhost|confluence|crowd.


1 Answers

Use the socksNonProxyHosts system property; this is undocumented, but exists in Oracle JDKs 8 thru 11, and probably others too.

java -DsocksProxyHost=mySocksServer -DsocksProxyPort=8888 -DsocksNonProxyHosts=127.0.0.1 [...]

TL;DR

The property socksNonProxyHosts is found in the source code for sun.net.spi.DefaultProxySelector (thanks to @rince)

However, the documentation for the Java networking properties implies this doesn't exist, so its use may be unstable:

Once a SOCKS proxy is specified in this manner, all TCP connections will be attempted through the proxy.

(Emphasis added)

You might be able to use the Proxy and/or ProxySelector classes, but:

  • ProxySelector is only applicable if your application uses URLConnection to establish connections.

  • Proxy is applicable for arbitrary sockets ... but only if you can supply the Proxy object as a parameter to the relevant Socket constructor calls. (And you would need logic to supply different Proxy objects depending on what your code is trying to connect to.)

There's a bug for this RFE for this. The ticket suggests another workaround. Apparently, if the java.net.useSystemProxies property is true, then (on some platforms) the default proxy selector will respect exclude hosts specified in the appropriate system proxy settings.

like image 65
Stephen C Avatar answered Sep 25 '22 10:09

Stephen C