如何为 SOCKS 代理设置 nonProxyHosts? [英] How to set nonProxyHosts for a SOCKS proxy?
问题描述
我想为 SOCKS5 代理设置 nonProxyHosts
列表,即应该使用直接连接的主机名列表.
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.
作为 oracle 文档 描述,有名为 http.nonProxyHosts
和 ftp.nonProxyHosts
的选项来设置 HTTP 和 FTP 的代理排除,但没有针对 SOCKS 代理的特定设置.
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.
我尝试了 http.nonProxyHosts
,但这不会影响 SOCKS 连接.
I tried http.nonProxyHosts
, but this doesn't affect SOCKS connections.
SOCKS 代理通过以下方式设置:
The SOCKS proxy is set up via:
System.setProperty("socksProxyHost", "192.168.10.10");
System.setProperty("socksProxyPort", "3128");
但这会导致即使是到 localhost
的 DB 连接也在使用 SOCKS 代理,这是不可接受的.
这个应该怎么用?如何从代理连接中排除某些主机?
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?
推荐答案
使用 socksNonProxyHosts
系统属性;这是未记录的,但存在于 Oracle JDK 8 到 11 中,可能还有其他版本.
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;博士
TL;DR
属性 socksNonProxyHosts
可在 sun.net.spi.DefaultProxySelector(感谢@rince)
The property socksNonProxyHosts
is found in the source code for sun.net.spi.DefaultProxySelector (thanks to @rince)
但是,文档Java 网络属性意味着它不存在,因此它的使用可能不稳定:
However, the documentation for the Java networking properties implies this doesn't exist, so its use may be unstable:
以这种方式指定 SOCKS 代理后,将通过该代理尝试所有 TCP 连接.
Once a SOCKS proxy is specified in this manner, all TCP connections will be attempted through the proxy.
(强调)
您也许可以使用 Proxy
和/或 ProxySelector
类,但是:
You might be able to use the Proxy
and/or ProxySelector
classes, but:
ProxySelector
仅适用于您的应用程序使用URLConnection
建立连接的情况.
ProxySelector
is only applicable if your application usesURLConnection
to establish connections.
Proxy
适用于任意套接字...但前提是您可以将 Proxy
对象作为参数提供给相关的 Socket代码> 构造函数调用.(并且您需要逻辑来提供不同的
Proxy
对象,具体取决于您的代码尝试连接的对象.)
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.)
有一个 为此 RFE 的错误.该票建议另一种解决方法.显然,如果 java.net.useSystemProxies
属性为 true
,那么(在某些平台上)默认代理选择器将尊重在适当的系统代理设置中指定的排除主机.
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.
这篇关于如何为 SOCKS 代理设置 nonProxyHosts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!