如何为SOCKS代理设置nonProxyHosts? [英] How to set nonProxyHosts for a SOCKS proxy?

查看:246
本文介绍了如何为SOCKS代理设置nonProxyHosts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为SOCKS5代理设置 nonProxyHosts 列表,即应使用直接连接的主机名列表.

作为 Oracle文档描述,有一些名为 http.nonProxyHosts ftp.nonProxyHosts 的选项可以设置HTTP和FTP的代理排除,但是没有针对SOCKS代理的特定设置.

我尝试了 http.nonProxyHosts ,但这不会影响SOCKS连接.

SOCKS代理是通过以下方式设置的:

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

但这会导致即使与 localhost 的数据库连接也正在使用SOCKS代理,这是不可接受的.

应该如何使用?如何从代理连接中排除某些主机?

解决方案

使用 socksNonProxyHosts 系统属性;这没有记录,但是存在于Oracle JDK 8至11中,也可能存在于其他版本中.

 <代码> java -DsocksProxyHost = mySocksServer -DsocksProxyPort = 8888 -DsocksNonProxyHosts = 127.0.0.1 [...] 


TL;博士

socksNonProxyHosts /net/spi/DefaultProxySelector.java"rel =" noreferrer> sun.net.spi.DefaultProxySelector (感谢@rince)

但是,文档Java网络属性表明该属性不存在,因此其使用可能不稳定:

以这种方式指定SOCKS代理后,将尝试通过该代理尝试所有 TCP连接.

(添加了强调)

您也许可以使用 Proxy 和/或 ProxySelector 类,但是:

  • ProxySelector 仅在您的应用程序使用 URLConnection 建立连接时适用.

  • Proxy 适用于任意套接字……但前提是您可以将 Proxy 对象作为参数提供给相关的 Socket 构造函数调用.(并且您需要逻辑来提供不同的 Proxy 对象,具体取决于您的代码尝试连接的对象.)

有一个此RFE的错误.该票证提出了另一种解决方法.显然,如果 java.net.useSystemProxies 属性为 true ,则(在某些平台上)默认代理选择器将排除在适当的系统代理设置中指定的主机./p>

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.

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.

这篇关于如何为SOCKS代理设置nonProxyHosts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆