如何检查Java是否正在使用配置的代理服务器?

huangapple go评论75阅读模式
英文:

How to check if Java is using configured proxy server?

问题

我在运行命令中设置了-Dhttps.proxyHost=,有没有一些日志记录我可以启用,以便在进行代理调用时可以看到?我需要验证这是否在起作用。

我还通过-Dhttp.nonProxyHosts=设置了一些主机白名单,因此我想确保它不会对这些主机进行代理。

英文:

I have the -Dhttps.proxyHost= set in the run command, is there some kind of logging I can enable to see when it is proxying a call? I need to verify that this is working.

I have also whitelisted a number of hosts via -Dhttp.nonProxyHosts= so I want to make sure it is not proxying these.

答案1

得分: 2

1. 使用 java.util.logging

创建名为 logging.properties 的文件,内容如下:

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
sun.net.www.protocol.http.HttpURLConnection.level = FINEST

然后添加 -Djava.util.logging.config.file=/path/to/logging.properties 到 JVM 选项。

现在,当通过代理建立 HTTPS 连接时,您将会看到日志消息:

May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: ProxySelector 请求 https://example.com/
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient New
FINEST: 正在查找 URL https://example.com 的 HttpClient,代理值为 HTTP @ 127.0.0.1:443
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient <init>
FINEST: 创建新的 HttpsClient,url:https://example.com,代理:HTTP @ 127.0.0.1:443,连接超时:-1
May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: 使用的代理:HTTP @ 127.0.0.1:443

2. 另一种选项,假设您在 Linux 上,在跟踪所有 connect 系统调用时:

$ sudo strace -f -p <PID> -e connect

在使用 strace 附加到 Java 进程的 <PID> 后,您将看到进程连接的所有地址。如果使用了代理,将会显示代理的地址。

[pid 12345] connect(263, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (操作现在正在进行中)
英文:

1. Using java.util.logging

Create logging.properties file with the following contents:

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
sun.net.www.protocol.http.HttpURLConnection.level = FINEST

Then add -Djava.util.logging.config.file=/path/to/logging.properties JVM option.

Now you'll see log messages whenever HTTPS connection is established through a proxy:

May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: ProxySelector Request for https://example.com/
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient New
FINEST: Looking for HttpClient for URL https://example.com and proxy value of HTTP @ 127.0.0.1:443
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient &lt;init&gt;
FINEST: Creating new HttpsClient with url:https://example.com and proxy:HTTP @ 127.0.0.1:443 with connect timeout:-1
May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: Proxy used: HTTP @ 127.0.0.1:443

2. Another option, assuming you are on Linux, is to trace all connect system calls:

$ sudo strace -f -p &lt;PID&gt; -e connect

After attaching to a Java process &lt;PID&gt; with strace, you'll see all addresses where the process connects to. If a proxy is used, there will be an address of the proxy.

[pid 12345] connect(263, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr(&quot;127.0.0.1&quot;)}, 16) = -1 EINPROGRESS (Operation now in progress)

huangapple
  • 本文由 发表于 2020年5月30日 01:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/62092009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定