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?
Use the
socksNonProxyHosts
system property; this is undocumented, but exists in Oracle JDKs 8 thru 11, and probably others too.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:
(Emphasis added)
You might be able to use the
Proxy
and/orProxySelector
classes, but:ProxySelector
is only applicable if your application usesURLConnection
to establish connections.Proxy
is applicable for arbitrary sockets ... but only if you can supply theProxy
object as a parameter to the relevantSocket
constructor calls. (And you would need logic to supply differentProxy
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 istrue
, then (on some platforms) the default proxy selector will respect exclude hosts specified in the appropriate system proxy settings.