DFox DFox - 4 months ago 72
Java Question

How to stop Java from preresolving my host while browsing via Proxy ( Tor )?

I am currently developing a java application that is using a tor proxy running on localhost to fetch a script that is displaying the requesters ip.

I would like the work to be done mostly by the tor proxy, so that it works in its most "natural" state. My current Code looks similar to this:

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


The problem I am now encountering is the following warn message from the tor proxy console:

[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.'


For me, it looks like Java is resolving the ip adress of myhost.com without me wanting it to do so. As already mentioned, I need to run the tor proxy in a possibly perfect environment.

Is there any way to prevent java from pre-resolving the host and just passing the whole URL to the tor proxy?

Thank you very much for every answer!

Answer

I found a simple answer myself, it is as easy as reading the API for InetSocketAddress

To stop Java from preresolving the Host, just use:

InetSocketAddress unresolvedAdr = InetSocketAddress.createUnresolved(host, port);

If you are interested, here is my full solution (in my special case with 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