我目前正在开发一个java应用程序,它使用在localhost上运行的tor代理来获取显示请求者ip的脚本。
我希望这项工作主要由tor代理完成,以便它以最“自然”的状态运行。我目前的代码与此类似:
SocketAddress TorProxyAddress = new InetSocketAddress("127.0.0.1", 9050);
Proxy TorProxy = new Proxy(Proxy.Type.SOCKS, TorProxyAddress);
URL url = new URL("https://myhost.com/ip2.php");
URLConnection conn = url.openConnection(TorProxy);
// ... reading the input stream etc
我现在遇到的问题是来自tor代理控制台的以下警告消息:
[warn] Your application (using socks5 to port 443) is giving Tor only an IP address. Applications that do DNS resolves themselves may leak information. Consider using Socks4A (e.g. via privoxy or socat) instead. For more information, please see https://wiki.torproject.org/TheOnionRouter/TorFAQ#SOCKSAndDNS.'
对我来说,看起来Java正在解决myhost.com的ip地址,而我并不希望它这样做。如前所述,我需要在一个可能完美的环境中运行tor代理。
有没有办法阻止java预先解析主机并将整个URL传递给tor代理?
非常感谢您的每一个答案!
答案 0 :(得分:3)
我自己找到了一个简单的答案,就像阅读InetSocketAddress
要阻止Java预先解析主机,只需使用:
InetSocketAddress unresolvedAdr = InetSocketAddress.createUnresolved(host, port);
如果您有兴趣,这是我的完整解决方案(在我的特殊情况下使用ssl-stuff):
TorProxyAddress = new InetSocketAddress("127.0.0.1", 9050);
Proxy TorProxy = new Proxy(Proxy.Type.SOCKS, TorProxyAddress);
Socket underlying = new Socket(TorProxy);
InetSocketAddress unresolvedAdr = InetSocketAddress.createUnresolved(host, port);
underlying.connect(unresolvedAdr);
SSLSocket socket = (SSLSocket) SockFactory.createSocket(underlying, "127.0.0.1", 9050, true);
// ... Read from / write to Socket