I want to use Java Socket with SOCKS proxy to connect on Tor (I am using Orbot and Tor service is running on port 9050 ).
I got the error :
I/System.out: java.net.UnknownHostException: Host is unresolved: 3g2upl4pq6kufc4m.onion
My code is as follow :
// setup proxy
address = new InetSocketAddress('localhost', 9050);
proxy = new java.net.Proxy(java.net.Proxy.Type.SOCKS,address);
// setup socket
client = new Socket(proxy);
inet = new InetSocketAddress("3g2upl4pq6kufc4m.onion", 80);
// connect
client.connect(inet);
This is legitimate, as Android will probably perform DNS resolution via DNS server specified in network configuration and the resolution of onion address will not work.
How do I specify to let Tor service to perform the DNS resolution (Tor will normally handle the hostname from the TCP packet) ?
The correct way of doing this is to use
InetSocketAddress.createUnresolved(...)
method but this throws an exception on Android.As a work around we need to implement Socks ourselves like here: http://www.mit.edu/~foley/TinFoil/src/tinfoil/TorLib.java
An explanation is here: https://github.com/acomminos/OnionKit/blob/master/libnetcipher/src/info/guardianproject/onionkit/proxy/SocksProxyClientConnOperator.java
Basically: